Skip to content

Commit

Permalink
Attempt to implement matrix->slack reactions.
Browse files Browse the repository at this point in the history
it sucks though
  • Loading branch information
Cadair committed May 22, 2019
1 parent 1186bd7 commit 6a1fe7f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 11 deletions.
61 changes: 57 additions & 4 deletions lib/BridgedRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,55 @@ BridgedRoom.prototype.toEntry = function() {
return entry;
};

BridgedRoom.prototype.onMatrixReaction = async function(message) {
if (!this._slack_bot_token) return Promise.resolve();

const relates_to = message.content['m.relates_to'];
const eventStore = this._main.getEventStore();
const event = await eventStore.getEntryByMatrixId(message.room_id, relates_to.event_id);

// If we don't get an event then exit
if (event === null) {
log.debug("Could not find event to react to.");
return Promise.resolve();
}

// Convert the unicode emoji into a slack emote name
let emoji_key_name = "";
if (relates_to.key.startsWith(":") && relates_to.key.endsWith(":")) {
emoji_key_name = relates_to.key;
} else {
emoji_key_name = substitutions.reverseEmojiIndex[relates_to.key];
}
emoji_key_name = emoji_key_name.substring(1, emoji_key_name.length - 1);

// TODO: This only works once from matrix as we are sending the event as the
// bot user.
var body = {channel: this._slack_channel_id,
timestamp: event.remoteEventId,
name: emoji_key_name,
as_user: false};

const sendMessageParams = {
method: "POST",
json: true,
uri: "https://slack.com/api/reactions.add",
body: body,
};

if (this._slack_bot_token) {
sendMessageParams.headers = {
Authorization: 'Bearer ' + this._slack_bot_token
};
}

const res = await rp(sendMessageParams);
if (!res || !res.ok) {
log.error("HTTP Error: ", res);
}
return res;
};

BridgedRoom.prototype.onMatrixRedaction = async function(message) {
if (!this._slack_bot_token) return Promise.resolve();

Expand Down Expand Up @@ -219,7 +268,11 @@ BridgedRoom.prototype.onMatrixRedaction = async function(message) {
};
}

return await rp(sendMessageParams);
const res = await rp(sendMessageParams);
if (!res || !res.ok) {
log.error("HTTP Error: ", res);
}
return res;
};

BridgedRoom.prototype.onMatrixEdit = async function(message) {
Expand Down Expand Up @@ -314,16 +367,16 @@ BridgedRoom.prototype.onMatrixMessage = async function(message) {
};

BridgedRoom.prototype.onSlackReactionAdded = async function(message) {
// TODO: Check for reflections
// We need a way of filtering out our own ghost here.
// But the events come back with a userID not a botID
const ghost = await this._main.getGhostForSlackMessage(message);
var roomID = this.getMatrixRoomId();
await ghost.update(message, this);

const reaction = `:${message.reaction}:`;
log.error(reaction);
log.debug(substitutions.emojiIndex);
const reaction_key = substitutions.emojiIndex[reaction] || reaction;

const eventStore = this._main.getEventStore();
const event = await eventStore.getEntryByRemoteId(message.item.channel, message.item.ts);

return ghost.sendReaction(roomID, event.eventId, reaction_key, message.item.channel, message.event_ts);
Expand Down
12 changes: 12 additions & 0 deletions lib/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,18 @@ Main.prototype.onMatrixEvent = function(ev) {
return;
}

// Handle a m.reaction event
if (ev.type === "m.reaction") {
room.onMatrixReaction(ev).then(
() => endTimer({outcome: "success"}),
(e) => {
log.error("Failed procesing matrix message: ", e);
endTimer({outcome: "fail"});
}
);
return;
}

// Handle a m.room.message event
if (ev.type === "m.room.message" || ev.content) {
if (ev.content['m.relates_to'] !== undefined) {
Expand Down
7 changes: 0 additions & 7 deletions lib/SlackGhost.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
"use strict";

var url = require('url');
var https = require('https');
var rp = require('request-promise');
const Slackdown = require('Slackdown');
const substitutions = require("./substitutions");
const log = require("matrix-appservice-bridge").Logging.get("SlackGhost");
const BridgeLib = require("matrix-appservice-bridge");
const StoreEvent = BridgeLib.StoreEvent;
Expand Down Expand Up @@ -181,18 +178,15 @@ SlackGhost.prototype.sendText = function(room_id, text, slackRoomID, slackEventT
};

SlackGhost.prototype.sendReaction = async function(room_id, event_id, key, slackRoomId, slackEventTs) {
log.error(event_id);
const content = {
"m.relates_to": {
"event_id": event_id,
"rel_type": "m.annotation",
"key": key}
};

log.debug(content);
const intent = await this.getIntent();
const matrixEvent = await intent.sendEvent(room_id, "m.reaction", content);
log.error(matrixEvent);

// Add this event to the eventStore
const event = new StoreEvent(room_id, matrixEvent.event_id, slackRoomId, slackEventTs);
Expand All @@ -204,7 +198,6 @@ SlackGhost.prototype.sendReaction = async function(room_id, event_id, key, slack

SlackGhost.prototype.sendMessage = function(room_id, msg, slackRoomID, slackEventTS) {
return this.getIntent().sendMessage(room_id, msg).then((matrixEvent) => {
log.debug(msg);
this._main.incCounter("sent_messages", {side: "matrix"});

// Add this event to the eventStore
Expand Down
8 changes: 8 additions & 0 deletions lib/substitutions.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ var emojiIndex = (function () {
return index;
})();

const reverseEmojiIndex = {};

for (let j in emojiIndex){
if (!Object.prototype.hasOwnProperty.call(emojiIndex, j)) continue;
reverseEmojiIndex[emojiIndex[j]] = j;
}

function replaceAll(string, old, replacement) {
return string.split(old).join(replacement);
}
Expand Down Expand Up @@ -351,6 +358,7 @@ function makeDiff(prev, curr) {
}

module.exports = {
reverseEmojiIndex: reverseEmojiIndex,
emojiIndex: emojiIndex,
matrixToSlack: matrixToSlack,
slackToMatrix: slackToMatrix,
Expand Down

0 comments on commit 6a1fe7f

Please sign in to comment.