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

flag.gno

1.00 Kb · 40 lines
 1package boards2
 2
 3import (
 4	"strconv"
 5
 6	"gno.land/p/gnoland/boards/v0"
 7	"gno.land/p/nt/bptree/v0"
 8)
 9
10// DefaultFlaggingThreshold defines the default number of flags that hides flaggable items.
11const DefaultFlaggingThreshold = 1
12
13var gFlaggingThresholds bptree.BPTree // 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 := gFlaggingThresholds.Get(bid.String()); v != nil {
37		return v.(int)
38	}
39	return DefaultFlaggingThreshold
40}