1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-mock.git synced 2024-11-13 01:22:44 +00:00
cchat-mock/message.go

120 lines
2.3 KiB
Go
Raw Normal View History

2020-05-20 07:13:12 +00:00
package mock
import (
"strconv"
2020-06-03 23:13:06 +00:00
"strings"
2020-05-20 07:13:12 +00:00
"time"
"github.com/Pallinder/go-randomdata"
"github.com/diamondburned/cchat"
2020-06-04 04:36:46 +00:00
"github.com/diamondburned/cchat-mock/segments"
2020-05-20 07:13:12 +00:00
"github.com/diamondburned/cchat/text"
)
type Message struct {
id uint32
time time.Time
2020-06-04 04:36:46 +00:00
author text.Rich
2020-05-20 07:13:12 +00:00
content string
nonce string
}
var (
2020-06-03 23:13:06 +00:00
_ cchat.MessageCreate = (*Message)(nil)
_ cchat.MessageUpdate = (*Message)(nil)
_ cchat.MessageDelete = (*Message)(nil)
_ cchat.MessageNonce = (*Message)(nil)
_ cchat.MessageMentioned = (*Message)(nil)
2020-05-20 07:13:12 +00:00
)
2020-06-04 04:36:46 +00:00
func newEmptyMessage(id uint32, author text.Rich) Message {
2020-06-03 23:13:06 +00:00
return Message{
id: id,
author: author,
}
}
2020-06-04 04:36:46 +00:00
func newRandomMessage(id uint32, author text.Rich) Message {
2020-06-03 23:13:06 +00:00
return Message{
id: id,
time: time.Now(),
author: author,
content: randomdata.Paragraph(),
}
}
2020-06-04 04:36:46 +00:00
func echoMessage(sendable cchat.SendableMessage, id uint32, author text.Rich) Message {
2020-05-20 07:13:12 +00:00
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()
2020-06-04 04:36:46 +00:00
var author = randomdata.SillyName()
2020-05-20 07:13:12 +00:00
return Message{
2020-06-04 04:36:46 +00:00
id: id,
time: now,
author: text.Rich{
Content: author,
Segments: []text.Segment{segments.NewRandomColored(author)},
},
2020-05-20 07:13:12 +00:00
content: randomdata.Paragraph(),
}
}
func (m Message) ID() string {
return strconv.Itoa(int(m.id))
}
func (m Message) Time() time.Time {
return m.time
}
2020-06-03 23:13:06 +00:00
func (m Message) Author() cchat.MessageAuthor {
return Author{name: m.author}
2020-05-20 07:13:12 +00:00
}
func (m Message) Content() text.Rich {
return text.Rich{Content: m.content}
}
func (m Message) Nonce() string {
return m.nonce
}
2020-06-03 23:13:06 +00:00
// Mentioned is true when the message content contains the author's name.
func (m Message) Mentioned() bool {
// hack
2020-06-04 04:36:46 +00:00
return strings.Contains(m.content, m.author.Content)
2020-06-03 23:13:06 +00:00
}
type Author struct {
2020-06-04 04:36:46 +00:00
name text.Rich
2020-06-03 23:13:06 +00:00
}
2020-06-04 04:36:46 +00:00
var (
_ cchat.MessageAuthor = (*Author)(nil)
_ cchat.MessageAuthorAvatar = (*Author)(nil)
)
2020-06-03 23:13:06 +00:00
func (a Author) ID() string {
2020-06-04 04:36:46 +00:00
return a.name.Content
2020-06-03 23:13:06 +00:00
}
func (a Author) Name() text.Rich {
2020-06-04 04:36:46 +00:00
return a.name
2020-06-03 23:13:06 +00:00
}
func (a Author) Avatar() string {
return "https://gist.github.com/diamondburned/945744c2b5ce0aa0581c9267a4e5cf24/raw/598069da673093aaca4cd4aa0ede1a0e324e9a3a/astolfo_selfie.png"
}