diff --git a/backend/alembic/versions/73c1a03321bd_add_table_for_comment_spam.py b/backend/alembic/versions/73c1a03321bd_add_table_for_comment_spam.py new file mode 100644 index 00000000..c8cc8a6f --- /dev/null +++ b/backend/alembic/versions/73c1a03321bd_add_table_for_comment_spam.py @@ -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 ### diff --git a/backend/comments/crud.py b/backend/comments/crud.py index a636c2a3..6e2de640 100644 --- a/backend/comments/crud.py +++ b/backend/comments/crud.py @@ -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 diff --git a/backend/comments/models.py b/backend/comments/models.py index 16cd24ae..73060ae5 100644 --- a/backend/comments/models.py +++ b/backend/comments/models.py @@ -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")) diff --git a/backend/comments/schemas.py b/backend/comments/schemas.py index 45b3bde7..66b1589b 100644 --- a/backend/comments/schemas.py +++ b/backend/comments/schemas.py @@ -21,6 +21,7 @@ class Comment(CommentCreate): updated_at: date created_at: date story: Story = None + reported: bool = None class Config: orm_mode = True @@ -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 diff --git a/backend/router/comments.py b/backend/router/comments.py index 485bfe33..8815f40e 100644 --- a/backend/router/comments.py +++ b/backend/router/comments.py @@ -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) @@ -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"}, ) @@ -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)