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 snapshot.addActor(op.Author)
37
38 var target TimelineItem
39
40 for i, item := range snapshot.Timeline {
41 h := item.Hash()
42
43 if h == op.Target {
44 target = snapshot.Timeline[i]
45 break
46 }
47 }
48
49 if target == nil {
50 // Target not found, edit is a no-op
51 return
52 }
53
54 comment := Comment{
55 id: string(op.Target),
56 Message: op.Message,
57 Files: op.Files,
58 UnixTime: timestamp.Timestamp(op.UnixTime),
59 }
60
61 switch target.(type) {
62 case *CreateTimelineItem:
63 item := target.(*CreateTimelineItem)
64 item.Append(comment)
65
66 case *AddCommentTimelineItem:
67 item := target.(*AddCommentTimelineItem)
68 item.Append(comment)
69 }
70
71 // Updating the corresponding comment
72
73 for i := range snapshot.Comments {
74 if snapshot.Comments[i].Id() == string(op.Target) {
75 snapshot.Comments[i].Message = op.Message
76 snapshot.Comments[i].Files = op.Files
77 break
78 }
79 }
80}
81
82func (op *EditCommentOperation) GetFiles() []git.Hash {
83 return op.Files
84}
85
86func (op *EditCommentOperation) Validate() error {
87 if err := opBaseValidate(op, EditCommentOp); err != nil {
88 return err
89 }
90
91 if !op.Target.IsValid() {
92 return fmt.Errorf("target hash is invalid")
93 }
94
95 if !text.Safe(op.Message) {
96 return fmt.Errorf("message is not fully printable")
97 }
98
99 return nil
100}
101
102// Workaround to avoid the inner OpBase.MarshalJSON overriding the outer op
103// MarshalJSON
104func (op *EditCommentOperation) MarshalJSON() ([]byte, error) {
105 base, err := json.Marshal(op.OpBase)
106 if err != nil {
107 return nil, err
108 }
109
110 // revert back to a flat map to be able to add our own fields
111 var data map[string]interface{}
112 if err := json.Unmarshal(base, &data); err != nil {
113 return nil, err
114 }
115
116 data["target"] = op.Target
117 data["message"] = op.Message
118 data["files"] = op.Files
119
120 return json.Marshal(data)
121}
122
123// Workaround to avoid the inner OpBase.MarshalJSON overriding the outer op
124// MarshalJSON
125func (op *EditCommentOperation) UnmarshalJSON(data []byte) error {
126 // Unmarshal OpBase and the op separately
127
128 base := OpBase{}
129 err := json.Unmarshal(data, &base)
130 if err != nil {
131 return err
132 }
133
134 aux := struct {
135 Target git.Hash `json:"target"`
136 Message string `json:"message"`
137 Files []git.Hash `json:"files"`
138 }{}
139
140 err = json.Unmarshal(data, &aux)
141 if err != nil {
142 return err
143 }
144
145 op.OpBase = base
146 op.Target = aux.Target
147 op.Message = aux.Message
148 op.Files = aux.Files
149
150 return nil
151}
152
153// Sign post method for gqlgen
154func (op *EditCommentOperation) IsAuthored() {}
155
156func NewEditCommentOp(author identity.Interface, unixTime int64, target git.Hash, message string, files []git.Hash) *EditCommentOperation {
157 return &EditCommentOperation{
158 OpBase: newOpBase(EditCommentOp, author, unixTime),
159 Target: target,
160 Message: message,
161 Files: files,
162 }
163}
164
165// Convenience function to apply the operation
166func EditComment(b Interface, author identity.Interface, unixTime int64, target git.Hash, message string) (*EditCommentOperation, error) {
167 return EditCommentWithFiles(b, author, unixTime, target, message, nil)
168}
169
170func EditCommentWithFiles(b Interface, author identity.Interface, unixTime int64, target git.Hash, message string, files []git.Hash) (*EditCommentOperation, error) {
171 editCommentOp := NewEditCommentOp(author, unixTime, target, message, files)
172 if err := editCommentOp.Validate(); err != nil {
173 return nil, err
174 }
175 b.Append(editCommentOp)
176 return editCommentOp, nil
177}