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

thread.gno

3.85 Kb · 136 lines
  1package hub
  2
  3import (
  4	"gno.land/p/gnoland/boards/v0"
  5)
  6
  7// Thread defines a type for board threads.
  8type Thread struct {
  9	// id is the unique identifier of the thread.
 10	id uint64
 11
 12	// originalBoardID contains the board ID of the original thread when current is a repost.
 13	originalBoardID uint64
 14
 15	// originalThreadID contains the ID of the original thread when current is a repost.
 16	originalThreadID uint64
 17
 18	// boardID is the board ID where thread is created.
 19	boardID uint64
 20
 21	// title contains thread's title.
 22	title string
 23
 24	// body contains content of the thread.
 25	body string
 26
 27	// hidden indicates that thread is hidden.
 28	hidden bool
 29
 30	// readonly indicates that thread is readonly.
 31	readonly bool
 32
 33	// commentCount contains the number of thread comments.
 34	// Count only includes top level comment, replies are not included.
 35	commentCount int
 36
 37	// repostCount contains the number of times thread has been reposted.
 38	repostCount int
 39
 40	// flagCount contains the number of flags that thread has.
 41	flagCount int
 42
 43	// creator is the account address that created the thread.
 44	creator address
 45
 46	// createdAt is thread's creation time as Unix time.
 47	createdAt int64
 48
 49	// updatedAt is thread's update time as unix time.
 50	updatedAt int64
 51}
 52
 53// ID returns the unique identifier of the thread.
 54func (t Thread) ID() uint64 { return t.id }
 55
 56// OriginalBoardID returns the board ID of the original thread when current is a repost.
 57func (t Thread) OriginalBoardID() uint64 { return t.originalBoardID }
 58
 59// OriginalThreadID returns the ID of the original thread when current is a repost.
 60func (t Thread) OriginalThreadID() uint64 { return t.originalThreadID }
 61
 62// BoardID returns the board ID where the thread is created.
 63func (t Thread) BoardID() uint64 { return t.boardID }
 64
 65// Title returns the thread's title.
 66func (t Thread) Title() string { return t.title }
 67
 68// Body returns the content of the thread.
 69func (t Thread) Body() string { return t.body }
 70
 71// Hidden indicates that the thread is hidden.
 72func (t Thread) Hidden() bool { return t.hidden }
 73
 74// Readonly indicates that the thread is readonly.
 75func (t Thread) Readonly() bool { return t.readonly }
 76
 77// CommentCount returns the number of thread comments.
 78// Count only includes top level comment, replies are not included.
 79func (t Thread) CommentCount() int { return t.commentCount }
 80
 81// RepostCount returns the number of times the thread has been reposted.
 82func (t Thread) RepostCount() int { return t.repostCount }
 83
 84// FlagCount returns the number of flags that the thread has.
 85func (t Thread) FlagCount() int { return t.flagCount }
 86
 87// Creator returns the account address that created the thread.
 88func (t Thread) Creator() address { return t.creator }
 89
 90// CreatedAt returns the thread's creation time as Unix time.
 91func (t Thread) CreatedAt() int64 { return t.createdAt }
 92
 93// UpdatedAt returns the thread's update time as Unix time.
 94func (t Thread) UpdatedAt() int64 { return t.updatedAt }
 95
 96// NewSafeThread creates a safe thread.
 97func NewSafeThread(ref *boards.Post) Thread {
 98	if ref == nil {
 99		panic("post reference is nil")
100	}
101	if !boards.IsThread(ref) {
102		panic("post is not a thread")
103	}
104
105	var commentCount int
106	if ref.Replies != nil {
107		commentCount = ref.Replies.Size()
108	}
109
110	var repostCount int
111	if ref.Reposts != nil {
112		repostCount = ref.Reposts.Size()
113	}
114
115	var flagCount int
116	if ref.Flags != nil {
117		flagCount = ref.Flags.Size()
118	}
119
120	return Thread{
121		id:               uint64(ref.ID),
122		originalBoardID:  uint64(ref.OriginalBoardID),
123		originalThreadID: uint64(ref.ParentID),
124		boardID:          uint64(ref.Board.ID),
125		title:            ref.Title,
126		body:             ref.Body,
127		hidden:           ref.Hidden,
128		readonly:         ref.Readonly,
129		commentCount:     commentCount,
130		repostCount:      repostCount,
131		flagCount:        flagCount,
132		creator:          ref.Creator,
133		createdAt:        timeToUnix(ref.CreatedAt),
134		updatedAt:        timeToUnix(ref.UpdatedAt),
135	}
136}