From ec2687b543cd33e98794ef84913b7506d08350e8 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 30 Sep 2024 20:08:09 +0200 Subject: [PATCH] add cmts --- examples/gno.land/r/gnoland/hof/errors.gno | 1 + examples/gno.land/r/gnoland/hof/hof.gno | 111 ++++++++---------- examples/gno.land/r/gnoland/hof/rendering.gno | 49 ++++++++ examples/gno.land/r/leon/cmts/cmts.gno | 28 +++++ examples/gno.land/r/leon/cmts/gno.mod | 1 + 5 files changed, 128 insertions(+), 62 deletions(-) create mode 100644 examples/gno.land/r/gnoland/hof/rendering.gno create mode 100644 examples/gno.land/r/leon/cmts/cmts.gno create mode 100644 examples/gno.land/r/leon/cmts/gno.mod diff --git a/examples/gno.land/r/gnoland/hof/errors.gno b/examples/gno.land/r/gnoland/hof/errors.gno index 4847b5a19cd..9ca22a2c7d3 100644 --- a/examples/gno.land/r/gnoland/hof/errors.gno +++ b/examples/gno.land/r/gnoland/hof/errors.gno @@ -5,6 +5,7 @@ import "errors" var ( ErrNoSuchItem = errors.New("hof: no such item exists") ErrNoSuchExhibition = errors.New("hof: no such exhibition exists") + ErrInvalidTitle = errors.New("hof: invalid title") ErrDoubleUpvote = errors.New("hof: cannot upvote twice") ErrDoubleDownvote = errors.New("hof: cannot downvote twice") ) diff --git a/examples/gno.land/r/gnoland/hof/hof.gno b/examples/gno.land/r/gnoland/hof/hof.gno index ece4f4b65df..d2ef85615ad 100644 --- a/examples/gno.land/r/gnoland/hof/hof.gno +++ b/examples/gno.land/r/gnoland/hof/hof.gno @@ -6,20 +6,21 @@ package hof import ( "gno.land/p/demo/avl" "gno.land/p/demo/seqid" - "gno.land/p/demo/ufmt" "std" + "strings" "time" ) var ( exhibCounter seqid.ID permanent *Exhibition + temporary *avl.Tree // id > *Exhibition ) type ( Exhibition struct { - id seqid.ID // fixed - itemCounter seqid.ID // + id seqid.ID // fixed per exhibition + itemCounter seqid.ID title string description string startTime *time.Time // given in RFC3339 @@ -43,8 +44,11 @@ func init() { title: "Permanent Exhibition", items: avl.NewTree(), } + + temporary = avl.NewTree() } +// Register registers your realm to the permanent exhibition func Register() { submission := std.PrevRealm() pkgpath := submission.PkgPath() @@ -67,73 +71,56 @@ func Register() { }) } -func RegisterFor(exhibID string) { - submission := std.PrevRealm() - pkgpath := submission.PkgPath() +// CreateTemporaryExhibition creates a temporary exhibition to be displayed while it's active +func CreateTemporaryExhibition(title, description string, start, end *time.Time) error { + // todo add admin checks - // must not exist and must be called from code - if permanent.items.Has(pkgpath) || submission.IsUser() { - panic("item exists or not code call") + if strings.TrimSpace(title) == "" { + return ErrInvalidTitle } - id := permanent.itemCounter.Next() - permanent.items.Set( - id.String(), - &Item{ - id: id, - pkgpath: pkgpath, - deployer: std.GetOrigCaller(), - blockNum: std.GetHeight(), - upvote: avl.NewTree(), - downvote: avl.NewTree(), - }) -} - -func Render(_ string) string { - out := "# Hall of Fame\n\n" - - out += permanent.Render() - - // todo render temp exhibitions - - return out -} - -func (e Exhibition) Render() string { - //out := ufmt.Sprintf("## %s\n\n", e.title) - //out += ufmt.Sprintf("%s\n\n", e.description) - - out := "
\n\n" - - e.items.ReverseIterate("", "", func(key string, value interface{}) bool { - out += "
\n\n" - id, _ := seqid.FromString(key) - out += ufmt.Sprintf("### Submission #%d\n\n", int(id)) - out += value.(*Item).Render() - out += "
" - - return false - }) + id := exhibCounter.Next() + e := &Exhibition{ + id: id, + title: title, + description: description, + items: avl.NewTree(), + startTime: start, + endTime: end, + } - out += "
\n\n" + temporary.Set(id.String(), e) - return out + return nil } -func (i Item) Render() string { - //subpath := i.pkgpath[strings.Index(i.pkgpath, "/"):] - - out := ufmt.Sprintf("\n```\n%s\n```\n\n", i.pkgpath) - out += ufmt.Sprintf("by %s\n\n", i.deployer.String()) - out += ufmt.Sprintf("Published at Block #%d\n\n", i.blockNum) - out += ufmt.Sprintf(` -#### [%d 👍](/r/gnoland/hof?help&__func=Upvote&exhibitionID=0&itemID=%s) - [%d 👎](/r/gnoland/hof?help&__func=Downvote&exhibitionID=0&itemID=%s)`, i.upvote.Size(), i.id.String(), i.downvote.Size(), i.id.String()) - - return out -} +// RegisterFor registers your realm for a specific exhibition +//func RegisterFor(exhibID string) { +// submission := std.PrevRealm() +// pkgpath := submission.PkgPath() +// +// raw, ok := temporary +// +// // must not exist and must be called from code +// if permanent.items.Has(pkgpath) || submission.IsUser() { +// panic("item exists or not code call") +// } +// +// id := permanent.itemCounter.Next() +// permanent.items.Set( +// id.String(), +// &Item{ +// id: id, +// pkgpath: pkgpath, +// deployer: std.GetOrigCaller(), +// blockNum: std.GetHeight(), +// upvote: avl.NewTree(), +// downvote: avl.NewTree(), +// }) +//} func Upvote(exhibID, itemID seqid.ID) error { - if exhibID == seqid.ID(0) { + if exhibID == seqid.ID(0) { // permanent exhibition raw, ok := permanent.items.Get(itemID.String()) if !ok { return ErrNoSuchItem @@ -155,7 +142,7 @@ func Upvote(exhibID, itemID seqid.ID) error { } func Downvote(exhibID, itemID seqid.ID) error { - if exhibID == seqid.ID(0) { + if exhibID == seqid.ID(0) { // permanent exhibition raw, ok := permanent.items.Get(itemID.String()) if !ok { return ErrNoSuchItem diff --git a/examples/gno.land/r/gnoland/hof/rendering.gno b/examples/gno.land/r/gnoland/hof/rendering.gno new file mode 100644 index 00000000000..4f8b6fce0b0 --- /dev/null +++ b/examples/gno.land/r/gnoland/hof/rendering.gno @@ -0,0 +1,49 @@ +package hof + +import ( + "github.com/gnolang/gno/examples/gno.land/p/demo/seqid" + "github.com/gnolang/gno/examples/gno.land/p/demo/ufmt" +) + +func Render(_ string) string { + out := "# Hall of Fame\n\n" + + out += permanent.Render() + + // todo render temp exhibitions + + return out +} + +func (e Exhibition) Render() string { + out := ufmt.Sprintf("## %s\n\n", e.title) + out += ufmt.Sprintf("%s\n\n", e.description) + + out += "
\n\n" + + e.items.ReverseIterate("", "", func(key string, value interface{}) bool { + out += "
\n\n" + id, _ := seqid.FromString(key) + out += ufmt.Sprintf("### Submission #%d\n\n", int(id)) + out += value.(*Item).Render() + out += "
" + + return false + }) + + out += "
\n\n" + + return out +} + +func (i Item) Render() string { + //subpath := i.pkgpath[strings.Index(i.pkgpath, "/"):] + + out := ufmt.Sprintf("\n```\n%s\n```\n\n", i.pkgpath) + out += ufmt.Sprintf("by %s\n\n", i.deployer.String()) + out += ufmt.Sprintf("Published at Block #%d\n\n", i.blockNum) + out += ufmt.Sprintf(` +#### [%d 👍](/r/gnoland/hof?help&__func=Upvote&exhibID=0&itemID=%s) - [%d 👎](/r/gnoland/hof?help&__func=Downvote&exhibID=0&itemID=%s)`, i.upvote.Size(), i.id.String(), i.downvote.Size(), i.id.String()) + + return out +} diff --git a/examples/gno.land/r/leon/cmts/cmts.gno b/examples/gno.land/r/leon/cmts/cmts.gno new file mode 100644 index 00000000000..be7b7394f18 --- /dev/null +++ b/examples/gno.land/r/leon/cmts/cmts.gno @@ -0,0 +1,28 @@ +// Package cmts a simple, pluggable, yet configurable comment functionality set +package cmts + +import ( + "github.com/gnolang/gno/examples/gno.land/p/demo/avl" + "github.com/gnolang/gno/examples/gno.land/p/demo/ownable" + "github.com/gnolang/gno/gnovm/stdlibs/std" +) + +type CommentSet struct { + owner *ownable.Ownable + set *avl.Tree +} + +type Comment struct { + text string + author std.Address + up, down int + replies *avl.Tree + deletable bool +} + +func NewCommentSet() *CommentSet { + return &CommentSet{ + owner: ownable.New(), + set: avl.NewTree(), + } +} diff --git a/examples/gno.land/r/leon/cmts/gno.mod b/examples/gno.land/r/leon/cmts/gno.mod new file mode 100644 index 00000000000..d99e4fc5c66 --- /dev/null +++ b/examples/gno.land/r/leon/cmts/gno.mod @@ -0,0 +1 @@ +module gno.land/p/leon/cmts \ No newline at end of file