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/social feeds tip filter flags #7

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6b210f7
wip: init + testing social feed
hthieu1110 Jul 3, 2023
7562657
feat: add tests
hthieu1110 Jul 4, 2023
8f67fcb
wip: try to output object to strinng
hthieu1110 Jul 4, 2023
3f5d519
wip: stringify objects
Jul 5, 2023
72eb493
feat: user binutils to convert data to buffer
hthieu1110 Jul 5, 2023
121faab
feat: add binutils
hthieu1110 Jul 5, 2023
5dc2d12
Merge branch 'feat/add-socia-feed-realm' of github.com:TERITORI/gno i…
hthieu1110 Jul 5, 2023
87cf403
wip: encoding reactions
Jul 6, 2023
7c1f7a2
feat: add GetPost by id
hthieu1110 Jul 6, 2023
fc909b0
wip: handle subposts
hthieu1110 Jul 10, 2023
e2a6a60
feat: add pagination for query posts/comments
Jul 10, 2023
736143e
feat: handle pginnations for posts
hthieu1110 Jul 10, 2023
c683cd3
chore: remove uneeded files
hthieu1110 Jul 10, 2023
c6ea0c6
Merge remote-tracking branch 'origin' into feat/add-socia-feed-realm
hthieu1110 Aug 3, 2023
18d7e3b
fix: remove unneeded AssertOrig
hthieu1110 Aug 3, 2023
b48e66d
feat: add render + remove check for origCaller
hthieu1110 Aug 5, 2023
6fb95d7
feat: add extra binutils
hthieu1110 Aug 5, 2023
dc88135
feat: add extra binutils
hthieu1110 Aug 5, 2023
a440d7f
chore: update module name
hthieu1110 Aug 5, 2023
5adb1a9
chore: update module name
hthieu1110 Aug 5, 2023
20481ba
feat: handle all categories
hthieu1110 Aug 8, 2023
030a5fb
feat: update social_feeds_v4
hthieu1110 Aug 8, 2023
5f05ad7
chore: optimize test code
hthieu1110 Aug 12, 2023
0f93a72
Add TipPost + tests
hthieu1110 Aug 12, 2023
480ebcc
Add filter by user + test
hthieu1110 Aug 12, 2023
e681af4
feat: add migration to social_feeds
hthieu1110 Aug 13, 2023
03e03a9
wip: add flag
hthieu1110 Aug 14, 2023
c662f34
wip: add flags
hthieu1110 Aug 14, 2023
2e93a46
wip
hthieu1110 Aug 15, 2023
208face
wip: add missinng files
hthieu1110 Aug 15, 2023
f9ffa3c
fix: test bank send
hthieu1110 Aug 15, 2023
d282920
chore: change branch
hthieu1110 Aug 16, 2023
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
42 changes: 42 additions & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,45 @@ clean:
GOFMT_FLAGS ?= -w
fmt:
go run -modfile ../misc/devdeps/go.mod mvdan.cc/gofumpt $(GOFMT_FLAGS) `find . -name "*.gno"`

.PHONY: create.pkg
create.pkg:
gnokey maketx addpkg \
-deposit="1ugnot" \
-gas-fee="1ugnot" \
-gas-wanted="5000000" \
-broadcast="true" \
-pkgdir="." \
-pkgpath="gno.land/r/demo/social_feeds_v3" \
-chainid "teritori-1" \
-remote "https://testnet.gno.teritori.com:26657" \
test1

.PHONY: create.feed
create.feed:
gnokey maketx call \
-pkgpath "gno.land/r/demo/social_feeds_v4" \
-func "CreateFeed" \
-gas-fee 1000000ugnot \
-gas-wanted 3000000 \
-send "" \
-broadcast \
-args "teritori" \
-chainid "teritori-1" \
-remote "https://testnet.gno.teritori.com:26657" \
test1

.PHONE: create.post
create.post:
gnokey maketx call \
-pkgpath "gno.land/r/demo/social_feeds_v3" \
-func "CreatePost" \
-gas-fee 1000000ugnot \
-gas-wanted 2000000 \
-send "" \
-broadcast \
-args "1" \
-args "0" \
-args "2" \
-args '{"gifs": [], "files": [], "title": "", "message": "Hello world !", "hashtags": [], "mentions": [], "createdAt": "2023-08-03T01:39:45.522Z", "updatedAt": "2023-08-03T01:39:45.522Z"}' \
test1
34 changes: 34 additions & 0 deletions examples/gno.land/p/demo/binutils/binutils.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package binutils

import (
"encoding/binary"
"errors"
)

var ErrInvalidLengthPrefixedString = errors.New("invalid length-prefixed string")

func EncodeLengthPrefixedStringUint16BE(s string) []byte {
b := make([]byte, 2+len(s))
binary.BigEndian.PutUint16(b, uint16(len(s)))
copy(b[2:], s)
return b
}

func DecodeLengthPrefixedStringUint16BE(b []byte) (string, []byte, error) {
if len(b) < 2 {
return "", nil, ErrInvalidLengthPrefixedString
}
l := binary.BigEndian.Uint16(b)
if len(b) < 2+int(l) {
return "", nil, ErrInvalidLengthPrefixedString
}
return string(b[2 : 2+l]), b[l+2:], nil
}

func MustDecodeLengthPrefixedStringUint16BE(b []byte) (string, []byte) {
s, r, err := DecodeLengthPrefixedStringUint16BE(b)
if err != nil {
panic(err)
}
return s, r
}
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/binutils/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/binutils
162 changes: 162 additions & 0 deletions examples/gno.land/p/demo/flags_index/flags_index.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package flags_index

import (
"strconv"

"gno.land/p/demo/avl"
)

type FlagID string

type FlagCount struct {
FlagID FlagID
Count uint64
}

type FlagsIndex struct {
flagsCounts []*FlagCount // sorted by count descending; TODO: optimize using big brain datastructure
flagsCountsByID *avl.Tree // key: flagID -> FlagCount
flagsByFlaggerID *avl.Tree // key: flaggerID -> *avl.Tree key: flagID -> struct{}
}

func NewFlagsIndex() *FlagsIndex {
return &FlagsIndex{
flagsCountsByID: avl.NewTree(),
flagsByFlaggerID: avl.NewTree(),
}
}

func (fi *FlagsIndex) HasFlagged(flagID FlagID, flaggerID string) bool {
if flagsByFlagID, ok := fi.flagsByFlaggerID.Get(flaggerID); ok {
if flagsByFlagID.(*avl.Tree).Has(string(flagID)) {
return true
}
}
return false
}

func (fi *FlagsIndex) GetFlagCount(flagID FlagID) uint64 {
if flagCount, ok := fi.flagsCountsByID.Get(string(flagID)); ok {
return flagCount.(*FlagCount).Count
}
return 0
}

func (fi *FlagsIndex) GetFlags(limit uint64, offset uint64) []*FlagCount {
if limit == 0 {
return nil
}
if offset >= uint64(len(fi.flagsCounts)) {
return nil
}
if offset+limit > uint64(len(fi.flagsCounts)) {
limit = uint64(len(fi.flagsCounts)) - offset
}
return fi.flagsCounts[offset : offset+limit]
}

func (fi *FlagsIndex) Flag(flagID FlagID, flaggerID string) {
// update flagsByFlaggerID
var flagsByFlagID *avl.Tree
if existingFlagsByFlagID, ok := fi.flagsByFlaggerID.Get(flaggerID); ok {
flagsByFlagID = existingFlagsByFlagID.(*avl.Tree)
if flagsByFlagID.(*avl.Tree).Has(string(flagID)) {
panic("already flagged")
}
} else {
newFlagsByFlagID := avl.NewTree()
fi.flagsByFlaggerID.Set(flaggerID, newFlagsByFlagID)
flagsByFlagID = newFlagsByFlagID
}
flagsByFlagID.Set(string(flagID), struct{}{})

// update flagsCountsByID and flagsCounts
iFlagCount, ok := fi.flagsCountsByID.Get(string(flagID))
if !ok {
flagCount := &FlagCount{FlagID: flagID, Count: 1}
fi.flagsCountsByID.Set(string(flagID), flagCount)
fi.flagsCounts = append(fi.flagsCounts, flagCount) // this is valid because 1 will always be the lowest count and we want the newest flags to be last
} else {
flagCount := iFlagCount.(*FlagCount)
flagCount.Count++
// move flagCount to correct position in flagsCounts
for i := len(fi.flagsCounts) - 1; i > 0; i-- {
if fi.flagsCounts[i].Count > fi.flagsCounts[i-1].Count {
fi.flagsCounts[i], fi.flagsCounts[i-1] = fi.flagsCounts[i-1], fi.flagsCounts[i]
} else {
break
}
}
}
}

func (fi *FlagsIndex) ClearFlagCount(flagID FlagID) {
// find flagCount in byID
if !fi.flagsCountsByID.Has(string(flagID)) {
panic("flag ID not found")
}

// remove from byID
fi.flagsCountsByID.Remove(string(flagID))

// remove from byCount, we need to recreate the slice since splicing is broken
newByCount := []*FlagCount{}
for i := range fi.flagsCounts {
if fi.flagsCounts[i].FlagID == flagID {
continue
}
newByCount = append(newByCount, fi.flagsCounts[i])
}
fi.flagsCounts = newByCount

// update flagsByFlaggerID
var empty []string
fi.flagsByFlaggerID.Iterate("", "", func(key string, value interface{}) bool {
t := value.(*avl.Tree)
t.Remove(string(flagID))
if t.Size() == 0 {
empty = append(empty, key)
}
return false
})
for _, key := range empty {
fi.flagsByFlaggerID.Remove(key)
}
}

func (fi *FlagsIndex) Dump() string {
str := ""

str += "## flagsCounts:\n"
for i := range fi.flagsCounts {
str += "- "
if fi.flagsCounts[i] == nil {
str += "nil (" + strconv.Itoa(i) + ")\n"
continue
}
str += string(fi.flagsCounts[i].FlagID) + " " + strconv.FormatUint(fi.flagsCounts[i].Count, 10) + "\n"
}

str += "\n## flagsCountsByID:\n"
fi.flagsCountsByID.Iterate("", "", func(key string, value interface{}) bool {
str += "- "
if value == nil {
str += "nil (" + key + ")\n"
return false
}
str += key + ": " + string(value.(*FlagCount).FlagID) + " " + strconv.FormatUint(value.(*FlagCount).Count, 10) + "\n"
return false
})

str += "\n## flagsByFlaggerID:\n"
fi.flagsByFlaggerID.Iterate("", "", func(key string, value interface{}) bool {
str += "- " + key + ":\n"
value.(*avl.Tree).Iterate("", "", func(key string, value interface{}) bool {
str += " - " + key + "\n"
return false
})
return false
})

return str
}
5 changes: 5 additions & 0 deletions examples/gno.land/p/demo/flags_index/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module gno.land/p/demo/flags_index

require (
"gno.land/p/demo/avl" v0.0.0-latest
)
41 changes: 41 additions & 0 deletions examples/gno.land/r/demo/social_feeds/CMD.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
gnokey maketx call \
-pkgpath "gno.land/r/demo/social_feeds" \
-func "CreateFeed" \
-gas-fee 1000000ugnot \
-gas-wanted 3000000 \
-send "" \
-broadcast \
-args "teritori" \
test1

gnokey maketx call \
-pkgpath "gno.land/r/demo/social_feeds" \
-func "CreatePost" \
-gas-fee 1000000ugnot \
-gas-wanted 2000000 \
-send "" \
-broadcast \
-args "1" \
-args "0" \
-args "2" \
-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 addpkg \
-deposit="1ugnot" \
-gas-fee="1ugnot" \
-gas-wanted="5000000" \
-broadcast="true" \
-pkgdir="." \
-pkgpath="gno.land/r/demo/social_feeds_v2" \
test1

gnokey maketx call \
-pkgpath "gno.land/r/demo/social_feeds" \
-func "MigrateFromPreviousFeed" \
-gas-fee 1000000ugnot \
-gas-wanted 2000000 \
-send "" \
-broadcast \
test1

12 changes: 12 additions & 0 deletions examples/gno.land/r/demo/social_feeds/binutils_extra.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package social_feeds

import (
"encoding/binary"
)

func EncodeLengthPrefixedStringUint32BE(s string) []byte {
b := make([]byte, 4+len(s))
binary.BigEndian.PutUint32(b, uint32(len(s)))
copy(b[4:], s)
return b
}
Loading
Loading