diff --git a/api/client.go b/api/client.go index d7832a00ed2d4ba6eb3f6cfce08d51b8eb21d61e..36cf6203ebc06a89af8f6416fb95de673f655087 100644 --- a/api/client.go +++ b/api/client.go @@ -25,7 +25,6 @@ import ( "gitlab.com/elixxir/client/rekey" "gitlab.com/elixxir/client/user" "gitlab.com/elixxir/comms/connect" - "gitlab.com/elixxir/comms/mixmessages" "gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/large" "gitlab.com/elixxir/crypto/signature/rsa" @@ -596,48 +595,3 @@ func (cl *Client) WriteToSessionFile(replacement string, store globals.Storage) return nil } - -// RegisterForNotifications sends a message to notification bot indicating it -// is registering for notifications -func (cl *Client) RegisterForNotifications(notificationToken []byte) error { - // Pull the host from the manage - notificationBotHost, ok := cl.receptionManager.Comms.GetHost(id.NOTIFICATION_BOT) - if !ok { - return errors.New("Failed to retrieve host for notification bot") - } - - // Send the register message - _, err := cl.receptionManager.Comms.RegisterForNotifications(notificationBotHost, - &mixmessages.NotificationToken{ - Token: notificationToken, - }) - if err != nil { - err := errors.Errorf( - "RegisterForNotifications: Unable to register for notifications! %s", err) - return err - } - - return nil - -} - -// UnregisterForNotifications sends a message to notification bot indicating it -// no longer wants to be registered for notifications -func (cl *Client) UnregisterForNotifications() error { - // Pull the host from the manage - notificationBotHost, ok := cl.receptionManager.Comms.GetHost(id.NOTIFICATION_BOT) - if !ok { - return errors.New("Failed to retrieve host for notification bot") - } - - // Send the unregister message - _, err := cl.receptionManager.Comms.UnregisterForNotifications(notificationBotHost) - if err != nil { - err := errors.Errorf( - "RegisterForNotifications: Unable to register for notifications! %s", err) - return err - } - - return nil - -} diff --git a/api/client_test.go b/api/client_test.go index 3260a0f70b58024b6b7d220655ecaa308f4360d6..a1d46003f90db6f8a3cca3a87dd99cc4731096af 100644 --- a/api/client_test.go +++ b/api/client_test.go @@ -560,49 +560,3 @@ func TestClient_GetCommManager(t *testing.T) { t.Error("Received session not the same as the real session") } } - -// Happy path -func TestClient_RegisterForNotifications(t *testing.T) { - // Initialize client with dummy storage - storage := DummyStorage{LocationA: "Blah", StoreA: []byte{'a', 'b', 'c'}} - client, err := NewClient(&storage, "hello", "", def) - if err != nil { - t.Errorf("Failed to initialize dummy client: %s", err.Error()) - } - - // InitNetwork to gateways and reg server - err = client.InitNetwork() - - if err != nil { - t.Errorf("Client failed of connect: %+v", err) - } - - token := make([]byte, 32) - - err = client.RegisterForNotifications(token) - if err != nil { - t.Errorf("Expected happy path, received error: %+v", err) - } -} - -// Happy path -func TestClient_UnregisterForNotifications(t *testing.T) { - // Initialize client with dummy storage - storage := DummyStorage{LocationA: "Blah", StoreA: []byte{'a', 'b', 'c'}} - client, err := NewClient(&storage, "hello", "", def) - if err != nil { - t.Errorf("Failed to initialize dummy client: %s", err.Error()) - } - - // InitNetwork to gateways and reg server - err = client.InitNetwork() - - if err != nil { - t.Errorf("Client failed of connect: %+v", err) - } - - err = client.UnregisterForNotifications() - if err != nil { - t.Errorf("Expected happy path, received error: %+v", err) - } -} diff --git a/api/notifications.go b/api/notifications.go new file mode 100644 index 0000000000000000000000000000000000000000..663daa315379e88496d186f027377978dda0c80f --- /dev/null +++ b/api/notifications.go @@ -0,0 +1,52 @@ +package api + +import ( + "github.com/pkg/errors" + "gitlab.com/elixxir/comms/mixmessages" + "gitlab.com/elixxir/primitives/id" +) + +// RegisterForNotifications sends a message to notification bot indicating it +// is registering for notifications +func (cl *Client) RegisterForNotifications(notificationToken []byte) error { + // Pull the host from the manage + notificationBotHost, ok := cl.receptionManager.Comms.GetHost(id.NOTIFICATION_BOT) + if !ok { + return errors.New("Failed to retrieve host for notification bot") + } + + // Send the register message + _, err := cl.receptionManager.Comms.RegisterForNotifications(notificationBotHost, + &mixmessages.NotificationToken{ + Token: notificationToken, + }) + if err != nil { + err := errors.Errorf( + "RegisterForNotifications: Unable to register for notifications! %s", err) + return err + } + + return nil + +} + +// UnregisterForNotifications sends a message to notification bot indicating it +// no longer wants to be registered for notifications +func (cl *Client) UnregisterForNotifications() error { + // Pull the host from the manage + notificationBotHost, ok := cl.receptionManager.Comms.GetHost(id.NOTIFICATION_BOT) + if !ok { + return errors.New("Failed to retrieve host for notification bot") + } + + // Send the unregister message + _, err := cl.receptionManager.Comms.UnregisterForNotifications(notificationBotHost) + if err != nil { + err := errors.Errorf( + "RegisterForNotifications: Unable to register for notifications! %s", err) + return err + } + + return nil + +} diff --git a/api/notifications_test.go b/api/notifications_test.go new file mode 100644 index 0000000000000000000000000000000000000000..b83345d4b2568563b4204c820ce1d1a36111dc89 --- /dev/null +++ b/api/notifications_test.go @@ -0,0 +1,50 @@ +package api + +import "testing" + +// Happy path +func TestClient_RegisterForNotifications(t *testing.T) { + // Initialize client with dummy storage + storage := DummyStorage{LocationA: "Blah", StoreA: []byte{'a', 'b', 'c'}} + client, err := NewClient(&storage, "hello", "", def) + if err != nil { + t.Errorf("Failed to initialize dummy client: %s", err.Error()) + return + } + + // InitNetwork to gateways and reg server + err = client.InitNetwork() + + if err != nil { + t.Errorf("Client failed of connect: %+v", err) + } + + token := make([]byte, 32) + + err = client.RegisterForNotifications(token) + if err != nil { + t.Errorf("Expected happy path, received error: %+v", err) + } +} + +// Happy path +func TestClient_UnregisterForNotifications(t *testing.T) { + // Initialize client with dummy storage + storage := DummyStorage{LocationA: "Blah", StoreA: []byte{'a', 'b', 'c'}} + client, err := NewClient(&storage, "hello", "", def) + if err != nil { + t.Errorf("Failed to initialize dummy client: %s", err.Error()) + } + + // InitNetwork to gateways and reg server + err = client.InitNetwork() + + if err != nil { + t.Errorf("Client failed of connect: %+v", err) + } + + err = client.UnregisterForNotifications() + if err != nil { + t.Errorf("Expected happy path, received error: %+v", err) + } +}