flag.gno
1.14 Kb ยท 43 lines
1package boards2
2
3import (
4 "strconv"
5
6 "gno.land/p/nt/avl"
7)
8
9// DefaultFlaggingThreshold defines the default number of flags that hides flaggable items.
10const DefaultFlaggingThreshold = 1
11
12var gFlaggingThresholds avl.Tree // string(board ID) -> int
13
14type Flaggable interface {
15 // Flag adds a new flag to an item.
16 // It returns false when the user flagging already flagged the item.
17 Flag(user address, reason string) bool
18
19 // FlagsCount returns number of times item was flagged.
20 FlagsCount() int
21}
22
23// flagItem adds a flag to a flaggable item.
24// Returns whether flag count threshold is reached and item can be hidden.
25// Panics if flag count threshold was already reached.
26func flagItem(item Flaggable, user address, reason string, threshold int) bool {
27 if item.FlagsCount() >= threshold {
28 panic("item flag count threshold exceeded: " + strconv.Itoa(threshold))
29 }
30
31 if !item.Flag(user, reason) {
32 panic("item has been already flagged by " + user.String())
33 }
34
35 return item.FlagsCount() == threshold
36}
37
38func getFlaggingThreshold(bid BoardID) int {
39 if v, ok := gFlaggingThresholds.Get(bid.String()); ok {
40 return v.(int)
41 }
42 return DefaultFlaggingThreshold
43}