2018-09-28 21:39:39 +03:00
|
|
|
package bug
|
2018-07-13 23:53:53 +03:00
|
|
|
|
2018-07-14 23:18:40 +03:00
|
|
|
import (
|
2018-09-15 14:15:00 +03:00
|
|
|
"fmt"
|
|
|
|
|
2022-08-19 00:34:05 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entities/identity"
|
2019-08-11 15:08:03 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2021-02-20 17:48:44 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity/dag"
|
2020-07-01 20:39:02 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2018-09-15 14:15:00 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/text"
|
2019-02-25 01:05:03 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/timestamp"
|
2018-07-14 23:18:40 +03:00
|
|
|
)
|
2018-07-13 23:53:53 +03:00
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
var _ Operation = &AddCommentOperation{}
|
2021-02-20 17:48:44 +03:00
|
|
|
var _ dag.OperationWithFiles = &AddCommentOperation{}
|
2018-07-13 23:53:53 +03:00
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
// AddCommentOperation will add a new comment in the bug
|
2018-07-13 23:53:53 +03:00
|
|
|
type AddCommentOperation struct {
|
2022-07-25 14:16:16 +03:00
|
|
|
dag.OpBase
|
2019-08-11 15:08:03 +03:00
|
|
|
Message string `json:"message"`
|
2018-08-06 21:31:20 +03:00
|
|
|
// TODO: change for a map[string]util.hash to store the filename ?
|
2020-07-01 20:39:02 +03:00
|
|
|
Files []repository.Hash `json:"files"`
|
2018-07-13 23:53:53 +03:00
|
|
|
}
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
func (op *AddCommentOperation) Id() entity.Id {
|
2022-07-25 14:16:16 +03:00
|
|
|
return dag.IdOperation(op, &op.OpBase)
|
2018-09-29 00:51:47 +03:00
|
|
|
}
|
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
func (op *AddCommentOperation) Apply(snapshot *Snapshot) {
|
2022-07-25 14:16:16 +03:00
|
|
|
snapshot.addActor(op.Author())
|
|
|
|
snapshot.addParticipant(op.Author())
|
2019-03-31 23:32:35 +03:00
|
|
|
|
2021-05-03 12:45:15 +03:00
|
|
|
opId := op.Id()
|
|
|
|
|
2018-09-28 21:39:39 +03:00
|
|
|
comment := Comment{
|
2021-05-03 12:45:15 +03:00
|
|
|
combinedId: entity.CombineIds(snapshot.Id(), opId),
|
|
|
|
targetId: opId,
|
|
|
|
Message: op.Message,
|
|
|
|
Author: op.Author(),
|
|
|
|
Files: op.Files,
|
|
|
|
unixTime: timestamp.Timestamp(op.UnixTime),
|
2018-07-13 23:53:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
snapshot.Comments = append(snapshot.Comments, comment)
|
2018-09-29 21:41:19 +03:00
|
|
|
|
2018-09-30 18:15:54 +03:00
|
|
|
item := &AddCommentTimelineItem{
|
2021-04-11 23:08:21 +03:00
|
|
|
CommentTimelineItem: NewCommentTimelineItem(comment),
|
2018-09-30 18:15:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
snapshot.Timeline = append(snapshot.Timeline, item)
|
2018-07-13 23:53:53 +03:00
|
|
|
}
|
2018-07-25 19:01:32 +03:00
|
|
|
|
2020-07-01 20:39:02 +03:00
|
|
|
func (op *AddCommentOperation) GetFiles() []repository.Hash {
|
2018-09-12 17:57:04 +03:00
|
|
|
return op.Files
|
2018-08-03 00:37:49 +03:00
|
|
|
}
|
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
func (op *AddCommentOperation) Validate() error {
|
2021-02-14 13:36:32 +03:00
|
|
|
if err := op.OpBase.Validate(op, AddCommentOp); err != nil {
|
2018-09-15 14:15:00 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !text.Safe(op.Message) {
|
|
|
|
return fmt.Errorf("message is not fully printable")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-01 20:39:02 +03:00
|
|
|
func NewAddCommentOp(author identity.Interface, unixTime int64, message string, files []repository.Hash) *AddCommentOperation {
|
2018-09-29 21:41:19 +03:00
|
|
|
return &AddCommentOperation{
|
2022-07-25 14:16:16 +03:00
|
|
|
OpBase: dag.NewOpBase(AddCommentOp, author, unixTime),
|
2018-07-25 22:25:26 +03:00
|
|
|
Message: message,
|
2018-09-12 17:57:04 +03:00
|
|
|
Files: files,
|
2018-07-25 22:25:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 12:45:15 +03:00
|
|
|
// AddCommentTimelineItem replace a AddComment operation in the Timeline and hold its edition history
|
2018-09-30 18:15:54 +03:00
|
|
|
type AddCommentTimelineItem struct {
|
|
|
|
CommentTimelineItem
|
|
|
|
}
|
|
|
|
|
2022-07-25 14:16:16 +03:00
|
|
|
// IsAuthored is a sign post method for gqlgen
|
2019-06-23 19:32:22 +03:00
|
|
|
func (a *AddCommentTimelineItem) IsAuthored() {}
|
|
|
|
|
2022-07-31 15:38:32 +03:00
|
|
|
// AddComment is a convenience function to add a comment to a bug
|
|
|
|
func AddComment(b Interface, author identity.Interface, unixTime int64, message string, files []repository.Hash, metadata map[string]string) (*AddCommentOperation, error) {
|
|
|
|
op := NewAddCommentOp(author, unixTime, message, files)
|
|
|
|
for key, val := range metadata {
|
|
|
|
op.SetMetadata(key, val)
|
|
|
|
}
|
|
|
|
if err := op.Validate(); err != nil {
|
2018-10-02 00:31:16 +03:00
|
|
|
return nil, err
|
2018-09-15 14:15:00 +03:00
|
|
|
}
|
2022-07-31 15:38:32 +03:00
|
|
|
b.Append(op)
|
|
|
|
return op, nil
|
2018-07-25 19:01:32 +03:00
|
|
|
}
|