thread.gno
4.28 Kb · 156 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// User returns the user that flagged.
17func (f Flag) User() address { return f.user }
18
19// Reason returns the reason for flagging.
20func (f Flag) Reason() string { return f.reason }
21
22// NewSafeFlag creates a safe flag.
23func NewSafeFlag(ref boards.Flag) Flag {
24 return Flag{
25 user: ref.User,
26 reason: ref.Reason,
27 }
28}
29
30// Thread defines a type for board threads.
31type Thread struct {
32 // id is the unique identifier of the thread.
33 id uint64
34
35 // originalBoardID contains the board ID of the original thread when current is a repost.
36 originalBoardID uint64
37
38 // originalThreadID contains the ID of the original thread when current is a repost.
39 originalThreadID uint64
40
41 // boardID is the board ID where thread is created.
42 boardID uint64
43
44 // title contains thread's title.
45 title string
46
47 // body contains content of the thread.
48 body string
49
50 // hidden indicates that thread is hidden.
51 hidden bool
52
53 // readonly indicates that thread is readonly.
54 readonly bool
55
56 // commentCount contains the number of thread comments.
57 // Count only includes top level comment, replies are not included.
58 commentCount int
59
60 // repostCount contains the number of times thread has been reposted.
61 repostCount int
62
63 // flagCount contains the number of flags that thread has.
64 flagCount int
65
66 // creator is the account address that created the thread.
67 creator address
68
69 // createdAt is thread's creation time as Unix time.
70 createdAt int64
71
72 // updatedAt is thread's update time as unix time.
73 updatedAt int64
74}
75
76// ID returns the unique identifier of the thread.
77func (t Thread) ID() uint64 { return t.id }
78
79// OriginalBoardID returns the board ID of the original thread when current is a repost.
80func (t Thread) OriginalBoardID() uint64 { return t.originalBoardID }
81
82// OriginalThreadID returns the ID of the original thread when current is a repost.
83func (t Thread) OriginalThreadID() uint64 { return t.originalThreadID }
84
85// BoardID returns the board ID where the thread is created.
86func (t Thread) BoardID() uint64 { return t.boardID }
87
88// Title returns the thread's title.
89func (t Thread) Title() string { return t.title }
90
91// Body returns the content of the thread.
92func (t Thread) Body() string { return t.body }
93
94// Hidden indicates that the thread is hidden.
95func (t Thread) Hidden() bool { return t.hidden }
96
97// Readonly indicates that the thread is readonly.
98func (t Thread) Readonly() bool { return t.readonly }
99
100// CommentCount returns the number of thread comments.
101// Count only includes top level comment, replies are not included.
102func (t Thread) CommentCount() int { return t.commentCount }
103
104// RepostCount returns the number of times the thread has been reposted.
105func (t Thread) RepostCount() int { return t.repostCount }
106
107// FlagCount returns the number of flags that the thread has.
108func (t Thread) FlagCount() int { return t.flagCount }
109
110// Creator returns the account address that created the thread.
111func (t Thread) Creator() address { return t.creator }
112
113// CreatedAt returns the thread's creation time as Unix time.
114func (t Thread) CreatedAt() int64 { return t.createdAt }
115
116// UpdatedAt returns the thread's update time as Unix time.
117func (t Thread) UpdatedAt() int64 { return t.updatedAt }
118
119// NewSafeThread creates a safe thread.
120func NewSafeThread(ref *boards.Post) Thread {
121 if !boards.IsThread(ref) {
122 panic("post is not a thread")
123 }
124
125 var commentCount int
126 if ref.Replies != nil {
127 commentCount = ref.Replies.Size()
128 }
129
130 var repostCount int
131 if ref.Reposts != nil {
132 repostCount = ref.Reposts.Size()
133 }
134
135 var flagCount int
136 if ref.Flags != nil {
137 flagCount = ref.Flags.Size()
138 }
139
140 return Thread{
141 id: uint64(ref.ID),
142 originalBoardID: uint64(ref.OriginalBoardID),
143 originalThreadID: uint64(ref.ParentID),
144 boardID: uint64(ref.Board.ID),
145 title: ref.Title,
146 body: ref.Body,
147 hidden: ref.Hidden,
148 readonly: ref.Readonly,
149 commentCount: commentCount,
150 repostCount: repostCount,
151 flagCount: flagCount,
152 creator: ref.Creator,
153 createdAt: timeToUnix(ref.CreatedAt),
154 updatedAt: timeToUnix(ref.UpdatedAt),
155 }
156}