1package microposts
2
3import (
4 "errors"
5 "std"
6 "time"
7
8 "gno.land/p/demo/ufmt"
9 "gno.land/p/moul/md"
10)
11
12var posts []*Post
13
14type Post struct {
15 text string
16 author std.Address
17 createdAt time.Time
18}
19
20// CreatePost is automatically exposed as an endpoint
21// It can be accessed by anyone who has a user ID (key pair)
22func CreatePost(text string) error {
23 if text == "" {
24 return errors.New("empty post text")
25 }
26
27 posts = append(posts, &Post{
28 text: text,
29 author: std.PrevRealm().Addr(), // provided by env
30 createdAt: time.Now(),
31 })
32
33 return nil
34}
35
36func Render(_ string) string {
37 out := md.H1("Posts")
38
39 if len(posts) == 0 {
40 out += "No posts."
41 return out
42 }
43
44 for i := len(posts) - 1; i >= 0; i-- {
45 out += md.H3(ufmt.Sprintf("Post #%d\n\n", i))
46 out += posts[i].String()
47 out += "---\n\n"
48 }
49
50 return out
51}
52
53func (p Post) String() string {
54 out := p.text
55 out += "\n\n"
56 out += ufmt.Sprintf("_%s, by %s_", p.createdAt.Format("02 Jan 2006, 15:04"), p.author)
57 out += "\n\n"
58
59 return out
60}
microposts.gno
0.99 Kb ยท 60 lines