post.gno
3.92 Kb · 176 lines
1package boards
2
3import (
4 "strings"
5 "time"
6)
7
8// Post defines a generic type for posts.
9// A post can be either a thread or a reply.
10type Post struct {
11 // ID is the unique identifier of the post.
12 ID ID
13
14 // ParentID is the ID of the parent post.
15 ParentID ID
16
17 // ThreadID contains the post ID of the thread where current post is created.
18 // If current post is a thread it contains post's ID.
19 // It should be used when current post is a thread or reply.
20 ThreadID ID
21
22 // OriginalBoardID contains the board ID of the original post when current post is a repost.
23 OriginalBoardID ID
24
25 // Board contains the board where post is created.
26 Board *Board
27
28 // Title contains the post's title.
29 Title string
30
31 // Body contains content of the post.
32 Body string
33
34 // Hidden indicates that the post is hidden.
35 Hidden bool
36
37 // Readonly indicates that the post is readonly.
38 Readonly bool
39
40 // Replies stores post replies.
41 Replies PostStorage
42
43 // Reposts stores reposts of the current post.
44 // It should be used when post is a thread.
45 Reposts RepostStorage
46
47 // Flags stores users flags for the current post.
48 Flags FlagStorage
49
50 // Creator is the account address that created the post.
51 Creator address
52
53 // Meta allows storing post metadata.
54 Meta any
55
56 // CreatedAt is the post's creation time.
57 CreatedAt time.Time
58
59 // UpdatedAt is the post's update time.
60 UpdatedAt time.Time
61}
62
63// Summary return a summary of the post's body.
64// It returns the body making sure that the length is limited to 80 characters.
65func (post Post) Summary() string {
66 return SummaryOf(post.Body, 80)
67}
68
69// SetID sets post ID value.
70func (post *Post) SetID(v ID) {
71 post.ID = v
72}
73
74// SetParentID sets post's parent ID value.
75func (post *Post) SetParentID(v ID) {
76 post.ParentID = v
77}
78
79// SetThreadID sets thread ID value.
80func (post *Post) SetThreadID(v ID) {
81 post.ThreadID = v
82}
83
84// SetOriginalBoardID sets the board ID of the original post when current post is a repost.
85func (post *Post) SetOriginalBoardID(v ID) {
86 post.OriginalBoardID = v
87}
88
89// SetBoard sets the board where post was created.
90func (post *Post) SetBoard(v *Board) {
91 post.Board = v
92}
93
94// SetTitle sets title value.
95func (post *Post) SetTitle(v string) {
96 post.Title = v
97}
98
99// SetBody sets post's content.
100func (post *Post) SetBody(v string) {
101 post.Body = v
102}
103
104// SetHidden sets hidden value.
105func (post *Post) SetHidden(v bool) {
106 post.Hidden = v
107}
108
109// SetReadonly sets readonly value.
110func (post *Post) SetReadonly(v bool) {
111 post.Readonly = v
112}
113
114// SetReplyStorage sets the storage where post replies are stored.
115func (post *Post) SetReplyStorage(v PostStorage) {
116 post.Replies = v
117}
118
119// SetRepostStorage sets the storage where thread reposts are stored.
120func (post *Post) SetRepostStorage(v RepostStorage) {
121 post.Reposts = v
122}
123
124// SetFlagStorage sets the storage where post flags are stored.
125func (post *Post) SetFlagStorage(v FlagStorage) {
126 post.Flags = v
127}
128
129// SetCreator sets the address of the account that created the post.
130func (post *Post) SetCreator(v address) {
131 post.Creator = v
132}
133
134// SetCreatedAt sets the time when post was created.
135func (post *Post) SetCreatedAt(v time.Time) {
136 post.CreatedAt = v
137}
138
139// SetUpdatedAt sets the time when a post value was updated.
140func (post *Post) SetUpdatedAt(v time.Time) {
141 post.UpdatedAt = v
142}
143
144// IsThread checks if a post is a thread.
145// When a post is not a thread it's considered a thread's reply/comment.
146func IsThread(p *Post) bool {
147 if p == nil {
148 return false
149 }
150 return p.ThreadID == p.ID
151}
152
153// IsRepost checks if a thread is a repost.
154func IsRepost(thread *Post) bool {
155 if thread == nil {
156 return false
157 }
158 return thread.OriginalBoardID != 0
159}
160
161// SummaryOf returns a summary of a text.
162func SummaryOf(text string, length int) string {
163 text = strings.TrimSpace(text)
164 if text == "" {
165 return ""
166 }
167
168 lines := strings.SplitN(text, "\n", 2)
169 line := lines[0]
170 if len(line) > length {
171 line = line[:(length-3)] + "..."
172 } else if len(lines) > 1 {
173 line = line + "..."
174 }
175 return line
176}