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"));
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,9 +26,15 @@ app.command("/check-eligiblity", async ctx => {
let userId = ctx.context.userId;
let matchedBy = "no input"
if (match = text.match(/\<\@(.+)\|(.+)>/)) {
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"
@ -35,32 +51,53 @@ app.command("/check-eligiblity", async ctx => {
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 <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
})
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 {
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}*.`
}
},
...(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```"
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.)" : ""}`
}
}
])
]
})
}

View file

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