diff options
author | Wim <wim@42.be> | 2022-02-05 14:45:54 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-05 14:45:54 +0100 |
commit | 6438a3dba3c4cb241f1e2633ae6b23efd113d684 (patch) | |
tree | d13fe894fc1a20b19c2fe3fdb16b6b24e74bc721 /bridge/slack | |
parent | 4b226a6a638b7572c0fcbf8964bfb02e675b2e6b (diff) | |
download | matterbridge-msglm-6438a3dba3c4cb241f1e2633ae6b23efd113d684.tar.gz matterbridge-msglm-6438a3dba3c4cb241f1e2633ae6b23efd113d684.tar.bz2 matterbridge-msglm-6438a3dba3c4cb241f1e2633ae6b23efd113d684.zip |
Add support for deleting files from slack to discord. Fixes #1705 (#1709)
We create a new event EventFileDelete which will be used to delete
specific uploaded files using the Extra["file"] in the config.Message.
We also add a new NativeID key to the FileInfo struct which will contain
the native file ID of the sending bridge.
When a new file is added to the config.Message.Extra["file"] map, now
the bridge native file ID should be added here.
When the receiving bridge receives such a message, it should keep an
internal mapping of NativeID <> bridge fileid/message id. In the case of
discord we map it to the resulted discord message ID after uploading it.
Now when a bridge deletes a file, it should send a EventFileDelete and
setting the ID to the native file ID of the bridge.
When the receiving bridge will get this event it'll look into the
NativeID <> bridge id mapping to find their internal ID and use it to
delete the specific file on their side.
For now this is implemented for slack to discord but this will be add to
other bridges where useful.
Diffstat (limited to 'bridge/slack')
-rw-r--r-- | bridge/slack/handlers.go | 34 | ||||
-rw-r--r-- | bridge/slack/slack.go | 37 |
2 files changed, 51 insertions, 20 deletions
diff --git a/bridge/slack/handlers.go b/bridge/slack/handlers.go index 4dc57d0d..c469b9a5 100644 --- a/bridge/slack/handlers.go +++ b/bridge/slack/handlers.go @@ -27,7 +27,8 @@ func (b *Bslack) handleSlack() { b.Log.Debug("Start listening for Slack messages") for message := range messages { // don't do any action on deleted/typing messages - if message.Event != config.EventUserTyping && message.Event != config.EventMsgDelete { + if message.Event != config.EventUserTyping && message.Event != config.EventMsgDelete && + message.Event != config.EventFileDelete { b.Log.Debugf("<= Sending message from %s on %s to gateway", message.Username, b.Account) // cleanup the message message.Text = b.replaceMention(message.Text) @@ -76,6 +77,13 @@ func (b *Bslack) handleSlackClient(messages chan *config.Message) { continue } messages <- rmsg + case *slack.FileDeletedEvent: + rmsg, err := b.handleFileDeletedEvent(ev) + if err != nil { + b.Log.Errorf("%#v", err) + continue + } + messages <- rmsg case *slack.OutgoingErrorEvent: b.Log.Debugf("%#v", ev.Error()) case *slack.ChannelJoinedEvent: @@ -222,6 +230,26 @@ func (b *Bslack) handleMessageEvent(ev *slack.MessageEvent) (*config.Message, er return rmsg, nil } +func (b *Bslack) handleFileDeletedEvent(ev *slack.FileDeletedEvent) (*config.Message, error) { + if rawChannel, ok := b.cache.Get(cfileDownloadChannel + ev.FileID); ok { + channel, err := b.channels.getChannelByID(rawChannel.(string)) + if err != nil { + return nil, err + } + + return &config.Message{ + Event: config.EventFileDelete, + Text: config.EventFileDelete, + Channel: channel.Name, + Account: b.Account, + ID: ev.FileID, + Protocol: b.Protocol, + }, nil + } + + return nil, fmt.Errorf("channel ID for file ID %s not found", ev.FileID) +} + func (b *Bslack) handleStatusEvent(ev *slack.MessageEvent, rmsg *config.Message) bool { switch ev.SubType { case sChannelJoined, sMemberJoined: @@ -281,6 +309,8 @@ func (b *Bslack) handleAttachments(ev *slack.MessageEvent, rmsg *config.Message) // If we have files attached, download them (in memory) and put a pointer to it in msg.Extra. for i := range ev.Files { + // keep reference in cache on which channel we added this file + b.cache.Add(cfileDownloadChannel+ev.Files[i].ID, ev.Channel) if err := b.handleDownloadFile(rmsg, &ev.Files[i], false); err != nil { b.Log.Errorf("Could not download incoming file: %#v", err) } @@ -330,7 +360,7 @@ func (b *Bslack) handleDownloadFile(rmsg *config.Message, file *slack.File, retr // that the comment is not duplicated. comment := rmsg.Text rmsg.Text = "" - helper.HandleDownloadData(b.Log, rmsg, file.Name, comment, file.URLPrivateDownload, data, b.General) + helper.HandleDownloadData2(b.Log, rmsg, file.Name, file.ID, comment, file.URLPrivateDownload, data, b.General) return nil } diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index 5db0c33e..55231c38 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -36,24 +36,25 @@ type Bslack struct { } const ( - sHello = "hello" - sChannelJoin = "channel_join" - sChannelLeave = "channel_leave" - sChannelJoined = "channel_joined" - sMemberJoined = "member_joined_channel" - sMessageChanged = "message_changed" - sMessageDeleted = "message_deleted" - sSlackAttachment = "slack_attachment" - sPinnedItem = "pinned_item" - sUnpinnedItem = "unpinned_item" - sChannelTopic = "channel_topic" - sChannelPurpose = "channel_purpose" - sFileComment = "file_comment" - sMeMessage = "me_message" - sUserTyping = "user_typing" - sLatencyReport = "latency_report" - sSystemUser = "system" - sSlackBotUser = "slackbot" + sHello = "hello" + sChannelJoin = "channel_join" + sChannelLeave = "channel_leave" + sChannelJoined = "channel_joined" + sMemberJoined = "member_joined_channel" + sMessageChanged = "message_changed" + sMessageDeleted = "message_deleted" + sSlackAttachment = "slack_attachment" + sPinnedItem = "pinned_item" + sUnpinnedItem = "unpinned_item" + sChannelTopic = "channel_topic" + sChannelPurpose = "channel_purpose" + sFileComment = "file_comment" + sMeMessage = "me_message" + sUserTyping = "user_typing" + sLatencyReport = "latency_report" + sSystemUser = "system" + sSlackBotUser = "slackbot" + cfileDownloadChannel = "file_download_channel" tokenConfig = "Token" incomingWebhookConfig = "WebhookBindAddress" |