posts.gno
1.39 Kb ยท 54 lines
1package minisocial
2
3import (
4 "chain/runtime"
5 "errors"
6 "time"
7
8 "gno.land/p/moul/helplink"
9 "gno.land/p/nt/ufmt"
10)
11
12var posts []*Post // inefficient for large amounts of posts; see v2
13
14// CreatePost creates a new post
15func CreatePost(_ realm, 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: runtime.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 // Create a clickable link to create a post
34 output += helplink.Func("Write a post!", "CreatePost", "text", "")
35 output += "\n\n"
36
37 // Handle the edge case
38 if len(posts) == 0 {
39 output += "No posts.\n"
40 return output
41 }
42
43 // Let's append the text of each post to the output
44 for i, post := range posts {
45 // Let's append some post metadata
46 output += ufmt.Sprintf("#### Post #%d\n\n", i)
47 // Add the stringified post
48 output += post.String()
49 // Add a line break for cleaner UI
50 output += "---\n\n"
51 }
52
53 return output
54}