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"
|
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
"github.com/MichaelMure/git-bug/identity"
|
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/git"
|
|
|
|
"github.com/MichaelMure/git-bug/util/text"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ Operation = &EditCommentOperation{}
|
|
|
|
|
|
|
|
// EditCommentOperation will change a comment in the bug
|
|
|
|
type EditCommentOperation struct {
|
2018-10-01 12:37:17 +03:00
|
|
|
OpBase
|
2019-01-19 21:23:31 +03:00
|
|
|
Target git.Hash
|
|
|
|
Message string
|
|
|
|
Files []git.Hash
|
2018-09-29 21:41:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (op *EditCommentOperation) base() *OpBase {
|
2018-10-01 12:37:17 +03:00
|
|
|
return &op.OpBase
|
2018-09-29 21:41:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (op *EditCommentOperation) Hash() (git.Hash, error) {
|
|
|
|
return hashOperation(op)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op *EditCommentOperation) Apply(snapshot *Snapshot) {
|
|
|
|
// Todo: currently any message can be edited, even by a different author
|
|
|
|
// crypto signature are needed.
|
|
|
|
|
|
|
|
var target TimelineItem
|
|
|
|
var commentIndex int
|
|
|
|
|
|
|
|
for i, item := range snapshot.Timeline {
|
2018-09-30 18:15:54 +03:00
|
|
|
h := item.Hash()
|
2018-09-29 21:41:19 +03:00
|
|
|
|
|
|
|
if h == op.Target {
|
|
|
|
target = snapshot.Timeline[i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track the index in the []Comment
|
|
|
|
switch item.(type) {
|
|
|
|
case *CreateTimelineItem, *CommentTimelineItem:
|
|
|
|
commentIndex++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if target == nil {
|
|
|
|
// Target not found, edit is a no-op
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-30 12:00:39 +03:00
|
|
|
comment := Comment{
|
|
|
|
Message: op.Message,
|
|
|
|
Files: op.Files,
|
|
|
|
UnixTime: Timestamp(op.UnixTime),
|
|
|
|
}
|
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
switch target.(type) {
|
|
|
|
case *CreateTimelineItem:
|
|
|
|
item := target.(*CreateTimelineItem)
|
2018-09-30 12:00:39 +03:00
|
|
|
item.Append(comment)
|
2018-09-29 21:41:19 +03:00
|
|
|
|
2018-09-30 18:15:54 +03:00
|
|
|
case *AddCommentTimelineItem:
|
|
|
|
item := target.(*AddCommentTimelineItem)
|
2018-09-30 12:00:39 +03:00
|
|
|
item.Append(comment)
|
2018-09-29 21:41:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
snapshot.Comments[commentIndex].Message = op.Message
|
|
|
|
snapshot.Comments[commentIndex].Files = op.Files
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op *EditCommentOperation) GetFiles() []git.Hash {
|
|
|
|
return op.Files
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op *EditCommentOperation) Validate() error {
|
|
|
|
if err := opBaseValidate(op, EditCommentOp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !op.Target.IsValid() {
|
|
|
|
return fmt.Errorf("target hash is invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !text.Safe(op.Message) {
|
|
|
|
return fmt.Errorf("message is not fully printable")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-19 21:23:31 +03:00
|
|
|
// Workaround to avoid the inner OpBase.MarshalJSON overriding the outer op
|
|
|
|
// MarshalJSON
|
|
|
|
func (op *EditCommentOperation) MarshalJSON() ([]byte, error) {
|
|
|
|
base, err := json.Marshal(op.OpBase)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// revert back to a flat map to be able to add our own fields
|
|
|
|
var data map[string]interface{}
|
|
|
|
if err := json.Unmarshal(base, &data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data["target"] = op.Target
|
|
|
|
data["message"] = op.Message
|
|
|
|
data["files"] = op.Files
|
|
|
|
|
|
|
|
return json.Marshal(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Workaround to avoid the inner OpBase.MarshalJSON overriding the outer op
|
|
|
|
// MarshalJSON
|
|
|
|
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 {
|
|
|
|
Target git.Hash `json:"target"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Files []git.Hash `json:"files"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
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() {}
|
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
func NewEditCommentOp(author identity.Interface, unixTime int64, target git.Hash, message string, files []git.Hash) *EditCommentOperation {
|
2018-09-29 21:41:19 +03:00
|
|
|
return &EditCommentOperation{
|
|
|
|
OpBase: newOpBase(EditCommentOp, author, unixTime),
|
|
|
|
Target: target,
|
|
|
|
Message: message,
|
|
|
|
Files: files,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience function to apply the operation
|
2018-11-21 20:56:12 +03:00
|
|
|
func EditComment(b Interface, author identity.Interface, unixTime int64, target git.Hash, message string) (*EditCommentOperation, error) {
|
2018-09-29 21:41:19 +03:00
|
|
|
return EditCommentWithFiles(b, author, unixTime, target, message, nil)
|
|
|
|
}
|
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
func EditCommentWithFiles(b Interface, author identity.Interface, unixTime int64, target git.Hash, message string, files []git.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
|
|
|
}
|