public.gno
1.68 Kb ยท 75 lines
1package groups
2
3import (
4 "std"
5
6 "gno.land/r/sys/users"
7)
8
9//----------------------------------------
10// Public facing functions
11
12func GetGroupIDFromName(name string) (GroupID, bool) {
13 groupI, exists := gGroupsByName.Get(name)
14 if !exists {
15 return 0, false
16 }
17 return groupI.(*Group).id, true
18}
19
20func CreateGroup(name string) GroupID {
21 crossing()
22 std.AssertOriginCall()
23 caller := std.OriginCaller()
24 usernameOf(caller)
25 url := "/r/demo/groups:" + name
26 group := newGroup(url, name, caller)
27 gidkey := groupIDKey(group.id)
28 gGroups.Set(gidkey, group)
29 gGroupsByName.Set(name, group)
30 return group.id
31}
32
33func AddMember(gid GroupID, address string, weight int, metadata string) MemberID {
34 crossing()
35 std.AssertOriginCall()
36 caller := std.OriginCaller()
37 usernameOf(caller)
38 group := getGroup(gid)
39 if !group.HasPermission(caller, EditPermission) {
40 panic("unauthorized to edit group")
41 }
42 user := users.ResolveAddress(std.Address(address))
43 if user == nil {
44 panic("unknown address " + address)
45 }
46 mid := group.lastMemberID
47 member := group.newMember(mid, std.Address(address), weight, metadata)
48 midkey := memberIDKey(mid)
49 group.members.Set(midkey, member)
50 mid++
51 group.lastMemberID = mid
52 return member.id
53}
54
55func DeleteGroup(gid GroupID) {
56 crossing()
57 std.AssertOriginCall()
58 caller := std.OriginCaller()
59 group := getGroup(gid)
60 if !group.HasPermission(caller, DeletePermission) {
61 panic("unauthorized to delete group")
62 }
63 group.deleteGroup()
64}
65
66func DeleteMember(gid GroupID, mid MemberID) {
67 crossing()
68 std.AssertOriginCall()
69 caller := std.OriginCaller()
70 group := getGroup(gid)
71 if !group.HasPermission(caller, DeletePermission) {
72 panic("unauthorized to delete member")
73 }
74 group.deleteMember(mid)
75}