misc.gno

1.88 Kb ยท 95 lines
 1package groups
 2
 3import (
 4	"strconv"
 5	"strings"
 6
 7	"gno.land/r/sys/users"
 8)
 9
10//----------------------------------------
11// private utility methods
12// XXX ensure these cannot be called from public.
13
14func getGroup(gid GroupID) *Group {
15	gidkey := groupIDKey(gid)
16	group_, exists := gGroups.Get(gidkey)
17	if !exists {
18		panic("group id (" + gid.String() + ") does not exists")
19	}
20	group := group_.(*Group)
21	return group
22}
23
24func incGetGroupID() GroupID {
25	gGroupsCtr++
26	return GroupID(gGroupsCtr)
27}
28
29func padLeft(str string, length int) string {
30	if len(str) >= length {
31		return str
32	}
33	return strings.Repeat(" ", length-len(str)) + str
34}
35
36func padZero(u64 uint64, length int) string {
37	str := strconv.Itoa(int(u64))
38	if len(str) >= length {
39		return str
40	}
41	return strings.Repeat("0", length-len(str)) + str
42}
43
44func groupIDKey(gid GroupID) string {
45	return padZero(uint64(gid), 10)
46}
47
48func memberIDKey(mid MemberID) string {
49	return padZero(uint64(mid), 10)
50}
51
52func indentBody(indent string, body string) string {
53	lines := strings.Split(body, "\n")
54	res := ""
55	for i, line := range lines {
56		if i > 0 {
57			res += "\n"
58		}
59		res += indent + line
60	}
61	return res
62}
63
64// NOTE: length must be greater than 3.
65func summaryOf(str string, length int) string {
66	lines := strings.SplitN(str, "\n", 2)
67	line := lines[0]
68	if len(line) > length {
69		line = line[:(length-3)] + "..."
70	} else if len(lines) > 1 {
71		// len(line) <= 80
72		line = line + "..."
73	}
74	return line
75}
76
77func displayAddressMD(addr address) string {
78	user := users.ResolveAddress(addr)
79	if user == nil {
80		return "[" + addr.String() + "](/u/" + addr.String() + ")"
81	}
82	return "[@" + user.Name() + "](/u/" + user.Name() + ")"
83}
84
85func usernameOf(addr address) string {
86	user := users.ResolveAddress(addr)
87	if user == nil {
88		panic("user not found")
89	}
90	return user.Name()
91}
92
93func isValidPermission(perm Permission) bool {
94	return perm == EditPermission || perm == DeletePermission
95}