Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(boards2): change public delete reply function to soft delete replies #3606

Merged
Merged
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
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
Loading