mirror of
https://github.com/diamondburned/cchat-mock.git
synced 2024-12-26 13:17:11 +00:00
Initial commit
This commit is contained in:
commit
fcba545af1
5
LICENSE
Normal file
5
LICENSE
Normal file
|
@ -0,0 +1,5 @@
|
|||
Copyright 2020 diamondburned
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
88
channel.go
Normal file
88
channel.go
Normal file
|
@ -0,0 +1,88 @@
|
|||
package mock
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/rand"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
"github.com/diamondburned/cchat"
|
||||
)
|
||||
|
||||
type Channel struct {
|
||||
session *Service
|
||||
name string
|
||||
done chan struct{}
|
||||
send chan Message // ideally this should be another type
|
||||
lastID uint32
|
||||
}
|
||||
|
||||
var (
|
||||
_ cchat.Server = (*Channel)(nil)
|
||||
_ cchat.ServerMessage = (*Channel)(nil)
|
||||
)
|
||||
|
||||
func (ch *Channel) Name() (string, error) {
|
||||
return ch.name, nil
|
||||
}
|
||||
|
||||
func (ch *Channel) JoinServer(container cchat.MessagesContainer) error {
|
||||
nextid := func() uint32 {
|
||||
return atomic.AddUint32(&ch.lastID, 1)
|
||||
}
|
||||
|
||||
// Write the backlog.
|
||||
for i := 0; i < 30; i++ {
|
||||
container.CreateMessage(randomMessage(nextid()))
|
||||
}
|
||||
|
||||
ch.done = make(chan struct{})
|
||||
go func() {
|
||||
ticker := time.Tick(10 * time.Second)
|
||||
for {
|
||||
select {
|
||||
case <-ticker:
|
||||
container.CreateMessage(randomMessage(nextid()))
|
||||
case msg := <-ch.send:
|
||||
container.CreateMessage(msg)
|
||||
case <-ch.done:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ch *Channel) LeaveServer() error {
|
||||
ch.done <- struct{}{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ch *Channel) SendMessage(msg cchat.SendableMessage) error {
|
||||
if emulateAustralianInternet() {
|
||||
return errors.New("Failed to send message: Australian Internet unsupported.")
|
||||
}
|
||||
|
||||
ch.send <- echoMessage(msg, atomic.AddUint32(&ch.lastID, 1), ch.session.username)
|
||||
return nil
|
||||
}
|
||||
|
||||
func generateChannels(s *Service, amount int) []cchat.Server {
|
||||
var channels = make([]cchat.Server, amount)
|
||||
for i := range channels {
|
||||
channels[i] = &Channel{session: s, name: "#" + randomdata.Noun()}
|
||||
}
|
||||
return channels
|
||||
}
|
||||
|
||||
// emulate network latency
|
||||
func emulateAustralianInternet() (lost bool) {
|
||||
var ms = rand.Intn(internetMaxLatency) + internetMinLatency
|
||||
<-time.After(time.Duration(ms) * time.Millisecond)
|
||||
|
||||
// because australia, drop packet 20% of the time if internetCanFail is
|
||||
// true.
|
||||
return internetCanFail && rand.Intn(100) < 20
|
||||
}
|
8
go.mod
Normal file
8
go.mod
Normal file
|
@ -0,0 +1,8 @@
|
|||
module github.com/diamondburned/cchat-mock
|
||||
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/Pallinder/go-randomdata v1.2.0 // indirect
|
||||
github.com/diamondburned/cchat v0.0.2 // indirect
|
||||
)
|
6
go.sum
Normal file
6
go.sum
Normal file
|
@ -0,0 +1,6 @@
|
|||
github.com/Pallinder/go-randomdata v1.2.0 h1:DZ41wBchNRb/0GfsePLiSwb0PHZmT67XY00lCDlaYPg=
|
||||
github.com/Pallinder/go-randomdata v1.2.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y=
|
||||
github.com/diamondburned/cchat v0.0.1 h1:ABoYPQ+uhsUopdDDw62jFsEjvsz6dlOttL/xIeU4wy4=
|
||||
github.com/diamondburned/cchat v0.0.1/go.mod h1:2MdhWABRer4WhwcuLR0b2VY5S22Y1zDTpFqriAFrC08=
|
||||
github.com/diamondburned/cchat v0.0.2 h1:WfynDllfkUeTWKGwNWz5AWEXL88rl+6FO/uSfMdNiss=
|
||||
github.com/diamondburned/cchat v0.0.2/go.mod h1:2MdhWABRer4WhwcuLR0b2VY5S22Y1zDTpFqriAFrC08=
|
69
message.go
Normal file
69
message.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
package mock
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
"github.com/diamondburned/cchat"
|
||||
"github.com/diamondburned/cchat/text"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
id uint32
|
||||
time time.Time
|
||||
author string
|
||||
content string
|
||||
nonce string
|
||||
}
|
||||
|
||||
var (
|
||||
_ cchat.MessageCreate = (*Message)(nil)
|
||||
_ cchat.MessageUpdate = (*Message)(nil)
|
||||
_ cchat.MessageDelete = (*Message)(nil)
|
||||
_ cchat.MessageNonce = (*Message)(nil)
|
||||
)
|
||||
|
||||
func echoMessage(sendable cchat.SendableMessage, id uint32, author string) Message {
|
||||
var echo = Message{
|
||||
id: id,
|
||||
time: time.Now(),
|
||||
author: author,
|
||||
content: sendable.Content(),
|
||||
}
|
||||
if noncer, ok := sendable.(cchat.MessageNonce); ok {
|
||||
echo.nonce = noncer.Nonce()
|
||||
}
|
||||
return echo
|
||||
}
|
||||
|
||||
func randomMessage(id uint32) Message {
|
||||
var now = time.Now()
|
||||
return Message{
|
||||
id: id,
|
||||
time: now,
|
||||
author: randomdata.SillyName(),
|
||||
content: randomdata.Paragraph(),
|
||||
nonce: now.Format(time.RFC3339Nano),
|
||||
}
|
||||
}
|
||||
|
||||
func (m Message) ID() string {
|
||||
return strconv.Itoa(int(m.id))
|
||||
}
|
||||
|
||||
func (m Message) Time() time.Time {
|
||||
return m.time
|
||||
}
|
||||
|
||||
func (m Message) Author() text.Rich {
|
||||
return text.Rich{Content: m.author}
|
||||
}
|
||||
|
||||
func (m Message) Content() text.Rich {
|
||||
return text.Rich{Content: m.content}
|
||||
}
|
||||
|
||||
func (m Message) Nonce() string {
|
||||
return m.nonce
|
||||
}
|
44
server.go
Normal file
44
server.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package mock
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
"github.com/diamondburned/cchat"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
session *Service
|
||||
name string
|
||||
children []cchat.Server
|
||||
}
|
||||
|
||||
var (
|
||||
_ cchat.Server = (*Server)(nil)
|
||||
_ cchat.ServerList = (*Server)(nil)
|
||||
)
|
||||
|
||||
func (sv *Server) Name() (string, error) {
|
||||
return sv.name, nil
|
||||
}
|
||||
|
||||
func (sv *Server) Servers(container cchat.ServersContainer) error {
|
||||
container.SetServers(sv.children)
|
||||
return nil
|
||||
}
|
||||
|
||||
func GenerateServers(s *Service) []cchat.Server {
|
||||
return generateServers(s, rand.Intn(45))
|
||||
}
|
||||
|
||||
func generateServers(s *Service, amount int) []cchat.Server {
|
||||
var channels = make([]cchat.Server, amount)
|
||||
for i := range channels {
|
||||
channels[i] = &Server{
|
||||
session: s,
|
||||
name: randomdata.Noun(),
|
||||
children: generateChannels(s, rand.Intn(12)),
|
||||
}
|
||||
}
|
||||
return channels
|
||||
}
|
86
service.go
Normal file
86
service.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
// Package mock contains a mock cchat backend.
|
||||
package mock
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
|
||||
"github.com/diamondburned/cchat"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
username string
|
||||
servers []cchat.Server
|
||||
}
|
||||
|
||||
var (
|
||||
_ cchat.Service = (*Service)(nil)
|
||||
_ cchat.Authenticator = (*Service)(nil)
|
||||
_ cchat.Configurator = (*Service)(nil)
|
||||
)
|
||||
|
||||
func NewService() cchat.Service {
|
||||
return &Service{}
|
||||
}
|
||||
|
||||
func (s *Service) AuthenticateForm() []cchat.AuthenticateEntry {
|
||||
return []cchat.AuthenticateEntry{{
|
||||
Name: "Username",
|
||||
}}
|
||||
}
|
||||
|
||||
func (s *Service) Authenticate(form []string) error {
|
||||
s.username = form[0]
|
||||
s.servers = GenerateServers(s)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) Name() (string, error) {
|
||||
return "Mock backend", nil
|
||||
}
|
||||
|
||||
func (s *Service) Servers(container cchat.ServersContainer) error {
|
||||
container.SetServers(s.servers)
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
// channel.go @ emulateAustralianInternet
|
||||
internetCanFail = true
|
||||
// 500ms ~ 3s
|
||||
internetMinLatency = 500
|
||||
internetMaxLatency = 2500
|
||||
)
|
||||
|
||||
func (s *Service) Configuration() (map[string]string, error) {
|
||||
return map[string]string{
|
||||
"internet.canFail": strconv.FormatBool(internetCanFail),
|
||||
"internet.minLatency": strconv.Itoa(internetMinLatency),
|
||||
"internet.maxLatency": strconv.Itoa(internetMaxLatency),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) SetConfiguration(config map[string]string) error {
|
||||
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,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Reference in a new issue