From f01dfe0160214cce1f6a9cd752333f523ea78980 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Fri, 9 Sep 2022 00:22:22 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20Improve=20reactions=20logic=F0=9F=A7=90?= =?UTF-8?q?=F0=9F=A4=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- slack-service/app.js | 47 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/slack-service/app.js b/slack-service/app.js index 79abf7fa..c9df2025 100644 --- a/slack-service/app.js +++ b/slack-service/app.js @@ -20,6 +20,8 @@ const { App } = require("@slack/bolt"); Sentry.init({ dsn: process.env.SENTRY_DSN }); const maxPotato = process.env.MAX_POTATO +const TYPE_MESSAGE = 'message' +const TYPE_REACTION = 'reaction' // Initialize the slack App const app = new App({ @@ -156,13 +158,13 @@ async function getUserDbId(slackId) { } /// Figure out who gets potato -async function givePotato({user, text, channel, ts}) { +async function givePotato({user, text, channel, ts, type}) { const senderSlackId = user; const senderDBId = await getUserDbId(senderSlackId); // Regex to find all the mentions const regex = /<.*?>/g; - const userSlackIdsFound = text.match(regex); + const userSlackIdsFound = text.match(regex) || []; // Extract the ids from the mention let receiverSlackIds = userSlackIdsFound @@ -179,6 +181,22 @@ async function givePotato({user, text, channel, ts}) { }); }; + // Handle the case, where a user reacts to a message which + // does not mention any other users + if (type === TYPE_REACTION && receiverSlackIds.length == 0) { + const result = await app.client.conversations.history({ + channel: channel, + latest: ts, + inclusive: true, + limit: 1 + }); + + if (result.messages[0]?.user !== null) { + // Hardcode the reciever of potato to the author of the message + receiverSlackIds = [result.messages[0].user]; + } + } + // Check if the only person recieving a potato is the creator of the message // and blame them... if (_.isEqual(receiverSlackIds, [`${senderSlackId}`])) { @@ -195,7 +213,12 @@ async function givePotato({user, text, channel, ts}) { } const receiversCount = receiverSlackIds.length; - const potatoCount = (text.match(/:potato:/g) || []).length; + let potatoCount = (text.match(/:potato:/g) || []).length; + + // Only give out one potato, if someone reacts + if (type === TYPE_REACTION) { + potatoCount = 1; + } // Check that there are potatos left to give for the sender (sender ids) const potatoesGivenToday = await getPotatoesGivenToday(senderDBId); @@ -261,6 +284,7 @@ app.message(":potato:", async ({message}) => await givePotato({ text: message.text, channel: message.channel, ts: message.ts, + type: TYPE_MESSAGE, }) ); @@ -268,15 +292,20 @@ app.message(":potato:", async ({message}) => await givePotato({ /// Listens to incoming :potato: reactions app.event("reaction_added", async ({ event }) => { if (event.reaction === "potato") { + // Fetch the message the potato reaction was added to + const result = await app.client.conversations.history({ + channel: event.item.channel, + latest: event.item.ts, + inclusive: true, + limit: 1 + }); + await givePotato({ user: event.user, - // Set the text of the message to the slack user_id of the creator - // of the message the reaction was added to. - // We do this messy stuff to be able to work with the same interface - // givePotato() provides. - text: `<@${event.item_user}> :${event.reaction}:`, + text: result.messages[0].text, channel: event.item.channel, ts: event.item.ts, + type: TYPE_REACTION, }); } }); @@ -332,7 +361,7 @@ app.event("app_home_opened", async ({ event, client, context }) => { "type": "section", "text": { "type": "mrkdwn", - "text": "You can gib people potato to show your much like them and recognize them for all the toll things they do." + "text": "You can gib people potato to show you much like them and recognize them for all the toll things they do." } }, {