/
comment.go
196 lines (165 loc) · 5.79 KB
/
comment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package keeper
import (
"encoding/binary"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gitopia/gitopia/v4/x/gitopia/types"
)
// GetCommentCount get the total number of comment
func (k Keeper) GetCommentCount(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.CommentCountKey))
byteKey := types.KeyPrefix(types.CommentCountKey)
bz := store.Get(byteKey)
// Count doesn't exist: no element
if bz == nil {
return 0
}
// Parse bytes
return binary.BigEndian.Uint64(bz)
}
// SetCommentCount set the total number of comment
func (k Keeper) SetCommentCount(ctx sdk.Context, count uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.CommentCountKey))
byteKey := types.KeyPrefix(types.CommentCountKey)
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)
store.Set(byteKey, bz)
}
// AppendComment appends a comment in the store with a new id and update the count
func (k Keeper) AppendComment(
ctx sdk.Context,
comment types.Comment,
) uint64 {
// Create the comment
count := k.GetCommentCount(ctx)
// Set the ID of the appended value
comment.Id = count
var store prefix.Store
if comment.Parent == types.CommentParentIssue {
store = prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.GetCommentKeyForIssue(comment.RepositoryId, comment.ParentIid)),
)
} else {
store = prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.GetCommentKeyForPullRequest(comment.RepositoryId, comment.ParentIid)),
)
}
appendedValue := k.cdc.MustMarshal(&comment)
store.Set(GetCommentIDBytes(comment.CommentIid), appendedValue)
// Update comment count
k.SetCommentCount(ctx, count+1)
return count
}
// SetComment set a specific comment in the store
func (k Keeper) SetComment(ctx sdk.Context, comment types.Comment) {
var store prefix.Store
if comment.Parent == types.CommentParentIssue {
store = prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.GetCommentKeyForIssue(comment.RepositoryId, comment.ParentIid)),
)
} else {
store = prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.GetCommentKeyForPullRequest(comment.RepositoryId, comment.ParentIid)),
)
}
b := k.cdc.MustMarshal(&comment)
store.Set(GetCommentIDBytes(comment.CommentIid), b)
}
// GetIssueComment returns a comment from its id
func (k Keeper) GetIssueComment(ctx sdk.Context, repositoryId uint64, issueIid uint64, commentIid uint64) (val types.Comment, found bool) {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.GetCommentKeyForIssue(repositoryId, issueIid)),
)
b := store.Get(GetCommentIDBytes(commentIid))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// GetPullRequestComment returns a comment from its id
func (k Keeper) GetPullRequestComment(ctx sdk.Context, repositoryId uint64, pullRequestIid uint64, commentIid uint64) (val types.Comment, found bool) {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.GetCommentKeyForPullRequest(repositoryId, pullRequestIid)),
)
b := store.Get(GetCommentIDBytes(commentIid))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// RemoveIssueComment removes a comment from the store
func (k Keeper) RemoveIssueComment(ctx sdk.Context, repositoryId uint64, issueIid uint64, commentIid uint64) {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.GetCommentKeyForIssue(repositoryId, issueIid)),
)
store.Delete(GetCommentIDBytes(commentIid))
}
// RemovePullRequestComment removes a comment from the store
func (k Keeper) RemovePullRequestComment(ctx sdk.Context, repositoryId uint64, pullRequestIid uint64, commentIid uint64) {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.GetCommentKeyForPullRequest(repositoryId, pullRequestIid)),
)
store.Delete(GetCommentIDBytes(commentIid))
}
// GetAllComment returns all comment
func (k Keeper) GetAllComment(ctx sdk.Context) (list []types.Comment) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.CommentKey))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.Comment
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// GetAllIssueComment returns all issue comment for repository
func (k Keeper) GetAllIssueComment(ctx sdk.Context, repositoryId uint64, issueIid uint64) (list []types.Comment) {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.GetCommentKeyForIssue(repositoryId, issueIid)),
)
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.Comment
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// GetAllPullRequestComment returns all issue comment for repository
func (k Keeper) GetAllPullRequestComment(ctx sdk.Context, repositoryId uint64, pullRequestIid uint64) (list []types.Comment) {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.GetCommentKeyForPullRequest(repositoryId, pullRequestIid)),
)
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.Comment
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// GetCommentIDBytes returns the byte representation of the ID
func GetCommentIDBytes(id uint64) []byte {
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, id)
return bz
}
// GetCommentIDFromBytes returns ID in uint64 format from a byte array
func GetCommentIDFromBytes(bz []byte) uint64 {
return binary.BigEndian.Uint64(bz)
}