1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-mock.git synced 2024-11-08 07:14:21 +00:00
cchat-mock/internal/message/message.go

101 lines
2 KiB
Go
Raw Normal View History

2020-10-04 20:47:41 +00:00
package message
import (
"strings"
"time"
"github.com/diamondburned/aqs/incr"
"github.com/diamondburned/cchat"
"github.com/diamondburned/cchat/text"
_ "github.com/diamondburned/aqs/data"
)
type Message struct {
Header
author Author
content string
nonce string
2020-10-04 20:47:41 +00:00
}
var (
_ cchat.MessageCreate = (*Message)(nil)
_ cchat.MessageUpdate = (*Message)(nil)
_ cchat.MessageDelete = (*Message)(nil)
_ cchat.Noncer = (*Message)(nil)
2020-10-04 20:47:41 +00:00
)
func NewEmpty(id uint32, author Author) Message {
return Message{
Header: Header{id: id},
author: author,
}
}
func NewRandomFromMessage(old Message) Message {
return NewRandom(old.id, old.author)
}
func NewRandom(id uint32, author Author) Message {
return Message{
Header: Header{id: id, time: time.Now()},
author: author,
content: incr.RandomQuote(author.char),
}
}
func Echo(sendable cchat.SendableMessage, id uint32, author Author) Message {
var echo = Message{
Header: Header{id: id, time: time.Now()},
author: author,
content: sendable.Content(),
}
if noncer := sendable.AsNoncer(); noncer != nil {
echo.nonce = noncer.Nonce()
}
2020-10-04 20:47:41 +00:00
return echo
}
func Random(id uint32) Message {
return RandomWithAuthor(id, RandomAuthor())
}
func RandomWithAuthor(id uint32, author Author) Message {
return Message{
Header: Header{id: id, time: time.Now()},
author: author,
content: incr.RandomQuote(author.char),
}
}
func (m Message) Author() cchat.Author {
return m.author
}
func (m Message) RealAuthor() Author {
return m.author
}
// AuthorName returns the message author's username in string.
func (m Message) AuthorName() string {
return m.author.name.Content
}
func (m Message) Content() text.Rich {
return text.Rich{Content: m.content}
}
func (m Message) Nonce() string {
return m.nonce
}
2020-10-04 20:47:41 +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)
}
func (m *Message) SetContent(content string) {
m.content = content
}