Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

thread.gno

2.44 Kb · 103 lines
  1package boards
  2
  3import (
  4	"errors"
  5	"strings"
  6	"time"
  7
  8	"gno.land/p/nt/ufmt/v0"
  9)
 10
 11// NewThread creates a new board thread.
 12func NewThread(b *Board, creator address, title, body string) (*Post, error) {
 13	if b == nil {
 14		return nil, errors.New("thread requires a parent board")
 15	}
 16
 17	if !creator.IsValid() {
 18		return nil, ufmt.Errorf("invalid thread creator address: %s", creator)
 19	}
 20
 21	title = strings.TrimSpace(title)
 22	if title == "" {
 23		return nil, errors.New("thread title is required")
 24	}
 25
 26	body = strings.TrimSpace(body)
 27	if body == "" {
 28		return nil, errors.New("thread body is required")
 29	}
 30
 31	id := b.ThreadsSequence.Next()
 32	return &Post{
 33		ID:        id,
 34		ThreadID:  id,
 35		Board:     b,
 36		Title:     title,
 37		Body:      body,
 38		Replies:   NewPostStorage(),
 39		Reposts:   NewRepostStorage(),
 40		Flags:     NewFlagStorage(),
 41		Creator:   creator,
 42		CreatedAt: time.Now(),
 43	}, nil
 44}
 45
 46// MustNewThread creates a new thread or panics on error.
 47func MustNewThread(b *Board, creator address, title, body string) *Post {
 48	t, err := NewThread(b, creator, title, body)
 49	if err != nil {
 50		panic(err)
 51	}
 52	return t
 53}
 54
 55// NewRepost creates a new thread that is a repost of a thread from another board.
 56func NewRepost(thread *Post, dst *Board, creator address) (*Post, error) {
 57	if thread == nil {
 58		return nil, errors.New("thread to repost is required")
 59	}
 60
 61	if thread.Board == nil {
 62		return nil, errors.New("original thread has no board assigned")
 63	}
 64
 65	if dst == nil {
 66		return nil, errors.New("thread repost requires a destination board")
 67	}
 68
 69	if IsRepost(thread) {
 70		return nil, errors.New("reposting a thread that is a repost is not allowed")
 71	}
 72
 73	if !IsThread(thread) {
 74		return nil, errors.New("post must be a thread to be reposted to another board")
 75	}
 76
 77	if !creator.IsValid() {
 78		return nil, ufmt.Errorf("invalid thread repost creator address: %s", creator)
 79	}
 80
 81	id := dst.ThreadsSequence.Next()
 82	return &Post{
 83		ID:              id,
 84		ThreadID:        id,
 85		ParentID:        thread.ID,
 86		OriginalBoardID: thread.Board.ID,
 87		Board:           dst,
 88		Replies:         NewPostStorage(),
 89		Reposts:         NewRepostStorage(),
 90		Flags:           NewFlagStorage(),
 91		Creator:         creator,
 92		CreatedAt:       time.Now(),
 93	}, nil
 94}
 95
 96// MustNewRepost creates a new thread that is a repost of a thread from another board or panics on error.
 97func MustNewRepost(thread *Post, dst *Board, creator address) *Post {
 98	r, err := NewRepost(thread, dst, creator)
 99	if err != nil {
100		panic(err)
101	}
102	return r
103}