op_edit_comment.go

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