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 6 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
56 changes: 52 additions & 4 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,22 @@ 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)
dst.threads.Set(pid.Key(), repost)
if !dst.IsPrivate() {
post.reposts.Set(dst.id.Key(), pid)
}

args := Args{creator, dst.id, pid}
dst.perms.WithPermission(creator, PermissionThreadRepost, args, func(_ Args) {
post.repostsCount++
dst.threads.Set(pid.Key(), repost)
if !dst.IsPrivate() {
post.reposts.Set(dst.id.Key(), pid)
}
})
x1unix marked this conversation as resolved.
Show resolved Hide resolved

return repost
}

Expand Down Expand Up @@ -271,6 +282,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 post is not available\n\n"), nil
x1unix marked this conversation as resolved.
Show resolved Hide resolved
}

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 +320,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
19 changes: 19 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 @@ -45,19 +46,33 @@ func TestPostSetVisible(t *testing.T) {
}

func TestPostAddRepostTo(t *testing.T) {
testAdminAddr := testutils.TestAddress("testAdmin")

cases := []struct {
name, title, body string
dstBoard *Board
thread *Post
creator std.Address
setup func() *Post
err string
}{
{
name: "repost thread",
title: "Repost Title",
body: "Repost body",
dstBoard: newBoard(42, "dst123", testAdminAddr),
creator: testAdminAddr,
setup: func() *Post {
return createTestThread(t)
},
},
{
name: "should check permissions",
title: "Repost Title",
body: "Repost body",
dstBoard: newBoard(42, "dst123", testutils.TestAddress("creatorDstBoard")),
setup: func() *Post { return createTestThread(t) },
err: "unauthorized",
},
{
name: "invalid repost from reply",
Expand All @@ -74,6 +89,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
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/boards2/public.gno
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ 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
}
Expand Down
Loading