Skip to content

Commit

Permalink
feat(boards2): change public delete reply function to soft delete rep…
Browse files Browse the repository at this point in the history
…lies (#3606)

Replies are soft deleted by replacing their body with a deleted message
to keep sub comments

Related to
#3583 (review)
  • Loading branch information
jeronimoalbi authored Jan 27, 2025
1 parent 1e24a33 commit 2f99874
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
4 changes: 4 additions & 0 deletions examples/gno.land/r/demo/boards2/post.gno
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ func (post *Post) Update(title string, body string) {
post.updatedAt = time.Now()
}

func (post *Post) HasReplies() bool {
return post.replies.Size() > 0
}

func (thread *Post) GetReply(pid PostID) (_ *Post, found bool) {
v, found := thread.repliesAll.Get(pid.Key())
if !found {
Expand Down
5 changes: 5 additions & 0 deletions examples/gno.land/r/demo/boards2/post_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func TestNewThread(t *testing.T) {
uassert.True(t, thread.GetUpdatedAt().IsZero())
uassert.Equal(t, title, thread.GetTitle())
uassert.Equal(t, body[:77]+"...", thread.GetSummary())
uassert.False(t, thread.HasReplies())
uassert.Equal(t, url, thread.GetURL())
uassert.Equal(t, replyURL, thread.GetReplyFormURL())
uassert.Equal(t, repostURL, thread.GetRepostFormURL())
Expand All @@ -160,6 +161,7 @@ func TestThreadAddReply(t *testing.T) {
uassert.Equal(t, threadID+1, uint(reply.GetPostID()))
uassert.Equal(t, reply.GetCreator(), replier)
uassert.Equal(t, reply.GetBody(), body)
uassert.True(t, thread.HasReplies())
}

func TestThreadGetReply(t *testing.T) {
Expand Down Expand Up @@ -297,6 +299,7 @@ func TestNewReply(t *testing.T) {
uassert.Equal(t, uint(replyID), uint(reply.GetPostID()))
uassert.False(t, reply.GetCreatedAt().IsZero())
uassert.True(t, reply.GetUpdatedAt().IsZero())
uassert.False(t, reply.HasReplies())
uassert.Equal(t, body[:77]+"...", reply.GetSummary())
uassert.Equal(t, url, reply.GetURL())
uassert.Equal(t, replyURL, reply.GetReplyFormURL())
Expand All @@ -320,6 +323,8 @@ func TestReplyAddReply(t *testing.T) {
uassert.Equal(t, parentReplyID+1, uint(reply.GetPostID()))
uassert.Equal(t, reply.GetCreator(), replier)
uassert.Equal(t, reply.GetBody(), body)
uassert.False(t, reply.HasReplies())
uassert.True(t, parentReply.HasReplies())
}

func TestReplyGetReply(t *testing.T) {
Expand Down
21 changes: 12 additions & 9 deletions examples/gno.land/r/demo/boards2/public.gno
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,21 @@ func DeleteReply(bid BoardID, threadID, replyID PostID) {

board := mustGetBoard(bid)
thread := mustGetThread(board, threadID)
assertReplyExists(thread, replyID)

// TODO: Hide reply when the caller is the owner of the reply without permission
// TODO: Support removing reply and children though proposals?
reply := mustGetReply(thread, replyID)
assertReplyVisible(reply)

caller := std.GetOrigCaller()
args := Args{bid, threadID, replyID}
gPerm.WithPermission(caller, PermissionReplyDelete, args, func(a Args) {
board := mustGetBoard(bid)
thread := mustGetThread(board, threadID)
if caller != reply.GetCreator() {
assertHasBoardPermission(board, caller, PermissionReplyDelete)
}

// Soft delete reply by changing its body when it contains
// sub-replies, otherwise hard delete it.
if reply.HasReplies() {
reply.Update(reply.GetTitle(), "this reply has been deleted")
} else {
thread.DeleteReply(replyID)
})
}
}

func EditThread(bid BoardID, threadID PostID, title, body string) {
Expand Down

0 comments on commit 2f99874

Please sign in to comment.