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 // Recreate the combined Id to match on
63 combinedId := entity.CombineIds(snapshot.Id(), op.ColumnId)
64
65 for _, column := range snapshot.Columns {
66 if column.CombinedId == combinedId {
67 column.Items = append(column.Items, &Draft{
68 combinedId: entity.CombineIds(snapshot.id, op.Id()),
69 Author: op.Author(),
70 Title: op.Title,
71 Message: op.Message,
72 unixTime: timestamp.Timestamp(op.UnixTime),
73 })
74
75 snapshot.addParticipant(op.Author())
76 return
77 }
78 }
79}
80
81func NewAddItemDraftOp(author identity.Interface, unixTime int64, columnId entity.Id, title, message string, files []repository.Hash) *AddItemDraftOperation {
82 return &AddItemDraftOperation{
83 OpBase: dag.NewOpBase(AddItemDraftOp, author, unixTime),
84 ColumnId: columnId,
85 Title: title,
86 Message: message,
87 Files: files,
88 }
89}
90
91// AddItemDraft is a convenience function to add a draft item to a Board
92func 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) {
93 op := NewAddItemDraftOp(author, unixTime, columnId, title, message, files)
94 for key, val := range metadata {
95 op.SetMetadata(key, val)
96 }
97 if err := op.Validate(); err != nil {
98 return entity.UnsetCombinedId, nil, err
99 }
100 b.Append(op)
101 return entity.CombineIds(b.Id(), op.Id()), op, nil
102}