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(boardsv2): initialize reposting #3469

Closed
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
45 changes: 45 additions & 0 deletions examples/gno.land/r/demo/boards2/post.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

	// TODO: figure out a way to decouple posts from a global storage.

I'm thinking we should eventually decouple it by having the render functions implemented as realm functions instead of Post methods, otherwise repostBoardID should instead be repostBoard being of type *Board.

cc @salmad3

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a separate issue.

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"
Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions examples/gno.land/r/demo/boards2/post_test.gno
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package boards2

import (
"std"
"strings"
"testing"

Expand Down Expand Up @@ -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
}{
Expand All @@ -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)
}
Expand Down
14 changes: 11 additions & 3 deletions examples/gno.land/r/demo/boards2/public.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
Loading