comment.go

 1package bug
 2
 3import (
 4	"github.com/dustin/go-humanize"
 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
12// Comment represent a comment in a Bug
13type Comment struct {
14	id      entity.Id
15	Author  identity.Interface
16	Message string
17	Files   []repository.Hash
18
19	// Creation time of the comment.
20	// Should be used only for human display, never for ordering as we can't rely on it in a distributed system.
21	UnixTime timestamp.Timestamp
22}
23
24const compiledCommentIdFormat = "BCBCBCBBBCBBBBCBBBBCBBBBCBBBBCBBBBCBBBBC"
25
26// Id return the Comment identifier
27func (c Comment) Id() entity.Id {
28	if c.id == "" {
29		// simply panic as it would be a coding error
30		// (using an id of an identity not stored yet)
31		panic("no id yet")
32	}
33	return c.id
34}
35
36func DeriveCommentId(bugId entity.ID, commentId entity.ID) entity.ID {
37	commentIdString := commentId
38	bugIdString := bugId
39	id := ""
40	for _, char := range compiledCommentIdFormat {
41		if char == 'B' {
42			id += string(bugIdString[0])
43			bugIdString = bugIdString[1:]
44		} else {
45			id += string(commentIdString[0])
46			commentIdString = commentIdString[1:]
47		}
48	}
49	return id
50}
51
52func SplitCommentId(prefix string) (bugPrefix string, commentPrefix string) {
53	commentIdPrefix := ""
54	bugIdPrefix := ""
55
56	for i, char := range id {
57		if compiledCommentIdFormat[i] == 'B' {
58			bugIdPrefix += string(char)
59		} else {
60			commentIdPrefix += string(char)
61		}
62	}
63	return bugIdPrefix, commentIdPrefix
64}
65
66// FormatTimeRel format the UnixTime of the comment for human consumption
67func (c Comment) FormatTimeRel() string {
68	return humanize.Time(c.UnixTime.Time())
69}
70
71func (c Comment) FormatTime() string {
72	return c.UnixTime.Time().Format("Mon Jan 2 15:04:05 2006 +0200")
73}
74
75// Sign post method for gqlgen
76func (c Comment) IsAuthored() {}