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

Added get token to notifications

parent 726d2194
No related branches found
No related tags found
1 merge request!617Project/haven beta
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
package bindings package bindings
import ( import (
"encoding/json"
"github.com/pkg/errors" "github.com/pkg/errors"
"gitlab.com/elixxir/client/v4/notifications" "gitlab.com/elixxir/client/v4/notifications"
"sync" "sync"
...@@ -40,6 +41,29 @@ func (n *Notifications) RemoveToken() error { ...@@ -40,6 +41,29 @@ func (n *Notifications) RemoveToken() error {
return n.manager.RemoveToken() 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 // SetMaxState sets the maximum functional state of any identity
// downstream moduals will be told to clamp any state greater than maxState // downstream moduals will be told to clamp any state greater than maxState
// down to maxState. Depending on UX requirements, they may still show the // down to maxState. Depending on UX requirements, they may still show the
......
...@@ -97,6 +97,9 @@ type Manager interface { ...@@ -97,6 +97,9 @@ type Manager interface {
// It will remove all registered identities if it is the last Token. // It will remove all registered identities if it is the last Token.
RemoveToken() error 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 // RegisterUpdateCallback registers a callback to be used to receive updates
// to changes in notifications. Because this is being called after // to changes in notifications. Because this is being called after
// initialization, a poll of state via the get function will be necessary // initialization, a poll of state via the get function will be necessary
......
...@@ -86,3 +86,15 @@ func (m *manager) RemoveToken() error { ...@@ -86,3 +86,15 @@ func (m *manager) RemoveToken() error {
m.deleteTokenUnsafe() m.deleteTokenUnsafe()
return nil 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, "", ""
}
}
...@@ -90,3 +90,52 @@ func TestManager_RemoveToken_Smoke(t *testing.T) { ...@@ -90,3 +90,52 @@ func TestManager_RemoveToken_Smoke(t *testing.T) {
err) 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")
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment