diff --git a/examples/gno.land/r/demo/boards2/post.gno b/examples/gno.land/r/demo/boards2/post.gno index 5a79302af22..8b0530fa8ff 100644 --- a/examples/gno.land/r/demo/boards2/post.gno +++ b/examples/gno.land/r/demo/boards2/post.gno @@ -38,6 +38,7 @@ type Post struct { threadID PostID // original Post.id parentID PostID // parent Post.id (if reply or repost) repostBoardID BoardID // original Board.id (if repost) + repostsCount uint64 createdAt time.Time updatedAt time.Time } @@ -158,12 +159,19 @@ func (post *Post) AddRepostTo(creator std.Address, title, body string, dst *Boar panic("cannot repost non-thread post") } + if post.isHidden { + panic("thread has been flagged as inappropriate") + } + pid := dst.incGetPostID() repost := newPost(dst, pid, creator, title, body, pid, post.id, post.board.id) + + post.repostsCount++ dst.threads.Set(pid.Key(), repost) if !dst.IsPrivate() { post.reposts.Set(dst.id.Key(), pid) } + return repost } @@ -271,6 +279,29 @@ func (post *Post) RenderSummary() string { return s } +func (post *Post) renderSourcePost(indent string) (string, *Post) { + if post.repostBoardID == 0 { + return "", nil + } + + // TODO: figure out a way to decouple posts from a global storage. + board, ok := getBoard(post.repostBoardID) + if !ok { + return indentBody(indent, "> Source board is not available\n\n"), nil + } + + srcPost, ok := board.GetThread(post.parentID) + if !ok { + return indentBody(indent, "> Source post is not available\n\n"), nil + } + + if srcPost.isHidden { + return indentBody(indent, "> Source post has been flagged as inappropriate\n\n"), nil + } + + return indentBody(indent, srcPost.GetSummary()) + "\n", srcPost +} + func (post *Post) Render(indent string, levels int) string { if post == nil { return "nil post" @@ -286,13 +317,27 @@ func (post *Post) Render(indent string, levels int) string { s += indent + "\n" } + srcContent, srcPost := post.renderSourcePost(indent) + s += indentBody(indent, post.body) + "\n" // TODO: indent body lines. + s += srcContent s += indent + "\\- " + newUserLink(post.creator) + ", " s += newLink(post.createdAt.Format(dateFormat), postURL) + + if post.repostsCount > 0 { + s += ", " + strconv.FormatUint(post.repostsCount, 10) + " reposts" + } + + if srcPost != nil { + s += " \\[" + newLink("see source post", srcPost.GetURL()) + "\\]" + } + s += " \\[" + newLink("reply", post.GetReplyFormURL()) + "]" + if post.IsThread() { s += " \\[" + newLink("repost", post.GetRepostFormURL()) + "]" } + s += " \\[" + newLink("x", post.GetDeleteFormURL()) + "]\n" if levels > 0 { diff --git a/examples/gno.land/r/demo/boards2/post_test.gno b/examples/gno.land/r/demo/boards2/post_test.gno index 232b4ea75cd..9ccf22ce060 100644 --- a/examples/gno.land/r/demo/boards2/post_test.gno +++ b/examples/gno.land/r/demo/boards2/post_test.gno @@ -1,6 +1,7 @@ package boards2 import ( + "std" "strings" "testing" @@ -49,6 +50,7 @@ func TestPostAddRepostTo(t *testing.T) { name, title, body string dstBoard *Board thread *Post + creator std.Address setup func() *Post err string }{ @@ -74,6 +76,10 @@ func TestPostAddRepostTo(t *testing.T) { thread = tc.setup() ) + if tc.creator != "" { + creator = tc.creator + } + createRepost := func() { repost = thread.AddRepostTo(creator, tc.title, tc.body, tc.dstBoard) } diff --git a/examples/gno.land/r/demo/boards2/public.gno b/examples/gno.land/r/demo/boards2/public.gno index ebc39a45bf0..d594c02d63b 100644 --- a/examples/gno.land/r/demo/boards2/public.gno +++ b/examples/gno.land/r/demo/boards2/public.gno @@ -97,7 +97,7 @@ func CreateReply(bid BoardID, threadID, replyID PostID, body string) PostID { // TODO: Assert thread is not locked // TODO: Assert that caller is a board member (when board type is invite only) var reply *Post - if replyID == threadID { + if replyID == 0 { // When the parent reply is the thread just add reply to thread reply = thread.AddReply(caller, body) } else { @@ -143,8 +143,16 @@ func CreateRepost(bid BoardID, threadID PostID, title, body string, dstBoardID B dst := mustGetBoard(dstBoardID) thread := mustGetThread(board, threadID) - repost := thread.AddRepostTo(caller, title, body, dst) - return repost.id + + var repostId PostID + + permArgs := Args{caller, board.GetID(), threadID, dst.GetID()} + dst.GetPermissions().WithPermission(caller, PermissionThreadRepost, permArgs, func(_ Args) { + repost := thread.AddRepostTo(caller, title, body, dst) + repostId = repost.GetPostID() + }) + + return repostId } func DeleteThread(bid BoardID, threadID PostID) {