flag.gno

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