1package bug
 2
 3import (
 4	"strings"
 5
 6	"github.com/MichaelMure/git-bug/entity"
 7	"github.com/MichaelMure/git-bug/identity"
 8	"github.com/MichaelMure/git-bug/util/git"
 9	"github.com/MichaelMure/git-bug/util/timestamp"
10)
11
12type TimelineItem interface {
13	// ID return the identifier of the item
14	Id() entity.Id
15}
16
17// CommentHistoryStep hold one version of a message in the history
18type CommentHistoryStep struct {
19	// The author of the edition, not necessarily the same as the author of the
20	// original comment
21	Author identity.Interface
22	// The new message
23	Message  string
24	UnixTime timestamp.Timestamp
25}
26
27// CommentTimelineItem is a TimelineItem that holds a Comment and its edition history
28type CommentTimelineItem struct {
29	id        entity.Id
30	Author    identity.Interface
31	Message   string
32	Files     []git.Hash
33	CreatedAt timestamp.Timestamp
34	LastEdit  timestamp.Timestamp
35	History   []CommentHistoryStep
36}
37
38func NewCommentTimelineItem(ID entity.Id, comment Comment) CommentTimelineItem {
39	return CommentTimelineItem{
40		id:        ID,
41		Author:    comment.Author,
42		Message:   comment.Message,
43		Files:     comment.Files,
44		CreatedAt: comment.UnixTime,
45		LastEdit:  comment.UnixTime,
46		History: []CommentHistoryStep{
47			{
48				Message:  comment.Message,
49				UnixTime: comment.UnixTime,
50			},
51		},
52	}
53}
54
55func (c *CommentTimelineItem) Id() entity.Id {
56	return c.id
57}
58
59// Append will append a new comment in the history and update the other values
60func (c *CommentTimelineItem) Append(comment Comment) {
61	c.Message = comment.Message
62	c.Files = comment.Files
63	c.LastEdit = comment.UnixTime
64	c.History = append(c.History, CommentHistoryStep{
65		Author:   comment.Author,
66		Message:  comment.Message,
67		UnixTime: comment.UnixTime,
68	})
69}
70
71// Edited say if the comment was edited
72func (c *CommentTimelineItem) Edited() bool {
73	return len(c.History) > 1
74}
75
76// MessageIsEmpty return true is the message is empty or only made of spaces
77func (c *CommentTimelineItem) MessageIsEmpty() bool {
78	return len(strings.TrimSpace(c.Message)) == 0
79}