op_edit_comment.go

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