Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

wall: scope comment deletion to post

Neeraj Guptabe277394bc2e
diff --git a/server/wall/controller/posts.go b/server/wall/controller/posts.go
index 4fa3cfed67..252c579887 100644
--- a/server/wall/controller/posts.go
+++ b/server/wall/controller/posts.go
@@ -322,13 +322,24 @@ func (c *PostsController) DeleteComment(ctx *gin.Context, postID, commentID stri
if err != nil {
return err
}
- _, err = strconv.ParseInt(strings.TrimSpace(postID), 10, 64)
- if err != nil {
+ postIDInt, err := strconv.ParseInt(strings.TrimSpace(postID), 10, 64)
+ if err != nil || postIDInt <= 0 {
return ente.NewBadRequestWithMessage("invalid postID")
}
commentIDInt, err := strconv.ParseInt(strings.TrimSpace(commentID), 10, 64)
if err != nil || commentIDInt <= 0 {
return ente.NewBadRequestWithMessage("invalid commentID")
}
- return c.PostsRepo.DeleteComment(ctx.Request.Context(), commentIDInt, userID)
+ post, err := c.PostsRepo.GetPost(ctx.Request.Context(), postIDInt, userID)
+ if err != nil {
+ return err
+ }
+ wall, err := c.WallsRepo.GetWallByID(ctx.Request.Context(), post.WallID)
+ if err != nil {
+ return err
+ }
+ if err := c.auth.canViewWall(ctx.Request.Context(), &viewerAuth{UserID: userID}, wall); err != nil {
+ return err
+ }
+ return c.PostsRepo.DeleteComment(ctx.Request.Context(), postIDInt, commentIDInt, userID)
}
diff --git a/server/wall/controller/posts_test.go b/server/wall/controller/posts_test.go
index 408b46c0f3..46d141efe3 100644
--- a/server/wall/controller/posts_test.go
+++ b/server/wall/controller/posts_test.go
@@ -60,6 +60,14 @@ func countWallComments(t *testing.T, db *sql.DB, postID int64) int {
return count
}
+func countActiveWallComments(t *testing.T, db *sql.DB, postID int64) int {
+ t.Helper()
+ var count int
+ err := db.QueryRow(`SELECT COUNT(*) FROM wall_post_comments WHERE post_id = $1 AND is_deleted = FALSE`, postID).Scan(&count)
+ require.NoError(t, err)
+ return count
+}
+
func TestCreateCommentRejectsParentFromAnotherPost(t *testing.T) {
posts, repos, ctx := setupPostsControllerTest(t)
aliceID := insertWallControllerUser(t, repos, "[email protected]", "alice-public")
@@ -124,3 +132,37 @@ func TestCreateCommentAcceptsTopLevelParentOnSamePost(t *testing.T) {
require.Equal(t, parent.CommentID, *resp.ParentCommentID)
require.Equal(t, 2, countWallComments(t, repos.Posts.DB, postID))
}
+
+func TestDeleteCommentRejectsRoutePostMismatch(t *testing.T) {
+ posts, repos, ctx := setupPostsControllerTest(t)
+ aliceID := insertWallControllerUser(t, repos, "[email protected]", "alice-public")
+ wall, err := repos.Walls.CreateWall(ctx, aliceID, "alice", "alice-wall-key", "alice-profile")
+ require.NoError(t, err)
+ firstPostID, err := repos.Posts.CreatePost(ctx, aliceID, wall.WallID, "post-key-1", nil, wall.CurrentVersion, nil)
+ require.NoError(t, err)
+ secondPostID, err := repos.Posts.CreatePost(ctx, aliceID, wall.WallID, "post-key-2", nil, wall.CurrentVersion, nil)
+ require.NoError(t, err)
+ comment, err := repos.Posts.CreateComment(ctx, firstPostID, aliceID, "comment", nil)
+ require.NoError(t, err)
+
+ err = posts.DeleteComment(newWallControllerContext(aliceID), strconv.FormatInt(secondPostID, 10), strconv.FormatInt(comment.CommentID, 10))
+
+ require.ErrorIs(t, err, sql.ErrNoRows)
+ require.Equal(t, 1, countActiveWallComments(t, repos.Posts.DB, firstPostID))
+}
+
+func TestDeleteCommentDeletesOwnCommentOnRoutePost(t *testing.T) {
+ posts, repos, ctx := setupPostsControllerTest(t)
+ aliceID := insertWallControllerUser(t, repos, "[email protected]", "alice-public")
+ wall, err := repos.Walls.CreateWall(ctx, aliceID, "alice", "alice-wall-key", "alice-profile")
+ require.NoError(t, err)
+ postID, err := repos.Posts.CreatePost(ctx, aliceID, wall.WallID, "post-key", nil, wall.CurrentVersion, nil)
+ require.NoError(t, err)
+ comment, err := repos.Posts.CreateComment(ctx, postID, aliceID, "comment", nil)
+ require.NoError(t, err)
+
+ err = posts.DeleteComment(newWallControllerContext(aliceID), strconv.FormatInt(postID, 10), strconv.FormatInt(comment.CommentID, 10))
+
+ require.NoError(t, err)
+ require.Equal(t, 0, countActiveWallComments(t, repos.Posts.DB, postID))
+}
diff --git a/server/wall/repo/module_test.go b/server/wall/repo/module_test.go
index 6a391eb4f9..6d5cde0ab9 100644
--- a/server/wall/repo/module_test.go
+++ b/server/wall/repo/module_test.go
@@ -755,6 +755,36 @@ func TestListTopLevelCommentsPaginates(t *testing.T) {
require.Empty(t, nextCursor)
}
+func TestDeleteCommentRequiresPostAndAuthorMatch(t *testing.T) {
+ ctx := context.Background()
+ module := newWallTestModule(t)
+
+ aliceID := insertWallUser(t, module, "[email protected]", "alice-public")
+ bobID := insertWallUser(t, module, "[email protected]", "bob-public")
+ wall, err := module.Walls.CreateWall(ctx, aliceID, "alice", "alice-wall-key", "alice-profile")
+ require.NoError(t, err)
+ _, err = module.Walls.CreateWall(ctx, bobID, "bob", "bob-wall-key", "bob-profile")
+ require.NoError(t, err)
+ firstPostID, err := module.Posts.CreatePost(ctx, aliceID, wall.WallID, "post-key-1", nil, wall.CurrentVersion, nil)
+ require.NoError(t, err)
+ secondPostID, err := module.Posts.CreatePost(ctx, aliceID, wall.WallID, "post-key-2", nil, wall.CurrentVersion, nil)
+ require.NoError(t, err)
+ comment, err := module.Posts.CreateComment(ctx, firstPostID, bobID, "comment", nil)
+ require.NoError(t, err)
+
+ err = module.Posts.DeleteComment(ctx, secondPostID, comment.CommentID, bobID)
+ require.ErrorIs(t, err, sql.ErrNoRows)
+ require.Equal(t, 1, countActivePostComments(t, module, firstPostID))
+
+ err = module.Posts.DeleteComment(ctx, firstPostID, comment.CommentID, aliceID)
+ require.ErrorIs(t, err, sql.ErrNoRows)
+ require.Equal(t, 1, countActivePostComments(t, module, firstPostID))
+
+ err = module.Posts.DeleteComment(ctx, firstPostID, comment.CommentID, bobID)
+ require.NoError(t, err)
+ require.Equal(t, 0, countActivePostComments(t, module, firstPostID))
+}
+
func ptr(value string) *string {
return &value
}
@@ -767,6 +797,14 @@ func sqlNullString(value string) sql.NullString {
return sql.NullString{String: value, Valid: value != ""}
}
+func countActivePostComments(t *testing.T, module *Module, postID int64) int {
+ t.Helper()
+ var count int
+ err := module.Posts.DB.QueryRow(`SELECT COUNT(*) FROM wall_post_comments WHERE post_id = $1 AND is_deleted = FALSE`, postID).Scan(&count)
+ require.NoError(t, err)
+ return count
+}
+
func requireQueuedTempObject(t *testing.T, module *Module, objectKey, purpose, bucketID string) {
t.Helper()
var gotPurpose, gotBucketID string
diff --git a/server/wall/repo/posts.go b/server/wall/repo/posts.go
index a129dd6dad..d19c7139c4 100644
--- a/server/wall/repo/posts.go
+++ b/server/wall/repo/posts.go
@@ -377,8 +377,8 @@ func (r *PostsRepository) ListReplies(ctx context.Context, postID, viewerID int6
return result, stacktrace.Propagate(rows.Err(), "")
}
-func (r *PostsRepository) DeleteComment(ctx context.Context, commentID, viewerID int64) error {
- res, err := r.DB.ExecContext(ctx, `UPDATE wall_post_comments SET is_deleted = TRUE WHERE comment_id = $1 AND author_id = $2`, commentID, viewerID)
+func (r *PostsRepository) DeleteComment(ctx context.Context, postID, commentID, viewerID int64) error {
+ res, err := r.DB.ExecContext(ctx, `UPDATE wall_post_comments SET is_deleted = TRUE WHERE post_id = $1 AND comment_id = $2 AND author_id = $3 AND is_deleted = FALSE`, postID, commentID, viewerID)
if err != nil {
return stacktrace.Propagate(err, "")
}