Search Apps Documentation Source Content File Folder Download Copy Actions Download

reply.gno

1.18 Kb · 55 lines
 1package boards
 2
 3import (
 4	"errors"
 5	"strings"
 6	"time"
 7
 8	"gno.land/p/nt/ufmt/v0"
 9)
10
11// NewReply creates a new reply to a thread or another reply.
12func NewReply(parent *Post, creator address, body string) (*Post, error) {
13	if parent == nil {
14		return nil, errors.New("reply requires a parent thread or reply")
15	}
16
17	if parent.ThreadID == 0 {
18		return nil, errors.New("parent has no thread ID assigned")
19	}
20
21	if parent.Board == nil {
22		return nil, errors.New("parent has no board assigned")
23	}
24
25	if !creator.IsValid() {
26		return nil, ufmt.Errorf("invalid reply creator address: %s", creator)
27	}
28
29	body = strings.TrimSpace(body)
30	if body == "" {
31		return nil, errors.New("reply body is required")
32	}
33
34	id := parent.Board.ThreadsSequence.Next()
35	return &Post{
36		ID:        id,
37		ParentID:  parent.ID,
38		ThreadID:  parent.ThreadID,
39		Board:     parent.Board,
40		Body:      body,
41		Replies:   NewPostStorage(),
42		Flags:     NewFlagStorage(),
43		Creator:   creator,
44		CreatedAt: time.Now(),
45	}, nil
46}
47
48// MustNewReply creates a new reply or panics on error.
49func MustNewReply(parent *Post, creator address, body string) *Post {
50	p, err := NewReply(parent, creator, body)
51	if err != nil {
52		panic(err)
53	}
54	return p
55}