2020-06-04 23:00:41 +00:00
|
|
|
package keyring
|
|
|
|
|
|
|
|
import (
|
2020-06-07 07:06:13 +00:00
|
|
|
"github.com/diamondburned/cchat"
|
2020-07-09 07:19:18 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/keyring/driver"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/keyring/driver/json"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/keyring/driver/keyring"
|
2020-06-04 23:00:41 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/log"
|
2020-07-09 07:19:18 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/config"
|
2020-06-04 23:00:41 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-07-09 07:19:18 +00:00
|
|
|
// Declare a keyring store with fallbacks.
|
|
|
|
var store = driver.NewStore(
|
|
|
|
keyring.NewProvider(),
|
|
|
|
json.NewProvider(config.DirPath()), // fallback
|
|
|
|
)
|
2020-06-04 23:00:41 +00:00
|
|
|
|
2020-06-07 04:27:28 +00:00
|
|
|
type Session struct {
|
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
Data map[string]string
|
|
|
|
}
|
2020-06-04 23:00:41 +00:00
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
// ConvertSession attempts to get the session data from the given cchat session.
|
|
|
|
// It returns nil if it can't do it.
|
|
|
|
func ConvertSession(ses cchat.Session) *Session {
|
|
|
|
var name = ses.Name().Content
|
|
|
|
|
2020-06-07 07:06:13 +00:00
|
|
|
saver, ok := ses.(cchat.SessionSaver)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := saver.Save()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(errors.Wrapf(err, "Failed to save session ID %s (%s)", ses.ID(), name))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Treat the ID as name if none is provided. This is a shitty hack around
|
|
|
|
// backends that only set the name after returning.
|
|
|
|
if name == "" {
|
|
|
|
name = ses.ID()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Session{
|
|
|
|
ID: ses.ID(),
|
|
|
|
Name: name,
|
|
|
|
Data: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
func SaveSessions(service cchat.Service, sessions []Session) {
|
|
|
|
if err := store.Set(service.Name().Content, sessions); err != nil {
|
2020-06-04 23:00:41 +00:00
|
|
|
log.Warn(errors.Wrap(err, "Error saving session"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestoreSessions restores all sessions of the service asynchronously, then
|
|
|
|
// calls the auth callback inside the Gtk main thread.
|
2020-07-14 07:24:55 +00:00
|
|
|
func RestoreSessions(service cchat.Service) (sessions []Session) {
|
2020-06-04 23:00:41 +00:00
|
|
|
// Ignore the error, it's not important.
|
2020-07-14 07:24:55 +00:00
|
|
|
if err := store.Get(service.Name().Content, &sessions); err != nil {
|
2020-06-04 23:00:41 +00:00
|
|
|
log.Warn(err)
|
|
|
|
}
|
2020-06-07 04:27:28 +00:00
|
|
|
return
|
2020-06-04 23:00:41 +00:00
|
|
|
}
|
2020-06-14 18:19:06 +00:00
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
func RestoreSession(service cchat.Service, id string) *Session {
|
|
|
|
var sessions = RestoreSessions(service)
|
2020-06-14 18:19:06 +00:00
|
|
|
for _, session := range sessions {
|
|
|
|
if session.ID == id {
|
|
|
|
return &session
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|