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
|
2020-12-17 22:28:44 +00:00
|
|
|
nonce string
|
2020-10-04 20:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ cchat.MessageCreate = (*Message)(nil)
|
|
|
|
_ cchat.MessageUpdate = (*Message)(nil)
|
|
|
|
_ cchat.MessageDelete = (*Message)(nil)
|
2020-12-17 22:28:44 +00:00
|
|
|
_ 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(),
|
|
|
|
}
|
2020-12-17 22:28:44 +00:00
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2020-12-17 22:28:44 +00:00
|
|
|
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
|
|
|
|
}
|