1package bug
2
3import (
4 "encoding/json"
5 "fmt"
6
7 "github.com/MichaelMure/git-bug/identity"
8 "github.com/MichaelMure/git-bug/util/timestamp"
9
10 "github.com/MichaelMure/git-bug/util/git"
11 "github.com/MichaelMure/git-bug/util/text"
12)
13
14var _ Operation = &EditCommentOperation{}
15
16// EditCommentOperation will change a comment in the bug
17type EditCommentOperation struct {
18 OpBase
19 Target git.Hash
20 Message string
21 Files []git.Hash
22}
23
24func (op *EditCommentOperation) base() *OpBase {
25 return &op.OpBase
26}
27
28func (op *EditCommentOperation) Hash() (git.Hash, error) {
29 return hashOperation(op)
30}
31
32func (op *EditCommentOperation) Apply(snapshot *Snapshot) {
33 // Todo: currently any message can be edited, even by a different author
34 // crypto signature are needed.
35
36 var target TimelineItem
37 var commentIndex int
38
39 for i, item := range snapshot.Timeline {
40 h := item.Hash()
41
42 if h == op.Target {
43 target = snapshot.Timeline[i]
44 break
45 }
46
47 // Track the index in the []Comment
48 switch item.(type) {
49 case *CreateTimelineItem, *CommentTimelineItem:
50 commentIndex++
51 }
52 }
53
54 if target == nil {
55 // Target not found, edit is a no-op
56 return
57 }
58
59 comment := Comment{
60 Message: op.Message,
61 Files: op.Files,
62 UnixTime: timestamp.Timestamp(op.UnixTime),
63 }
64
65 switch target.(type) {
66 case *CreateTimelineItem:
67 item := target.(*CreateTimelineItem)
68 item.Append(comment)
69
70 case *AddCommentTimelineItem:
71 item := target.(*AddCommentTimelineItem)
72 item.Append(comment)
73 }
74
75 snapshot.Comments[commentIndex].Message = op.Message
76 snapshot.Comments[commentIndex].Files = op.Files
77}
78
79func (op *EditCommentOperation) GetFiles() []git.Hash {
80 return op.Files
81}
82
83func (op *EditCommentOperation) Validate() error {
84 if err := opBaseValidate(op, EditCommentOp); err != nil {
85 return err
86 }
87
88 if !op.Target.IsValid() {
89 return fmt.Errorf("target hash is invalid")
90 }
91
92 if !text.Safe(op.Message) {
93 return fmt.Errorf("message is not fully printable")
94 }
95
96 return nil
97}
98
99// Workaround to avoid the inner OpBase.MarshalJSON overriding the outer op
100// MarshalJSON
101func (op *EditCommentOperation) MarshalJSON() ([]byte, error) {
102 base, err := json.Marshal(op.OpBase)
103 if err != nil {
104 return nil, err
105 }
106
107 // revert back to a flat map to be able to add our own fields
108 var data map[string]interface{}
109 if err := json.Unmarshal(base, &data); err != nil {
110 return nil, err
111 }
112
113 data["target"] = op.Target
114 data["message"] = op.Message
115 data["files"] = op.Files
116
117 return json.Marshal(data)
118}
119
120// Workaround to avoid the inner OpBase.MarshalJSON overriding the outer op
121// MarshalJSON
122func (op *EditCommentOperation) UnmarshalJSON(data []byte) error {
123 // Unmarshal OpBase and the op separately
124
125 base := OpBase{}
126 err := json.Unmarshal(data, &base)
127 if err != nil {
128 return err
129 }
130
131 aux := struct {
132 Target git.Hash `json:"target"`
133 Message string `json:"message"`
134 Files []git.Hash `json:"files"`
135 }{}
136
137 err = json.Unmarshal(data, &aux)
138 if err != nil {
139 return err
140 }
141
142 op.OpBase = base
143 op.Target = aux.Target
144 op.Message = aux.Message
145 op.Files = aux.Files
146
147 return nil
148}
149
150// Sign post method for gqlgen
151func (op *EditCommentOperation) IsAuthored() {}
152
153func NewEditCommentOp(author identity.Interface, unixTime int64, target git.Hash, message string, files []git.Hash) *EditCommentOperation {
154 return &EditCommentOperation{
155 OpBase: newOpBase(EditCommentOp, author, unixTime),
156 Target: target,
157 Message: message,
158 Files: files,
159 }
160}
161
162// Convenience function to apply the operation
163func EditComment(b Interface, author identity.Interface, unixTime int64, target git.Hash, message string) (*EditCommentOperation, error) {
164 return EditCommentWithFiles(b, author, unixTime, target, message, nil)
165}
166
167func EditCommentWithFiles(b Interface, author identity.Interface, unixTime int64, target git.Hash, message string, files []git.Hash) (*EditCommentOperation, error) {
168 editCommentOp := NewEditCommentOp(author, unixTime, target, message, files)
169 if err := editCommentOp.Validate(); err != nil {
170 return nil, err
171 }
172 b.Append(editCommentOp)
173 return editCommentOp, nil
174}