diff --git a/bug/comment.go b/bug/comment.go index 4c9d118ebb4cb79f07dc953aedaa3748fd63a2fb..61f22fa79154630faf7de7b8a3e3cddb650a32ec 100644 --- a/bug/comment.go +++ b/bug/comment.go @@ -21,6 +21,8 @@ type Comment struct { UnixTime timestamp.Timestamp } +const compiledCommentIdFormat = "BCBCBCBBBCBBBBCBBBBCBBBBCBBBBCBBBBCBBBBC" + // Id return the Comment identifier func (c Comment) Id() entity.Id { if c.id == "" { @@ -31,6 +33,36 @@ func (c Comment) Id() entity.Id { return c.id } +func CompileCommentId(bugId string, commentId string) string { + commentIdString := commentId + bugIdString := bugId + id := "" + for _, char := range compiledCommentIdFormat { + if char == 'B' { + id += string(bugIdString[0]) + bugIdString = bugIdString[1:] + } else { + id += string(commentIdString[0]) + commentIdString = commentIdString[1:] + } + } + return id +} + +func UnpackCommentId(id string) (string, string) { + commentIdPrefix := "" + bugIdPrefix := "" + + for i, char := range id { + if compiledCommentIdFormat[i] == 'B' { + bugIdPrefix += string(char) + } else { + commentIdPrefix += string(char) + } + } + return bugIdPrefix, commentIdPrefix +} + // FormatTimeRel format the UnixTime of the comment for human consumption func (c Comment) FormatTimeRel() string { return humanize.Time(c.UnixTime.Time()) diff --git a/bug/comment_test.go b/bug/comment_test.go new file mode 100644 index 0000000000000000000000000000000000000000..c2b29d9375c601adcdca39e354a7bc513f88630a --- /dev/null +++ b/bug/comment_test.go @@ -0,0 +1,24 @@ +package bug + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCompileUnpackCommentId(t *testing.T) { + id1 := "abcdefghijklmnopqrstuvwxyz1234" + id2 := "ABCDEFGHIJ" + expectedId := "aAbBcCdefDghijEklmnFopqrGstuvHwxyzI1234J" + + compiledId := CompileCommentId(id1, id2) + assert.Equal(t, expectedId, compiledId) + + unpackedId1, unpackedId2 := UnpackCommentId(compiledId) + assert.Equal(t, id1, unpackedId1) + assert.Equal(t, id2, unpackedId2) + + unpackedId1, unpackedId2 = UnpackCommentId(expectedId[:6]) + assert.Equal(t, unpackedId1, id1[:3]) + assert.Equal(t, unpackedId2, id2[:3]) +} diff --git a/bug/op_add_comment.go b/bug/op_add_comment.go index 3f19e42e784185336811160dae437e7c527fff3b..4d1fa271d2ed2075a5380e3387f42a8a9e05209b 100644 --- a/bug/op_add_comment.go +++ b/bug/op_add_comment.go @@ -37,7 +37,7 @@ func (op *AddCommentOperation) Apply(snapshot *Snapshot) { snapshot.addParticipant(op.Author) comment := Comment{ - id: op.Id(), + id: entity.Id(CompileCommentId(snapshot.Id().String(), op.Id().String())), Message: op.Message, Author: op.Author, Files: op.Files, diff --git a/bug/op_create.go b/bug/op_create.go index 9bb40d35ded864c2155f368f489ddd1e1db5f6b3..49c941974120f2617b8448e705e81cb0cace89e1 100644 --- a/bug/op_create.go +++ b/bug/op_create.go @@ -40,7 +40,7 @@ func (op *CreateOperation) Apply(snapshot *Snapshot) { snapshot.Title = op.Title comment := Comment{ - id: op.Id(), + id: entity.Id(CompileCommentId(snapshot.Id().String(), op.Id().String())), Message: op.Message, Author: op.Author, UnixTime: timestamp.Timestamp(op.UnixTime),