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/identity"
 11	"github.com/MichaelMure/git-bug/repository"
 12	"github.com/MichaelMure/git-bug/util/timestamp"
 13
 14	"github.com/MichaelMure/git-bug/util/text"
 15)
 16
 17var _ Operation = &EditCommentOperation{}
 18var _ dag.OperationWithFiles = &EditCommentOperation{}
 19
 20// EditCommentOperation will change a comment in the bug
 21type EditCommentOperation struct {
 22	dag.OpBase
 23	Target  entity.Id         `json:"target"`
 24	Message string            `json:"message"`
 25	Files   []repository.Hash `json:"files"`
 26}
 27
 28func (op *EditCommentOperation) Id() entity.Id {
 29	return dag.IdOperation(op, &op.OpBase)
 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	// Recreate the Comment Id to match on
 37	commentId := entity.CombineIds(snapshot.Id(), op.Target)
 38
 39	var target TimelineItem
 40	for i, item := range snapshot.Timeline {
 41		if item.Id() == commentId {
 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:       commentId,
 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].Id() == commentId {
 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	return nil
101}
102
103func NewEditCommentOp(author identity.Interface, unixTime int64, target entity.Id, message string, files []repository.Hash) *EditCommentOperation {
104	return &EditCommentOperation{
105		OpBase:  dag.NewOpBase(EditCommentOp, author, unixTime),
106		Target:  target,
107		Message: message,
108		Files:   files,
109	}
110}
111
112// EditComment is a convenience function to apply the operation
113func EditComment(b Interface, author identity.Interface, unixTime int64, target entity.Id, message string) (*EditCommentOperation, error) {
114	return EditCommentWithFiles(b, author, unixTime, target, message, nil)
115}
116
117func EditCommentWithFiles(b Interface, author identity.Interface, unixTime int64, target entity.Id, message string, files []repository.Hash) (*EditCommentOperation, error) {
118	editCommentOp := NewEditCommentOp(author, unixTime, target, message, files)
119	if err := editCommentOp.Validate(); err != nil {
120		return nil, err
121	}
122	b.Append(editCommentOp)
123	return editCommentOp, nil
124}
125
126// EditCreateComment is a convenience function to edit the body of a bug (the first comment)
127func EditCreateComment(b Interface, author identity.Interface, unixTime int64, message string) (*EditCommentOperation, error) {
128	createOp := b.FirstOp().(*CreateOperation)
129	return EditComment(b, author, unixTime, createOp.Id(), message)
130}
131
132// EditCreateCommentWithFiles is a convenience function to edit the body of a bug (the first comment)
133func EditCreateCommentWithFiles(b Interface, author identity.Interface, unixTime int64, message string, files []repository.Hash) (*EditCommentOperation, error) {
134	createOp := b.FirstOp().(*CreateOperation)
135	return EditCommentWithFiles(b, author, unixTime, createOp.Id(), message, files)
136}