2018-10-12 14:16:34 -07:00
|
|
|
package bslack
|
|
|
|
|
|
|
|
import (
|
2020-03-22 10:39:11 -07:00
|
|
|
"errors"
|
2018-10-12 14:16:34 -07:00
|
|
|
"fmt"
|
|
|
|
"html"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2020-03-01 11:59:19 -08:00
|
|
|
"github.com/slack-go/slack"
|
2018-10-12 14:16:34 -07:00
|
|
|
)
|
|
|
|
|
2020-03-22 10:39:11 -07:00
|
|
|
// ErrEventIgnored is for events that should be ignored
|
|
|
|
var ErrEventIgnored = errors.New("this event message should ignored")
|
|
|
|
|
2018-10-12 14:16:34 -07:00
|
|
|
func (b *Bslack) handleSlack() {
|
|
|
|
messages := make(chan *config.Message)
|
2020-05-13 15:27:34 -07:00
|
|
|
if b.GetString(incomingWebhookConfig) != "" && b.GetString(tokenConfig) == "" {
|
2018-10-12 14:16:34 -07:00
|
|
|
b.Log.Debugf("Choosing webhooks based receiving")
|
|
|
|
go b.handleMatterHook(messages)
|
|
|
|
} else {
|
|
|
|
b.Log.Debugf("Choosing token based receiving")
|
|
|
|
go b.handleSlackClient(messages)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
b.Log.Debug("Start listening for Slack messages")
|
|
|
|
for message := range messages {
|
2019-04-23 14:29:15 -07:00
|
|
|
// don't do any action on deleted/typing messages
|
2022-02-05 05:45:54 -08:00
|
|
|
if message.Event != config.EventUserTyping && message.Event != config.EventMsgDelete &&
|
|
|
|
message.Event != config.EventFileDelete {
|
2018-11-08 11:45:40 -08:00
|
|
|
b.Log.Debugf("<= Sending message from %s on %s to gateway", message.Username, b.Account)
|
2019-04-23 14:29:15 -07:00
|
|
|
// cleanup the message
|
|
|
|
message.Text = b.replaceMention(message.Text)
|
|
|
|
message.Text = b.replaceVariable(message.Text)
|
|
|
|
message.Text = b.replaceChannel(message.Text)
|
|
|
|
message.Text = b.replaceURL(message.Text)
|
2019-10-26 16:10:59 -07:00
|
|
|
message.Text = b.replaceb0rkedMarkDown(message.Text)
|
2019-04-23 14:29:15 -07:00
|
|
|
message.Text = html.UnescapeString(message.Text)
|
2018-10-12 14:16:34 -07:00
|
|
|
|
2019-04-23 14:29:15 -07:00
|
|
|
// Add the avatar
|
|
|
|
message.Avatar = b.users.getAvatar(message.UserID)
|
|
|
|
}
|
2018-10-12 14:16:34 -07:00
|
|
|
|
|
|
|
b.Log.Debugf("<= Message is %#v", message)
|
|
|
|
b.Remote <- *message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bslack) handleSlackClient(messages chan *config.Message) {
|
|
|
|
for msg := range b.rtm.IncomingEvents {
|
2019-10-03 15:19:50 -07:00
|
|
|
if msg.Type != sUserTyping && msg.Type != sHello && msg.Type != sLatencyReport {
|
2018-10-12 14:16:34 -07:00
|
|
|
b.Log.Debugf("== Receiving event %#v", msg.Data)
|
|
|
|
}
|
|
|
|
switch ev := msg.Data.(type) {
|
2018-11-08 11:45:40 -08:00
|
|
|
case *slack.UserTypingEvent:
|
2018-11-08 12:52:10 -08:00
|
|
|
if !b.GetBool("ShowUserTyping") {
|
|
|
|
continue
|
|
|
|
}
|
2018-11-08 11:45:40 -08:00
|
|
|
rmsg, err := b.handleTypingEvent(ev)
|
2020-03-22 10:39:11 -07:00
|
|
|
if err == ErrEventIgnored {
|
|
|
|
continue
|
|
|
|
} else if err != nil {
|
2018-11-08 11:45:40 -08:00
|
|
|
b.Log.Errorf("%#v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
messages <- rmsg
|
2018-10-12 14:16:34 -07:00
|
|
|
case *slack.MessageEvent:
|
|
|
|
if b.skipMessageEvent(ev) {
|
|
|
|
b.Log.Debugf("Skipped message: %#v", ev)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rmsg, err := b.handleMessageEvent(ev)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("%#v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
messages <- rmsg
|
2022-02-05 05:45:54 -08:00
|
|
|
case *slack.FileDeletedEvent:
|
|
|
|
rmsg, err := b.handleFileDeletedEvent(ev)
|
|
|
|
if err != nil {
|
2022-09-05 10:31:45 -07:00
|
|
|
b.Log.Printf("%#v", err)
|
2022-02-05 05:45:54 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
messages <- rmsg
|
2018-10-12 14:16:34 -07:00
|
|
|
case *slack.OutgoingErrorEvent:
|
|
|
|
b.Log.Debugf("%#v", ev.Error())
|
|
|
|
case *slack.ChannelJoinedEvent:
|
2018-11-07 12:35:59 -08:00
|
|
|
// When we join a channel we update the full list of users as
|
|
|
|
// well as the information for the channel that we joined as this
|
|
|
|
// should now tell that we are a member of it.
|
2019-03-15 13:23:09 -07:00
|
|
|
b.channels.registerChannel(ev.Channel)
|
2018-10-12 14:16:34 -07:00
|
|
|
case *slack.ConnectedEvent:
|
|
|
|
b.si = ev.Info
|
2019-03-15 13:23:09 -07:00
|
|
|
b.channels.populateChannels(true)
|
|
|
|
b.users.populateUsers(true)
|
2018-10-12 14:16:34 -07:00
|
|
|
case *slack.InvalidAuthEvent:
|
|
|
|
b.Log.Fatalf("Invalid Token %#v", ev)
|
|
|
|
case *slack.ConnectionErrorEvent:
|
|
|
|
b.Log.Errorf("Connection failed %#v %#v", ev.Error(), ev.ErrorObj)
|
2018-12-15 13:57:54 -08:00
|
|
|
case *slack.MemberJoinedChannelEvent:
|
2019-03-15 13:23:09 -07:00
|
|
|
b.users.populateUser(ev.User)
|
2020-03-18 15:03:20 -07:00
|
|
|
case *slack.HelloEvent, *slack.LatencyReport, *slack.ConnectingEvent:
|
2019-01-02 14:55:00 -08:00
|
|
|
continue
|
2021-10-16 14:36:30 -07:00
|
|
|
case *slack.UserChangeEvent:
|
|
|
|
b.users.invalidateUser(ev.User.ID)
|
2018-10-12 14:16:34 -07:00
|
|
|
default:
|
2018-12-15 13:57:54 -08:00
|
|
|
b.Log.Debugf("Unhandled incoming event: %T", ev)
|
2018-10-12 14:16:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bslack) handleMatterHook(messages chan *config.Message) {
|
|
|
|
for {
|
|
|
|
message := b.mh.Receive()
|
|
|
|
b.Log.Debugf("receiving from matterhook (slack) %#v", message)
|
|
|
|
if message.UserName == "slackbot" {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-12 16:02:14 -07:00
|
|
|
messages <- &config.Message{
|
|
|
|
Username: message.UserName,
|
|
|
|
Text: message.Text,
|
|
|
|
Channel: message.ChannelName,
|
|
|
|
}
|
2018-10-12 14:16:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:35:59 -08:00
|
|
|
// skipMessageEvent skips event that need to be skipped :-)
|
|
|
|
func (b *Bslack) skipMessageEvent(ev *slack.MessageEvent) bool {
|
|
|
|
switch ev.SubType {
|
|
|
|
case sChannelLeave, sChannelJoin:
|
|
|
|
return b.GetBool(noSendJoinConfig)
|
|
|
|
case sPinnedItem, sUnpinnedItem:
|
|
|
|
return true
|
2018-11-26 01:47:04 -08:00
|
|
|
case sChannelTopic, sChannelPurpose:
|
|
|
|
// Skip the event if our bot/user account changed the topic/purpose
|
|
|
|
if ev.User == b.si.User.ID {
|
|
|
|
return true
|
|
|
|
}
|
2018-10-12 14:16:34 -07:00
|
|
|
}
|
2018-11-07 12:35:59 -08:00
|
|
|
|
2020-03-21 13:03:12 -07:00
|
|
|
// Check for our callback ID
|
|
|
|
hasOurCallbackID := false
|
|
|
|
if len(ev.Blocks.BlockSet) == 1 {
|
|
|
|
block, ok := ev.Blocks.BlockSet[0].(*slack.SectionBlock)
|
|
|
|
hasOurCallbackID = ok && block.BlockID == "matterbridge_"+b.uuid
|
|
|
|
}
|
|
|
|
|
2019-03-27 14:54:18 -07:00
|
|
|
if ev.SubMessage != nil {
|
|
|
|
// It seems ev.SubMessage.Edited == nil when slack unfurls.
|
|
|
|
// Do not forward these messages. See Github issue #266.
|
|
|
|
if ev.SubMessage.ThreadTimestamp != ev.SubMessage.Timestamp &&
|
|
|
|
ev.SubMessage.Edited == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// see hidden subtypes at https://api.slack.com/events/message
|
|
|
|
// these messages are sent when we add a message to a thread #709
|
|
|
|
if ev.SubType == "message_replied" && ev.Hidden {
|
|
|
|
return true
|
|
|
|
}
|
2020-04-18 13:30:49 -07:00
|
|
|
if len(ev.SubMessage.Blocks.BlockSet) == 1 {
|
|
|
|
block, ok := ev.SubMessage.Blocks.BlockSet[0].(*slack.SectionBlock)
|
|
|
|
hasOurCallbackID = ok && block.BlockID == "matterbridge_"+b.uuid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip any messages that we made ourselves or from 'slackbot' (see #527).
|
|
|
|
if ev.Username == sSlackBotUser ||
|
|
|
|
(b.rtm != nil && ev.Username == b.si.User.Name) || hasOurCallbackID {
|
|
|
|
return true
|
2018-10-12 14:16:34 -07:00
|
|
|
}
|
2018-11-15 10:23:46 -08:00
|
|
|
|
|
|
|
if len(ev.Files) > 0 {
|
|
|
|
return b.filesCached(ev.Files)
|
|
|
|
}
|
|
|
|
return false
|
2018-11-14 12:00:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bslack) filesCached(files []slack.File) bool {
|
|
|
|
for i := range files {
|
|
|
|
if !b.fileCached(&files[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2018-10-12 14:16:34 -07:00
|
|
|
}
|
|
|
|
|
2018-11-07 12:35:59 -08:00
|
|
|
// handleMessageEvent handles the message events. Together with any called sub-methods,
|
|
|
|
// this method implements the following event processing pipeline:
|
|
|
|
//
|
|
|
|
// 1. Check if the message should be ignored.
|
|
|
|
// NOTE: This is not actually part of the method below but is done just before it
|
|
|
|
// is called via the 'skipMessageEvent()' method.
|
|
|
|
// 2. Populate the Matterbridge message that will be sent to the router based on the
|
|
|
|
// received event and logic that is common to all events that are not skipped.
|
|
|
|
// 3. Detect and handle any message that is "status" related (think join channel, etc.).
|
|
|
|
// This might result in an early exit from the pipeline and passing of the
|
|
|
|
// pre-populated message to the Matterbridge router.
|
|
|
|
// 4. Handle the specific case of messages that edit existing messages depending on
|
|
|
|
// configuration.
|
|
|
|
// 5. Handle any attachments of the received event.
|
|
|
|
// 6. Check that the Matterbridge message that we end up with after at the end of the
|
|
|
|
// pipeline is valid before sending it to the Matterbridge router.
|
2018-10-12 14:16:34 -07:00
|
|
|
func (b *Bslack) handleMessageEvent(ev *slack.MessageEvent) (*config.Message, error) {
|
2018-11-07 12:35:59 -08:00
|
|
|
rmsg, err := b.populateReceivedMessage(ev)
|
2018-10-12 14:16:34 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:35:59 -08:00
|
|
|
// Handle some message types early.
|
|
|
|
if b.handleStatusEvent(ev, rmsg) {
|
|
|
|
return rmsg, nil
|
2018-10-12 14:16:34 -07:00
|
|
|
}
|
|
|
|
|
2018-11-07 12:35:59 -08:00
|
|
|
b.handleAttachments(ev, rmsg)
|
2018-10-12 14:16:34 -07:00
|
|
|
|
2018-11-07 12:35:59 -08:00
|
|
|
// Verify that we have the right information and the message
|
|
|
|
// is well-formed before sending it out to the router.
|
|
|
|
if len(ev.Files) == 0 && (rmsg.Text == "" || rmsg.Username == "") {
|
|
|
|
if ev.BotID != "" {
|
|
|
|
// This is probably a webhook we couldn't resolve.
|
|
|
|
return nil, fmt.Errorf("message handling resulted in an empty bot message (probably an incoming webhook we couldn't resolve): %#v", ev)
|
2018-10-12 14:16:34 -07:00
|
|
|
}
|
2019-02-15 09:05:10 -08:00
|
|
|
if ev.SubMessage != nil {
|
|
|
|
return nil, fmt.Errorf("message handling resulted in an empty message: %#v with submessage %#v", ev, ev.SubMessage)
|
|
|
|
}
|
2018-11-07 12:35:59 -08:00
|
|
|
return nil, fmt.Errorf("message handling resulted in an empty message: %#v", ev)
|
2018-10-12 14:16:34 -07:00
|
|
|
}
|
2018-11-07 12:35:59 -08:00
|
|
|
return rmsg, nil
|
|
|
|
}
|
2018-10-12 14:16:34 -07:00
|
|
|
|
2022-02-05 05:45:54 -08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:35:59 -08:00
|
|
|
func (b *Bslack) handleStatusEvent(ev *slack.MessageEvent, rmsg *config.Message) bool {
|
|
|
|
switch ev.SubType {
|
|
|
|
case sChannelJoined, sMemberJoined:
|
|
|
|
// There's no further processing needed on channel events
|
|
|
|
// so we return 'true'.
|
|
|
|
return true
|
|
|
|
case sChannelJoin, sChannelLeave:
|
2018-10-12 16:02:14 -07:00
|
|
|
rmsg.Username = sSystemUser
|
2018-11-15 11:43:43 -08:00
|
|
|
rmsg.Event = config.EventJoinLeave
|
2018-11-07 12:35:59 -08:00
|
|
|
case sChannelTopic, sChannelPurpose:
|
2019-03-15 13:23:09 -07:00
|
|
|
b.channels.populateChannels(false)
|
2018-11-15 11:43:43 -08:00
|
|
|
rmsg.Event = config.EventTopicChange
|
2018-11-25 02:08:57 -08:00
|
|
|
case sMessageChanged:
|
|
|
|
rmsg.Text = ev.SubMessage.Text
|
|
|
|
// handle deleted thread starting messages
|
|
|
|
if ev.SubMessage.Text == "This message was deleted." {
|
|
|
|
rmsg.Event = config.EventMsgDelete
|
|
|
|
return true
|
|
|
|
}
|
2018-11-07 12:35:59 -08:00
|
|
|
case sMessageDeleted:
|
2018-11-15 11:43:43 -08:00
|
|
|
rmsg.Text = config.EventMsgDelete
|
|
|
|
rmsg.Event = config.EventMsgDelete
|
2018-11-19 12:28:23 -08:00
|
|
|
rmsg.ID = ev.DeletedTimestamp
|
2018-11-07 12:35:59 -08:00
|
|
|
// If a message is being deleted we do not need to process
|
|
|
|
// the event any further so we return 'true'.
|
|
|
|
return true
|
|
|
|
case sMeMessage:
|
2018-11-15 11:43:43 -08:00
|
|
|
rmsg.Event = config.EventUserAction
|
2018-10-12 14:16:34 -07:00
|
|
|
}
|
2018-11-07 12:35:59 -08:00
|
|
|
return false
|
|
|
|
}
|
2018-10-12 14:16:34 -07:00
|
|
|
|
2022-05-09 13:56:19 -07:00
|
|
|
func getMessageTitle(attach *slack.Attachment) string {
|
|
|
|
if attach.TitleLink != "" {
|
|
|
|
return fmt.Sprintf("[%s](%s)\n", attach.Title, attach.TitleLink)
|
|
|
|
}
|
|
|
|
return attach.Title
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:35:59 -08:00
|
|
|
func (b *Bslack) handleAttachments(ev *slack.MessageEvent, rmsg *config.Message) {
|
|
|
|
// File comments are set by the system (because there is no username given).
|
|
|
|
if ev.SubType == sFileComment {
|
|
|
|
rmsg.Username = sSystemUser
|
2018-10-12 14:16:34 -07:00
|
|
|
}
|
|
|
|
|
2018-11-07 12:35:59 -08:00
|
|
|
// See if we have some text in the attachments.
|
|
|
|
if rmsg.Text == "" {
|
2022-05-09 13:56:19 -07:00
|
|
|
for i, attach := range ev.Attachments {
|
2018-11-07 12:35:59 -08:00
|
|
|
if attach.Text != "" {
|
|
|
|
if attach.Title != "" {
|
2022-05-09 13:56:19 -07:00
|
|
|
rmsg.Text = getMessageTitle(&ev.Attachments[i])
|
2018-11-07 12:35:59 -08:00
|
|
|
}
|
|
|
|
rmsg.Text += attach.Text
|
2022-05-09 13:56:19 -07:00
|
|
|
if attach.Footer != "" {
|
|
|
|
rmsg.Text += "\n\n" + attach.Footer
|
|
|
|
}
|
2018-11-07 12:35:59 -08:00
|
|
|
} else {
|
|
|
|
rmsg.Text = attach.Fallback
|
|
|
|
}
|
2018-10-12 14:16:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:35:59 -08:00
|
|
|
// Save the attachments, so that we can send them to other slack (compatible) bridges.
|
2018-10-12 14:16:34 -07:00
|
|
|
if len(ev.Attachments) > 0 {
|
2018-10-12 16:02:14 -07:00
|
|
|
rmsg.Extra[sSlackAttachment] = append(rmsg.Extra[sSlackAttachment], ev.Attachments)
|
2018-10-12 14:16:34 -07:00
|
|
|
}
|
|
|
|
|
2018-11-07 12:35:59 -08:00
|
|
|
// If we have files attached, download them (in memory) and put a pointer to it in msg.Extra.
|
2018-11-12 06:58:00 -08:00
|
|
|
for i := range ev.Files {
|
2022-02-05 05:45:54 -08:00
|
|
|
// keep reference in cache on which channel we added this file
|
|
|
|
b.cache.Add(cfileDownloadChannel+ev.Files[i].ID, ev.Channel)
|
2018-12-19 13:01:05 -08:00
|
|
|
if err := b.handleDownloadFile(rmsg, &ev.Files[i], false); err != nil {
|
2018-11-07 12:35:59 -08:00
|
|
|
b.Log.Errorf("Could not download incoming file: %#v", err)
|
2018-10-12 14:16:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 11:45:40 -08:00
|
|
|
func (b *Bslack) handleTypingEvent(ev *slack.UserTypingEvent) (*config.Message, error) {
|
2020-03-22 10:39:11 -07:00
|
|
|
if ev.User == b.si.User.ID {
|
|
|
|
return nil, ErrEventIgnored
|
|
|
|
}
|
2019-03-15 13:23:09 -07:00
|
|
|
channelInfo, err := b.channels.getChannelByID(ev.Channel)
|
2018-11-08 11:45:40 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-12 06:58:00 -08:00
|
|
|
return &config.Message{
|
2018-11-08 11:45:40 -08:00
|
|
|
Channel: channelInfo.Name,
|
|
|
|
Account: b.Account,
|
2018-11-15 11:43:43 -08:00
|
|
|
Event: config.EventUserTyping,
|
2018-11-12 06:58:00 -08:00
|
|
|
}, nil
|
2018-11-08 11:45:40 -08:00
|
|
|
}
|
|
|
|
|
2018-11-07 12:35:59 -08:00
|
|
|
// handleDownloadFile handles file download
|
2018-12-19 13:01:05 -08:00
|
|
|
func (b *Bslack) handleDownloadFile(rmsg *config.Message, file *slack.File, retry bool) error {
|
2018-11-14 12:00:21 -08:00
|
|
|
if b.fileCached(file) {
|
2018-11-07 12:35:59 -08:00
|
|
|
return nil
|
2018-10-12 14:16:34 -07:00
|
|
|
}
|
2018-11-07 12:35:59 -08:00
|
|
|
// Check that the file is neither too large nor blacklisted.
|
|
|
|
if err := helper.HandleDownloadSize(b.Log, rmsg, file.Name, int64(file.Size), b.General); err != nil {
|
|
|
|
b.Log.WithError(err).Infof("Skipping download of incoming file.")
|
|
|
|
return nil
|
2018-10-12 14:16:34 -07:00
|
|
|
}
|
|
|
|
|
2018-11-07 12:35:59 -08:00
|
|
|
// Actually download the file.
|
|
|
|
data, err := helper.DownloadFileAuth(file.URLPrivateDownload, "Bearer "+b.GetString(tokenConfig))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("download %s failed %#v", file.URLPrivateDownload, err)
|
2018-10-12 14:16:34 -07:00
|
|
|
}
|
|
|
|
|
2018-12-19 13:01:05 -08:00
|
|
|
if len(*data) != file.Size && !retry {
|
|
|
|
b.Log.Debugf("Data size (%d) is not equal to size declared (%d)\n", len(*data), file.Size)
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
return b.handleDownloadFile(rmsg, file, true)
|
|
|
|
}
|
|
|
|
|
2018-11-12 06:58:00 -08:00
|
|
|
// If a comment is attached to the file(s) it is in the 'Text' field of the Slack messge event
|
|
|
|
// and should be added as comment to only one of the files. We reset the 'Text' field to ensure
|
|
|
|
// that the comment is not duplicated.
|
|
|
|
comment := rmsg.Text
|
|
|
|
rmsg.Text = ""
|
2022-02-05 05:45:54 -08:00
|
|
|
helper.HandleDownloadData2(b.Log, rmsg, file.Name, file.ID, comment, file.URLPrivateDownload, data, b.General)
|
2018-11-07 12:35:59 -08:00
|
|
|
return nil
|
|
|
|
}
|
2018-10-12 14:16:34 -07:00
|
|
|
|
2019-01-18 09:35:31 -08:00
|
|
|
// handleGetChannelMembers handles messages containing the GetChannelMembers event
|
|
|
|
// Sends a message to the router containing *config.ChannelMembers
|
|
|
|
func (b *Bslack) handleGetChannelMembers(rmsg *config.Message) bool {
|
|
|
|
if rmsg.Event != config.EventGetChannelMembers {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-03-15 13:23:09 -07:00
|
|
|
cMembers := b.channels.getChannelMembers(b.users)
|
2019-01-18 09:35:31 -08:00
|
|
|
|
|
|
|
extra := make(map[string][]interface{})
|
|
|
|
extra[config.EventGetChannelMembers] = append(extra[config.EventGetChannelMembers], cMembers)
|
|
|
|
msg := config.Message{
|
|
|
|
Extra: extra,
|
|
|
|
Event: config.EventGetChannelMembers,
|
|
|
|
Account: b.Account,
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Log.Debugf("sending msg to remote %#v", msg)
|
|
|
|
b.Remote <- msg
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-11-14 12:00:21 -08:00
|
|
|
// fileCached implements Matterbridge's caching logic for files
|
|
|
|
// shared via Slack.
|
|
|
|
//
|
|
|
|
// We consider that a file was cached if its ID was added in the last minute or
|
|
|
|
// it's name was registered in the last 10 seconds. This ensures that an
|
|
|
|
// identically named file but with different content will be uploaded correctly
|
|
|
|
// (the assumption is that such name collisions will not occur within the given
|
|
|
|
// timeframes).
|
|
|
|
func (b *Bslack) fileCached(file *slack.File) bool {
|
|
|
|
if ts, ok := b.cache.Get("file" + file.ID); ok && time.Since(ts.(time.Time)) < time.Minute {
|
2018-11-07 12:35:59 -08:00
|
|
|
return true
|
2018-11-14 12:00:21 -08:00
|
|
|
} else if ts, ok = b.cache.Get("filename" + file.Name); ok && time.Since(ts.(time.Time)) < 10*time.Second {
|
2018-11-07 12:35:59 -08:00
|
|
|
return true
|
2018-10-12 14:16:34 -07:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|