Skip to content
Snippets Groups Projects
Commit b02ef837 authored by benjamin's avatar benjamin
Browse files

fixed notifications callbacks

parent b86435db
Branches
Tags
1 merge request!617Project/haven beta
......@@ -308,10 +308,11 @@ func (m *manager) addToGroupUnsafeRAM(nID *id.ID, reg registration) {
// deleteNotificationUnsafeRAM removes the given notification registration from
// the in ram storage, both to notification and groups
// must be called under the lock
func (m *manager) deleteNotificationUnsafeRAM(nid *id.ID) {
// returns the group becasue it may be nesseary
func (m *manager) deleteNotificationUnsafeRAM(nid *id.ID) string {
reg, exists := m.notifications[*nid]
if !exists {
return
return ""
}
groupList := m.group[reg.Group]
......@@ -323,6 +324,8 @@ func (m *manager) deleteNotificationUnsafeRAM(nid *id.ID) {
}
delete(m.notifications, *nid)
return reg.Group
}
// setTokenUnsafe sets the token in ram and on disk, locally only. Returns true
......
......@@ -426,6 +426,7 @@ func TestManager_maxStateUpdate(t *testing.T) {
var setMax NotificationState
skip := true
for i := 0; i < numGroups; i++ {
groupName := fmt.Sprintf("grp_%d", i)
nid := id.NewIdFromUInt(uint64(i), id.User, t)
......@@ -434,6 +435,9 @@ func TestManager_maxStateUpdate(t *testing.T) {
localI := i
cb := func(group Group, created, edits, deletions []*id.ID,
maxState NotificationState) {
if skip {
return
}
if created != nil || edits != nil || deletions != nil {
t.Errorf("actions are not nil")
}
......@@ -448,6 +452,7 @@ func TestManager_maxStateUpdate(t *testing.T) {
}
m.Set(nid, groupName, []byte{0}, NotificationState(i%3))
}
skip = false
for i := Mute; i <= Push; i++ {
setMax = i
......@@ -623,10 +628,13 @@ func TestManager_deleteNotificationUnsafeRAM(t *testing.T) {
// some groups and partially from other
for i := 0; i < numTests; i++ {
if (i%2) == 1 || i < numTests/2 {
mInternal.deleteNotificationUnsafeRAM(nids[i])
grp := mInternal.deleteNotificationUnsafeRAM(nids[i])
if _, exists := mInternal.notifications[*nids[i]]; exists {
t.Errorf("Failed to delete %s", nids[i])
}
if grp != regs[i].Group {
t.Errorf("group does not match, %s vs %s", grp, regs[i].Group)
}
}
}
......
......@@ -63,6 +63,19 @@ func (m *manager) Set(toBeNotifiedOn *id.ID, group string, metadata []byte,
// update in ram storage
m.upsertNotificationUnsafeRAM(toBeNotifiedOn, reg)
// call the callback if it exists to notify that an update exists
if cb, cbExists := m.callbacks[group]; cbExists {
// can be nil if the last element was deleted
g, _ := m.group[group]
var created, updated []*id.ID
if exists {
updated = []*id.ID{toBeNotifiedOn}
} else {
created = []*id.ID{toBeNotifiedOn}
}
go cb(g.DeepCopy(), created, updated, nil, m.maxState)
}
return nil
}
......@@ -97,7 +110,14 @@ func (m *manager) Delete(toBeNotifiedOn *id.ID) error {
_, err := m.remote.DeleteMapElement(notificationsMap, elementName,
notificationsMapVersion)
m.deleteNotificationUnsafeRAM(toBeNotifiedOn)
group := m.deleteNotificationUnsafeRAM(toBeNotifiedOn)
// call the callback if it exists to notify that an update exists
if cb, cbExists := m.callbacks[group]; cbExists {
// can be nil if the last element was deleted
g, _ := m.group[group]
go cb(g.DeepCopy(), nil, nil, []*id.ID{toBeNotifiedOn}, m.maxState)
}
return err
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment