summaryrefslogtreecommitdiffstats
path: root/gateway
diff options
context:
space:
mode:
authorWim <wim@42.be>2018-02-20 00:54:35 +0100
committerWim <wim@42.be>2018-02-20 01:15:25 +0100
commit7886f05e881e45303087a5ae116657f0f6164d81 (patch)
tree733afc4f86ee4a1995121f705164b62b14677338 /gateway
parentf58be0d1c1236a6a07a6917f3258a5608e1364f8 (diff)
downloadmatterbridge-msglm-7886f05e881e45303087a5ae116657f0f6164d81.tar.gz
matterbridge-msglm-7886f05e881e45303087a5ae116657f0f6164d81.tar.bz2
matterbridge-msglm-7886f05e881e45303087a5ae116657f0f6164d81.zip
Download (and upload) avatar images from mattermost and telegram when mediaserver is configured. Closes #362
An extra avatarMap (cache) is created for mattermost and telegram. If MediaServerUpload is configured, the avatar images of users are downloaded the first time a user sends a message. If this download succeeds a message with EVENT_AVATAR_DOWNLOAD is sent to the originating protocol. This message also contains a SHA field (in msg.Extra["file"]), if this is not empty, the sha will be added to the avatarMap. (so we now have a userid-sha cache) Next time this user sends a message, the MediaServerUpload/sha/userid.png URL will be used as the avatar field.
Diffstat (limited to 'gateway')
-rw-r--r--gateway/gateway.go28
1 files changed, 23 insertions, 5 deletions
diff --git a/gateway/gateway.go b/gateway/gateway.go
index c16264a2..6fe7f0ea 100644
--- a/gateway/gateway.go
+++ b/gateway/gateway.go
@@ -178,6 +178,14 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
}
}
}
+
+ // Avatar downloads are only relevant for telegram and mattermost for now
+ if msg.Event == config.EVENT_AVATAR_DOWNLOAD {
+ if dest.Protocol != "mattermost" &&
+ dest.Protocol != "telegram" {
+ return brMsgIDs
+ }
+ }
// only relay join/part when configged
if msg.Event == config.EVENT_JOIN_LEAVE && !gw.Bridges[dest.Account].Config.ShowJoinPart {
return brMsgIDs
@@ -185,6 +193,7 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
if msg.Event == config.EVENT_TOPIC_CHANGE && !gw.Bridges[dest.Account].Config.ShowTopicChange {
return brMsgIDs
}
+
// broadcast to every out channel (irc QUIT)
if msg.Channel == "" && msg.Event != config.EVENT_JOIN_LEAVE {
log.Debug("empty channel")
@@ -194,9 +203,16 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
origmsg := msg
channels := gw.getDestChannel(&msg, *dest)
for _, channel := range channels {
- // do not send to ourself
- if channel.ID == getChannelID(origmsg) {
- continue
+ // Only send the avatar download event to ourselves.
+ if msg.Event == config.EVENT_AVATAR_DOWNLOAD {
+ if channel.ID != getChannelID(origmsg) {
+ continue
+ }
+ } else {
+ // do not send to ourself for any other event
+ if channel.ID == getChannelID(origmsg) {
+ continue
+ }
}
log.Debugf("Sending %#v from %s (%s) to %s (%s)", msg, msg.Account, originchannel, dest.Account, channel.Name)
msg.Channel = channel.Name
@@ -362,15 +378,17 @@ func (gw *Gateway) handleFiles(msg *config.Message) {
durl := gw.Config.General.MediaServerDownload + "/" + sha1sum + "/" + fi.Name
extra := msg.Extra["file"][i].(config.FileInfo)
extra.URL = durl
- extra.SHA = sha1sum
- msg.Extra["file"][i] = extra
req, _ := http.NewRequest("PUT", url, reader)
req.Header.Set("Content-Type", "binary/octet-stream")
_, err := client.Do(req)
if err != nil {
log.Errorf("mediaserver upload failed: %#v", err)
+ continue
}
log.Debugf("mediaserver download URL = %s", durl)
+ // we uploaded the file successfully. Add the SHA
+ extra.SHA = sha1sum
+ msg.Extra["file"][i] = extra
}
}
}