timeline.go

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