From 59dee68ce0963be7a9186a1f875e87e1eda5c7db Mon Sep 17 00:00:00 2001 From: DaInfLoop Date: Wed, 4 Sep 2024 17:15:22 +0100 Subject: [PATCH] add different states + only allow viewing of other if whitelisted --- index.ts | 89 +++++++++++++++++++++++++++++++++++++--------------- package.json | 1 - 2 files changed, 63 insertions(+), 27 deletions(-) diff --git a/index.ts b/index.ts index 75b41c5..be4fec0 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,4 @@ -import type { User } from "@slack/web-api/dist/response/UsersInfoResponse"; +import type { UsersInfoResponse } from "@slack/web-api"; const { App, ExpressReceiver } = (await import("@slack/bolt")); import "dotenv/config"; @@ -8,6 +8,16 @@ const app = new App({ signingSecret: process.env.SLACK_SIGNING_SECRET, }); +const whitelist: string[] = [] + +function checkUserOk(user: UsersInfoResponse['user']) { + if (whitelist.includes(user!.id!)) return true + + console.log(user) + + return user!.is_admin || user!.is_owner || user!.is_primary_owner +} + app.command("/check-eligiblity", async ctx => { await ctx.ack(); @@ -16,10 +26,16 @@ app.command("/check-eligiblity", async ctx => { let userId = ctx.context.userId; let matchedBy = "no input" - if (match = text.match(/\<\@(.+)\|(.+)>/)) { - userId = match[1]; - matchedBy = "user mention" - } else if (text) + const iUser = await ctx.client.users.info({ user: ctx.context.userId! }); + + if ((match = text.match(/\<\@(.+)\|(.+)>/))) { + if (!checkUserOk(iUser.user!)) { + matchedBy = "not allowed" + } else { + userId = match[1]; + matchedBy = "user mention" + } + } else if (text) matchedBy = "invalid input" @@ -27,47 +43,68 @@ app.command("/check-eligiblity", async ctx => { method: "POST", headers: { 'content-type': 'application/json' }, body: JSON.stringify({ - "slack_id": userId + "slack_id": userId }), redirect: "follow" }).then(res => res.json()) - if (res === `User ${userId} not found!`) + if (res === `User ${userId} not found!`) return await ctx.respond({ response_type: 'ephemeral', - text: `Either ${matchedBy !== "user mention" ? "you haven't" : `<@${userId}> hasn't`} verified, or ${matchedBy !== "user mention" ? "your" : "their"} verification hasn't been accepted.${matchedBy !== "user mention" ? "\nCheck out the to verify." : ""}`, + text: `${matchedBy !== "user mention" ? "You aren't" : `<@${userId}> isn't`} verified and therefore aren't eligible for rewards from your program.${matchedBy !== "user mention" ? `\nCheck out the to verify.` : ""}${matchedBy == "not allowed" ? " (Only whitelisted users can check other people's verification statuses.)" : ""}`, unfurl_links: true }) + else if (res.status === "Insufficient") { + return await ctx.respond({ + response_type: 'ephemeral', + text: `${matchedBy !== "user mention" ? "You" : `<@${userId}>`} provided insufficient evidence that ${matchedBy !== "user mention" ? "you" : "they"} are a student.${matchedBy !== "user mention" ? `\nCheck out the to re-verify.` : ""}${matchedBy == "not allowed" ? " (Only whitelisted users can check other people's verification statuses.)" : ""}`, + unfurl_links: true + }) + } + + else if (res.status === "Unknown") { + return await ctx.respond({ + response_type: 'ephemeral', + text: `${matchedBy !== "user mention" ? "Your verification" : `<@${userId}>'s verification`} has not been accepted yet.${matchedBy == "not allowed" ? " (Only whitelisted users can check other people's verification statuses.)" : ""}`, + unfurl_links: true + }) + } + + else if (res.status === "Ineligible") { + if (matchedBy === "user mention") { + return await ctx.respond({ + response_type: 'ephemeral', + text: `<@${userId}>'s verification has been denied.` + }) + } else { + return await ctx.respond({ + response_type: 'ephemeral', + text: `Your verification has been denied. If you believe this to be a mistake, please contact an admin of the program you are applying for.${matchedBy == "not allowed" ? " (Only whitelisted users can check other people's verification statuses.)" : ""},` + }) + } + } + + else { return await ctx.respond({ response_type: 'ephemeral', - text: `${matchedBy !== "user mention" ? "You have verified your" : `<@${userId}> has verified their`} student status, and ${matchedBy !== "user mention" ? "are" : "is"} ${res.status}.`, + text: `${matchedBy !== "user mention" ? "You have verified your" : `<@${userId}> has verified their`} student status, and ${matchedBy !== "user mention" ? "are" : "is"} ${res.status}.${matchedBy == "not allowed" ? " (Only whitelisted users can check other people's verification statuses.)" : ""}`, blocks: [ { type: 'section', text: { type: 'mrkdwn', - text: `${matchedBy !== "user mention" ? "You have verified your" : `<@${userId}> has verified their`} student status, and ${matchedBy !== "user mention" ? "are" : "is"} *${res.status}*.` + text: `${matchedBy !== "user mention" ? "You have verified your" : `<@${userId}> has verified their`} student status, and ${matchedBy !== "user mention" ? "are" : "is"} *${res.status}*.${matchedBy == "not allowed" ? " (Only whitelisted users can check other people's verification statuses.)" : ""}` } - }, - ...(matchedBy == "user mention" ? [] : [ - { - type: 'section', - // @ts-ignore silly typings - text: { - type: 'mrkdwn', - text: `*Raw JSON output from the Eligiblity API:*\n` + "```\n" + JSON.stringify(res, null, 2) + "\n```" - } - } - ]) + } ] - }) + }) } }) -;(async () => { - await app.start(60275); + ; (async () => { + await app.start(60275); - console.log('⚡️ Bolt app is running!'); -})(); + console.log('⚡️ Bolt app is running!'); + })(); diff --git a/package.json b/package.json index 62ef5b4..02f38e6 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,6 @@ "version": "1.0.0", "main": "index.ts", "type": "module", - "scripts": { "test": "echo \"Error: no test specified\" && exit 1" },