capabilities.gno
2.10 Kb · 66 lines
1package memba_notes_v1
2
3import (
4 "gno.land/p/samcrew/avl"
5 "strconv"
6)
7
8// Only explicit public grants are stored. Private notes keep their existing ACL.
9var publicWriteGrants = avl.NewTree()
10
11func publicWritesEnabled(n *note) bool {
12 _, granted := publicWriteGrants.Get(idText(n.ID))
13 return !n.Deleted && n.Mode == ModePublicOpen && granted
14}
15func resetPublicWrites(n *note) {
16 if _, granted := publicWriteGrants.Get(idText(n.ID)); granted {
17 publicWriteGrants.Remove(idText(n.ID))
18 }
19}
20
21// Community editing never grants ResolveComment or any owner/moderator action.
22// Keep requireWrite unchanged for those existing permissions.
23// While paused the grant is ignored, so only the existing ACL can edit.
24func requireContentWrite(cur realm, n *note, who address) {
25 if !paused && publicWritesEnabled(n) && cur.Previous().IsUserCall() {
26 return
27 }
28 requireWrite(cur, n, who)
29}
30
31func SetPublicWrites(cur realm, id [16]byte, baseRevision uint64, on bool, opID [16]byte) {
32 who := enter(cur, false)
33 defer leave()
34 mutateSetPublicWrites(cur, who, id, baseRevision, on, opID)
35}
36func mutateSetPublicWrites(cur realm, who address, id [16]byte, baseRevision uint64, on bool, opID [16]byte) {
37 n := live(id)
38 owner(n, who)
39 expected(n, baseRevision)
40 if n.Mode != ModePublicOpen {
41 panic("notes: public-open note required")
42 }
43 operation(n, opID)
44 if publicWritesEnabled(n) == on {
45 panic("notes: public writes unchanged")
46 }
47 if on {
48 publicWriteGrants.Set(idText(id), true)
49 } else {
50 resetPublicWrites(n)
51 }
52 bump(n, who, opID, "public-writes")
53}
54
55// Separate schema: existing NoteJSON consumers retain their exact field set.
56func PublicCapabilitiesJSON(idHex string) string {
57 v, ok := notes.Get(idText(readID(idHex)))
58 if !ok {
59 return "null"
60 }
61 n := v.(*note)
62 if !public(n.Mode) {
63 return "null"
64 }
65 return `{"schema":"memba-notes/public-capabilities/v1","id":` + q(idText(n.ID)) + `,"state_revision":` + u(n.Revision) + `,"owner_generation":` + u(n.OwnerGeneration) + `,"mode":` + strconv.Itoa(int(n.Mode)) + `,"deleted":` + strconv.FormatBool(n.Deleted) + `,"allow_public_writes":` + strconv.FormatBool(publicWritesEnabled(n)) + `}`
66}