mirror of
https://github.com/diamondburned/cchat-mock.git
synced 2025-01-29 21:27:22 +00:00
Implemented SessionSaver and SessionRestorer
This commit is contained in:
parent
807afeffb7
commit
bc9f49c387
2
go.mod
2
go.mod
|
@ -4,5 +4,5 @@ go 1.14
|
|||
|
||||
require (
|
||||
github.com/Pallinder/go-randomdata v1.2.0
|
||||
github.com/diamondburned/cchat v0.0.9
|
||||
github.com/diamondburned/cchat v0.0.10
|
||||
)
|
||||
|
|
2
go.sum
2
go.sum
|
@ -18,4 +18,6 @@ github.com/diamondburned/cchat v0.0.8 h1:/PmI23SFHJcjYBWNBwQbp36n7fDvDu+NMnQuhM5
|
|||
github.com/diamondburned/cchat v0.0.8/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||
github.com/diamondburned/cchat v0.0.9 h1:+F96eDDuaOg4v4dz3GBDWbEW4dZ/k5uGrDp33/yeXR8=
|
||||
github.com/diamondburned/cchat v0.0.9/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||
github.com/diamondburned/cchat v0.0.10 h1:aiUVgGre5E/HV+Iw6tmBVbuGctQI+JndV9nIDoYuRPY=
|
||||
github.com/diamondburned/cchat v0.0.10/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
|
|
41
service.go
41
service.go
|
@ -3,6 +3,7 @@ package mock
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/diamondburned/cchat"
|
||||
|
@ -13,17 +14,30 @@ func init() {
|
|||
services.RegisterService(&Service{})
|
||||
}
|
||||
|
||||
// ErrInvalidSession is returned if SessionRestore is given a bad session.
|
||||
var ErrInvalidSession = errors.New("invalid session")
|
||||
|
||||
type Service struct{}
|
||||
|
||||
var (
|
||||
_ cchat.Service = (*Service)(nil)
|
||||
_ cchat.Configurator = (*Service)(nil)
|
||||
_ cchat.Service = (*Service)(nil)
|
||||
_ cchat.Configurator = (*Service)(nil)
|
||||
_ cchat.SessionRestorer = (*Service)(nil)
|
||||
)
|
||||
|
||||
func (s Service) Name() string {
|
||||
return "Mock"
|
||||
}
|
||||
|
||||
func (s Service) RestoreSession(storage map[string]string) (cchat.Session, error) {
|
||||
username, ok := storage["username"]
|
||||
if !ok {
|
||||
return nil, ErrInvalidSession
|
||||
}
|
||||
|
||||
return newSession(username), nil
|
||||
}
|
||||
|
||||
func (s Service) Authenticate() cchat.Authenticator {
|
||||
return Authenticator{}
|
||||
}
|
||||
|
@ -39,9 +53,7 @@ func (Authenticator) AuthenticateForm() []cchat.AuthenticateEntry {
|
|||
}
|
||||
|
||||
func (Authenticator) Authenticate(form []string) (cchat.Session, error) {
|
||||
ses := &Session{username: form[0]}
|
||||
ses.servers = GenerateServers(ses)
|
||||
return ses, nil
|
||||
return newSession(form[0]), nil
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -87,10 +99,19 @@ func unmarshalConfig(config map[string]string, key string, value interface{}) er
|
|||
type Session struct {
|
||||
username string
|
||||
servers []cchat.Server
|
||||
lastid uint32
|
||||
lastid uint32 // used for generation
|
||||
}
|
||||
|
||||
var _ cchat.Session = (*Session)(nil)
|
||||
var (
|
||||
_ cchat.Session = (*Session)(nil)
|
||||
_ cchat.SessionSaver = (*Session)(nil)
|
||||
)
|
||||
|
||||
func newSession(username string) *Session {
|
||||
ses := &Session{username: username}
|
||||
ses.servers = GenerateServers(ses)
|
||||
return ses
|
||||
}
|
||||
|
||||
func (s *Session) Name() (string, error) {
|
||||
return s.username, nil
|
||||
|
@ -100,3 +121,9 @@ func (s *Session) Servers(container cchat.ServersContainer) error {
|
|||
container.SetServers(s.servers)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Session) Save() (map[string]string, error) {
|
||||
return map[string]string{
|
||||
"username": s.username,
|
||||
}, nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue