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	snapshot.addActor(op.Author)
 37
 38	var target TimelineItem
 39	var commentIndex int
 40
 41	for i, item := range snapshot.Timeline {
 42		h := item.Hash()
 43
 44		if h == op.Target {
 45			target = snapshot.Timeline[i]
 46			break
 47		}
 48
 49		// Track the index in the []Comment
 50		switch item.(type) {
 51		case *CreateTimelineItem, *AddCommentTimelineItem:
 52			commentIndex++
 53		}
 54	}
 55
 56	if target == nil {
 57		// Target not found, edit is a no-op
 58		return
 59	}
 60
 61	comment := Comment{
 62		id:       string(op.Target),
 63		Message:  op.Message,
 64		Files:    op.Files,
 65		UnixTime: timestamp.Timestamp(op.UnixTime),
 66	}
 67
 68	switch target.(type) {
 69	case *CreateTimelineItem:
 70		item := target.(*CreateTimelineItem)
 71		item.Append(comment)
 72
 73	case *AddCommentTimelineItem:
 74		item := target.(*AddCommentTimelineItem)
 75		item.Append(comment)
 76	}
 77
 78	snapshot.Comments[commentIndex].Message = op.Message
 79	snapshot.Comments[commentIndex].Files = op.Files
 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}