Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

reviews_actions.gno

3.66 Kb · 108 lines
  1package memba_weighted_host
  2
  3import (
  4	"chain"
  5	"gno.land/p/samcrew/memba_weighted_policy"
  6	"strconv"
  7)
  8
  9const ReviewsV1Path = "gno.land/r/samcrew/memba_reviews_v1"
 10const ReviewsV2Path = "gno.land/r/samcrew/memba_reviews_v2"
 11
 12type ReviewsPolicy struct {
 13	dao       address
 14	target    string
 15	successor address
 16}
 17
 18func NewReviewsPolicy(dao address, target string, successor address) ReviewsPolicy {
 19	if dao != chain.PackageAddress("gno.land/r/samcrew/memba_dao") {
 20		panic("reviews policy requires the founding DAO address")
 21	}
 22	if target != ReviewsV1Path && target != ReviewsV2Path {
 23		panic("unsupported reviews target")
 24	}
 25	if !successor.IsValid() || successor == dao || successor == chain.PackageAddress(target) {
 26		panic("reviews policy requires a valid distinct successor")
 27	}
 28	return ReviewsPolicy{dao: dao, target: target, successor: successor}
 29}
 30func (p ReviewsPolicy) DAO() address       { return p.dao }
 31func (p ReviewsPolicy) Target() string     { return p.target }
 32func (p ReviewsPolicy) Successor() address { return p.successor }
 33
 34type ReviewsActionKind string
 35
 36const (
 37	ReviewsAcceptModerator ReviewsActionKind = "accept-moderator"
 38	ReviewsReturnModerator ReviewsActionKind = "return-moderator"
 39	ReviewsAbortReturn     ReviewsActionKind = "abort-return"
 40	ReviewsHideReview      ReviewsActionKind = "hide-review"
 41	ReviewsHideComment     ReviewsActionKind = "hide-comment"
 42	ReviewsUnhide          ReviewsActionKind = "unhide"
 43)
 44
 45type ReviewsAction struct {
 46	kind      ReviewsActionKind
 47	target    string
 48	id        uint64
 49	recipient address
 50}
 51
 52func (p ReviewsPolicy) Accept() ReviewsAction {
 53	return ReviewsAction{kind: ReviewsAcceptModerator, target: p.target}
 54}
 55func (p ReviewsPolicy) Return() ReviewsAction {
 56	return ReviewsAction{kind: ReviewsReturnModerator, target: p.target, recipient: p.successor}
 57}
 58func (p ReviewsPolicy) AbortReturn() ReviewsAction {
 59	return ReviewsAction{kind: ReviewsAbortReturn, target: p.target, recipient: p.successor}
 60}
 61func (p ReviewsPolicy) HideReview(id uint64) ReviewsAction {
 62	return p.itemAction(ReviewsHideReview, id)
 63}
 64func (p ReviewsPolicy) HideComment(id uint64) ReviewsAction {
 65	return p.itemAction(ReviewsHideComment, id)
 66}
 67func (p ReviewsPolicy) Unhide(id uint64) ReviewsAction { return p.itemAction(ReviewsUnhide, id) }
 68func (p ReviewsPolicy) itemAction(kind ReviewsActionKind, id uint64) ReviewsAction {
 69	if id == 0 {
 70		panic("reviews item ID must be positive")
 71	}
 72	return ReviewsAction{kind: kind, target: p.target, id: id}
 73}
 74func (a ReviewsAction) Kind() ReviewsActionKind { return a.kind }
 75func (a ReviewsAction) Target() string          { return a.target }
 76func (a ReviewsAction) ID() uint64              { return a.id }
 77func (a ReviewsAction) Recipient() address      { return a.recipient }
 78func (a ReviewsAction) Category() memba_weighted_policy.Category {
 79	a.Bytes()
 80	switch a.kind {
 81	case ReviewsHideReview, ReviewsHideComment, ReviewsUnhide:
 82		return memba_weighted_policy.Routine
 83	default:
 84		return memba_weighted_policy.Critical
 85	}
 86}
 87func (a ReviewsAction) Bytes() string {
 88	if a.target != ReviewsV1Path && a.target != ReviewsV2Path {
 89		panic("unsupported reviews target")
 90	}
 91	switch a.kind {
 92	case ReviewsAcceptModerator:
 93		if a.id != 0 || a.recipient != "" {
 94			panic("malformed reviews authority action")
 95		}
 96	case ReviewsReturnModerator, ReviewsAbortReturn:
 97		if a.id != 0 || !a.recipient.IsValid() {
 98			panic("malformed reviews authority action")
 99		}
100	case ReviewsHideReview, ReviewsHideComment, ReviewsUnhide:
101		if a.id == 0 || a.recipient != "" {
102			panic("malformed reviews item action")
103		}
104	default:
105		panic("unsupported reviews action")
106	}
107	return "reviews/v1|" + a.target + "|" + string(a.kind) + "|" + strconv.FormatUint(a.id, 10) + "|" + string(a.recipient)
108}