op_add_item_draft.go

 1package board
 2
 3import (
 4	"fmt"
 5
 6	"github.com/git-bug/git-bug/entities/identity"
 7	"github.com/git-bug/git-bug/entity"
 8	"github.com/git-bug/git-bug/entity/dag"
 9	"github.com/git-bug/git-bug/repository"
10	"github.com/git-bug/git-bug/util/text"
11	"github.com/git-bug/git-bug/util/timestamp"
12)
13
14var _ Operation = &AddItemDraftOperation{}
15
16type AddItemDraftOperation struct {
17	dag.OpBase
18	ColumnId entity.Id         `json:"column"`
19	Title    string            `json:"title"`
20	Message  string            `json:"message"`
21	Files    []repository.Hash `json:"files"`
22}
23
24func (op *AddItemDraftOperation) Id() entity.Id {
25	return dag.IdOperation(op, &op.OpBase)
26}
27
28func (op *AddItemDraftOperation) GetFiles() []repository.Hash {
29	return op.Files
30}
31
32func (op *AddItemDraftOperation) Validate() error {
33	if err := op.OpBase.Validate(op, AddItemDraftOp); err != nil {
34		return err
35	}
36
37	if err := op.ColumnId.Validate(); err != nil {
38		return err
39	}
40
41	if text.Empty(op.Title) {
42		return fmt.Errorf("title is empty")
43	}
44	if !text.SafeOneLine(op.Title) {
45		return fmt.Errorf("title has unsafe characters")
46	}
47
48	if !text.Safe(op.Message) {
49		return fmt.Errorf("message is not fully printable")
50	}
51
52	for _, file := range op.Files {
53		if !file.IsValid() {
54			return fmt.Errorf("invalid file hash")
55		}
56	}
57
58	return nil
59}
60
61func (op *AddItemDraftOperation) Apply(snapshot *Snapshot) {
62	snapshot.addParticipant(op.Author())
63
64	for _, column := range snapshot.Columns {
65		if column.Id == op.ColumnId {
66			column.Items = append(column.Items, &Draft{
67				combinedId: entity.CombineIds(snapshot.id, op.Id()),
68				Author:     op.Author(),
69				Title:      op.Title,
70				Message:    op.Message,
71				unixTime:   timestamp.Timestamp(op.UnixTime),
72			})
73			return
74		}
75	}
76}
77
78func NewAddItemDraftOp(author identity.Interface, unixTime int64, columnId entity.Id, title, message string, files []repository.Hash) *AddItemDraftOperation {
79	return &AddItemDraftOperation{
80		OpBase:   dag.NewOpBase(AddItemDraftOp, author, unixTime),
81		ColumnId: columnId,
82		Title:    title,
83		Message:  message,
84		Files:    files,
85	}
86}
87
88// AddItemDraft is a convenience function to add a draft item to a Board
89func AddItemDraft(b Interface, author identity.Interface, unixTime int64, columnId entity.Id, title, message string, files []repository.Hash, metadata map[string]string) (entity.CombinedId, *AddItemDraftOperation, error) {
90	op := NewAddItemDraftOp(author, unixTime, columnId, title, message, files)
91	for key, val := range metadata {
92		op.SetMetadata(key, val)
93	}
94	if err := op.Validate(); err != nil {
95		return entity.UnsetCombinedId, nil, err
96	}
97	b.Append(op)
98	return entity.CombineIds(b.Id(), op.Id()), op, nil
99}