Skip to content

Commit

Permalink
feat: add hiddenPostsForUser
Browse files Browse the repository at this point in the history
  • Loading branch information
hthieu1110 committed Aug 18, 2023
2 parents cecc1fd + cefd249 commit 5871be5
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 5 deletions.
15 changes: 15 additions & 0 deletions examples/gno.land/r/demo/social_feeds/CMD.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ gnokey maketx call \
-args "1" \
test1

gnokey maketx call \
-pkgpath "gno.land/r/demo/social_feeds" \
-func "HidePostForMe" \
-gas-fee 1000000ugnot \
-gas-wanted 3000000 \
-send "" \
-broadcast \
-args "1" \
-args "1" \
test1

// Query posts
gnokey query vm/qeval --data 'gno.land/r/demo/social_feeds
GetPosts(1, "", []uint64{}, 0, 10)'

gnokey maketx addpkg \
-deposit="1ugnot" \
-gas-fee="1ugnot" \
Expand Down
58 changes: 55 additions & 3 deletions examples/gno.land/r/demo/social_feeds/feed.gno
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ type Feed struct {
name string
creator std.Address
owner std.Address
posts avl.Tree // Post.id -> *Post
posts avl.Tree // pidkey -> *Post
createdAt time.Time
deleted avl.Tree

flags *flags_index.FlagsIndex
flags *flags_index.FlagsIndex
hiddenPostsByUser avl.Tree // std.Address => *avl.Tree (postID => bool)

postsCtr uint64
}
Expand Down Expand Up @@ -101,6 +102,39 @@ func (feed *Feed) FlagPost(flagBy std.Address, pid PostID) {
feed.flags.Flag(flagID, flagBy.String())
}

func (feed *Feed) HidePostForUser(caller std.Address, pid PostID) {
userAddr := caller.String()

value, exists := feed.hiddenPostsByUser.Get(userAddr)
var hiddenPosts *avl.Tree
if exists {
hiddenPosts = value.(*avl.Tree)
} else {
hiddenPosts = avl.NewTree()
feed.hiddenPostsByUser.Set(userAddr, hiddenPosts)
}

if hiddenPosts.Has(pid.String()) {
panic("PostID is already hidden: " + pid.String())
}

hiddenPosts.Set(pid.String(), true)
}

func (feed *Feed) UnHidePostForUser(userAddress std.Address, pid PostID) {
value, exists := feed.hiddenPostsByUser.Get(userAddress.String())
var hiddenPosts *avl.Tree
if exists {
hiddenPosts = value.(*avl.Tree)
_, removed := hiddenPosts.Remove(pid.String())
if !removed {
panic("Post is not hidden: " + pid.String())
}
} else {
panic("User has not hidden post: " + pid.String())
}
}

func (feed *Feed) Render() string {
pkgpath := std.CurrentRealmPath()

Expand All @@ -127,9 +161,27 @@ func (feed *Feed) Render() string {
return false
})

str += "------------------------- \n\n"
str += "-------------------------\n"
str += feed.flags.Dump()
}

str += "---------------------------------------\n"
if feed.hiddenPostsByUser.Size() > 0 {
str += "Hidden posts by users:\n\n"

feed.hiddenPostsByUser.Iterate("", "", func(userAddr string, value interface{}) bool {
hiddenPosts := value.(*avl.Tree)
str += "\nUser address: " + userAddr + "\n"

hiddenPosts.Iterate("", "", func(pid string, value interface{}) bool {
str += "- PostID: " + pid + "\n"
return false
})

return false
})
}

return str
}

Expand Down
43 changes: 41 additions & 2 deletions examples/gno.land/r/demo/social_feeds/feeds_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package social_feeds

import (
"encoding/base64"
"fmt"
"std"
"strconv"
"strings"
Expand All @@ -24,6 +23,7 @@ var (
cat1 = uint64(1)
cat2 = uint64(2)
user = testutils.TestAddress("user")
filter_all = []uint64{}
)

func getFeed1() *Feed {
Expand Down Expand Up @@ -164,7 +164,6 @@ func testFilterByCategories(t *testing.T) {
filter_cat1_2 := []uint64{1, 2}
filter_cat9 := []uint64{9}
filter_cat1_2_9 := []uint64{1, 2, 9}
filter_all := []uint64{}

feedID2 := CreateFeed("teritori2")
feed2 := mustGetFeed(feedID2)
Expand Down Expand Up @@ -401,6 +400,44 @@ func testMigrate(t *testing.T) {
}
}

func testHidePostForMe(t *testing.T) {
user := std.Address("user")
std.TestSetOrigCaller(user)

feedID8 := CreateFeed("teritor8")
feed8 := mustGetFeed(feedID8)

postIDToHide := CreatePost(feed8.id, rootPostID, cat1, "metadata1")
postID := CreatePost(feed8.id, rootPostID, cat1, "metadata1")

if count := countPosts(feed8.id, filter_all, 10); count != 2 {
t.Fatalf("expected posts count: 2, got %q.", count)
}

// Hide a post for me
HidePostForMe(feed8.id, postIDToHide)

if count := countPosts(feed8.id, filter_all, 10); count != 1 {
t.Fatalf("expected posts count after hidding: 1, got %q.", count)
}

// Query from another user should return full list
another := std.Address("another")
std.TestSetOrigCaller(another)

if count := countPosts(feed8.id, filter_all, 10); count != 2 {
t.Fatalf("expected posts count from another: 2, got %q.", count)
}

// UnHide a post for me
std.TestSetOrigCaller(user)
UnHidePostForMe(feed8.id, postIDToHide)

if count := countPosts(feed8.id, filter_all, 10); count != 2 {
t.Fatalf("expected posts count after unhidding: 2, got %q.", count)
}
}

func Test(t *testing.T) {
// Setup ================================================================
// NOTE: Dont know why std.GetOrigCaller in users.Register is always = std.GetCallerAt(1) here
Expand Down Expand Up @@ -428,4 +465,6 @@ func Test(t *testing.T) {
testFlagPost(t)

testMigrate(t)

testHidePostForMe(t)
}
24 changes: 24 additions & 0 deletions examples/gno.land/r/demo/social_feeds/public.gno
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ func GetPostsWithCaller(fid FeedID, callerAddrStr string, user string, categorie
return false
}

// Check if post is in hidden list
value, exists := feed.hiddenPostsByUser.Get(caller.String())
if exists {
hiddenPosts := value.(*avl.Tree)
// If post.id exists in hiddenPosts tree => that post is hidden
if hiddenPosts.Has(post.id.String()) {
return false
}
}

if skipped < offset {
skipped++
return false
Expand Down Expand Up @@ -220,6 +230,20 @@ func FlagPost(fid FeedID, pid PostID) {
feed.FlagPost(caller, pid)
}

func HidePostForMe(fid FeedID, pid PostID) {
caller := std.PrevRealm().Addr()
feed := mustGetFeed(fid)

feed.HidePostForUser(caller, pid)
}

func UnHidePostForMe(fid FeedID, pid PostID) {
caller := std.PrevRealm().Addr()
feed := mustGetFeed(fid)

feed.UnHidePostForUser(caller, pid)
}

func GetFlags(fid FeedID, limit uint64, offset uint64) string {
feed := mustGetFeed(fid)

Expand Down

0 comments on commit 5871be5

Please sign in to comment.