op_edit_comment.go

  1package bug
  2
  3import (
  4	"fmt"
  5
  6	"github.com/pkg/errors"
  7
  8	"github.com/MichaelMure/git-bug/entity"
  9	"github.com/MichaelMure/git-bug/entity/dag"
 10	"github.com/MichaelMure/git-bug/repository"
 11	"github.com/MichaelMure/git-bug/util/timestamp"
 12
 13	"github.com/MichaelMure/git-bug/util/text"
 14)
 15
 16var _ Operation = &EditCommentOperation{}
 17var _ entity.OperationWithFiles = &EditCommentOperation{}
 18
 19// EditCommentOperation will change a comment in the bug
 20type EditCommentOperation struct {
 21	dag.OpBase
 22	Target  entity.Id         `json:"target"`
 23	Message string            `json:"message"`
 24	Files   []repository.Hash `json:"files"`
 25}
 26
 27func (op *EditCommentOperation) Id() entity.Id {
 28	return dag.IdOperation(op, &op.OpBase)
 29}
 30
 31func (op *EditCommentOperation) Apply(snapshot *Snapshot) {
 32	// Todo: currently any message can be edited, even by a different author
 33	// crypto signature are needed.
 34
 35	// Recreate the combined Id to match on
 36	combinedId := entity.CombineIds(snapshot.Id(), op.Target)
 37
 38	var target TimelineItem
 39	for i, item := range snapshot.Timeline {
 40		if item.CombinedId() == combinedId {
 41			target = snapshot.Timeline[i]
 42			break
 43		}
 44	}
 45
 46	if target == nil {
 47		// Target not found, edit is a no-op
 48		return
 49	}
 50
 51	comment := Comment{
 52		combinedId: combinedId,
 53		targetId:   op.Target,
 54		Message:    op.Message,
 55		Files:      op.Files,
 56		unixTime:   timestamp.Timestamp(op.UnixTime),
 57	}
 58
 59	switch target := target.(type) {
 60	case *CreateTimelineItem:
 61		target.Append(comment)
 62	case *AddCommentTimelineItem:
 63		target.Append(comment)
 64	default:
 65		// somehow, the target matched on something that is not a comment
 66		// we make the op a no-op
 67		return
 68	}
 69
 70	snapshot.addActor(op.Author())
 71
 72	// Updating the corresponding comment
 73
 74	for i := range snapshot.Comments {
 75		if snapshot.Comments[i].CombinedId() == combinedId {
 76			snapshot.Comments[i].Message = op.Message
 77			snapshot.Comments[i].Files = op.Files
 78			break
 79		}
 80	}
 81}
 82
 83func (op *EditCommentOperation) GetFiles() []repository.Hash {
 84	return op.Files
 85}
 86
 87func (op *EditCommentOperation) Validate() error {
 88	if err := op.OpBase.Validate(op, EditCommentOp); err != nil {
 89		return err
 90	}
 91
 92	if err := op.Target.Validate(); err != nil {
 93		return errors.Wrap(err, "target hash is invalid")
 94	}
 95
 96	if !text.Safe(op.Message) {
 97		return fmt.Errorf("message is not fully printable")
 98	}
 99
100	for _, file := range op.Files {
101		if !file.IsValid() {
102			return fmt.Errorf("invalid file hash")
103		}
104	}
105
106	return nil
107}
108
109func NewEditCommentOp(author entity.Identity, unixTime int64, target entity.Id, message string, files []repository.Hash) *EditCommentOperation {
110	return &EditCommentOperation{
111		OpBase:  dag.NewOpBase(EditCommentOp, author, unixTime),
112		Target:  target,
113		Message: message,
114		Files:   files,
115	}
116}
117
118// EditComment is a convenience function to apply the operation
119func EditComment(b Interface, author entity.Identity, unixTime int64, target entity.Id, message string, files []repository.Hash, metadata map[string]string) (entity.CombinedId, *EditCommentOperation, error) {
120	op := NewEditCommentOp(author, unixTime, target, message, files)
121	for key, val := range metadata {
122		op.SetMetadata(key, val)
123	}
124	if err := op.Validate(); err != nil {
125		return entity.UnsetCombinedId, nil, err
126	}
127	b.Append(op)
128	return entity.CombineIds(b.Id(), target), op, nil
129}
130
131// EditCreateComment is a convenience function to edit the body of a bug (the first comment)
132func EditCreateComment(b Interface, author entity.Identity, unixTime int64, message string, files []repository.Hash, metadata map[string]string) (entity.CombinedId, *EditCommentOperation, error) {
133	createOp := b.FirstOp().(*CreateOperation)
134	return EditComment(b, author, unixTime, createOp.Id(), message, files, metadata)
135}