posts.gno
1.37 Kb ยท 55 lines
1package minisocial
2
3import (
4 "errors"
5 "std"
6 "time"
7
8 "gno.land/p/demo/ufmt"
9 "gno.land/p/moul/helplink"
10)
11
12var posts []*Post // inefficient for large amounts of posts; see v2
13
14// CreatePost creates a new post
15func CreatePost(text string) error {
16 // If the body of the post is empty, return an error
17 if text == "" {
18 return errors.New("empty post text")
19 }
20
21 // Append the new post to the list
22 posts = append(posts, &Post{
23 text: text, // Set the input text
24 author: std.PreviousRealm().Address(), // The author of the address is the previous realm, the realm that called this one
25 createdAt: time.Now(), // Capture the time of the transaction, in this case the block timestamp
26 })
27
28 return nil
29}
30
31func Render(_ string) string {
32 output := "# MiniSocial\n\n" // \n is needed just like in standard Markdown
33
34 // Create a clickable link to create a post
35 output += helplink.Func("Write a post!", "CreatePost", "text", "")
36 output += "\n\n"
37
38 // Handle the edge case
39 if len(posts) == 0 {
40 output += "No posts.\n"
41 return output
42 }
43
44 // Let's append the text of each post to the output
45 for i, post := range posts {
46 // Let's append some post metadata
47 output += ufmt.Sprintf("#### Post #%d\n\n", i)
48 // Add the stringified post
49 output += post.String()
50 // Add a line break for cleaner UI
51 output += "---\n\n"
52 }
53
54 return output
55}