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