6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-07-03 00:33:38 +00:00
prologic-saltyim/internal/pwa/components/state.go
mlctrez fcc4f53f20 navigation drawer is now fixed for > 900px windows (#157)
Co-authored-by: mlctrez <mlctrez@gmail.com>
Reviewed-on: https://git.mills.io/saltyim/saltyim/pulls/157
Reviewed-by: James Mills <james@mills.io>
Co-authored-by: mlctrez <mlctrez@noreply@mills.io>
Co-committed-by: mlctrez <mlctrez@noreply@mills.io>
2022-04-07 03:43:09 +00:00

50 lines
1.4 KiB
Go

package components
import (
"bytes"
"fmt"
"github.com/maxence-charriere/go-app/v9/pkg/app"
"go.mills.io/saltyim"
)
const (
saltyIdentityKey = "salty-identity"
saltyStateKey = "salty-state"
)
func GetIdentityFromState(ctx app.Context) (identity *saltyim.Identity, err error) {
var identityString string
ctx.GetState(saltyIdentityKey, &identityString)
if identityString != "" {
return saltyim.GetIdentity(saltyim.WithIdentityBytes([]byte(identityString)))
}
return nil, fmt.Errorf("missing state at key %q", saltyIdentityKey)
}
func SetIdentityToState(ctx app.Context, identity *saltyim.Identity) (err error) {
if identity == nil || identity.Contents() == nil || len(identity.Contents()) == 0 {
return fmt.Errorf("no identity contents")
}
ctx.SetState(saltyIdentityKey, string(identity.Contents()), app.Persist, app.Encrypt)
return
}
func GetStateFromState(ctx app.Context) (state *saltyim.State, err error) {
var stateJson string
ctx.GetState(saltyStateKey, &stateJson)
if stateJson == "" {
return nil, fmt.Errorf("no state found")
}
return saltyim.LoadState(bytes.NewBufferString(stateJson))
}
func SetStateToState(ctx app.Context, state *saltyim.State) (err error) {
jsonBytes, err := state.Bytes()
if err != nil {
return fmt.Errorf("error serializing state: %w", err)
}
ctx.SetState(saltyStateKey, string(jsonBytes), app.Persist, app.Encrypt)
return
}