2020-09-08 04:44:09 +00:00
|
|
|
package message
|
2020-06-16 03:57:33 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2020-12-20 05:44:26 +00:00
|
|
|
"github.com/diamondburned/arikawa/v2/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v2/gateway"
|
2020-06-16 03:57:33 +00:00
|
|
|
"github.com/diamondburned/cchat"
|
2020-09-08 04:44:09 +00:00
|
|
|
"github.com/diamondburned/cchat-discord/internal/discord/state"
|
|
|
|
"github.com/diamondburned/cchat-discord/internal/segments"
|
2020-10-07 01:53:15 +00:00
|
|
|
"github.com/diamondburned/cchat-discord/internal/segments/mention"
|
2020-12-31 02:58:36 +00:00
|
|
|
"github.com/diamondburned/cchat-discord/internal/segments/reference"
|
2020-06-16 03:57:33 +00:00
|
|
|
"github.com/diamondburned/cchat/text"
|
|
|
|
)
|
|
|
|
|
|
|
|
type messageHeader struct {
|
2020-08-15 22:37:44 +00:00
|
|
|
id discord.MessageID
|
2020-06-16 03:57:33 +00:00
|
|
|
time discord.Timestamp
|
2020-12-19 05:46:12 +00:00
|
|
|
nonce string
|
2020-08-15 22:37:44 +00:00
|
|
|
channelID discord.ChannelID
|
|
|
|
guildID discord.GuildID
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ cchat.MessageHeader = (*messageHeader)(nil)
|
|
|
|
|
|
|
|
func newHeader(msg discord.Message) messageHeader {
|
|
|
|
var h = messageHeader{
|
|
|
|
id: msg.ID,
|
|
|
|
time: msg.Timestamp,
|
|
|
|
channelID: msg.ChannelID,
|
|
|
|
guildID: msg.GuildID,
|
|
|
|
}
|
2020-08-15 22:37:44 +00:00
|
|
|
if msg.EditedTimestamp.IsValid() {
|
2020-06-16 03:57:33 +00:00
|
|
|
h.time = msg.EditedTimestamp
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2020-12-19 05:46:12 +00:00
|
|
|
func newHeaderNonce(msg discord.Message, nonce string) messageHeader {
|
|
|
|
h := newHeader(msg)
|
|
|
|
h.nonce = nonce
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
func NewHeaderDelete(d *gateway.MessageDeleteEvent) messageHeader {
|
|
|
|
return messageHeader{
|
|
|
|
id: d.ID,
|
|
|
|
time: discord.Timestamp(time.Now()),
|
|
|
|
channelID: d.ChannelID,
|
|
|
|
guildID: d.GuildID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 23:20:51 +00:00
|
|
|
func (m messageHeader) ID() cchat.ID {
|
2020-06-16 03:57:33 +00:00
|
|
|
return m.id.String()
|
|
|
|
}
|
|
|
|
|
2020-12-19 05:46:12 +00:00
|
|
|
func (m messageHeader) Nonce() string { return m.nonce }
|
|
|
|
|
2020-12-17 08:01:58 +00:00
|
|
|
func (m messageHeader) MessageID() discord.MessageID { return m.id }
|
|
|
|
func (m messageHeader) ChannelID() discord.ChannelID { return m.channelID }
|
|
|
|
func (m messageHeader) GuildID() discord.GuildID { return m.guildID }
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
func (m messageHeader) Time() time.Time {
|
|
|
|
return m.time.Time()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
messageHeader
|
|
|
|
|
|
|
|
author Author
|
|
|
|
content text.Rich
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
mentioned bool
|
|
|
|
}
|
|
|
|
|
2020-10-05 03:45:34 +00:00
|
|
|
var (
|
|
|
|
_ cchat.MessageCreate = (*Message)(nil)
|
|
|
|
_ cchat.MessageUpdate = (*Message)(nil)
|
|
|
|
_ cchat.MessageDelete = (*Message)(nil)
|
2020-12-19 05:46:12 +00:00
|
|
|
_ cchat.Noncer = (*Message)(nil)
|
2020-10-05 03:45:34 +00:00
|
|
|
)
|
|
|
|
|
2020-09-08 04:44:09 +00:00
|
|
|
func NewMessageUpdateContent(msg discord.Message, s *state.Instance) Message {
|
2020-07-09 23:15:58 +00:00
|
|
|
// Check if content is empty.
|
|
|
|
if msg.Content == "" {
|
|
|
|
// Then grab the content from the state.
|
2020-12-20 05:44:26 +00:00
|
|
|
m, err := s.Cabinet.Message(msg.ChannelID, msg.ID)
|
2020-07-09 23:15:58 +00:00
|
|
|
if err == nil {
|
|
|
|
msg.Content = m.Content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-20 05:44:26 +00:00
|
|
|
var content = segments.ParseMessage(&msg, s.Cabinet)
|
2020-06-16 03:57:33 +00:00
|
|
|
return Message{
|
|
|
|
messageHeader: newHeader(msg),
|
2020-11-06 06:14:52 +00:00
|
|
|
content: content,
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 00:00:03 +00:00
|
|
|
func NewMessageUpdateAuthor(
|
2020-09-08 04:44:09 +00:00
|
|
|
msg discord.Message, member discord.Member, g discord.Guild, s *state.Instance) Message {
|
2020-07-30 00:00:03 +00:00
|
|
|
|
2020-12-31 02:58:36 +00:00
|
|
|
author := NewGuildMember(member, g, s)
|
|
|
|
if ref := ReferencedMessage(msg, s, true); ref != nil {
|
|
|
|
author.AddMessageReference(*ref, s)
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
return Message{
|
|
|
|
messageHeader: newHeader(msg),
|
2020-07-30 00:00:03 +00:00
|
|
|
author: NewGuildMember(member, g, s),
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-19 05:46:12 +00:00
|
|
|
// NewGuildMessageCreate uses the session to create a message. It does not do
|
|
|
|
// API calls. Member is optional. This is the only call that populates the Nonce
|
|
|
|
// in the header.
|
|
|
|
func NewGuildMessageCreate(c *gateway.MessageCreateEvent, s *state.Instance) Message {
|
|
|
|
// Copy and change the nonce.
|
|
|
|
message := c.Message
|
|
|
|
message.Nonce = s.Nonces.Load(c.Nonce)
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
// This should not error.
|
2020-12-20 05:44:26 +00:00
|
|
|
g, err := s.Cabinet.Guild(c.GuildID)
|
2020-06-16 03:57:33 +00:00
|
|
|
if err != nil {
|
2020-12-19 05:46:12 +00:00
|
|
|
return NewMessage(message, s, NewUser(c.Author, s))
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 07:20:46 +00:00
|
|
|
if c.Member == nil {
|
2020-12-20 05:44:26 +00:00
|
|
|
c.Member, _ = s.Cabinet.Member(c.GuildID, c.Author.ID)
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
2020-06-17 07:20:46 +00:00
|
|
|
if c.Member == nil {
|
2020-07-15 01:55:46 +00:00
|
|
|
s.MemberState.RequestMember(c.GuildID, c.Author.ID)
|
2020-12-19 05:46:12 +00:00
|
|
|
return NewMessage(message, s, NewUser(c.Author, s))
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 05:46:12 +00:00
|
|
|
return NewMessage(message, s, NewGuildMember(*c.Member, *g, s))
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBacklogMessage uses the session to create a message fetched from the
|
|
|
|
// backlog. It takes in an existing guild and tries to fetch a new member, if
|
|
|
|
// it's nil.
|
2020-09-08 04:44:09 +00:00
|
|
|
func NewBacklogMessage(m discord.Message, s *state.Instance, g discord.Guild) Message {
|
2020-06-16 03:57:33 +00:00
|
|
|
// If the message doesn't have a guild, then we don't need all the
|
|
|
|
// complicated member fetching process.
|
2020-08-15 22:37:44 +00:00
|
|
|
if !m.GuildID.IsValid() {
|
2020-07-30 00:00:03 +00:00
|
|
|
return NewMessage(m, s, NewUser(m.Author, s))
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 05:44:26 +00:00
|
|
|
mem, err := s.Cabinet.Member(m.GuildID, m.Author.ID)
|
2020-06-16 03:57:33 +00:00
|
|
|
if err != nil {
|
2020-07-15 01:55:46 +00:00
|
|
|
s.MemberState.RequestMember(m.GuildID, m.Author.ID)
|
2020-07-30 00:00:03 +00:00
|
|
|
return NewMessage(m, s, NewUser(m.Author, s))
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 00:00:03 +00:00
|
|
|
return NewMessage(m, s, NewGuildMember(*mem, g, s))
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 04:44:09 +00:00
|
|
|
func NewDirectMessage(m discord.Message, s *state.Instance) Message {
|
2020-07-30 00:00:03 +00:00
|
|
|
return NewMessage(m, s, NewUser(m.Author, s))
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 04:44:09 +00:00
|
|
|
func NewMessage(m discord.Message, s *state.Instance, author Author) Message {
|
2020-12-31 02:58:36 +00:00
|
|
|
var content text.Rich
|
|
|
|
|
|
|
|
if ref := ReferencedMessage(m, s, true); ref != nil {
|
|
|
|
// TODO: markup support
|
|
|
|
var refmsg = "> " + ref.Content
|
|
|
|
if len(refmsg) > 120 {
|
|
|
|
refmsg = refmsg[:120] + "..."
|
|
|
|
}
|
|
|
|
|
|
|
|
content.Content = refmsg + "\n"
|
|
|
|
content.Segments = []text.Segment{
|
|
|
|
reference.NewMessageSegment(0, len(refmsg), ref.ID),
|
|
|
|
}
|
|
|
|
|
|
|
|
author.AddMessageReference(*ref, s)
|
|
|
|
}
|
|
|
|
|
2020-07-30 00:00:03 +00:00
|
|
|
// Render the message content.
|
2020-12-31 02:58:36 +00:00
|
|
|
segments.ParseMessageRich(&content, &m, s.Cabinet)
|
2020-07-30 00:00:03 +00:00
|
|
|
|
|
|
|
// Request members in mentions if we're in a guild.
|
2020-08-15 22:37:44 +00:00
|
|
|
if m.GuildID.IsValid() {
|
2020-07-30 00:00:03 +00:00
|
|
|
for _, segment := range content.Segments {
|
2020-10-07 01:53:15 +00:00
|
|
|
if mention, ok := segment.(*mention.Segment); ok {
|
2020-07-30 00:00:03 +00:00
|
|
|
// If this is not a user mention, then skip.
|
2020-10-07 01:53:15 +00:00
|
|
|
if mention.User == nil {
|
2020-07-30 00:00:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we already have a member, then skip. We could check this
|
|
|
|
// using the timestamp, as we might have a user set into the
|
|
|
|
// member field
|
2020-10-07 01:53:15 +00:00
|
|
|
if mention.User.Member.Joined.IsValid() {
|
2020-07-30 00:00:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request the member.
|
2020-10-07 01:53:15 +00:00
|
|
|
s.MemberState.RequestMember(m.GuildID, mention.User.Member.User.ID)
|
2020-07-30 00:00:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
return Message{
|
2020-12-19 05:46:12 +00:00
|
|
|
messageHeader: newHeaderNonce(m, m.Nonce),
|
2020-06-16 03:57:33 +00:00
|
|
|
author: author,
|
2020-07-30 00:00:03 +00:00
|
|
|
content: content,
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 04:44:09 +00:00
|
|
|
func (m Message) Author() cchat.Author {
|
2020-08-15 22:37:44 +00:00
|
|
|
if !m.author.id.IsValid() {
|
2020-07-06 00:18:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-06-16 03:57:33 +00:00
|
|
|
return m.author
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Message) Content() text.Rich {
|
|
|
|
return m.content
|
|
|
|
}
|
|
|
|
|
2020-12-19 05:46:12 +00:00
|
|
|
func (m Message) Nonce() string {
|
|
|
|
return m.nonce
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
func (m Message) Mentioned() bool {
|
|
|
|
return m.mentioned
|
|
|
|
}
|
2020-12-31 02:58:36 +00:00
|
|
|
|
|
|
|
// ReferencedMessage searches for the referenced message if needed.
|
|
|
|
func ReferencedMessage(m discord.Message, s *state.Instance, wait bool) (reply *discord.Message) {
|
|
|
|
if m.ReferencedMessage != nil {
|
|
|
|
return m.ReferencedMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deleted or does not exist.
|
|
|
|
if m.Reference == nil || !m.Reference.MessageID.IsValid() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !wait {
|
|
|
|
reply, _ = s.Cabinet.Message(m.Reference.ChannelID, m.Reference.MessageID)
|
|
|
|
} else {
|
|
|
|
reply, _ = s.Message(m.Reference.ChannelID, m.Reference.MessageID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|