public.gno

1.64 Kb ยท 71 lines
 1package groups
 2
 3import (
 4	"std"
 5
 6	"gno.land/r/demo/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	std.AssertOriginCall()
22	caller := std.GetOrigCaller()
23	usernameOf(caller)
24	url := "/r/demo/groups:" + name
25	group := newGroup(url, name, caller)
26	gidkey := groupIDKey(group.id)
27	gGroups.Set(gidkey, group)
28	gGroupsByName.Set(name, group)
29	return group.id
30}
31
32func AddMember(gid GroupID, address string, weight int, metadata string) MemberID {
33	std.AssertOriginCall()
34	caller := std.GetOrigCaller()
35	usernameOf(caller)
36	group := getGroup(gid)
37	if !group.HasPermission(caller, EditPermission) {
38		panic("unauthorized to edit group")
39	}
40	user := users.GetUserByAddress(std.Address(address))
41	if user == nil {
42		panic("unknown address " + address)
43	}
44	mid := group.lastMemberID
45	member := group.newMember(mid, std.Address(address), weight, metadata)
46	midkey := memberIDKey(mid)
47	group.members.Set(midkey, member)
48	mid++
49	group.lastMemberID = mid
50	return member.id
51}
52
53func DeleteGroup(gid GroupID) {
54	std.AssertOriginCall()
55	caller := std.GetOrigCaller()
56	group := getGroup(gid)
57	if !group.HasPermission(caller, DeletePermission) {
58		panic("unauthorized to delete group")
59	}
60	group.deleteGroup()
61}
62
63func DeleteMember(gid GroupID, mid MemberID) {
64	std.AssertOriginCall()
65	caller := std.GetOrigCaller()
66	group := getGroup(gid)
67	if !group.HasPermission(caller, DeletePermission) {
68		panic("unauthorized to delete member")
69	}
70	group.deleteMember(mid)
71}