Skip to content

Commit

Permalink
wip: handle flag posts
Browse files Browse the repository at this point in the history
  • Loading branch information
hthieu1110 committed Aug 17, 2023
1 parent d282920 commit cefd249
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 7 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 @@ -21,6 +21,21 @@ gnokey maketx call \
-args '{"gifs": [], "files": [], "title": "", "message": "Hello world 2 !", "hashtags": [], "mentions": [], "createdAt": "2023-08-03T01:39:45.522Z", "updatedAt": "2023-08-03T01:39:45.522Z"}' \
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 @@ -22,11 +22,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 @@ -96,6 +97,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 {
str := ""
str += "There are " + intToString(feed.posts.Size()) + " post(s) \n\n"
Expand All @@ -107,7 +141,7 @@ func (feed *Feed) Render() string {
}

post := value.(*Post)
postUrl := "/r/demo/social_feeds_v4:" + feed.name + "/" + post.id.String()
postUrl := "/r/demo/social_feeds:" + feed.name + "/" + post.id.String()

str += " * [" +
"PostID: " + post.id.String() +
Expand All @@ -120,6 +154,24 @@ func (feed *Feed) Render() string {
return false
})
}

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 @@ -383,6 +382,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 All @@ -408,4 +445,6 @@ func Test(t *testing.T) {
testFilterUser(t)

testMigrate(t)

testHidePostForMe(t)
}
10 changes: 9 additions & 1 deletion examples/gno.land/r/demo/social_feeds/post.gno
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,20 @@ func (post *Post) Delete() {
post.deletedAt = time.Now()
}

// TODO: fix this, dont base on amount, we should base on value sent by user
/*
sent := std.GetOrigSend()
anonFeeCoin := std.Coin{"ugnot", int64(gDefaultAnonFee)}
if len(sent) == 1 && sent[0].IsGTE(anonFeeCoin) {
return true
}
return false
*/
func (post *Post) Tip(from std.Address, to std.Address, amount int64) {
if amount <= 0 {
panic("amount is not valid")
}

// banker := std.GetBanker(std.BankerTypeOrigSend)
banker := std.GetBanker(std.BankerTypeRealmSend)
coinsToSend := std.Coins{std.Coin{Denom: "ugnot", Amount: amount}}

Expand Down
31 changes: 30 additions & 1 deletion examples/gno.land/r/demo/social_feeds/public.gno
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,14 @@ func TipPost(fid FeedID, pid PostID, amount int64) {

// Get Posts list
func GetPosts(fid FeedID, user string, categories []uint64, offset uint64, limit uint8) string {
caller := std.PrevRealm().Addr()
// caller := std.PrevRealm().Addr()
// NOTE: dont know why we cannot get caller by std.PrevRealm().Addr()
// and seems that this line works
caller := std.GetCallerAt(2)
feed := mustGetFeed(fid)

panic(caller.String())

var postList []string
var skipped uint64

Expand All @@ -106,6 +111,16 @@ func GetPosts(fid FeedID, user string, categories []uint64, offset uint64, limit
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 @@ -170,6 +185,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 cefd249

Please sign in to comment.