feed_actions.gno
2.91 Kb · 93 lines
1package memba_weighted_host
2
3import (
4 "chain"
5 "gno.land/p/samcrew/memba_weighted_policy"
6)
7
8const FeedPath = "gno.land/r/samcrew/memba_feed_v1"
9
10type FeedActionKind string
11
12const (
13 FeedAcceptOwner FeedActionKind = "accept-owner"
14 FeedReturnOwner FeedActionKind = "return-owner"
15 FeedAbortReturn FeedActionKind = "abort-return"
16 FeedAddModerator FeedActionKind = "add-moderator"
17 FeedRemoveModerator FeedActionKind = "remove-moderator"
18 FeedUnpause FeedActionKind = "unpause"
19)
20
21type FeedPolicy struct{ dao, successor address }
22
23func NewFeedPolicy(dao, successor address) FeedPolicy {
24 if !dao.IsValid() || dao != chain.PackageAddress("gno.land/r/samcrew/memba_dao") {
25 panic("feed policy requires the founding DAO address")
26 }
27 if !successor.IsValid() || successor == dao || successor == chain.PackageAddress(FeedPath) {
28 panic("feed policy requires a valid distinct successor")
29 }
30 return FeedPolicy{dao: dao, successor: successor}
31}
32func (p FeedPolicy) DAO() address { return p.dao }
33func (p FeedPolicy) Successor() address { return p.successor }
34
35type FeedAction struct {
36 kind FeedActionKind
37 recipient address
38 subject address
39}
40
41func (p FeedPolicy) Accept() FeedAction { return FeedAction{kind: FeedAcceptOwner} }
42func (p FeedPolicy) Return() FeedAction {
43 return FeedAction{kind: FeedReturnOwner, recipient: p.successor}
44}
45func (p FeedPolicy) AbortReturn() FeedAction {
46 return FeedAction{kind: FeedAbortReturn, recipient: p.successor}
47}
48func (p FeedPolicy) AddModerator(who address) FeedAction {
49 if who == p.dao || who == chain.PackageAddress(FeedPath) {
50 panic("feed moderator must be an external account")
51 }
52 a := FeedAction{kind: FeedAddModerator, subject: who}
53 a.Bytes()
54 return a
55}
56func FeedRevokeModerator(who address) FeedAction {
57 a := FeedAction{kind: FeedRemoveModerator, subject: who}
58 a.Bytes()
59 return a
60}
61func FeedResume() FeedAction { return FeedAction{kind: FeedUnpause} }
62func (a FeedAction) Kind() FeedActionKind { return a.kind }
63func (a FeedAction) Recipient() address { return a.recipient }
64func (a FeedAction) Subject() address { return a.subject }
65func (a FeedAction) Bytes() string {
66 base := "feed/v1|" + FeedPath + "|" + string(a.kind)
67 switch a.kind {
68 case FeedAcceptOwner, FeedUnpause:
69 if a.recipient != "" || a.subject != "" {
70 panic("malformed feed action")
71 }
72 return base
73 case FeedReturnOwner, FeedAbortReturn:
74 if !a.recipient.IsValid() || a.subject != "" {
75 panic("malformed feed return action")
76 }
77 return base + "|" + string(a.recipient)
78 case FeedAddModerator, FeedRemoveModerator:
79 if !a.subject.IsValid() || a.recipient != "" {
80 panic("invalid feed moderator action")
81 }
82 return base + "|" + string(a.subject)
83 default:
84 panic("unsupported feed action")
85 }
86}
87func (a FeedAction) Category() memba_weighted_policy.Category {
88 a.Bytes()
89 if a.kind == FeedUnpause {
90 return memba_weighted_policy.Financial
91 }
92 return memba_weighted_policy.Critical
93}