From a5a6902c118d937da4fb106e71eeee0b5b25aee0 Mon Sep 17 00:00:00 2001
From: Jono Wenger <jono@elixxir.io>
Date: Thu, 27 Jul 2023 18:24:13 +0000
Subject: [PATCH] XX-4721 / Fix notification update function on init

---
 dm/interfaces.go           |  4 ----
 dm/notifications_test.go   | 29 -----------------------------
 notifications/interface.go |  3 ++-
 notifications/manager.go   |  8 ++++++--
 4 files changed, 8 insertions(+), 36 deletions(-)

diff --git a/dm/interfaces.go b/dm/interfaces.go
index 1c666b118..89a75f225 100644
--- a/dm/interfaces.go
+++ b/dm/interfaces.go
@@ -284,10 +284,6 @@ type cMixClient interface {
 type NotificationsManager interface {
 	Set(toBeNotifiedOn *id.ID, group string, metadata []byte,
 		status clientNotif.NotificationState) error
-	Get(toBeNotifiedOn *id.ID) (status clientNotif.NotificationState,
-		metadata []byte, group string, exists bool)
-	Delete(toBeNotifiedOn *id.ID) error
-	RegisterUpdateCallback(group string, nu clientNotif.Update)
 }
 
 // NickNameManager interface is an object that handles the mapping of nicknames
diff --git a/dm/notifications_test.go b/dm/notifications_test.go
index 449f1a636..43c88a191 100644
--- a/dm/notifications_test.go
+++ b/dm/notifications_test.go
@@ -460,32 +460,3 @@ func (m *mockNM) Set(
 	}
 	return nil
 }
-
-func (m *mockNM) Get(toBeNotifiedOn *id.ID) (clientNotif.NotificationState, []byte, string, bool) {
-	for group, ids := range m.channels {
-		for chanID, ni := range ids {
-			if chanID.Cmp(toBeNotifiedOn) {
-				return ni.Status, ni.Metadata, group, true
-			}
-		}
-	}
-
-	return clientNotif.Mute, nil, "", false
-}
-
-func (m *mockNM) Delete(toBeNotifiedOn *id.ID) error {
-	for group, ids := range m.channels {
-		if _, exists := ids[*toBeNotifiedOn]; exists {
-			delete(m.channels[group], *toBeNotifiedOn)
-			if _, exists = m.cbs[group]; exists {
-				go m.cbs[group](m.channels[group], nil, nil, nil, clientNotif.Push)
-			}
-			return nil
-		}
-	}
-	return nil
-}
-
-func (m *mockNM) RegisterUpdateCallback(group string, nu clientNotif.Update) {
-	m.cbs[group] = nu
-}
diff --git a/notifications/interface.go b/notifications/interface.go
index 9ce901a3d..4c96fc6ae 100644
--- a/notifications/interface.go
+++ b/notifications/interface.go
@@ -104,7 +104,8 @@ type Manager interface {
 	// to changes in notifications. Because this is being called after
 	// initialization, a poll of state via the get function will be necessary
 	// because notifications can be missed. You must rely on the data in the
-	// callback for the update and not poll the interface.
+	// callback for the update and not poll the interface. On registration, the
+	// callback will be called immediately with all saved IDs as created.
 	RegisterUpdateCallback(group string, nu Update)
 }
 
diff --git a/notifications/manager.go b/notifications/manager.go
index df22f0202..fd2059403 100644
--- a/notifications/manager.go
+++ b/notifications/manager.go
@@ -138,10 +138,14 @@ func NewOrLoadManager(identity xxdk.TransmissionIdentity, regSig []byte,
 func (m *manager) RegisterUpdateCallback(group string, nu Update) {
 	m.mux.Lock()
 	defer m.mux.Unlock()
-	m.callbacks[group] = nu
 	if g, ok := m.group[group]; ok {
-		nu(g, nil, nil, nil, m.maxState)
+		created := make([]*id.ID, 0, len(g))
+		for gid := range g {
+			created = append(created, &gid)
+		}
+		nu(g.DeepCopy(), created, nil, nil, m.maxState)
 	}
+	m.callbacks[group] = nu
 }
 
 // mapUpdate is the listener function which is called whenever the notifications
-- 
GitLab