summaryrefslogtreecommitdiffstats
path: root/bridge/discord/helpers.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2020-04-21 23:35:46 +0200
committerGitHub <noreply@github.com>2020-04-21 23:35:46 +0200
commiteaf92fca4ddf6bf78d2092618a4dc93b78af7140 (patch)
tree7c3c2f90b027117492aef33bcabeb744d51aef99 /bridge/discord/helpers.go
parent06b7bad7146607f46652c9bd766de3f95028c370 (diff)
downloadmatterbridge-msglm-eaf92fca4ddf6bf78d2092618a4dc93b78af7140.tar.gz
matterbridge-msglm-eaf92fca4ddf6bf78d2092618a4dc93b78af7140.tar.bz2
matterbridge-msglm-eaf92fca4ddf6bf78d2092618a4dc93b78af7140.zip
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.
Diffstat (limited to 'bridge/discord/helpers.go')
-rw-r--r--bridge/discord/helpers.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/bridge/discord/helpers.go b/bridge/discord/helpers.go
index 9ef234c5..00592f3a 100644
--- a/bridge/discord/helpers.go
+++ b/bridge/discord/helpers.go
@@ -208,6 +208,40 @@ 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 {