From 5cfc9c368f8800d7fa3ea70a478f68d1b2acdc94 Mon Sep 17 00:00:00 2001
From: josh <josh@elixxir.io>
Date: Fri, 14 Feb 2020 11:10:22 -0800
Subject: [PATCH] Add StartNotificationBot and CreateNotificationBot calls for
 refactor

---
 go.mod                              |  5 +++--
 go.sum                              | 10 ++++++----
 notifications/notifications.go      | 29 +++++++++++++++++------------
 notifications/notifications_test.go |  8 ++++++++
 4 files changed, 34 insertions(+), 18 deletions(-)

diff --git a/go.mod b/go.mod
index 79b9484..2525b37 100644
--- a/go.mod
+++ b/go.mod
@@ -23,12 +23,13 @@ require (
 	github.com/spf13/pflag v1.0.5 // indirect
 
 	github.com/spf13/viper v1.6.2
-	gitlab.com/elixxir/comms v0.0.0-20200212214943-7dbfb97820ae
+	gitlab.com/elixxir/comms v0.0.0-20200214190748-8fb0fc8e259d
 	gitlab.com/elixxir/crypto v0.0.0-20200206203107-b8926242da23
 	gitlab.com/elixxir/primitives v0.0.0-20200210205543-5c55c1f6949f
+	golang.org/x/crypto v0.0.0-20200214034016-1d94cc7ab1c6 // indirect
 	golang.org/x/exp v0.0.0-20200213203834-85f925bdd4d0 // indirect
 	golang.org/x/net v0.0.0-20200202094626-16171245cfb2
-	golang.org/x/tools v0.0.0-20200213224642-88e652f7a869 // indirect
+	golang.org/x/tools v0.0.0-20200214181226-ea829e2eb296 // indirect
 	google.golang.org/api v0.17.0
 	gopkg.in/ini.v1 v1.52.0 // indirect
 	mellium.im/sasl v0.0.0-20190815210834-e27ea4901008 // indirect
diff --git a/go.sum b/go.sum
index ecca673..1eb658b 100644
--- a/go.sum
+++ b/go.sum
@@ -230,8 +230,8 @@ github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGr
 github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
 github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
 github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
-gitlab.com/elixxir/comms v0.0.0-20200212214943-7dbfb97820ae h1:DjA5QZN87KWH/jeWEQj7IoHO/yyGRkxJFSUsCXj1Rfo=
-gitlab.com/elixxir/comms v0.0.0-20200212214943-7dbfb97820ae/go.mod h1:OEctgeBsmGXkm2OBZi/4psBBqzFkZZZHO3M9xqlSzEk=
+gitlab.com/elixxir/comms v0.0.0-20200214190748-8fb0fc8e259d h1:Lt5hseyMpmnhuyHIdibO4lG2JjFGMl6MuDAAmWTpKOg=
+gitlab.com/elixxir/comms v0.0.0-20200214190748-8fb0fc8e259d/go.mod h1:OEctgeBsmGXkm2OBZi/4psBBqzFkZZZHO3M9xqlSzEk=
 gitlab.com/elixxir/crypto v0.0.0-20200206203107-b8926242da23 h1:J9MKdOxLGzDZoLy2Q0CAxPlPjSH+k4NG3JhgvatAZjo=
 gitlab.com/elixxir/crypto v0.0.0-20200206203107-b8926242da23/go.mod h1:wWulHuSqxiGhvasduZrtyTTqy+7y5ebe440GdORhzig=
 gitlab.com/elixxir/primitives v0.0.0-20200131183153-e93c6b75019f h1:F0YwFZz4umoXOJ+xX34WIRrucuLgHCSyKxWOKGaEt5g=
@@ -261,6 +261,8 @@ golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72 h1:+ELyKg6m8UBf0nPFSqD0mi
 golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
 golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678 h1:wCWoJcFExDgyYx2m2hpHgwz8W3+FPdfldvIgzqDIhyg=
 golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
+golang.org/x/crypto v0.0.0-20200214034016-1d94cc7ab1c6 h1:Sy5bstxEqwwbYs6n0/pBuxKENqOeZUgD45Gp3Q3pqLg=
+golang.org/x/crypto v0.0.0-20200214034016-1d94cc7ab1c6/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
 golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
 golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
 golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -396,8 +398,8 @@ golang.org/x/tools v0.0.0-20200204074204-1cc6d1ef6c74/go.mod h1:TB2adYChydJhpapK
 golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
 golang.org/x/tools v0.0.0-20200212150539-ea181f53ac56 h1:DFtSed2q3HtNuVazwVDZ4nSRS/JrZEig0gz2BY4VNrg=
 golang.org/x/tools v0.0.0-20200212150539-ea181f53ac56/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
-golang.org/x/tools v0.0.0-20200213224642-88e652f7a869 h1:DPqS0AlgYBVHhG5jnEVScBXXIS+xjgn7O8s1E3sDqxc=
-golang.org/x/tools v0.0.0-20200213224642-88e652f7a869/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
+golang.org/x/tools v0.0.0-20200214181226-ea829e2eb296 h1:QY+wzp9wdyuVitcUPWBbrlxIcxYt20minP+vEeWGJew=
+golang.org/x/tools v0.0.0-20200214181226-ea829e2eb296/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
 golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
diff --git a/notifications/notifications.go b/notifications/notifications.go
index 0e20f00..7cb3972 100644
--- a/notifications/notifications.go
+++ b/notifications/notifications.go
@@ -32,10 +32,11 @@ type NotifyFunc func(*messaging.Client, string, *firebase.FirebaseComm, storage.
 
 // Params struct holds info passed in for configuration
 type Params struct {
-	Address  string
-	CertPath string
-	KeyPath  string
-	FBCreds  string
+	Address       string
+	CertPath      string
+	KeyPath       string
+	PublicAddress string
+	FBCreds       string
 }
 
 // Local impl for notifications; holds comms, storage object, creds and main functions
@@ -59,6 +60,7 @@ type NotificationComms interface {
 	RequestNotifications(host *connect.Host) (*pb.IDList, error)
 	RequestNdf(host *connect.Host, message *pb.NDFHash) (*pb.NDF, error)
 	RetrieveNdf(currentDef *ndf.NetworkDefinition) (*ndf.NetworkDefinition, error)
+	StartNotificationBot() error
 }
 
 // Main function for this repo accepts credentials and an impl
@@ -127,9 +129,18 @@ func StartNotifications(params Params, noTLS, noFirebase bool) (*Impl, error) {
 	impl.pollFunc = pollForNotifications
 	impl.notifyFunc = notifyUser
 
-	// Start notification comms server
+	// Create notification comms server
 	handler := NewImplementation(impl)
-	impl.Comms = notificationBot.StartNotificationBot(id.NOTIFICATION_BOT, params.Address, handler, cert, key)
+	impl.Comms, err = notificationBot.CreateNotificationBot(id.NOTIFICATION_BOT, params.PublicAddress, handler, cert, key)
+	if err != nil {
+		return nil, errors.Errorf("Failed to create notification bot's comm: %+v", err)
+	}
+
+	// Start up notification bot server
+	err = impl.Comms.StartNotificationBot()
+	if err != nil {
+		return nil, errors.Errorf("Could not start notification bot's server: %+v", err)
+	}
 
 	// Set up firebase messaging client
 	if !noFirebase {
@@ -193,9 +204,6 @@ func pollForNotifications(nb *Impl) (strings []string, e error) {
 
 // RegisterForNotifications is called by the client, and adds a user registration to our database
 func (nb *Impl) RegisterForNotifications(clientToken []byte, auth *connect.Auth) error {
-	if !auth.IsAuthenticated {
-		return errors.New("Cannot register for notifications: client is not authenticated")
-	}
 	// Implement this
 	u := &storage.User{
 		Id:    auth.Sender.GetId(),
@@ -210,9 +218,6 @@ func (nb *Impl) RegisterForNotifications(clientToken []byte, auth *connect.Auth)
 
 // UnregisterForNotifications is called by the client, and removes a user registration from our database
 func (nb *Impl) UnregisterForNotifications(auth *connect.Auth) error {
-	if !auth.IsAuthenticated {
-		return errors.New("Cannot unregister for notifications: client is not authenticated")
-	}
 	err := nb.Storage.DeleteUser(auth.Sender.GetId())
 	if err != nil {
 		return errors.Wrap(err, "Failed to unregister user with notifications")
diff --git a/notifications/notifications_test.go b/notifications/notifications_test.go
index e62adc2..52ed804 100644
--- a/notifications/notifications_test.go
+++ b/notifications/notifications_test.go
@@ -148,6 +148,10 @@ func (m mockPollComm) RetrieveNdf(currentDef *ndf.NetworkDefinition) (*ndf.Netwo
 	return nil, nil
 }
 
+func (m mockPollComm) StartNotificationBot() error {
+	return nil
+}
+
 type mockPollErrComm struct{}
 
 func (m mockPollErrComm) RequestNotifications(host *connect.Host) (*pb.IDList, error) {
@@ -166,6 +170,10 @@ func (m mockPollErrComm) RetrieveNdf(currentDef *ndf.NetworkDefinition) (*ndf.Ne
 	return nil, nil
 }
 
+func (m mockPollErrComm) StartNotificationBot() error {
+	return nil
+}
+
 // Unit test for PollForNotifications
 func TestPollForNotifications(t *testing.T) {
 	impl := &Impl{
-- 
GitLab