diff --git a/bindings/notifications.go b/bindings/notifications.go index cc84b2b927741a075c3cefd462fe401de2dd276f..3d28c87963ff92d5591590aaec25641927e68147 100644 --- a/bindings/notifications.go +++ b/bindings/notifications.go @@ -8,6 +8,7 @@ package bindings import ( + "encoding/json" "github.com/pkg/errors" "gitlab.com/elixxir/client/v4/notifications" "sync" @@ -40,6 +41,29 @@ func (n *Notifications) RemoveToken() error { return n.manager.RemoveToken() } +type GetTokenJson struct { + Exists bool `json:"exists"` + Token string `json:"token"` + App string `json:"app"` +} + +// GetToken returns the token if it exists +// +// { +// "exists":true, +// "Token":"Z1owNo+GvizWshVW/C5IJ1izPD5oqMkCGr+PsA5If4HZ", +// "App":"havenIOS" +// } +func (n *Notifications) GetToken() ([]byte, error) { + exist, token, app := n.manager.GetToken() + gtj := &GetTokenJson{ + Exists: exist, + Token: token, + App: app, + } + return json.Marshal(gtj) +} + // SetMaxState sets the maximum functional state of any identity // downstream moduals will be told to clamp any state greater than maxState // down to maxState. Depending on UX requirements, they may still show the diff --git a/notifications/interface.go b/notifications/interface.go index 66bd3646b17112e49af16fc1366e019b8dc63df0..9ce901a3d5edbf80d53637176b5fbd664b88f222 100644 --- a/notifications/interface.go +++ b/notifications/interface.go @@ -97,6 +97,9 @@ type Manager interface { // It will remove all registered identities if it is the last Token. RemoveToken() error + // GetToken returns the token if it is registered + GetToken() (has bool, token, app string) + // RegisterUpdateCallback registers a callback to be used to receive updates // to changes in notifications. Because this is being called after // initialization, a poll of state via the get function will be necessary diff --git a/notifications/token.go b/notifications/token.go index c0e437f30bf989169af1fbf3523e481ba8cb9299..64f5e65b5acfa500d4c19c820712188e54a23def 100644 --- a/notifications/token.go +++ b/notifications/token.go @@ -86,3 +86,15 @@ func (m *manager) RemoveToken() error { m.deleteTokenUnsafe() return nil } + +// GetToken returns the token if it is registered +func (m *manager) GetToken() (has bool, token, app string) { + m.mux.RLock() + defer m.mux.RUnlock() + if m.token.Token != "" { + return true, m.token.Token, m.token.App + } else { + return false, "", "" + } + +} diff --git a/notifications/token_test.go b/notifications/token_test.go index 81927e8fb37ec531dea29c3a42070e2f13d12d6f..4ca9da9b62dbc53c3dffa2ca56b69cd5c7d3954b 100644 --- a/notifications/token_test.go +++ b/notifications/token_test.go @@ -90,3 +90,52 @@ func TestManager_RemoveToken_Smoke(t *testing.T) { err) } } + +func TestManager_GetToken_Smoke(t *testing.T) { + + m, _, _ := buildTestingManager(t) + + testToken := "mickey" + testApp := "mouse" + + exist1, token1, app1 := m.GetToken() + + if exist1 { + t.Errorf("token exists when it shouldnt") + } + + if token1 != "" || app1 != "" { + t.Errorf("token values are set exists when they shouldnt be") + } + + err := m.AddToken(testToken, testApp) + if err != nil { + t.Errorf("Failed to add token when it should be "+ + "possible: %+v", err) + } + + exist2, token2, app2 := m.GetToken() + + if !exist2 { + t.Errorf("token doesnt exists when it shouldnt") + } + + if token2 != testToken || app2 != testApp { + t.Errorf("token values are not set correctly") + } + + err = m.RemoveToken() + if err != nil { + t.Errorf("remove token errored when it shouldnt have: %+v", err) + } + + exist3, token3, app3 := m.GetToken() + + if exist3 { + t.Errorf("token exists when it shouldnt") + } + + if token3 != "" || app3 != "" { + t.Errorf("token values are not set correctly") + } +}