Skip to content

Commit

Permalink
feat: Improve reactions logic🧐🤨 (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric authored Sep 12, 2022
1 parent 938ae7c commit 69401c9
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions slack-service/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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
Expand All @@ -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}`])) {
Expand All @@ -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);
Expand Down Expand Up @@ -261,22 +284,28 @@ app.message(":potato:", async ({message}) => await givePotato({
text: message.text,
channel: message.channel,
ts: message.ts,
type: TYPE_MESSAGE,
})
);


/// 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,
});
}
});
Expand Down Expand Up @@ -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."
}
},
{
Expand Down

0 comments on commit 69401c9

Please sign in to comment.