add different states + only allow viewing of other if whitelisted

This commit is contained in:
DaInfLoop 2024-09-04 17:15:22 +01:00
parent 38d539aa25
commit 59dee68ce0
No known key found for this signature in database
GPG key ID: 8B96C44DDF5756E4
2 changed files with 63 additions and 27 deletions

View file

@ -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")); const { App, ExpressReceiver } = (await import("@slack/bolt"));
import "dotenv/config"; import "dotenv/config";
@ -8,6 +8,16 @@ const app = new App({
signingSecret: process.env.SLACK_SIGNING_SECRET, 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 => { app.command("/check-eligiblity", async ctx => {
await ctx.ack(); await ctx.ack();
@ -16,10 +26,16 @@ app.command("/check-eligiblity", async ctx => {
let userId = ctx.context.userId; let userId = ctx.context.userId;
let matchedBy = "no input" let matchedBy = "no input"
if (match = text.match(/\<\@(.+)\|(.+)>/)) { const iUser = await ctx.client.users.info({ user: ctx.context.userId! });
userId = match[1];
matchedBy = "user mention" if ((match = text.match(/\<\@(.+)\|(.+)>/))) {
} else if (text) if (!checkUserOk(iUser.user!)) {
matchedBy = "not allowed"
} else {
userId = match[1];
matchedBy = "user mention"
}
} else if (text)
matchedBy = "invalid input" matchedBy = "invalid input"
@ -27,47 +43,68 @@ app.command("/check-eligiblity", async ctx => {
method: "POST", method: "POST",
headers: { 'content-type': 'application/json' }, headers: { 'content-type': 'application/json' },
body: JSON.stringify({ body: JSON.stringify({
"slack_id": userId "slack_id": userId
}), }),
redirect: "follow" redirect: "follow"
}).then(res => res.json()) }).then(res => res.json())
if (res === `User ${userId} not found!`) if (res === `User ${userId} not found!`)
return await ctx.respond({ return await ctx.respond({
response_type: 'ephemeral', 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 <https://forms.hackclub.com/eligibility|eligiblity form> 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 <https://forms.hackclub.com/eligibility?slack_id=${userId}|eligiblity form> to verify.` : ""}${matchedBy == "not allowed" ? " (Only whitelisted users can check other people's verification statuses.)" : ""}`,
unfurl_links: true 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 <https://forms.hackclub.com/eligibility?slack_id=${userId}|eligiblity form> 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 { else {
return await ctx.respond({ return await ctx.respond({
response_type: 'ephemeral', 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: [ blocks: [
{ {
type: 'section', type: 'section',
text: { text: {
type: 'mrkdwn', 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 () => { ; (async () => {
await app.start(60275); await app.start(60275);
console.log('⚡️ Bolt app is running!'); console.log('⚡️ Bolt app is running!');
})(); })();

View file

@ -3,7 +3,6 @@
"version": "1.0.0", "version": "1.0.0",
"main": "index.ts", "main": "index.ts",
"type": "module", "type": "module",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },