From 3f93ba61cb726acea24ce35b877999d5b7bd1a04 Mon Sep 17 00:00:00 2001 From: "diamondburned (Forefront)" Date: Wed, 3 Jun 2020 16:13:06 -0700 Subject: [PATCH] Upgraded to comply with cchat v0.0.13 --- channel.go | 51 +++++++++++++++++++++++++++++++++++++++++--------- go.mod | 2 +- go.sum | 6 ++++++ message.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 97 insertions(+), 17 deletions(-) diff --git a/channel.go b/channel.go index 8dec86f..2d507ff 100644 --- a/channel.go +++ b/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 } diff --git a/go.mod b/go.mod index 9db03bc..907f34e 100644 --- a/go.mod +++ b/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 ) diff --git a/go.sum b/go.sum index d077f65..48efe46 100644 --- a/go.sum +++ b/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= diff --git a/message.go b/message.go index e46ddbb..463a434 100644 --- a/message.go +++ b/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" +}