Add support for deleting messages from discord
This commit is contained in:
@@ -32,8 +32,6 @@ type Portal struct {
|
||||
bridge *Bridge
|
||||
log log.Logger
|
||||
|
||||
channel *discordgo.Channel
|
||||
|
||||
roomCreateLock sync.Mutex
|
||||
|
||||
discordMessages chan portalDiscordMessage
|
||||
@@ -159,39 +157,22 @@ func (p *Portal) messageLoop() {
|
||||
}
|
||||
|
||||
func (p *Portal) IsPrivateChat() bool {
|
||||
if p.channel != nil {
|
||||
return p.channel.Type == discordgo.ChannelTypeDM
|
||||
}
|
||||
|
||||
return false
|
||||
return p.Type == discordgo.ChannelTypeDM
|
||||
}
|
||||
|
||||
func (p *Portal) MainIntent() *appservice.IntentAPI {
|
||||
if p.IsPrivateChat() && p.channel != nil && len(p.channel.Recipients) == 1 {
|
||||
return p.bridge.GetPuppetByID(p.channel.Recipients[0].ID).DefaultIntent()
|
||||
if p.IsPrivateChat() && p.DMUser != "" {
|
||||
return p.bridge.GetPuppetByID(p.DMUser).DefaultIntent()
|
||||
}
|
||||
|
||||
return p.bridge.bot
|
||||
}
|
||||
|
||||
func (p *Portal) getMessagePuppet(user *User, message *discordgo.Message) *Puppet {
|
||||
p.log.Debugf("getMessagePuppet")
|
||||
if message.Author.ID == user.ID {
|
||||
return p.bridge.GetPuppetByID(user.ID)
|
||||
}
|
||||
|
||||
puppet := p.bridge.GetPuppetByID(message.Author.ID)
|
||||
puppet.SyncContact(user)
|
||||
|
||||
return puppet
|
||||
}
|
||||
|
||||
func (p *Portal) getMessageIntent(user *User, message *discordgo.Message) *appservice.IntentAPI {
|
||||
return p.getMessagePuppet(user, message).IntentFor(p)
|
||||
}
|
||||
|
||||
func (p *Portal) createMatrixRoom(user *User, channel *discordgo.Channel) error {
|
||||
p.channel = channel
|
||||
p.Type = channel.Type
|
||||
if p.Type == discordgo.ChannelTypeDM {
|
||||
p.DMUser = channel.Recipients[0].ID
|
||||
}
|
||||
|
||||
p.roomCreateLock.Lock()
|
||||
defer p.roomCreateLock.Unlock()
|
||||
@@ -288,7 +269,9 @@ func (p *Portal) handleDiscordMessages(msg portalDiscordMessage) {
|
||||
|
||||
switch msg.msg.(type) {
|
||||
case *discordgo.MessageCreate:
|
||||
p.handleDiscordMessage(msg.user, msg.msg.(*discordgo.MessageCreate).Message)
|
||||
p.handleDiscordMessageCreate(msg.user, msg.msg.(*discordgo.MessageCreate).Message)
|
||||
case *discordgo.MessageDelete:
|
||||
p.handleDiscordMessageDelete(msg.user, msg.msg.(*discordgo.MessageDelete).Message)
|
||||
case *discordgo.MessageReactionAdd:
|
||||
p.handleDiscordReaction(msg.user, msg.msg.(*discordgo.MessageReactionAdd).MessageReaction, true)
|
||||
case *discordgo.MessageReactionRemove:
|
||||
@@ -318,7 +301,7 @@ func (p *Portal) markMessageHandled(msg *database.Message, discordID string, mxi
|
||||
return msg
|
||||
}
|
||||
|
||||
func (p *Portal) handleDiscordMessage(user *User, msg *discordgo.Message) {
|
||||
func (p *Portal) handleDiscordMessageCreate(user *User, msg *discordgo.Message) {
|
||||
if user.ID == msg.Author.ID {
|
||||
return
|
||||
}
|
||||
@@ -353,6 +336,37 @@ func (p *Portal) handleDiscordMessage(user *User, msg *discordgo.Message) {
|
||||
p.markMessageHandled(nil, msg.ID, resp.EventID, msg.Author.ID, ts)
|
||||
}
|
||||
|
||||
func (p *Portal) handleDiscordMessageDelete(user *User, msg *discordgo.Message) {
|
||||
// The discord delete message object is pretty empty and doesn't include
|
||||
// the author so we have to use the DMUser from the portal that was added
|
||||
// at creation time if we're a DM. We'll might have similar issues when we
|
||||
// add guild message support, but we'll cross that bridge when we get
|
||||
// there.
|
||||
|
||||
// Find the message that we're working with.
|
||||
existing := p.bridge.db.Message.GetByDiscordID(p.Key, msg.ID)
|
||||
if existing == nil {
|
||||
p.log.Debugfln("failed to find message", msg.ID)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
var intent *appservice.IntentAPI
|
||||
|
||||
if p.Type == discordgo.ChannelTypeDM {
|
||||
intent = p.bridge.GetPuppetByID(p.DMUser).IntentFor(p)
|
||||
} else {
|
||||
p.log.Errorfln("no guilds yet...")
|
||||
}
|
||||
|
||||
_, err := intent.RedactEvent(p.MXID, existing.MatrixID)
|
||||
if err != nil {
|
||||
p.log.Warnfln("Failed to remove message %s: %v", existing.MatrixID, err)
|
||||
}
|
||||
|
||||
existing.Delete()
|
||||
}
|
||||
|
||||
func (p *Portal) syncParticipants(source *User, participants []*discordgo.User) {
|
||||
for _, participant := range participants {
|
||||
puppet := p.bridge.GetPuppetByID(participant.ID)
|
||||
|
||||
@@ -212,7 +212,8 @@ func (u *User) Connect() error {
|
||||
u.User.Session.AddHandler(u.channelPinsUpdateHandler)
|
||||
u.User.Session.AddHandler(u.channelUpdateHandler)
|
||||
|
||||
u.User.Session.AddHandler(u.messageHandler)
|
||||
u.User.Session.AddHandler(u.messageCreateHandler)
|
||||
u.User.Session.AddHandler(u.messageDeleteHandler)
|
||||
u.User.Session.AddHandler(u.reactionAddHandler)
|
||||
u.User.Session.AddHandler(u.reactionRemoveHandler)
|
||||
|
||||
@@ -249,6 +250,11 @@ func (u *User) channelCreateHandler(s *discordgo.Session, c *discordgo.ChannelCr
|
||||
|
||||
portal.Name = c.Name
|
||||
portal.Topic = c.Topic
|
||||
portal.Type = c.Type
|
||||
|
||||
if portal.Type == discordgo.ChannelTypeDM {
|
||||
portal.DMUser = c.Recipients[0].ID
|
||||
}
|
||||
|
||||
if c.Icon != "" {
|
||||
u.log.Debugln("channel icon", c.Icon)
|
||||
@@ -279,14 +285,32 @@ func (u *User) channelUpdateHandler(s *discordgo.Session, c *discordgo.ChannelUp
|
||||
u.log.Debugln("channel update")
|
||||
}
|
||||
|
||||
func (u *User) messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
func (u *User) messageCreateHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
if m.GuildID != "" {
|
||||
u.log.Debugln("ignoring message for guild")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
key := database.NewPortalKey(m.ChannelID, u.User.ID)
|
||||
key := database.NewPortalKey(m.ChannelID, u.ID)
|
||||
portal := u.bridge.GetPortalByID(key)
|
||||
|
||||
msg := portalDiscordMessage{
|
||||
msg: m,
|
||||
user: u,
|
||||
}
|
||||
|
||||
portal.discordMessages <- msg
|
||||
}
|
||||
|
||||
func (u *User) messageDeleteHandler(s *discordgo.Session, m *discordgo.MessageDelete) {
|
||||
if m.GuildID != "" {
|
||||
u.log.Debugln("ignoring message delete for guild message")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
key := database.NewPortalKey(m.ChannelID, u.ID)
|
||||
portal := u.bridge.GetPortalByID(key)
|
||||
|
||||
msg := portalDiscordMessage{
|
||||
|
||||
Reference in New Issue
Block a user