-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathforum.gno
56 lines (46 loc) · 1019 Bytes
/
forum.gno
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package forum
import (
"gno.land/p/demo/avl"
"gno.land/p/demo/ufmt"
"std"
"strconv"
"strings"
)
var (
idCounter int
threadList avl.Tree // id -> *Thread
)
type Thread struct {
ID int
Title string
Body string
Author std.Address
}
func NewThread(title, body string) (threadID int) {
idCounter++
threadList.Set(strconv.Itoa(idCounter), &Thread{
ID: idCounter,
Title: title,
Body: body,
Author: std.PrevRealm().Addr(),
})
return idCounter
}
func Render(param string) string {
if param != "" {
val, ok := threadList.Get(param)
if !ok {
panic("thread not found")
}
thread := val.(*Thread)
return ufmt.Sprintf("# %s\n\n%s", thread.Title, thread.Body)
}
var bld strings.Builder
bld.WriteString("# Forum\n")
threadList.Iterate("", "", func(key string, value interface{}) bool {
thread := value.(*Thread)
bld.WriteString(ufmt.Sprintf("- %s: [%s](./forum:%s) by %s\n", key, thread.Title, key, thread.Author.String()))
return false
})
return bld.String()
}