1package bug
2
3import (
4 "fmt"
5
6 "github.com/MichaelMure/git-bug/entities/identity"
7 "github.com/MichaelMure/git-bug/entity"
8 "github.com/MichaelMure/git-bug/entity/dag"
9 "github.com/MichaelMure/git-bug/repository"
10 "github.com/MichaelMure/git-bug/util/text"
11 "github.com/MichaelMure/git-bug/util/timestamp"
12)
13
14var _ Operation = &AddCommentOperation{}
15var _ dag.OperationWithFiles = &AddCommentOperation{}
16
17// AddCommentOperation will add a new comment in the bug
18type AddCommentOperation struct {
19 dag.OpBase
20 Message string `json:"message"`
21 // TODO: change for a map[string]util.hash to store the filename ?
22 Files []repository.Hash `json:"files"`
23}
24
25func (op *AddCommentOperation) Id() entity.Id {
26 return dag.IdOperation(op, &op.OpBase)
27}
28
29func (op *AddCommentOperation) Apply(snapshot *Snapshot) {
30 snapshot.addActor(op.Author())
31 snapshot.addParticipant(op.Author())
32
33 comment := Comment{
34 id: entity.CombineIds(snapshot.Id(), op.Id()),
35 Message: op.Message,
36 Author: op.Author(),
37 Files: op.Files,
38 UnixTime: timestamp.Timestamp(op.UnixTime),
39 }
40
41 snapshot.Comments = append(snapshot.Comments, comment)
42
43 item := &AddCommentTimelineItem{
44 CommentTimelineItem: NewCommentTimelineItem(comment),
45 }
46
47 snapshot.Timeline = append(snapshot.Timeline, item)
48}
49
50func (op *AddCommentOperation) GetFiles() []repository.Hash {
51 return op.Files
52}
53
54func (op *AddCommentOperation) Validate() error {
55 if err := op.OpBase.Validate(op, AddCommentOp); err != nil {
56 return err
57 }
58
59 if !text.Safe(op.Message) {
60 return fmt.Errorf("message is not fully printable")
61 }
62
63 return nil
64}
65
66func NewAddCommentOp(author identity.Interface, unixTime int64, message string, files []repository.Hash) *AddCommentOperation {
67 return &AddCommentOperation{
68 OpBase: dag.NewOpBase(AddCommentOp, author, unixTime),
69 Message: message,
70 Files: files,
71 }
72}
73
74// AddCommentTimelineItem hold a comment in the timeline
75type AddCommentTimelineItem struct {
76 CommentTimelineItem
77}
78
79// IsAuthored is a sign post method for gqlgen
80func (a *AddCommentTimelineItem) IsAuthored() {}
81
82// AddComment is a convenience function to add a comment to a bug
83func AddComment(b Interface, author identity.Interface, unixTime int64, message string, files []repository.Hash, metadata map[string]string) (*AddCommentOperation, error) {
84 op := NewAddCommentOp(author, unixTime, message, files)
85 for key, val := range metadata {
86 op.SetMetadata(key, val)
87 }
88 if err := op.Validate(); err != nil {
89 return nil, err
90 }
91 b.Append(op)
92 return op, nil
93}