Skip to content

Commit

Permalink
add cmts
Browse files Browse the repository at this point in the history
  • Loading branch information
leohhhn committed Sep 30, 2024
1 parent e2dcbca commit ec2687b
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 62 deletions.
1 change: 1 addition & 0 deletions examples/gno.land/r/gnoland/hof/errors.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
111 changes: 49 additions & 62 deletions examples/gno.land/r/gnoland/hof/hof.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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 := "<div class='columns-2'>\n\n"

e.items.ReverseIterate("", "", func(key string, value interface{}) bool {
out += "<div>\n\n"
id, _ := seqid.FromString(key)
out += ufmt.Sprintf("### Submission #%d\n\n", int(id))
out += value.(*Item).Render()
out += "</div>"

return false
})
id := exhibCounter.Next()
e := &Exhibition{
id: id,
title: title,
description: description,
items: avl.NewTree(),
startTime: start,
endTime: end,
}

out += "</div><!-- /columns-2 -->\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
Expand All @@ -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
Expand Down
49 changes: 49 additions & 0 deletions examples/gno.land/r/gnoland/hof/rendering.gno
Original file line number Diff line number Diff line change
@@ -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 += "<div class='columns-2'>\n\n"

e.items.ReverseIterate("", "", func(key string, value interface{}) bool {
out += "<div>\n\n"
id, _ := seqid.FromString(key)
out += ufmt.Sprintf("### Submission #%d\n\n", int(id))
out += value.(*Item).Render()
out += "</div>"

return false
})

out += "</div><!-- /columns-2 -->\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
}
28 changes: 28 additions & 0 deletions examples/gno.land/r/leon/cmts/cmts.gno
Original file line number Diff line number Diff line change
@@ -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(),
}
}
1 change: 1 addition & 0 deletions examples/gno.land/r/leon/cmts/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/leon/cmts

0 comments on commit ec2687b

Please sign in to comment.