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  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		id:       string(op.Target),
 61		Message:  op.Message,
 62		Files:    op.Files,
 63		UnixTime: timestamp.Timestamp(op.UnixTime),
 64	}
 65
 66	switch target.(type) {
 67	case *CreateTimelineItem:
 68		item := target.(*CreateTimelineItem)
 69		item.Append(comment)
 70
 71	case *AddCommentTimelineItem:
 72		item := target.(*AddCommentTimelineItem)
 73		item.Append(comment)
 74	}
 75
 76	snapshot.Comments[commentIndex].Message = op.Message
 77	snapshot.Comments[commentIndex].Files = op.Files
 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 !op.Target.IsValid() {
 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  git.Hash   `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 git.Hash, 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 git.Hash, 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 git.Hash, 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}