hub.gno
4.88 Kb · 195 lines
1package hub
2
3import (
4 "gno.land/p/gnoland/boards"
5
6 boards2 "gno.land/r/gnoland/boards2/v1"
7)
8
9// GetBoard returns a safe board.
10func GetBoard(id uint64) (Board, bool) {
11 b, found := getBoard(id)
12 if !found {
13 return Board{}, false
14 }
15 return NewSafeBoard(b), true
16}
17
18// GetThread returns a safe board thread.
19func GetThread(boardID, threadID uint64) (Thread, bool) {
20 t, found := getThread(boardID, threadID)
21 if !found {
22 return Thread{}, false
23 }
24 return NewSafeThread(t), true
25}
26
27// GetComment returns a safe thread comment.
28func GetComment(boardID, threadID, commentID uint64) (Comment, bool) {
29 c, found := getComment(boardID, threadID, commentID)
30 if !found {
31 return Comment{}, false
32 }
33 return NewSafeComment(c), true
34}
35
36// GetBoards returns a list with all boards.
37// To reverse iterate use a negative count.
38func GetBoards(start, count int) []Board {
39 var boards_ []Board
40
41 // Cross into current hub realm to be able to call protected `Iterate()` fucntion
42 func(realm) {
43 boards2.Iterate(start, count, func(b *boards.Board) bool {
44 boards_ = append(boards_, NewSafeBoard(b))
45 return false
46 })
47 }(cross)
48
49 return boards_
50}
51
52// GetThreads returns a list with threads of a board.
53// To reverse iterate use a negative count.
54func GetThreads(boardID uint64, start, count int) []Thread {
55 b, found := getBoard(boardID)
56 if !found {
57 return nil
58 }
59
60 var threads []Thread
61 b.Threads.Iterate(start, count, func(thread *boards.Post) bool {
62 threads = append(threads, NewSafeThread(thread))
63 return false
64 })
65 return threads
66}
67
68// GetMembers returns a list with the members of a board.
69// To reverse iterate use a negative count.
70func GetMembers(boardID uint64, start, count int) []boards.User {
71 b, found := getBoard(boardID)
72 if !found {
73 return nil
74 }
75
76 var members []boards.User
77 b.Permissions.IterateUsers(start, count, func(u boards.User) bool {
78 members = append(members, u)
79 return false
80 })
81 return members
82}
83
84// GetReposts returns a list with repost of a board thread.
85// To reverse iterate use a negative count.
86func GetReposts(boardID, threadID uint64, start, count int) []Thread {
87 t, found := getThread(boardID, threadID)
88 if !found {
89 return nil
90 }
91
92 var reposts []Thread
93 t.Reposts.Iterate(start, count, func(boardID, repostID boards.ID) bool {
94 r, found := GetThread(uint64(boardID), uint64(repostID))
95 if found {
96 reposts = append(reposts, r)
97 }
98 return false
99 })
100 return reposts
101}
102
103// GetFlag returns a list with thread or comment moderation flags.
104// To reverse iterate use a negative count.
105// Thread flags are returned when `commentID` is zero, or comment flags are returned otherwise.
106func GetFlags(boardID, threadID, commentID uint64, start, count int) []boards.Flag {
107 var storage boards.FlagStorage
108 if commentID == 0 {
109 t, found := getThread(boardID, threadID)
110 if !found {
111 return nil
112 }
113
114 storage = t.Flags
115 } else {
116 c, found := getComment(boardID, threadID, commentID)
117 if !found {
118 return nil
119 }
120
121 storage = c.Flags
122 }
123
124 var flags []boards.Flag
125 storage.Iterate(start, count, func(f boards.Flag) bool {
126 flags = append(flags, f)
127 return false
128 })
129 return flags
130}
131
132// GetComments returns a list with all thread comments and replies.
133// To reverse iterate use a negative count.
134// Top level comments can be filtered by checking `Comment.ParentID`, replies
135// always have a parent comment or reply, while comments have no parent.
136func GetComments(boardID, threadID uint64, start, count int) []Comment {
137 t, found := getThread(boardID, threadID)
138 if !found {
139 return nil
140 }
141
142 var comments []Comment
143 t.Replies.Iterate(start, count, func(comment *boards.Post) bool {
144 comments = append(comments, NewSafeComment(comment))
145 return false
146 })
147 return comments
148}
149
150// GetReplies returns a list with top level comment replies.
151// To reverse iterate use a negative count.
152func GetReplies(boardID, threadID, commentID uint64, start, count int) []Comment {
153 c, found := getComment(boardID, threadID, commentID)
154 if !found {
155 return nil
156 }
157
158 var replies []Comment
159 c.Replies.Iterate(start, count, func(comment *boards.Post) bool {
160 replies = append(replies, NewSafeComment(comment))
161 return false
162 })
163 return replies
164}
165
166func getBoard(boardID uint64) (*boards.Board, bool) {
167 // Cross into current hub realm to be able to call protected `GetBoard()` fucntion
168 return func(realm) (*boards.Board, bool) {
169 return boards2.GetBoard(boards.ID(boardID))
170 }(cross)
171}
172
173func getThread(boardID, threadID uint64) (*boards.Post, bool) {
174 b, found := getBoard(boardID)
175 if !found {
176 return nil, false
177 }
178
179 t, found := b.Threads.Get(boards.ID(threadID))
180 if !found {
181 // When thread is not found search it within hidden threads
182 meta := b.Meta.(*boards2.BoardMeta)
183 t, found = meta.HiddenThreads.Get(boards.ID(threadID))
184 }
185 return t, found
186}
187
188func getComment(boardID, threadID, commentID uint64) (*boards.Post, bool) {
189 t, found := getThread(boardID, threadID)
190 if !found {
191 return nil, false
192 }
193
194 return t.Replies.Get(boards.ID(commentID))
195}