Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

comment.gno

2.94 Kb · 107 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	// 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 boards.IsThread(ref) {
 81		panic("post is not a comment or reply")
 82	}
 83
 84	var replyCount int
 85	if ref.Replies != nil {
 86		replyCount = ref.Replies.Size()
 87	}
 88
 89	var flagCount int
 90	if ref.Flags != nil {
 91		flagCount = ref.Flags.Size()
 92	}
 93
 94	return Comment{
 95		id:         uint64(ref.ID),
 96		boardID:    uint64(ref.Board.ID),
 97		threadID:   uint64(ref.ThreadID),
 98		parentID:   uint64(ref.ParentID),
 99		body:       ref.Body,
100		hidden:     ref.Hidden,
101		replyCount: replyCount,
102		flagCount:  flagCount,
103		creator:    ref.Creator,
104		createdAt:  timeToUnix(ref.CreatedAt),
105		updatedAt:  timeToUnix(ref.UpdatedAt),
106	}
107}