Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Add table for comment spam

Revision ID: 73c1a03321bd
Revises: 4e16c97d06e7
Create Date: 2021-02-15 23:56:57.590999

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "73c1a03321bd"
down_revision = "4e16c97d06e7"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"comment_spams",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("updated_at", sa.DateTime(), nullable=True),
sa.Column("spam", sa.Boolean(), nullable=True),
sa.Column("comment_id", sa.Integer(), nullable=True),
sa.Column("story_id", sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(["comment_id"], ["comments.id"],),
sa.ForeignKeyConstraint(["story_id"], ["stories.id"],),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(
op.f("ix_comment_spams_id"), "comment_spams", ["id"], unique=False
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f("ix_comment_spams_id"), table_name="comment_spams")
op.drop_table("comment_spams")
# ### end Alembic commands ###
30 changes: 30 additions & 0 deletions backend/comments/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,33 @@ def count_like(db: Session, comment_id):
)

return {"like": like, "dislike": dislike}


def get_spam_by_comment_and_user(db, comment_id, story_id):
return (
db.query(models.CommentSpam)
.filter(
and_(
models.CommentSpam.comment_id == comment_id,
models.CommentSpam.story_id == story_id,
)
)
.first()
)


def report_comment(db: Session, comment_id, story_id, is_spam):
spam = schemas.CommentSpam(
spam=is_spam, comment_id=comment_id, story_id=story_id
)
db_spam = get_spam_by_comment_and_user(db, comment_id, story_id)

if db_spam:
update(db_spam.id, spam, models.CommentSpam, db)
else:
db_spam = models.CommentSpam(**spam.dict())
db.add(db_spam)
db.commit()

db.refresh(db_spam)
return db_spam
8 changes: 8 additions & 0 deletions backend/comments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ class CommentLike(Base):
like = Column(Boolean)
comment_id = Column(Integer, ForeignKey("comments.id"))
story_id = Column(Integer, ForeignKey("stories.id"))


class CommentSpam(Base):
__tablename__ = "comment_spams"

spam = Column(Boolean)
comment_id = Column(Integer, ForeignKey("comments.id"))
story_id = Column(Integer, ForeignKey("stories.id"))
13 changes: 13 additions & 0 deletions backend/comments/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Comment(CommentCreate):
updated_at: date
created_at: date
story: Story = None
reported: bool = None

class Config:
orm_mode = True
Expand All @@ -36,3 +37,15 @@ class CommentLike(CommentLikeCreate):

class Config:
orm_mode = True


class CommentSpamCreate(BaseModel):
spam: bool = None


class CommentSpam(CommentSpamCreate):
comment_id: int
story_id: int

class Config:
orm_mode = True
37 changes: 34 additions & 3 deletions backend/router/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,21 @@ def create_comment(


@router.get("/my_stories/{my_story_id}", response_model=List[schemas.Comment])
def get_comments_by_my_story(my_story_id: int, db: Session = Depends(get_db)):
return crud.get_comments_by_my_story(db, my_story_id)
def get_comments_by_my_story(
my_story_id: int,
current_story: stories_schemas.Story = Depends(main.get_current_story),
db: Session = Depends(get_db),
):
comments = crud.get_comments_by_my_story(db, my_story_id)
if current_story:
for comment in comments:
db_spam = crud.get_spam_by_comment_and_user(
db, comment.id, current_story.id
)
if db_spam:
comment.reported = db_spam.spam

return comments


@router.post("/{comment_id}", response_model=schemas.Comment)
Expand Down Expand Up @@ -82,7 +95,7 @@ def like_comment(
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="User must first share their story before "
+ "liking or dislike a comment",
+ "they can like or dislike a comment",
headers={"WWW-Authenticate": "Bearer"},
)

Expand All @@ -106,3 +119,21 @@ def count_like(
d["like_by_me"] = like_by_me

return JSONResponse(d, status_code=200,)


@router.post("/{comment_id}/report", response_model=schemas.CommentLike)
def report_comment(
comment_id: int,
dto: schemas.CommentSpamCreate,
current_story: stories_schemas.Story = Depends(main.get_current_story),
db: Session = Depends(get_db),
):
if not current_story:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="User must first share their story before "
+ "reporting a comment",
headers={"WWW-Authenticate": "Bearer"},
)

return crud.report_comment(db, comment_id, current_story.id, dto.spam)