Search Apps Documentation Source Content File Folder Download Copy Actions Download

thread.gno

2.79 Kb · 105 lines
  1package hub
  2
  3import (
  4	"gno.land/p/gnoland/boards"
  5)
  6
  7// Flag defines a type for thread and comment flags.
  8type Flag struct {
  9	// User is the user that flagged.
 10	User address
 11
 12	// Reason is the reason for flagging.
 13	Reason string
 14}
 15
 16// Thread defines a type for board threads.
 17type Thread struct {
 18	ref *boards.Post
 19
 20	// ID is the unique identifier of the thread.
 21	ID uint64
 22
 23	// OriginalBoardID contains the board ID of the original thread when current is a repost.
 24	OriginalBoardID uint64
 25
 26	// BoardID is the board ID where thread is created.
 27	BoardID uint64
 28
 29	// Title contains thread's title.
 30	Title string
 31
 32	// Body contains content of the thread.
 33	Body string
 34
 35	// Hidden indicates that thread is hidden.
 36	Hidden bool
 37
 38	// Readonly indicates that thread is readonly.
 39	Readonly bool
 40
 41	// CommentCount contains the number of thread comments.
 42	// Count only includes top level comment, replies are not included.
 43	CommentCount int
 44
 45	// RepostCount contains the number of times thread has been reposted.
 46	RepostCount int
 47
 48	// FlagCount contains the number of flags that thread has.
 49	FlagCount int
 50
 51	// Creator is the account address that created the thread.
 52	Creator address
 53
 54	// CreatedAt is thread's creation time as Unix time.
 55	CreatedAt int64
 56
 57	// UpdatedAt is thread's update time as unix time.
 58	UpdatedAt int64
 59}
 60
 61// IterateFlags iterates thread moderation flags.
 62// To reverse iterate use a negative count.
 63func (t Thread) IterateFlags(start, count int, fn func(boards.Flag) bool) bool {
 64	return t.ref.Flags.Iterate(start, count, fn)
 65}
 66
 67// IterateReposts iterates thread reposts.
 68// To reverse iterate use a negative count.
 69func (t Thread) IterateReposts(start, count int, fn func(boardID, repostThreadID uint64) bool) bool {
 70	return t.ref.Reposts.Iterate(start, count, func(boardID, repostThreadID boards.ID) bool {
 71		return fn(uint64(boardID), uint64(repostThreadID))
 72	})
 73}
 74
 75// IterateComments iterates thread comments.
 76// To reverse iterate use a negative count.
 77func (t Thread) IterateComments(start, count int, fn func(Comment) bool) bool {
 78	return t.ref.Replies.Iterate(start, count, func(comment *boards.Post) bool {
 79		return fn(NewSafeComment(comment))
 80	})
 81}
 82
 83// NewSafeThread creates a safe thread.
 84func NewSafeThread(ref *boards.Post) Thread {
 85	if !boards.IsThread(ref) {
 86		panic("post is not a thread")
 87	}
 88
 89	return Thread{
 90		ref:             ref,
 91		ID:              uint64(ref.ID),
 92		OriginalBoardID: uint64(ref.OriginalBoardID),
 93		BoardID:         uint64(ref.Board.ID),
 94		Title:           ref.Title,
 95		Body:            ref.Body,
 96		Hidden:          ref.Hidden,
 97		Readonly:        ref.Readonly,
 98		CommentCount:    ref.Replies.Size(),
 99		RepostCount:     ref.Reposts.Size(),
100		FlagCount:       ref.Flags.Size(),
101		Creator:         ref.Creator,
102		CreatedAt:       timeToUnix(ref.CreatedAt),
103		UpdatedAt:       timeToUnix(ref.UpdatedAt),
104	}
105}