1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-mock.git synced 2024-11-09 23:54:27 +00:00
cchat-mock/message.go

153 lines
3.1 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/diamondburned/aqs"
2020-05-20 07:13:12 +00:00
"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"
_ "github.com/diamondburned/aqs/data"
"github.com/diamondburned/aqs/incr"
2020-05-20 07:13:12 +00:00
)
const avatarURL = "https://gist.github.com/diamondburned/945744c2b5ce0aa0581c9267a4e5cf24/raw/598069da673093aaca4cd4aa0ede1a0e324e9a3a/astolfo_selfie.png"
2020-06-08 22:19:13 +00:00
type MessageHeader struct {
id uint32
time time.Time
}
var _ cchat.MessageHeader = (*Message)(nil)
2020-06-09 03:58:12 +00:00
func parseID(id string) (uint32, error) {
i, err := strconv.Atoi(id)
if err != nil {
return 0, err
}
return uint32(i), nil
}
2020-06-08 22:19:13 +00:00
func (m MessageHeader) ID() string {
return strconv.Itoa(int(m.id))
}
func (m MessageHeader) Time() time.Time {
return m.time
}
2020-05-20 07:13:12 +00:00
type Message struct {
2020-06-08 22:19:13 +00:00
MessageHeader
author Author
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
)
func newEmptyMessage(id uint32, author Author) Message {
2020-06-03 23:13:06 +00:00
return Message{
2020-06-08 22:19:13 +00:00
MessageHeader: MessageHeader{id: id},
author: author,
2020-06-03 23:13:06 +00:00
}
}
func newRandomMessage(id uint32, author Author) Message {
2020-06-03 23:13:06 +00:00
return Message{
2020-06-08 22:19:13 +00:00
MessageHeader: MessageHeader{id: id, time: time.Now()},
author: author,
content: incr.RandomQuote(author.char),
2020-06-03 23:13:06 +00:00
}
}
func echoMessage(sendable cchat.SendableMessage, id uint32, author Author) Message {
2020-05-20 07:13:12 +00:00
var echo = Message{
2020-06-08 22:19:13 +00:00
MessageHeader: MessageHeader{id: id, time: time.Now()},
author: author,
content: sendable.Content(),
2020-05-20 07:13:12 +00:00
}
if noncer, ok := sendable.(cchat.MessageNonce); ok {
echo.nonce = noncer.Nonce()
}
return echo
}
func randomMessage(id uint32) Message {
return randomMessageWithAuthor(id, randomAuthor())
}
func randomMessageWithAuthor(id uint32, author Author) Message {
2020-05-20 07:13:12 +00:00
return Message{
2020-06-08 22:19:13 +00:00
MessageHeader: MessageHeader{id: id, time: time.Now()},
author: author,
content: incr.RandomQuote(author.char),
2020-05-20 07:13:12 +00:00
}
}
2020-06-03 23:13:06 +00:00
func (m Message) Author() cchat.MessageAuthor {
return 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
return strings.Contains(m.content, m.author.name.Content)
2020-06-03 23:13:06 +00:00
}
type Author struct {
2020-06-04 04:36:46 +00:00
name text.Rich
char aqs.Character
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 newAuthor(name text.Rich) Author {
return Author{name: name}
}
2020-07-03 23:53:46 +00:00
func randomAuthor() Author {
var char = aqs.RandomCharacter()
2020-07-03 23:53:46 +00:00
return Author{
char: char,
2020-07-03 23:53:46 +00:00
name: text.Rich{
Content: char.Name,
Segments: []text.Segment{segments.NewColorful(char.Name, char.NameColor())},
2020-07-03 23:53:46 +00:00
},
}
}
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 {
if a.char.ImageURL != "" {
return a.char.ImageURL
}
return avatarURL
2020-06-03 23:13:06 +00:00
}