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

fixed notifications callbacks

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