Preserve threading for messages with files (slack) (#1781)

* Preserve threading for slack messages with files

* Update bridge/slack/slack.go

Co-authored-by: Wim <wim@42.be>
This commit is contained in:
Alexander 2022-03-30 23:22:37 +03:00 committed by GitHub
parent 6fe0cff342
commit d7b7ff7bb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -321,7 +321,7 @@ func (b *Bslack) sendRTM(msg config.Message) (string, error) {
} }
// Upload a file if it exists. // Upload a file if it exists.
if msg.Extra != nil { if len(msg.Extra) > 0 {
extraMsgs := helper.HandleExtra(&msg, b.General) extraMsgs := helper.HandleExtra(&msg, b.General)
for i := range extraMsgs { for i := range extraMsgs {
rmsg := &extraMsgs[i] rmsg := &extraMsgs[i]
@ -332,7 +332,7 @@ func (b *Bslack) sendRTM(msg config.Message) (string, error) {
} }
} }
// Upload files if necessary (from Slack, Telegram or Mattermost). // Upload files if necessary (from Slack, Telegram or Mattermost).
b.uploadFile(&msg, channelInfo.ID) return b.uploadFile(&msg, channelInfo.ID)
} }
// Post message. // Post message.
@ -443,7 +443,8 @@ func (b *Bslack) postMessage(msg *config.Message, channelInfo *slack.Channel) (s
} }
// uploadFile handles native upload of files // uploadFile handles native upload of files
func (b *Bslack) uploadFile(msg *config.Message, channelID string) { func (b *Bslack) uploadFile(msg *config.Message, channelID string) (string, error) {
var messageID string
for _, f := range msg.Extra["file"] { for _, f := range msg.Extra["file"] {
fi, ok := f.(config.FileInfo) fi, ok := f.(config.FileInfo)
if !ok { if !ok {
@ -471,13 +472,22 @@ func (b *Bslack) uploadFile(msg *config.Message, channelID string) {
}) })
if err != nil { if err != nil {
b.Log.Errorf("uploadfile %#v", err) b.Log.Errorf("uploadfile %#v", err)
return return "", err
} }
if res.ID != "" { if res.ID != "" {
b.Log.Debugf("Adding file ID %s to cache with timestamp %s", res.ID, ts.String()) b.Log.Debugf("Adding file ID %s to cache with timestamp %s", res.ID, ts.String())
b.cache.Add("file"+res.ID, ts) b.cache.Add("file"+res.ID, ts)
// search for message id by uploaded file in private/public channels, get thread timestamp from uploaded file
if v, ok := res.Shares.Private[channelID]; ok && len(v) > 0 {
messageID = v[0].Ts
}
if v, ok := res.Shares.Public[channelID]; ok && len(v) > 0 {
messageID = v[0].Ts
}
} }
} }
return messageID, nil
} }
func (b *Bslack) prepareMessageOptions(msg *config.Message) []slack.MsgOption { func (b *Bslack) prepareMessageOptions(msg *config.Message) []slack.MsgOption {