1package bug
 2
 3import (
 4	"strings"
 5
 6	"github.com/MichaelMure/git-bug/entities/identity"
 7	"github.com/MichaelMure/git-bug/entity"
 8	"github.com/MichaelMure/git-bug/repository"
 9	"github.com/MichaelMure/git-bug/util/timestamp"
10)
11
12type TimelineItem interface {
13	// CombinedId returns the global identifier of the item
14	CombinedId() entity.CombinedId
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	combinedId entity.CombinedId
30	Author     identity.Interface
31	Message    string
32	Files      []repository.Hash
33	CreatedAt  timestamp.Timestamp
34	LastEdit   timestamp.Timestamp
35	History    []CommentHistoryStep
36}
37
38func NewCommentTimelineItem(comment Comment) CommentTimelineItem {
39	return CommentTimelineItem{
40		// id: comment.id,
41		combinedId: comment.combinedId,
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) CombinedId() entity.CombinedId {
57	return c.combinedId
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}