summaryrefslogtreecommitdiffstats
path: root/bridge/discord/discord.go
diff options
context:
space:
mode:
Diffstat (limited to 'bridge/discord/discord.go')
-rw-r--r--bridge/discord/discord.go41
1 files changed, 37 insertions, 4 deletions
diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go
index 0ac2b50c..9b9065f8 100644
--- a/bridge/discord/discord.go
+++ b/bridge/discord/discord.go
@@ -10,10 +10,14 @@ import (
"github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/discord/transmitter"
"github.com/42wim/matterbridge/bridge/helper"
+ lru "github.com/hashicorp/golang-lru"
"github.com/matterbridge/discordgo"
)
-const MessageLength = 1950
+const (
+ MessageLength = 1950
+ cFileUpload = "file_upload"
+)
type Bdiscord struct {
*bridge.Config
@@ -35,10 +39,20 @@ type Bdiscord struct {
// Webhook specific logic
useAutoWebhooks bool
transmitter *transmitter.Transmitter
+ cache *lru.Cache
}
func New(cfg *bridge.Config) bridge.Bridger {
- b := &Bdiscord{Config: cfg}
+ newCache, err := lru.New(5000)
+ if err != nil {
+ cfg.Log.Fatalf("Could not create LRU cache: %v", err)
+ }
+
+ b := &Bdiscord{
+ Config: cfg,
+ cache: newCache,
+ }
+
b.userMemberMap = make(map[string]*discordgo.Member)
b.nickMemberMap = make(map[string]*discordgo.Member)
b.channelInfoMap = make(map[string]*config.ChannelInfo)
@@ -280,6 +294,21 @@ func (b *Bdiscord) handleEventBotUser(msg *config.Message, channelID string) (st
return "", err
}
+ // Delete a file
+ if msg.Event == config.EventFileDelete {
+ if msg.ID == "" {
+ return "", nil
+ }
+
+ if fi, ok := b.cache.Get(cFileUpload + msg.ID); ok {
+ err := b.c.ChannelMessageDelete(channelID, fi.(string)) // nolint:forcetypeassert
+ b.cache.Remove(cFileUpload + msg.ID)
+ return "", err
+ }
+
+ return "", fmt.Errorf("file %s not found", msg.ID)
+ }
+
// Upload a file if it exists
if msg.Extra != nil {
for _, rmsg := range helper.HandleExtra(msg, b.General) {
@@ -327,7 +356,6 @@ func (b *Bdiscord) handleEventBotUser(msg *config.Message, channelID string) (st
// handleUploadFile handles native upload of files
func (b *Bdiscord) handleUploadFile(msg *config.Message, channelID string) (string, error) {
- var err error
for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo)
file := discordgo.File{
@@ -340,10 +368,15 @@ func (b *Bdiscord) handleUploadFile(msg *config.Message, channelID string) (stri
Files: []*discordgo.File{&file},
AllowedMentions: b.getAllowedMentions(),
}
- _, err = b.c.ChannelMessageSendComplex(channelID, &m)
+ res, err := b.c.ChannelMessageSendComplex(channelID, &m)
if err != nil {
return "", fmt.Errorf("file upload failed: %s", err)
}
+
+ // link file_upload_nativeID (file ID from the original bridge) to our upload id
+ // so that we can remove this later when it eg needs to be deleted
+ b.cache.Add(cFileUpload+fi.NativeID, res.ID)
}
+
return "", nil
}