Skip to content

Commit 1eae0a8

Browse files
committed
Fixed a bug related to notification versions
When notifications were sent, Notification.Save() was called twice resulting in a new version. Fixed. Also, notification keys are prefixed by "n:id" instead of "n:".
1 parent 03a23a8 commit 1eae0a8

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

api/endpoints.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func H_Notify(c *gin.Context) {
243243
c.JSON(http.StatusBadRequest, gin.H{"error": err})
244244
return
245245
}
246-
ver, err = notification.Save()
246+
ver, err = notification.Update()
247247
if err != nil {
248248
c.JSON(http.StatusInternalServerError, gin.H{"error": err})
249249
return

cmd/export.go

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func exportData(cmd *cobra.Command, args []string) {
4444
exportEntities("l_user", []byte("l:user:"), outDir, &models.UserLog{})
4545
exportEntities("s_id", []byte("s:id:"), outDir, &models.Subscription{})
4646
exportEntities("f_id", []byte("f:id:"), outDir, &models.Frame{})
47+
exportEntities("n_", []byte("n:"), outDir, &models.Notification{})
4748
exportKeys("f_name", []byte("f:name:"), outDir)
4849
exportKeys("f_endpoint", []byte("f:endpoint:"), outDir)
4950
exportKeys("s_url", []byte("s:url:"), outDir)

models/notification.go

+20-7
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,16 @@ func (n *Notification) Send() error {
180180
}
181181

182182
func (n *Notification) Prefix() string {
183-
return "n:" + n.Id + ":000:"
183+
return "n:id:" + n.Id + ":000:"
184184
}
185185
func (n *Notification) PrefixBytes() []byte {
186-
return []byte("n:" + n.Id + ":000:")
186+
return []byte("n:id:" + n.Id + ":000:")
187187
}
188188

189189
func (n *Notification) Save() (int, error) {
190190
nextVersion := uint64(0)
191191
var err error
192-
prefix := []byte("n:" + n.Id + ":")
192+
prefix := []byte("n:id:" + n.Id + ":")
193193
for {
194194
keys, next, err := db.GetKeysWithPrefix(prefix, prefix, 100)
195195
if err != nil {
@@ -200,23 +200,36 @@ func (n *Notification) Save() (int, error) {
200200
break
201201
}
202202
}
203-
nextKey := []byte("n:" + n.Id + ":" + fmt.Sprintf("%03d", nextVersion))
203+
nextKey := []byte("n:id:" + n.Id + ":" + fmt.Sprintf("%03d", nextVersion))
204204
n.Version = &nextVersion
205205
notificationBytes, err := proto.Marshal(n)
206206
if err != nil {
207207
return 0, fmt.Errorf("Error marshaling notification: %v", err)
208208
}
209209
err = db.Set(nextKey, notificationBytes)
210210
if err != nil {
211-
return 0, fmt.Errorf("Error putting notification: %v", err)
211+
return 0, fmt.Errorf("Error saving notification: %v", err)
212212
}
213213
return int(nextVersion), nil
214214
}
215215

216+
func (n *Notification) Update() (int, error) {
217+
key := []byte("n:id:" + n.Id + ":" + fmt.Sprintf("%03d", n.GetVersion()))
218+
notificationBytes, err := proto.Marshal(n)
219+
if err != nil {
220+
return 0, fmt.Errorf("Error marshaling notification: %v", err)
221+
}
222+
err = db.Set(key, notificationBytes)
223+
if err != nil {
224+
return 0, fmt.Errorf("Error saving notification: %v", err)
225+
}
226+
return int(*n.Version), nil
227+
}
228+
216229
func (n *Notification) Load(id string) ([]*Notification, error) {
217230
var notifications []*Notification
218-
prefix := []byte("n:" + id + ":")
219-
next := []byte("n:" + id + ":0")
231+
prefix := []byte("n:id:" + id + ":")
232+
next := []byte("n:id:" + id + ":0")
220233
for {
221234
keys, next, err := db.GetKeysWithPrefix(prefix, next, 100)
222235
if err != nil {

0 commit comments

Comments
 (0)