Search Apps Documentation Source Content File Folder Download Copy Actions Download

format.gno

1.61 Kb ยท 83 lines
 1package boards2
 2
 3import (
 4	"strconv"
 5	"strings"
 6
 7	"gno.land/p/gnoland/boards"
 8	"gno.land/p/moul/md"
 9
10	"gno.land/r/gnoland/boards2/v1/permissions"
11	"gno.land/r/sys/users"
12)
13
14const dateFormat = "2006-01-02 3:04pm MST"
15
16func padLeft(s string, length int) string {
17	if len(s) >= length {
18		return s
19	}
20	return strings.Repeat(" ", length-len(s)) + s
21}
22
23func padZero(u64 uint64, length int) string {
24	s := strconv.Itoa(int(u64))
25	if len(s) >= length {
26		return s
27	}
28	return strings.Repeat("0", length-len(s)) + s
29}
30
31func indentBody(indent string, body string) string {
32	var (
33		res   string
34		lines = strings.Split(body, "\n")
35	)
36	for i, line := range lines {
37		if i > 0 {
38			// Add two spaces to keep newlines within Markdown
39			res += "  \n"
40		}
41		res += indent + line
42	}
43	return res
44}
45
46func summaryOf(text string, length int) string {
47	lines := strings.SplitN(text, "\n", 2)
48	line := lines[0]
49	if len(line) > length {
50		line = line[:(length-3)] + "..."
51	} else if len(lines) > 1 {
52		line = line + "..."
53	}
54	return line
55}
56
57func userLink(addr address) string {
58	if u := users.ResolveAddress(addr); u != nil {
59		return md.UserLink(u.Name())
60	}
61	return md.UserLink(addr.String())
62}
63
64func getRoleBadge(post *boards.Post) string {
65	if post == nil || post.Board == nil || post.Board.Permissions == nil {
66		return ""
67	}
68
69	perms := post.Board.Permissions
70	creator := post.Creator
71
72	// Check roles in order of priority
73	if perms.HasRole(creator, permissions.RoleOwner) {
74		return " `owner`"
75	}
76	if perms.HasRole(creator, permissions.RoleAdmin) {
77		return " `admin`"
78	}
79	if perms.HasRole(creator, permissions.RoleModerator) {
80		return " `mod`"
81	}
82	return ""
83}