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.Operation = &AddCommentOperation{}
16var _ entity.OperationWithFiles = &AddCommentOperation{}
17
18// AddCommentOperation will add a new comment in the bug
19type AddCommentOperation struct {
20 dag.OpBase
21 Message string `json:"message"`
22 // TODO: change for a map[string]util.hash to store the filename ?
23 Files []repository.Hash `json:"files"`
24}
25
26func (op *AddCommentOperation) Id() entity.Id {
27 return dag.IdOperation(op, &op.OpBase)
28}
29
30func (op *AddCommentOperation) Apply(snapshot *Snapshot) {
31 snapshot.addActor(op.Author())
32 snapshot.addParticipant(op.Author())
33
34 opId := op.Id()
35
36 comment := Comment{
37 combinedId: entity.CombineIds(snapshot.Id(), opId),
38 targetId: opId,
39 Message: op.Message,
40 Author: op.Author(),
41 Files: op.Files,
42 unixTime: timestamp.Timestamp(op.UnixTime),
43 }
44
45 snapshot.Comments = append(snapshot.Comments, comment)
46
47 item := &AddCommentTimelineItem{
48 CommentTimelineItem: NewCommentTimelineItem(comment),
49 }
50
51 snapshot.Timeline = append(snapshot.Timeline, item)
52}
53
54func (op *AddCommentOperation) GetFiles() []repository.Hash {
55 return op.Files
56}
57
58func (op *AddCommentOperation) Validate() error {
59 if err := op.OpBase.Validate(op, AddCommentOp); err != nil {
60 return err
61 }
62
63 if !text.Safe(op.Message) {
64 return fmt.Errorf("message is not fully printable")
65 }
66
67 for _, file := range op.Files {
68 if !file.IsValid() {
69 return fmt.Errorf("invalid file hash")
70 }
71 }
72
73 return nil
74}
75
76func NewAddCommentOp(author identity.Interface, unixTime int64, message string, files []repository.Hash) *AddCommentOperation {
77 return &AddCommentOperation{
78 OpBase: dag.NewOpBase(AddCommentOp, author, unixTime),
79 Message: message,
80 Files: files,
81 }
82}
83
84// AddCommentTimelineItem replace a AddComment operation in the Timeline and hold its edition history
85type AddCommentTimelineItem struct {
86 CommentTimelineItem
87}
88
89// IsAuthored is a sign post method for gqlgen
90func (a *AddCommentTimelineItem) IsAuthored() {}
91
92// AddComment is a convenience function to add a comment to a bug
93func AddComment(b Interface, author identity.Interface, unixTime int64, message string, files []repository.Hash, metadata map[string]string) (entity.CombinedId, *AddCommentOperation, error) {
94 op := NewAddCommentOp(author, unixTime, message, files)
95 for key, val := range metadata {
96 op.SetMetadata(key, val)
97 }
98 if err := op.Validate(); err != nil {
99 return entity.UnsetCombinedId, nil, err
100 }
101 b.Append(op)
102 return entity.CombineIds(b.Id(), op.Id()), op, nil
103}