Search Apps Documentation Source Content File Folder Download Copy Actions Download

board.gno

1.98 Kb · 83 lines
 1package hub
 2
 3import (
 4	"gno.land/p/gnoland/boards"
 5)
 6
 7// Member defines a type for board members.
 8type Member struct {
 9	Address address
10	Roles   []string
11}
12
13// Board defines a safe type for boards.
14type Board struct {
15	ref *boards.Board
16
17	// ID is the unique identifier of the board.
18	ID uint64
19
20	// Name is the current name of the board.
21	Name string
22
23	// Aliases contains a list of alternative names for the board.
24	Aliases []string
25
26	// Readonly indicates that the board is readonly.
27	Readonly bool
28
29	// ThreadsCount contains the number of threads within the board.
30	ThreadCount int
31
32	// MemberCount contains the number of members of the board.
33	MemberCount int
34
35	// Creator is the account address that created the board.
36	Creator address
37
38	// CreatedAt is the board's creation time as Unix time.
39	CreatedAt int64
40
41	// UpdatedAt is the board's update time as Unix time.
42	UpdatedAt int64
43}
44
45// IterateThreads iterates board threads by creation time.
46// To reverse iterate use a negative count.
47func (b Board) IterateThreads(start, count int, fn func(Thread) bool) bool {
48	return b.ref.Threads.Iterate(start, count, func(thread *boards.Post) bool {
49		return fn(NewSafeThread(thread))
50	})
51}
52
53// IterateMembers iterates board members.
54// To reverse iterate use a negative count.
55func (b Board) IterateMembers(start, count int, fn func(boards.User) bool) bool {
56	return b.ref.Permissions.IterateUsers(start, count, fn)
57}
58
59// NewSafeBoard creates a safe board.
60func NewSafeBoard(ref *boards.Board) Board {
61	var usersCount int
62	if ref.Permissions != nil {
63		usersCount = ref.Permissions.UsersCount()
64	}
65
66	var threadCount int
67	if ref.Threads != nil {
68		threadCount = ref.Threads.Size()
69	}
70
71	return Board{
72		ref:         ref,
73		ID:          uint64(ref.ID),
74		Name:        ref.Name,
75		Aliases:     append([]string(nil), ref.Aliases...),
76		Readonly:    ref.Readonly,
77		ThreadCount: threadCount,
78		MemberCount: usersCount,
79		Creator:     ref.Creator,
80		CreatedAt:   timeToUnix(ref.CreatedAt),
81		UpdatedAt:   timeToUnix(ref.UpdatedAt),
82	}
83}