Skip to content

Commit

Permalink
Add support for conversion of snippets to code messages in matrix
Browse files Browse the repository at this point in the history
Signed-off-by: Stuart Mumford <stuart@cadair.com>
  • Loading branch information
Cadair committed Sep 27, 2018
1 parent 62776bb commit a78cfc5
Showing 1 changed file with 58 additions and 14 deletions.
72 changes: 58 additions & 14 deletions lib/BridgedRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

var Promise = require('bluebird');

var url = require('url');
var https = require('https');
var substitutions = require("./substitutions");
var rp = require('request-promise');

Expand Down Expand Up @@ -370,20 +372,62 @@ BridgedRoom.prototype._handleSlackMessage = function(message, ghost) {
for (var i = 0; i < message.files.length; i++) {
var file = message.files[i];

return ghost.uploadContentFromURI(file, file.url_private, this._slack_bot_token).then((content_uri) => {
var matrixMessage = slackFileToMatrixMessage(file, content_uri);
return ghost.sendMessage(roomID, matrixMessage);
}).then(() => {
// TODO: Currently Matrix lacks a way to upload a "captioned image",
// so we just send a separate `m.image` and `m.text` message
// See https://github.com/matrix-org/matrix-doc/issues/906
if (message.text) {
var text = substitutions.slackToMatrix(
message.text
);
return ghost.sendText(roomID, text);
}
});
if (file.mode === "snippet") {
var options = url.parse(file.url_private);
options.headers = {
Authorization: 'Bearer ' + this._slack_bot_token
};
const req = https.get(options, (res) => {
let buffer = '';

res.on("data", (d) => {
buffer += d;
});

res.on("end", () => {
var code = '```';
code += '\n';
code += buffer;
code += '\n';
code += '```';
if (file.filetype) {
var html_code = '<pre><code class="language-' + file.filetype + '">';
}
else {
var html_code = '<pre><code>';
}
html_code += substitutions.htmlEscape(buffer);
html_code += '</code></pre>';

const content = {
body: code,
msgtype: "m.text",
formatted_body: html_code,
format: "org.matrix.custom.html"
};
return ghost.sendMessage(roomID, content);
});
});
req.on("error", (err) => {
reject("Failed to download");
});
}
else {
return ghost.uploadContentFromURI(file, file.url_private, this._slack_bot_token).then((content_uri) => {
var matrixMessage = slackFileToMatrixMessage(file, content_uri);
return ghost.sendMessage(roomID, matrixMessage);
}).then(() => {
// TODO: Currently Matrix lacks a way to upload a "captioned image",
// so we just send a separate `m.image` and `m.text` message
// See https://github.com/matrix-org/matrix-doc/issues/906
if (message.text) {
var text = substitutions.slackToMatrix(
message.text
);
return ghost.sendText(roomID, text);
}
});
}
}
}
else {
Expand Down

0 comments on commit a78cfc5

Please sign in to comment.