Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate body req of webhook and make tagUser optional #274

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions discord-scripts/discord-webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export default async function webhookDiscord(
) {
async function sendToDiscordChannel(
channelName: string,
tagUser: string,
title: string,
message: string,
tagUser: string = "0", // 0 means no user is tagged
) {
let channel: TextChannel | undefined
const guilds = discordClient.guilds.cache
Expand All @@ -30,7 +30,6 @@ export default async function webhookDiscord(
"Text-based channel with the given name not found in any guild",
)

const memberIds = tagUser.split(",")
const existingThread = channel.threads.cache.find(
(thread) => thread.name === title,
)
Expand All @@ -43,6 +42,7 @@ export default async function webhookDiscord(
reason: message,
})
if (tagUser !== "0") {
const memberIds = tagUser.split(",")
await Promise.all(
memberIds.map((id) => newThread.members.add(id.trim())),
)
Expand Down Expand Up @@ -71,12 +71,27 @@ export default async function webhookDiscord(
}
},
async (req: express.Request, res: express.Response) => {
const isBodyInvalid = ["channelName", "title", "message"].some(
(field) => {
const isFieldEmpty = !req.body?.[field]

if (isFieldEmpty) {
res.status(400).send(`Missing field: ${field}`)
}
return isFieldEmpty
},
)

if (isBodyInvalid) {
return
}

const { channelName, tagUser, title, message } = req.body

robot.logger.info(
`Received data: channelName = ${channelName}, title = ${title}, tagged users = ${tagUser} , message = ${message}`,
)
await sendToDiscordChannel(channelName, tagUser, title, message)
await sendToDiscordChannel(channelName, title, message, tagUser)

res.status(200).send("Message sent to Discord")
},
Expand Down
15 changes: 3 additions & 12 deletions scripts/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ const SUPER_ADMIN_USERS = ["@matt:thesis.co", "@shadowfiend:thesis.co"]

const ADMIN_USERS = [
...SUPER_ADMIN_USERS,
"@puppycodes:thesis.co",
"@carolyn:thesis.co",
"@gluzman:thesis.co",
"@jessiefrance:thesis.co",
"@veronica:thesis.co",
]

// Additional per-space admins beyond the core Thesis admins.
Expand All @@ -60,17 +58,10 @@ const SPACE_ADMINS: { [spaceRoomId: string]: string[] } = {
// Keep space.
"!YDpOcIsEpQabwiHpdV:thesis.co": ["@piotr.dyraga:thesis.co"],
// Tally Ho space.
"!wCfAwzfZOUHTYIDjRn:thesis.co": [
"@michaelh:thesis.co",
"@puppycodes:thesis.co",
],
"!wCfAwzfZOUHTYIDjRn:thesis.co": ["@michaelh:thesis.co"],
// Fold space.
"!SuBAnawNxcIXoCHfPM:thesis.co": [
"@tom:thesis.co",
"@willreeves:thesis.co",
"@puppycodes:thesis.co",
],
// Power Period space.
"!SuBAnawNxcIXoCHfPM:thesis.co": ["@tom:thesis.co", "@willreeves:thesis.co"],
// Embody space.
"!XEnwlDoWvSBvrloDVH:thesis.co": ["@anna:thesis.co"],
}

Expand Down
Loading