timeline.go

 1package bug
 2
 3import (
 4	"strings"
 5
 6	"github.com/MichaelMure/git-bug/identity"
 7	"github.com/MichaelMure/git-bug/util/git"
 8	"github.com/MichaelMure/git-bug/util/timestamp"
 9)
10
11type TimelineItem interface {
12	// Hash return the hash of the item
13	Hash() git.Hash
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 identity.Interface
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	hash      git.Hash
29	Author    identity.Interface
30	Message   string
31	Files     []git.Hash
32	CreatedAt timestamp.Timestamp
33	LastEdit  timestamp.Timestamp
34	History   []CommentHistoryStep
35}
36
37func NewCommentTimelineItem(hash git.Hash, comment Comment) CommentTimelineItem {
38	return CommentTimelineItem{
39		hash:      hash,
40		Author:    comment.Author,
41		Message:   comment.Message,
42		Files:     comment.Files,
43		CreatedAt: comment.UnixTime,
44		LastEdit:  comment.UnixTime,
45		History: []CommentHistoryStep{
46			{
47				Message:  comment.Message,
48				UnixTime: comment.UnixTime,
49			},
50		},
51	}
52}
53
54func (c *CommentTimelineItem) Hash() git.Hash {
55	return c.hash
56}
57
58// Append will append a new comment in the history and update the other values
59func (c *CommentTimelineItem) Append(comment Comment) {
60	c.Message = comment.Message
61	c.Files = comment.Files
62	c.LastEdit = comment.UnixTime
63	c.History = append(c.History, CommentHistoryStep{
64		Author:   comment.Author,
65		Message:  comment.Message,
66		UnixTime: comment.UnixTime,
67	})
68}
69
70// Edited say if the comment was edited
71func (c *CommentTimelineItem) Edited() bool {
72	return len(c.History) > 1
73}
74
75// MessageIsEmpty return true is the message is empty or only made of spaces
76func (c *CommentTimelineItem) MessageIsEmpty() bool {
77	return len(strings.TrimSpace(c.Message)) == 0
78}