flag.gno
0.98 Kb ยท 40 lines
1package boards2
2
3import (
4 "strconv"
5
6 "gno.land/p/gnoland/boards"
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
15// flagItem adds a flag to a post.
16// Returns whether flag count threshold is reached and post can be hidden.
17// Panics if flag count threshold was already reached.
18func flagItem(post *boards.Post, user address, reason string, threshold int) bool {
19 if post.Flags.Size() >= threshold {
20 panic("flag count threshold exceeded: " + strconv.Itoa(threshold))
21 }
22
23 if post.Flags.Exists(user) {
24 panic("post has been already flagged by " + user.String())
25 }
26
27 post.Flags.Add(boards.Flag{
28 User: user,
29 Reason: reason,
30 })
31
32 return post.Flags.Size() == threshold
33}
34
35func getFlaggingThreshold(bid boards.ID) int {
36 if v, ok := gFlaggingThresholds.Get(bid.String()); ok {
37 return v.(int)
38 }
39 return DefaultFlaggingThreshold
40}