1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-mock.git synced 2024-12-27 13:46:44 +00:00
cchat-mock/service.go

168 lines
3.7 KiB
Go
Raw Normal View History

2020-05-20 07:13:12 +00:00
// Package mock contains a mock cchat backend.
package mock
import (
"encoding/json"
"strconv"
2020-06-09 03:58:12 +00:00
"time"
2020-05-20 07:13:12 +00:00
"github.com/diamondburned/cchat"
2020-05-23 02:44:50 +00:00
"github.com/diamondburned/cchat/services"
2020-06-04 04:36:46 +00:00
"github.com/diamondburned/cchat/text"
"github.com/pkg/errors"
2020-05-20 07:13:12 +00:00
)
2020-05-23 02:44:50 +00:00
func init() {
services.RegisterService(&Service{})
2020-05-20 07:13:12 +00:00
}
// ErrInvalidSession is returned if SessionRestore is given a bad session.
var ErrInvalidSession = errors.New("invalid session")
2020-05-23 02:44:50 +00:00
type Service struct{}
2020-05-20 07:13:12 +00:00
var (
_ cchat.Service = (*Service)(nil)
_ cchat.Configurator = (*Service)(nil)
_ cchat.SessionRestorer = (*Service)(nil)
2020-05-20 07:13:12 +00:00
)
2020-06-09 03:58:12 +00:00
func (s Service) Name() text.Rich {
return text.Rich{Content: "Mock"}
2020-05-20 07:13:12 +00:00
}
func (s Service) RestoreSession(storage map[string]string) (cchat.Session, error) {
if err := simulateAustralianInternet(); err != nil {
return nil, errors.Wrap(err, "Restore failed")
}
username, ok := storage["username"]
if !ok {
return nil, ErrInvalidSession
}
return newSession(username), nil
}
func (s Service) Authenticate() cchat.Authenticator {
return Authenticator{}
}
type Authenticator struct{}
var _ cchat.Authenticator = (*Authenticator)(nil)
func (Authenticator) AuthenticateForm() []cchat.AuthenticateEntry {
return []cchat.AuthenticateEntry{
{
Name: "Username",
},
{
Name: "Password (ignored)",
Secret: true,
},
{
Name: "Paragraph (ignored)",
Multiline: true,
},
}
2020-05-20 07:13:12 +00:00
}
func (Authenticator) Authenticate(form []string) (cchat.Session, error) {
// SLOW IO TIME.
if err := simulateAustralianInternet(); err != nil {
return nil, errors.Wrap(err, "Authentication failed")
}
return newSession(form[0]), nil
2020-05-20 07:13:12 +00:00
}
var (
2020-06-09 03:58:12 +00:00
// channel.go @ simulateAustralianInternet
2020-05-20 07:13:12 +00:00
internetCanFail = true
// 500ms ~ 3s
internetMinLatency = 500
internetMaxLatency = 3000
2020-05-20 07:13:12 +00:00
)
2020-05-23 02:44:50 +00:00
func (s Service) Configuration() (map[string]string, error) {
2020-05-20 07:13:12 +00:00
return map[string]string{
"internet.canFail": strconv.FormatBool(internetCanFail),
"internet.minLatency": strconv.Itoa(internetMinLatency),
"internet.maxLatency": strconv.Itoa(internetMaxLatency),
}, nil
}
2020-05-23 02:44:50 +00:00
func (s Service) SetConfiguration(config map[string]string) error {
2020-05-20 07:13:12 +00:00
for _, err := range []error{
// shit code, would not recommend. It's only an ok-ish idea here because
// unmarshalConfig() returns ErrInvalidConfigAtField.
unmarshalConfig(config, "internet.canFail", &internetCanFail),
unmarshalConfig(config, "internet.minLatency", &internetMinLatency),
unmarshalConfig(config, "internet.maxLatency", &internetMaxLatency),
} {
if err != nil {
return err
}
}
return nil
}
func unmarshalConfig(config map[string]string, key string, value interface{}) error {
if err := json.Unmarshal([]byte(config[key]), value); err != nil {
return &cchat.ErrInvalidConfigAtField{
Key: key,
Err: err,
2020-05-20 07:13:12 +00:00
}
}
return nil
}
2020-05-23 02:44:50 +00:00
type Session struct {
username string
servers []cchat.Server
lastid uint32 // used for generation
2020-05-23 02:44:50 +00:00
}
var (
_ cchat.Icon = (*Session)(nil)
_ cchat.Session = (*Session)(nil)
_ cchat.SessionSaver = (*Session)(nil)
)
func newSession(username string) *Session {
ses := &Session{username: username}
ses.servers = GenerateServers(ses)
return ses
}
2020-05-23 02:44:50 +00:00
2020-06-04 04:36:46 +00:00
func (s *Session) ID() string {
return s.username
2020-05-23 02:44:50 +00:00
}
2020-06-09 03:58:12 +00:00
func (s *Session) Name() text.Rich {
return text.Rich{Content: s.username}
2020-06-03 23:24:43 +00:00
}
2020-05-23 02:44:50 +00:00
func (s *Session) Servers(container cchat.ServersContainer) error {
2020-06-09 03:58:12 +00:00
// Simulate slight IO.
<-time.After(time.Second)
2020-05-23 02:44:50 +00:00
container.SetServers(s.servers)
return nil
}
2020-06-09 03:58:12 +00:00
func (s *Session) Icon(iconer cchat.IconContainer) error {
// Simulate IO.
simulateAustralianInternet()
iconer.SetIcon(avatarURL)
2020-06-09 03:58:12 +00:00
return nil
}
func (s *Session) Save() (map[string]string, error) {
return map[string]string{
"username": s.username,
}, nil
}