Search Apps Documentation Source Content File Folder Download Copy Actions Download

comment.gno

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