Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 0554f1a61ad9baeaf9fb0e885d10e5d2585f9501 Author: Wim <wim@42.be> Date: Tue Apr 21 23:35:46 2020 +0000 Add an ID cache (discord). Fixes #1106 (#1111) When a webhook "edits" a message, it does this by deleting the message and creating a new one with the new content. On creation of this new message, we'll get another ID then already is know by the gateway in its id cache. So we add it in our own cache and replace it whenever we want to edit/delete it again. diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index a59735a..614512f 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -346 +348 @@ type Bdiscord struct {   membersMutex sync.RWMutex   userMemberMap map[string]*discordgo.Member   nickMemberMap map[string]*discordgo.Member + webhookCache map[string]string + webhookMutex sync.RWMutex  }    func New(cfg *bridge.Config) bridge.Bridger { @@ -416 +437 @@ func New(cfg *bridge.Config) bridge.Bridger {   b.userMemberMap = make(map[string]*discordgo.Member)   b.nickMemberMap = make(map[string]*discordgo.Member)   b.channelInfoMap = make(map[string]*config.ChannelInfo) + b.webhookCache = make(map[string]string)   if b.GetString("WebhookURL") != "" {   b.Log.Debug("Configuring Discord Incoming Webhook")   b.webhookID, b.webhookToken = b.splitURL(b.GetString("WebhookURL")) @@ -1886 +1918 @@ func (b *Bdiscord) JoinChannel(channel config.ChannelInfo) error {  func (b *Bdiscord) Send(msg config.Message) (string, error) {   b.Log.Debugf("=> Receiving %#v", msg)   + origMsgID := msg.ID +   channelID := b.getChannelID(msg.Channel)   if channelID == "" {   return "", fmt.Errorf("Could not find channelID for %v", msg.Channel) @@ -2306 +2357 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {     // If we are editing a message, delete the old message   if msg.ID != "" { + msg.ID = b.getCacheID(msg.ID)   b.Log.Debugf("Deleting edited webhook message")   err := b.c.ChannelMessageDelete(channelID, msg.ID)   if err != nil { @@ -2736 +2798 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {   if msg == nil {   return "", nil   } + + b.updateCacheID(origMsgID, msg.ID)   return msg.ID, nil   }   @@ -2836 +2917 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {   if msg.ID == "" {   return "", nil   } + msg.ID = b.getCacheID(msg.ID)   err := b.c.ChannelMessageDelete(channelID, msg.ID)   return "", err   } diff --git a/bridge/discord/helpers.go b/bridge/discord/helpers.go index 9ef234c..00592f3 100644 --- a/bridge/discord/helpers.go +++ b/bridge/discord/helpers.go @@ -2086 +20840 @@ func (b *Bdiscord) splitURL(url string) (string, string) {   return webhookURLSplit[webhookIdxID], webhookURLSplit[webhookIdxToken]  }   +// getcacheID tries to find a corresponding msgID in the webhook cache. +// if not found returns the original request. +func (b *Bdiscord) getCacheID(msgID string) string { + b.webhookMutex.RLock() + defer b.webhookMutex.RUnlock() + for k, v := range b.webhookCache { + if msgID == k { + return v + } + } + return msgID +} + +// updateCacheID updates the cache so that the newID takes the place of +// the original ID. This is used for edit/deletes in combination with webhooks +// as editing a message via webhook means deleting the message and creating a +// new message (with a new ID). This ID needs to be set instead of the original ID +func (b *Bdiscord) updateCacheID(origID, newID string) { + b.webhookMutex.Lock() + match := false + for k, v := range b.webhookCache { + if v == origID { + delete(b.webhookCache, k) + b.webhookCache[origID] = newID + match = true + continue + } + } + if !match && origID != "" { + b.webhookCache[origID] = newID + } + b.webhookMutex.Unlock() +} +  func enumerateUsernames(s string) []string {   onlySpace := true   for _, r := range s {