thread.gno
2.95 Kb · 109 lines
1package hub
2
3import (
4 "gno.land/p/gnoland/boards"
5)
6
7// Flag defines a type for thread and comment flags.
8type Flag struct {
9 // User is the user that flagged.
10 User address
11
12 // Reason is the reason for flagging.
13 Reason string
14}
15
16// Thread defines a type for board threads.
17type Thread struct {
18 ref *boards.Post
19
20 // ID is the unique identifier of the thread.
21 ID uint64
22
23 // OriginalBoardID contains the board ID of the original thread when current is a repost.
24 OriginalBoardID uint64
25
26 // OriginalThreadID contains the ID of the original thread when current is a repost.
27 OriginalThreadID uint64
28
29 // BoardID is the board ID where thread is created.
30 BoardID uint64
31
32 // Title contains thread's title.
33 Title string
34
35 // Body contains content of the thread.
36 Body string
37
38 // Hidden indicates that thread is hidden.
39 Hidden bool
40
41 // Readonly indicates that thread is readonly.
42 Readonly bool
43
44 // CommentCount contains the number of thread comments.
45 // Count only includes top level comment, replies are not included.
46 CommentCount int
47
48 // RepostCount contains the number of times thread has been reposted.
49 RepostCount int
50
51 // FlagCount contains the number of flags that thread has.
52 FlagCount int
53
54 // Creator is the account address that created the thread.
55 Creator address
56
57 // CreatedAt is thread's creation time as Unix time.
58 CreatedAt int64
59
60 // UpdatedAt is thread's update time as unix time.
61 UpdatedAt int64
62}
63
64// IterateFlags iterates thread moderation flags.
65// To reverse iterate use a negative count.
66func (t Thread) IterateFlags(start, count int, fn func(boards.Flag) bool) bool {
67 return t.ref.Flags.Iterate(start, count, fn)
68}
69
70// IterateReposts iterates thread reposts.
71// To reverse iterate use a negative count.
72func (t Thread) IterateReposts(start, count int, fn func(boardID, repostThreadID uint64) bool) bool {
73 return t.ref.Reposts.Iterate(start, count, func(boardID, repostThreadID boards.ID) bool {
74 return fn(uint64(boardID), uint64(repostThreadID))
75 })
76}
77
78// IterateComments iterates thread comments.
79// To reverse iterate use a negative count.
80func (t Thread) IterateComments(start, count int, fn func(Comment) bool) bool {
81 return t.ref.Replies.Iterate(start, count, func(comment *boards.Post) bool {
82 return fn(NewSafeComment(comment))
83 })
84}
85
86// NewSafeThread creates a safe thread.
87func NewSafeThread(ref *boards.Post) Thread {
88 if !boards.IsThread(ref) {
89 panic("post is not a thread")
90 }
91
92 return Thread{
93 ref: ref,
94 ID: uint64(ref.ID),
95 OriginalBoardID: uint64(ref.OriginalBoardID),
96 OriginalThreadID: uint64(ref.ParentID),
97 BoardID: uint64(ref.Board.ID),
98 Title: ref.Title,
99 Body: ref.Body,
100 Hidden: ref.Hidden,
101 Readonly: ref.Readonly,
102 CommentCount: ref.Replies.Size(),
103 RepostCount: ref.Reposts.Size(),
104 FlagCount: ref.Flags.Size(),
105 Creator: ref.Creator,
106 CreatedAt: timeToUnix(ref.CreatedAt),
107 UpdatedAt: timeToUnix(ref.UpdatedAt),
108 }
109}