2018-09-29 21:41:19 +03:00
|
|
|
package bug
|
|
|
|
|
|
|
|
import (
|
2019-01-19 21:23:31 +03:00
|
|
|
"encoding/json"
|
2018-09-29 21:41:19 +03:00
|
|
|
"fmt"
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2021-02-20 17:48:44 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity/dag"
|
2018-11-21 20:56:12 +03:00
|
|
|
"github.com/MichaelMure/git-bug/identity"
|
2020-07-01 20:39:02 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2019-02-25 01:05:03 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/timestamp"
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/text"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ Operation = &EditCommentOperation{}
|
2021-02-20 17:48:44 +03:00
|
|
|
var _ dag.OperationWithFiles = &EditCommentOperation{}
|
2018-09-29 21:41:19 +03:00
|
|
|
|
|
|
|
// EditCommentOperation will change a comment in the bug
|
|
|
|
type EditCommentOperation struct {
|
2018-10-01 12:37:17 +03:00
|
|
|
OpBase
|
2020-07-01 20:39:02 +03:00
|
|
|
Target entity.Id `json:"target"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Files []repository.Hash `json:"files"`
|
2018-09-29 21:41:19 +03:00
|
|
|
}
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
func (op *EditCommentOperation) Id() entity.Id {
|
2021-02-14 13:36:32 +03:00
|
|
|
return idOperation(op, &op.OpBase)
|
2018-09-29 21:41:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (op *EditCommentOperation) Apply(snapshot *Snapshot) {
|
|
|
|
// Todo: currently any message can be edited, even by a different author
|
|
|
|
// crypto signature are needed.
|
|
|
|
|
2021-04-11 23:08:21 +03:00
|
|
|
// Recreate the Comment Id to match on
|
|
|
|
commentId := entity.CombineIds(snapshot.Id(), op.Target)
|
2019-03-31 23:32:35 +03:00
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
var target TimelineItem
|
|
|
|
for i, item := range snapshot.Timeline {
|
2021-04-11 23:08:21 +03:00
|
|
|
if item.Id() == commentId {
|
2018-09-29 21:41:19 +03:00
|
|
|
target = snapshot.Timeline[i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if target == nil {
|
|
|
|
// Target not found, edit is a no-op
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-30 12:00:39 +03:00
|
|
|
comment := Comment{
|
2021-04-11 23:08:21 +03:00
|
|
|
id: commentId,
|
2018-09-30 12:00:39 +03:00
|
|
|
Message: op.Message,
|
|
|
|
Files: op.Files,
|
2019-02-25 01:05:03 +03:00
|
|
|
UnixTime: timestamp.Timestamp(op.UnixTime),
|
2018-09-30 12:00:39 +03:00
|
|
|
}
|
|
|
|
|
2020-02-08 18:17:15 +03:00
|
|
|
switch target := target.(type) {
|
2018-09-29 21:41:19 +03:00
|
|
|
case *CreateTimelineItem:
|
2020-02-08 18:17:15 +03:00
|
|
|
target.Append(comment)
|
2018-09-30 18:15:54 +03:00
|
|
|
case *AddCommentTimelineItem:
|
2020-02-08 18:17:15 +03:00
|
|
|
target.Append(comment)
|
2021-04-11 23:08:21 +03:00
|
|
|
default:
|
|
|
|
// somehow, the target matched on something that is not a comment
|
|
|
|
// we make the op a no-op
|
|
|
|
return
|
2018-09-29 21:41:19 +03:00
|
|
|
}
|
|
|
|
|
2021-04-11 23:08:21 +03:00
|
|
|
snapshot.addActor(op.Author_)
|
|
|
|
|
2019-04-17 19:27:55 +03:00
|
|
|
// Updating the corresponding comment
|
|
|
|
|
|
|
|
for i := range snapshot.Comments {
|
2021-04-11 23:08:21 +03:00
|
|
|
if snapshot.Comments[i].Id() == commentId {
|
2019-04-17 19:27:55 +03:00
|
|
|
snapshot.Comments[i].Message = op.Message
|
|
|
|
snapshot.Comments[i].Files = op.Files
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-09-29 21:41:19 +03:00
|
|
|
}
|
|
|
|
|
2020-07-01 20:39:02 +03:00
|
|
|
func (op *EditCommentOperation) GetFiles() []repository.Hash {
|
2018-09-29 21:41:19 +03:00
|
|
|
return op.Files
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op *EditCommentOperation) Validate() error {
|
2021-02-14 13:36:32 +03:00
|
|
|
if err := op.OpBase.Validate(op, EditCommentOp); err != nil {
|
2018-09-29 21:41:19 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
if err := op.Target.Validate(); err != nil {
|
|
|
|
return errors.Wrap(err, "target hash is invalid")
|
2018-09-29 21:41:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !text.Safe(op.Message) {
|
|
|
|
return fmt.Errorf("message is not fully printable")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-10 18:27:22 +03:00
|
|
|
// UnmarshalJSON is two steps JSON unmarshalling
|
2019-08-11 15:08:03 +03:00
|
|
|
// This workaround is necessary to avoid the inner OpBase.MarshalJSON
|
|
|
|
// overriding the outer op's MarshalJSON
|
2019-01-19 21:23:31 +03:00
|
|
|
func (op *EditCommentOperation) UnmarshalJSON(data []byte) error {
|
|
|
|
// Unmarshal OpBase and the op separately
|
|
|
|
|
|
|
|
base := OpBase{}
|
|
|
|
err := json.Unmarshal(data, &base)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
aux := struct {
|
2020-07-01 20:39:02 +03:00
|
|
|
Target entity.Id `json:"target"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Files []repository.Hash `json:"files"`
|
2019-01-19 21:23:31 +03:00
|
|
|
}{}
|
|
|
|
|
|
|
|
err = json.Unmarshal(data, &aux)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
op.OpBase = base
|
|
|
|
op.Target = aux.Target
|
|
|
|
op.Message = aux.Message
|
|
|
|
op.Files = aux.Files
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-23 19:11:37 +03:00
|
|
|
// Sign post method for gqlgen
|
|
|
|
func (op *EditCommentOperation) IsAuthored() {}
|
|
|
|
|
2020-07-01 20:39:02 +03:00
|
|
|
func NewEditCommentOp(author identity.Interface, unixTime int64, target entity.Id, message string, files []repository.Hash) *EditCommentOperation {
|
2018-09-29 21:41:19 +03:00
|
|
|
return &EditCommentOperation{
|
|
|
|
OpBase: newOpBase(EditCommentOp, author, unixTime),
|
|
|
|
Target: target,
|
|
|
|
Message: message,
|
|
|
|
Files: files,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 18:27:22 +03:00
|
|
|
// EditComment is a convenience function to apply the operation
|
2019-08-11 15:08:03 +03:00
|
|
|
func EditComment(b Interface, author identity.Interface, unixTime int64, target entity.Id, message string) (*EditCommentOperation, error) {
|
2018-09-29 21:41:19 +03:00
|
|
|
return EditCommentWithFiles(b, author, unixTime, target, message, nil)
|
|
|
|
}
|
|
|
|
|
2020-07-01 20:39:02 +03:00
|
|
|
func EditCommentWithFiles(b Interface, author identity.Interface, unixTime int64, target entity.Id, message string, files []repository.Hash) (*EditCommentOperation, error) {
|
2018-09-29 21:41:19 +03:00
|
|
|
editCommentOp := NewEditCommentOp(author, unixTime, target, message, files)
|
|
|
|
if err := editCommentOp.Validate(); err != nil {
|
2018-10-02 00:31:16 +03:00
|
|
|
return nil, err
|
2018-09-29 21:41:19 +03:00
|
|
|
}
|
|
|
|
b.Append(editCommentOp)
|
2018-10-02 00:31:16 +03:00
|
|
|
return editCommentOp, nil
|
2018-09-29 21:41:19 +03:00
|
|
|
}
|
2019-11-07 19:01:08 +03:00
|
|
|
|
2022-03-10 18:27:22 +03:00
|
|
|
// EditCreateComment is a convenience function to edit the body of a bug (the first comment)
|
2019-11-23 09:34:19 +03:00
|
|
|
func EditCreateComment(b Interface, author identity.Interface, unixTime int64, message string) (*EditCommentOperation, error) {
|
|
|
|
createOp := b.FirstOp().(*CreateOperation)
|
|
|
|
return EditComment(b, author, unixTime, createOp.Id(), message)
|
|
|
|
}
|
|
|
|
|
2022-03-10 18:27:22 +03:00
|
|
|
// EditCreateCommentWithFiles is a convenience function to edit the body of a bug (the first comment)
|
2020-07-01 20:39:02 +03:00
|
|
|
func EditCreateCommentWithFiles(b Interface, author identity.Interface, unixTime int64, message string, files []repository.Hash) (*EditCommentOperation, error) {
|
2019-11-23 09:34:19 +03:00
|
|
|
createOp := b.FirstOp().(*CreateOperation)
|
|
|
|
return EditCommentWithFiles(b, author, unixTime, createOp.Id(), message, files)
|
|
|
|
}
|