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/repository"
 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 should be the same as in Comment
30	id        entity.Id
31	Author    identity.Interface
32	Message   string
33	Files     []repository.Hash
34	CreatedAt timestamp.Timestamp
35	LastEdit  timestamp.Timestamp
36	History   []CommentHistoryStep
37}
38
39func NewCommentTimelineItem(comment Comment) CommentTimelineItem {
40	return CommentTimelineItem{
41		id:        comment.id,
42		Author:    comment.Author,
43		Message:   comment.Message,
44		Files:     comment.Files,
45		CreatedAt: comment.UnixTime,
46		LastEdit:  comment.UnixTime,
47		History: []CommentHistoryStep{
48			{
49				Message:  comment.Message,
50				UnixTime: comment.UnixTime,
51			},
52		},
53	}
54}
55
56func (c *CommentTimelineItem) Id() entity.Id {
57	return c.id
58}
59
60// Append will append a new comment in the history and update the other values
61func (c *CommentTimelineItem) Append(comment Comment) {
62	c.Message = comment.Message
63	c.Files = comment.Files
64	c.LastEdit = comment.UnixTime
65	c.History = append(c.History, CommentHistoryStep{
66		Author:   comment.Author,
67		Message:  comment.Message,
68		UnixTime: comment.UnixTime,
69	})
70}
71
72// Edited say if the comment was edited
73func (c *CommentTimelineItem) Edited() bool {
74	return len(c.History) > 1
75}
76
77// MessageIsEmpty return true is the message is empty or only made of spaces
78func (c *CommentTimelineItem) MessageIsEmpty() bool {
79	return len(strings.TrimSpace(c.Message)) == 0
80}