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(boardsv2): experiment API - WIP #2902

Merged
merged 19 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
64 changes: 64 additions & 0 deletions examples/gno.land/p/demo/boardsv2/app.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package boardsv2

import (
"gno.land/demo/p/boardsv2/post"
contentplugin "gno.land/demo/p/boardsv2/post/plugins/content"
)

// type Rating struct{}
//
// var ratingIndex = avl.Tree{}
// app.AddBoardHook(func (changeType int, change ChangeSet) {
// if changeType == 0 {
// ratingIndex.Set("...", )
// }
// })

type App struct {
st Storage
boards []Board
}

func New(s Storage, o ...Option) App {
a := App{
st: Storage,
}
// Define the rule for a spesific view.
boardsView := view.New(view.Filter{
Level: 0, // this will give me the list of the boards.
})

return a
}

func (a *App) AddBoard(name, title, description string) (*Board, error) {
p := post.New(contentplugin.TitleBasedContent{
Title: title,
Description: description,
})

// I want to create a query for listing threads under this new board.
threadView := view.New(view.Filter{
Level: 1,
SlugPrefix: name,
})
userActivityView := view.New(view.Filter{
LevelGte: 2,
By: func(content Content) []View {
c.Author // by account address
}
})

if err := post.Add(a.st, name, p); err != nil {
nil, err
}
return a.GetBoard(name), nil
}

func (a *App) GetBoard(name string) (board *Board, found bool) {

}

func (a *App) ListBoards() ([]*Board, error) {

}
24 changes: 24 additions & 0 deletions examples/gno.land/p/demo/boardsv2/board.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package boardsv2

type Board struct {
}

func (b *Board) AddPost() error {

}

func (b *Board) GetThread(id string) (post *Post, found bool) {

}

func (b *Board) ListThreads(id string) (post *Post, found bool) {
threadView.List() // there should be an iterator, pagination
}

func (b *Board) Fork() error {

}

func (b *Board) Lock() error {

}
8 changes: 7 additions & 1 deletion examples/gno.land/p/demo/boardsv2/boardsv2.gno
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
package boardsv2
// boardsv2 is a reddit like abstraction around post/*.
// You might implement other abstractions around post/* to create
// different type of dApps.
// refer to the app.gno file to get started.
package boardsv2


57 changes: 57 additions & 0 deletions examples/gno.land/p/demo/boardsv2/draft1/boards.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package boardsv2

import (
"errors"
"strconv"
)

// NOTE: Boards type should be a realm type
type Boards struct {
// NOTE: Most of all types could be saved within the same AVL tree, prefixing the level
// NOTE: We might want different AVL trees to avoid using level prefixes
// NOTE: We could introduce the stores concept for the posts type instead
// NOTE: Again, we could consider using Node instead of Post, and tree instead of posts (semantics)
posts avl.Tree // string(Post.Level + slug) -> *Post (post, comment, poll)
}

// NOTE: Iterating only Post type is confusing semantically
// NOTE: Potential need cast/switch/type-check when iterating.
func (b Boards) Iterate(path string, fn func(*Post) bool) bool {}
func (b Boards) IteratePosts(path string, fn func(*Post) bool) bool {}
func (b Boards) IterateComments(path string, fn func(*Post) bool) bool {}

// How to map render paths to actual post instances?
//
// AVL KEYS BY LEVEL PREFIX (start/end)
// Boards => 0_ ... 1_
// Posts => 1_BOARD/ ... 2_
// Comments => 2_BOARD/POST/ ... 3_
//
// HOW TO GUESS PREFIX FROM SLUG
// User enters a SLUG => (one part => 1_BOARD)(more than one part => 1_BOARD/POST)
// How to recognize comments? Should be URL accesible? We could use ":" as separator (not optimal)

func (b *Boards) Set(p *Post) (updated bool) {
key := newKey(p.Level, p.Slug())
return b.posts.Set(key, p)
}

func (b *Boards) Remove(level int, path string) (_ *Post, removed bool) {
key := newKey(level, path)
if v, removed := b.posts.Remove(key); removed {
return v.(*Post), true
}
return nil, false
}

func (b Boards) Get(level int, path string) (_ *Post, found bool) {
key := newKey(level, path)
if v, found := b.posts.Get(key); found {
return v.(*Post), true
}
return "", false
}

func newKey(level int, path string) string {
return strconv.Itoa(level) + "_" + path
}
27 changes: 27 additions & 0 deletions examples/gno.land/p/demo/boardsv2/draft1/content_board.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package boardsv2

const ContentTypeBoard = "boards:board"

var _ Content = (*BoardContent)(nil)

type BoardContent struct {
jeronimoalbi marked this conversation as resolved.
Show resolved Hide resolved
Name string
}

func NewBoard() *Post {
return &Post{
// ...
Level: LevelBoard,
Content: &BoardContent{
// ...
},
}
}

func (c BoardContent) Type() string {
return ContentTypeBoard
}

func (c BoardContent) Render() string {
return ""
}
27 changes: 27 additions & 0 deletions examples/gno.land/p/demo/boardsv2/draft1/content_comment.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package boardsv2

const ContentTypeComment = "boards:comment"

var _ Content = (*CommentContent)(nil)

type CommentContent struct {
Body string
}

func NewComment() *Post {
return &Post{
// ...
Level: LevelComment,
Content: &CommentContent{
// ...
},
}
}

func (c CommentContent) Type() string {
return ContentTypeComment
}

func (c CommentContent) Render() string {
return ""
}
32 changes: 32 additions & 0 deletions examples/gno.land/p/demo/boardsv2/draft1/content_poll.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package boardsv2

const ContentTypePoll = "boards:poll"

var _ Content = (*PollContent)(nil)

type PollContent struct {
Question string
Options []string
Votes []struct {
Address std.Adress
Option string
}
}

func NewPoll() *Post {
return &Post{
// ...
Level: LevelPost,
Content: &PollContent{
// ...
},
}
}

func (c PollContent) Type() string {
return ContentTypePoll
}

func (c PollContent) Render() string {
return ""
}
29 changes: 29 additions & 0 deletions examples/gno.land/p/demo/boardsv2/draft1/content_post.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package boardsv2

const ContentTypePost = "boards:post"

var _ Content = (*TextContent)(nil)

type TextContent struct {
Title string
Body string
Tags []string
}

func NewPost() *Post {
return &Post{
// ...
Level: LevelPost,
Content: &TextContent{
// ...
},
}
}

func (c TextContent) Type() string {
return ContentTypePost
}

func (c TextContent) Render() string {
return ""
}
50 changes: 50 additions & 0 deletions examples/gno.land/p/demo/boardsv2/draft1/features.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package boardsv2

func AddBoard(s PostStore, slug string /* ... */) (path string, _ error) {
// TODO: Finish implementation

return slug, nil
}

// NOTE: Define a pattern to add functionality to posts by type (AddComment, AddThread, AddPoll, Repost, Upvote, ...)
// NOTE: Maybe though functions that assert the right arguments
func AddComment(s PostStore, parentPath string, creator std.Address, message string) (path string, _ error) {
// Try to get parent as a post or a comment, otherwise parent doesn't support comments
p, found := b.Get(LevelPost, parentPath)
if !found {
p, found = b.Get(LevelComment, parentPath)
if !found {
return "", errors.New("parent post or comment not found: " + parentPath)
}
}

comment := NewComment(p /* ... */)

// TODO: Finish implementation

path = parentPath + "/" + comment.ID
return path, nil
}

// NOTE: Arguments could potentially be many, consider variadic + sane defaults (?)
func AddThread(s PostStore, parentPath, slug string, creator std.Address /* ... */) (path string, _ error) {
p, found := b.Get(LevelPost, parentPath)
if !found {
return "", errors.New("parent post not found: " + parentPath)
}

post := NewPost(p, slug /* ... */)

// TODO: Finish implementation

path = parentPath + "/" + post.ID
return path, nil
}

// ----- Other features -----
// type VotesStore interface {
// /*...*/
// }
//
// func Upvote(s VotesStore /* ... */) {}
// func DownVote(s VotesStore /* ... */) {}
43 changes: 43 additions & 0 deletions examples/gno.land/p/demo/boardsv2/draft1/post.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package boardsv2

import (
"strconv"
)

const (
LevelBoard = iota
LevelPost
LevelComment
)

type (
Content interface {
Type() string
Render() string
}

// TODO: Still have to consider how to add custom features like up/down voting, reputation, fork, lock, ...
Post struct {
ID string // NOTE: It would be nice to use a type alias for the ID field type
Content Content // NOTE: Maybe should be called Type (board as content is odd)
Parent *Post // NOTE: If all is a post we need to have a parent
Level int
Base *Post
Children []*Post
Forks []*Post
UpdatedAt time.Time
CreatedAt time.Time
Creator std.Address
}
)

func (p Post) Slug() string { // NOTE: Not optimal, calculate slug dynamically
if p.Parent == nil {
return p.ID
}
return p.Parent.Slug() + "/" + p.ID
}

func (p Post) NextIncrementalKey(baseKey string) string {
return baseKey + "/" + strconv.Itoa(len(p.Children))
}
7 changes: 7 additions & 0 deletions examples/gno.land/p/demo/boardsv2/draft1/store.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package boardsv2

// NOTE: Maybe we could abstract the location where posts are stored
type PostStore interface {
Set(*Post) (updated bool)
Get(level int, path string) (_ *Post, found bool) // NOTE: Level could be a type alias for better semantics
}
11 changes: 11 additions & 0 deletions examples/gno.land/p/demo/boardsv2/option.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package boardsv2

type Option struct{}

// LinearReputationPolicy allows upvoting or downvoting a post by one
// for each account.
func LinearReputationPolicy() Option {}

// TokenBasedReputationPolicy allows upvoting or downvoting a post propotional
// to the specified tokens that an account holds.
func TokenBasedReputationPolicy() Option {}
5 changes: 5 additions & 0 deletions examples/gno.land/p/demo/boardsv2/post/content.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package post

type Content interface {
Render() string
}
Loading
Loading