Browse Source

Merge branch 'issue-55' of isundil/mimouchat into devel

isundil 6 years ago
parent
commit
cc8c4c8076
5 changed files with 74 additions and 13 deletions
  1. 1 0
      cli/data.js
  2. 28 3
      srv/src/controller/apiController.js
  3. 15 0
      srv/src/multichatManager.js
  4. 9 0
      srv/src/slack.js
  5. 21 10
      srv/src/slackHistory.js

+ 1 - 0
cli/data.js

@@ -45,6 +45,7 @@ SimpleChatSystem.prototype.sendTyping = function(chan) { console.error("unimplem
 SimpleChatSystem.prototype.markRead = function(chan, ts) { console.error("unimplemented"); };
 SimpleChatSystem.prototype.sendCommand = function(chan, cmd, args) { console.error("unimplemented"); };
 SimpleChatSystem.prototype.poll = function(knownVersion, now) { console.error("unimplemented"); return null; };
+SimpleChatSystem.prototype.getImage = function(path) { console.error("unimplemented"); return null; };
 
 /**
  * @constructor

+ 28 - 3
srv/src/controller/apiController.js

@@ -6,19 +6,19 @@ const config = require("../../config.js"),
     sessionManager = require("../session.js").SessionManager,
     accountConfigManager = require("../models/accountConfig.js").accountConfigManager;
 
-function recursiveGet(url, cb, redirectLoop) {
+function recursiveGet(url, cb, headers, redirectLoop) {
     var getFnc = http.get;
 
     if (url.substr(0, 8) === "https://")
         getFnc = https.get;
-    getFnc(url, (d) => {
+    getFnc(url, headers ? { headers: headers } : {}, (d) => {
         if (d.statusCode >= 300 && d.statusCode < 400 && d.headers["location"]) {
             if (!redirectLoop)
                 redirectLoop = [];
 
             if (redirectLoop.indexOf(d.headers["location"]) === -1 && redirectLoop.length < 5) {
                 redirectLoop.push(d.headers["location"]);
-                recursiveGet(d.headers["location"], cb, redirectLoop);
+                recursiveGet(d.headers["location"], cb, headers, redirectLoop);
                 return;
             }
         }
@@ -398,6 +398,31 @@ module.exports.ApiController = {
                 res.writeHeader("400", "Bad Request");
                 res.end();
             }
+        } else if (req.urlObj.match(["api", "serviceImage"])) {
+            if (req.urlObj.queryTokens.team && req.urlObj.queryTokens.path) {
+                var ctx = res.chatContext.getChatSystem(req.urlObj.queryTokens.team[0]);
+
+                if (!ctx) {
+                    res.writeHeader("404", "Not Found");
+                    res.end();
+                } else if (req.method !== 'GET') {
+                    res.writeHeader("400", "Bad Request");
+                    res.end();
+                } else {
+                    let url = ctx.getImage(req.urlObj.queryTokens.path[0]);
+                    if (!url) {
+                        res.writeHeader("400", "Bad Request");
+                        res.end();
+                    } else {
+                        recursiveGet(url.url, (d) => {
+                            d.pipe(res, { end: true });
+                        }, url.headers);
+                    }
+                }
+            } else {
+                res.writeHeader("400", "Bad Request");
+                res.end();
+            }
         } else if (req.urlObj.match(["api"])) {
             let knownVersion = (req.urlObj.queryTokens.v ? parseInt(req.urlObj.queryTokens.v[0], 10) : 0) || 0;
             res.chatContext.poll(knownVersion, req.reqT,

+ 15 - 0
srv/src/multichatManager.js

@@ -119,6 +119,12 @@ ChatSystem.prototype.sendCommand = function(chan, cmd, args) {};
 **/
 ChatSystem.prototype.poll = function(knownVersion, nowTs, withTyping) {};
 
+/**
+ * @param {string} path
+ * @return {({url: string, headers: Object})|null} path to get
+**/
+ChatSystem.prototype.getImage = function(path) {};
+
 /**
  * @constructor
 **/
@@ -223,6 +229,15 @@ MultiChatManager.prototype.getChannelIds = function(filter) {
     return ids;
 };
 
+MultiChatManager.prototype.getChatSystem =function(teamId) {
+    for (var i =0, nbCtx = this.contexts.length; i < nbCtx; i++) {
+        var sys = this.contexts[i]
+            ,ctx = sys.getChatContext();
+        if (ctx && ctx.team && ctx.team.id === teamId)
+            return sys;
+    }
+};
+
 /**
  * @param {string} userId
  * @return {Chatter|null}

+ 9 - 0
srv/src/slack.js

@@ -949,5 +949,14 @@ Slack.prototype.getChatContext = function() {
     return this.data;
 };
 
+Slack.prototype.getImage = function(path) {
+    return {
+        url: "https://files.slack.com/" +path.replace(/[^\/A-Za-z0-9\-_\.]/g, ''),
+        headers: {
+            "Authorization": "Bearer " +this.token
+        }
+    };
+};
+
 module.exports.Slack = Slack;
 

+ 21 - 10
srv/src/slackHistory.js

@@ -47,16 +47,27 @@ SlackHistory.prototype.prepareMessage = function(ev, targetId) {
 }
 
 SlackHistory.prototype.messageFactory = function(ev, ts) {
-    let img;
-    if (ev["file"] && ev["file"]["mimetype"].indexOf("image/") >= 0)
-        img = ev["file"]["thumb_360"] || ev["file"]["url_private"] || ev["file"]["permalink"] || undefined;
-    if (img) {
-        if (!ev["attachments"])
-            ev["attachments"] = [];
-        ev["attachments"].push({
-            "image_url": img
-        });
-    }
+    ev["files"] && ev["files"].forEach(f => {
+        if (f["mimetype"].indexOf("image/") >= 0) {
+            let imgUrl = f["thumb_360"] || f["url_private"] || f["permalink"];
+            if (imgUrl) {
+                if (!ev["attachments"])
+                    ev["attachments"] = [];
+                ev["attachments"].push({
+                    "image_url": imgUrl
+                });
+                return;
+            }
+        }
+        // FIXME manage uploads that are not images
+    });
+    if (ev["attachments"]) ev["attachments"].forEach(attachment => {
+        const slackDomainUri = "https://files.slack.com/";
+        if (attachment.image_url && attachment.image_url.startsWith(slackDomainUri)) {
+            attachment.remote_image_url = attachment.image_url;
+            attachment.image_url = "api/serviceImage?team=" +encodeURIComponent(this.ctx.data.team.id) +"&path=" +encodeURIComponent(attachment.image_url.substr(slackDomainUri.length));
+        }
+    }, this);
     if (!ev["user"]) {
         if (ev["bot_id"])
             ev["user"] = ev["bot_id"];