From 0f87dd6d8bccfb756b942c89b6e3e67634fbb81c Mon Sep 17 00:00:00 2001 From: Sorunome Date: Tue, 2 Mar 2021 19:33:24 +0100 Subject: [PATCH] fix emoji reactions --- src/slack.ts | 44 ++++++++++++++++++++++++++------------------ tsconfig.json | 6 ++++-- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/slack.ts b/src/slack.ts index e782246..4984ed5 100644 --- a/src/slack.ts +++ b/src/slack.ts @@ -309,19 +309,13 @@ export class App { client.on("reactionAdded", async (reaction: Slack.Reaction) => { log.verbose("Received new reaction"); const params = await this.getSendParams(puppetId, reaction.message); - const e = Emoji.get(reaction.reaction); - if (!e) { - return; - } + const e = this.slackToEmoji(`:${reaction.reaction}:`); await this.puppet.sendReaction(params, reaction.message.ts, e); }); client.on("reactionRemoved", async (reaction: Slack.Reaction) => { log.verbose("Received reaction remove"); const params = await this.getSendParams(puppetId, reaction.message); - const e = Emoji.get(reaction.reaction); - if (!e) { - return; - } + const e = this.slackToEmoji(`:${reaction.reaction}:`); await this.puppet.removeReaction(params, reaction.message.ts, e); }); p.client = client; @@ -667,11 +661,8 @@ export class App { log.warn(`Room ${room.roomId} not found!`); return; } - const e = Emoji.find(reaction); - if (!e) { - return; - } - await chan.sendReaction(eventId, e.key); + const e = this.emojiToSlack(reaction).slice(1, -1); + await chan.sendReaction(eventId, e); } public async handleMatrixRemoveReaction( @@ -689,11 +680,8 @@ export class App { log.warn(`Room ${room.roomId} not found!`); return; } - const e = Emoji.find(reaction); - if (!e) { - return; - } - await chan.removeReaction(eventId, e.key); + const e = this.emojiToSlack(reaction).slice(1, -1); + await chan.removeReaction(eventId, e); } public async handleMatrixImage( @@ -1078,4 +1066,24 @@ export class App { }, }; } + + private emojiToSlack(msg: string): string { + return msg.replace(/((?:\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])[\ufe00-\ufe0f]?)/gi, (s) => { + const e = Emoji.find(s); + if (e) { + return `:${e.key}:`; + } + return ""; + }); + } + + private slackToEmoji(msg: string): string { + return msg.replace(/:([^\s:]+?):/gi, (s) => { + const e = Emoji.get(s); + if (Emoji.find(e + "\ufe0f")) { + return e + "\ufe0f"; + } + return e; + }); + } } diff --git a/tsconfig.json b/tsconfig.json index 5295578..90beb18 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,11 +4,13 @@ "moduleResolution": "node", "target": "es2016", "noImplicitAny": false, - "inlineSourceMap": true, "outDir": "./build", "types": ["node"], "strictNullChecks": true, - "allowSyntheticDefaultImports": true + "allowSyntheticDefaultImports": true, + "incremental": true, + "sourceMap": true, + "allowJs": false }, "compileOnSave": true, "include": [