mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-07-08 11:01:20 +00:00
30 lines
816 B
Go
30 lines
816 B
Go
package components
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/maxence-charriere/go-app/v9/pkg/app"
|
|
"go.mills.io/saltyim"
|
|
)
|
|
|
|
const (
|
|
saltyIdentityKey = "salty-identity"
|
|
)
|
|
|
|
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
|
|
}
|