comment.gno
2.99 Kb · 110 lines
1package hub
2
3import (
4 "gno.land/p/gnoland/boards/v0"
5)
6
7// Comment defines a type for threads comment/replies.
8type Comment struct {
9 // id is the unique identifier of the comment.
10 id uint64
11
12 // boardID is the board ID where comment is created.
13 boardID uint64
14
15 // threadID is the ID of the thread where comment is created.
16 threadID uint64
17
18 // parentID is the ID of the parent comment or reply.
19 parentID uint64
20
21 // body contains the comment's content.
22 body string
23
24 // hidden indicates that comment is hidden.
25 hidden bool
26
27 // replyCount contains the number of comments replies.
28 // Count only includes top level replies, sub-replies are not included.
29 replyCount int
30
31 // flagCount contains the number of flags that comment has.
32 flagCount int
33
34 // creator is the account address that created the comment or reply.
35 creator address
36
37 // createdAt is thread's creation time as Unix time.
38 createdAt int64
39
40 // updatedAt is thread's update time as Unix time.
41 updatedAt int64
42}
43
44// ID returns the unique identifier of the comment.
45func (c Comment) ID() uint64 { return c.id }
46
47// BoardID returns the board ID where the comment is created.
48func (c Comment) BoardID() uint64 { return c.boardID }
49
50// ThreadID returns the ID of the thread where the comment is created.
51func (c Comment) ThreadID() uint64 { return c.threadID }
52
53// ParentID returns the ID of the parent comment or reply.
54func (c Comment) ParentID() uint64 { return c.parentID }
55
56// Body returns the comment's content.
57func (c Comment) Body() string { return c.body }
58
59// Hidden indicates that the comment is hidden.
60func (c Comment) Hidden() bool { return c.hidden }
61
62// ReplyCount returns the number of comment replies.
63// Count only includes top level replies, sub-replies are not included.
64func (c Comment) ReplyCount() int { return c.replyCount }
65
66// FlagCount returns the number of flags that the comment has.
67func (c Comment) FlagCount() int { return c.flagCount }
68
69// Creator returns the account address that created the comment or reply.
70func (c Comment) Creator() address { return c.creator }
71
72// CreatedAt returns the comment's creation time as Unix time.
73func (c Comment) CreatedAt() int64 { return c.createdAt }
74
75// UpdatedAt returns the comment's update time as Unix time.
76func (c Comment) UpdatedAt() int64 { return c.updatedAt }
77
78// NewSafeComment creates a safe comment.
79func NewSafeComment(ref *boards.Post) Comment {
80 if ref == nil {
81 panic("post reference is nil")
82 }
83 if boards.IsThread(ref) {
84 panic("post is not a comment or reply")
85 }
86
87 var replyCount int
88 if ref.Replies != nil {
89 replyCount = ref.Replies.Size()
90 }
91
92 var flagCount int
93 if ref.Flags != nil {
94 flagCount = ref.Flags.Size()
95 }
96
97 return Comment{
98 id: uint64(ref.ID),
99 boardID: uint64(ref.Board.ID),
100 threadID: uint64(ref.ThreadID),
101 parentID: uint64(ref.ParentID),
102 body: ref.Body,
103 hidden: ref.Hidden,
104 replyCount: replyCount,
105 flagCount: flagCount,
106 creator: ref.Creator,
107 createdAt: timeToUnix(ref.CreatedAt),
108 updatedAt: timeToUnix(ref.UpdatedAt),
109 }
110}