timeline.go

 1package bug
 2
 3import (
 4	"strings"
 5
 6	"github.com/MichaelMure/git-bug/entity"
 7	"github.com/MichaelMure/git-bug/repository"
 8	"github.com/MichaelMure/git-bug/util/timestamp"
 9)
10
11type TimelineItem interface {
12	// CombinedId returns the global identifier of the item
13	CombinedId() entity.CombinedId
14}
15
16// CommentHistoryStep hold one version of a message in the history
17type CommentHistoryStep struct {
18	// The author of the edition, not necessarily the same as the author of the
19	// original comment
20	Author entity.Identity
21	// The new message
22	Message  string
23	UnixTime timestamp.Timestamp
24}
25
26// CommentTimelineItem is a TimelineItem that holds a Comment and its edition history
27type CommentTimelineItem struct {
28	combinedId entity.CombinedId
29	Author     entity.Identity
30	Message    string
31	Files      []repository.Hash
32	CreatedAt  timestamp.Timestamp
33	LastEdit   timestamp.Timestamp
34	History    []CommentHistoryStep
35}
36
37func NewCommentTimelineItem(comment Comment) CommentTimelineItem {
38	return CommentTimelineItem{
39		// id: comment.id,
40		combinedId: comment.combinedId,
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) CombinedId() entity.CombinedId {
56	return c.combinedId
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}