mirror of
https://github.com/diamondburned/cchat-mock.git
synced 2025-03-20 17:09:19 +00:00
Upgraded to comply with cchat v0.0.13
This commit is contained in:
parent
47fa2491d2
commit
3f93ba61cb
51
channel.go
51
channel.go
|
@ -17,7 +17,7 @@ type Channel struct {
|
|||
id uint32
|
||||
name string
|
||||
done chan struct{}
|
||||
send chan Message // ideally this should be another type
|
||||
send chan cchat.SendableMessage // ideally this should be another type
|
||||
lastID uint32
|
||||
}
|
||||
|
||||
|
@ -37,24 +37,50 @@ func (ch *Channel) Name() (string, error) {
|
|||
}
|
||||
|
||||
func (ch *Channel) JoinServer(container cchat.MessagesContainer) error {
|
||||
nextid := func() uint32 {
|
||||
return atomic.AddUint32(&ch.lastID, 1)
|
||||
var lastAuthor string
|
||||
|
||||
var nextID = func() uint32 {
|
||||
id := ch.lastID
|
||||
ch.lastID++
|
||||
return id
|
||||
}
|
||||
var readID = func() uint32 {
|
||||
return atomic.LoadUint32(&ch.lastID)
|
||||
}
|
||||
var randomMsg = func() Message {
|
||||
msg := randomMessage(nextID())
|
||||
lastAuthor = msg.author
|
||||
return msg
|
||||
}
|
||||
|
||||
// Write the backlog.
|
||||
for i := 0; i < 30; i++ {
|
||||
container.CreateMessage(randomMessage(nextid()))
|
||||
container.CreateMessage(randomMsg())
|
||||
}
|
||||
|
||||
ch.done = make(chan struct{})
|
||||
ch.send = make(chan cchat.SendableMessage)
|
||||
|
||||
go func() {
|
||||
ticker := time.Tick(10 * time.Second)
|
||||
ticker := time.NewTicker(4 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
editTick := time.NewTicker(10 * time.Second)
|
||||
defer editTick.Stop()
|
||||
|
||||
deleteTick := time.NewTicker(15 * time.Second)
|
||||
defer deleteTick.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker:
|
||||
container.CreateMessage(randomMessage(nextid()))
|
||||
case msg := <-ch.send:
|
||||
container.CreateMessage(msg)
|
||||
container.CreateMessage(echoMessage(msg, nextID(), ch.session.username))
|
||||
case <-ticker.C:
|
||||
container.CreateMessage(randomMsg())
|
||||
case <-editTick.C:
|
||||
container.UpdateMessage(newRandomMessage(readID(), lastAuthor))
|
||||
case <-deleteTick.C:
|
||||
container.DeleteMessage(newEmptyMessage(readID(), lastAuthor))
|
||||
case <-ch.done:
|
||||
return
|
||||
}
|
||||
|
@ -66,6 +92,7 @@ func (ch *Channel) JoinServer(container cchat.MessagesContainer) error {
|
|||
|
||||
func (ch *Channel) LeaveServer() error {
|
||||
ch.done <- struct{}{}
|
||||
ch.send = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -74,7 +101,13 @@ func (ch *Channel) SendMessage(msg cchat.SendableMessage) error {
|
|||
return errors.New("Failed to send message: Australian Internet unsupported.")
|
||||
}
|
||||
|
||||
ch.send <- echoMessage(msg, atomic.AddUint32(&ch.lastID, 1), ch.session.username)
|
||||
go func() {
|
||||
// Make no guarantee that a message may arrive immediately when the
|
||||
// function exits.
|
||||
<-time.After(time.Second)
|
||||
ch.send <- msg
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
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.10
|
||||
github.com/diamondburned/cchat v0.0.13
|
||||
)
|
||||
|
|
6
go.sum
6
go.sum
|
@ -20,4 +20,10 @@ github.com/diamondburned/cchat v0.0.9 h1:+F96eDDuaOg4v4dz3GBDWbEW4dZ/k5uGrDp33/y
|
|||
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/diamondburned/cchat v0.0.11 h1:0KaR5ISr+8qanItXW538Krdb+ZIarR4j/4GSGqlLjjI=
|
||||
github.com/diamondburned/cchat v0.0.11/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||
github.com/diamondburned/cchat v0.0.12 h1:ahu5xTRGtMrsudbUy6G2cPIvu0s+3OvXzX5hCxZmADE=
|
||||
github.com/diamondburned/cchat v0.0.12/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||
github.com/diamondburned/cchat v0.0.13 h1:p8SyFjiRVCTjvwSJ4FsICGVYVZ3g0Iu02FrwmLuKiKE=
|
||||
github.com/diamondburned/cchat v0.0.13/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
|
|
55
message.go
55
message.go
|
@ -2,6 +2,7 @@ package mock
|
|||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
|
@ -18,12 +19,29 @@ type Message struct {
|
|||
}
|
||||
|
||||
var (
|
||||
_ cchat.MessageCreate = (*Message)(nil)
|
||||
_ cchat.MessageUpdate = (*Message)(nil)
|
||||
_ cchat.MessageDelete = (*Message)(nil)
|
||||
_ cchat.MessageNonce = (*Message)(nil)
|
||||
_ cchat.MessageCreate = (*Message)(nil)
|
||||
_ cchat.MessageUpdate = (*Message)(nil)
|
||||
_ cchat.MessageDelete = (*Message)(nil)
|
||||
_ cchat.MessageNonce = (*Message)(nil)
|
||||
_ cchat.MessageMentioned = (*Message)(nil)
|
||||
)
|
||||
|
||||
func newEmptyMessage(id uint32, author string) Message {
|
||||
return Message{
|
||||
id: id,
|
||||
author: author,
|
||||
}
|
||||
}
|
||||
|
||||
func newRandomMessage(id uint32, author string) Message {
|
||||
return Message{
|
||||
id: id,
|
||||
time: time.Now(),
|
||||
author: author,
|
||||
content: randomdata.Paragraph(),
|
||||
}
|
||||
}
|
||||
|
||||
func echoMessage(sendable cchat.SendableMessage, id uint32, author string) Message {
|
||||
var echo = Message{
|
||||
id: id,
|
||||
|
@ -44,7 +62,6 @@ func randomMessage(id uint32) Message {
|
|||
time: now,
|
||||
author: randomdata.SillyName(),
|
||||
content: randomdata.Paragraph(),
|
||||
nonce: now.Format(time.RFC3339Nano),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,8 +73,8 @@ func (m Message) Time() time.Time {
|
|||
return m.time
|
||||
}
|
||||
|
||||
func (m Message) Author() text.Rich {
|
||||
return text.Rich{Content: m.author}
|
||||
func (m Message) Author() cchat.MessageAuthor {
|
||||
return Author{name: m.author}
|
||||
}
|
||||
|
||||
func (m Message) Content() text.Rich {
|
||||
|
@ -67,3 +84,27 @@ func (m Message) Content() text.Rich {
|
|||
func (m Message) Nonce() string {
|
||||
return m.nonce
|
||||
}
|
||||
|
||||
// Mentioned is true when the message content contains the author's name.
|
||||
func (m Message) Mentioned() bool {
|
||||
// hack
|
||||
return strings.Contains(m.content, m.author)
|
||||
}
|
||||
|
||||
type Author struct {
|
||||
name string
|
||||
}
|
||||
|
||||
var _ cchat.MessageAuthor = (*Author)(nil)
|
||||
|
||||
func (a Author) ID() string {
|
||||
return a.name
|
||||
}
|
||||
|
||||
func (a Author) Name() text.Rich {
|
||||
return text.Rich{Content: a.name}
|
||||
}
|
||||
|
||||
func (a Author) Avatar() string {
|
||||
return "https://gist.github.com/diamondburned/945744c2b5ce0aa0581c9267a4e5cf24/raw/598069da673093aaca4cd4aa0ede1a0e324e9a3a/astolfo_selfie.png"
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue