Skip to content

Commit

Permalink
Validate body req of webhook and make tagUser optional (#274)
Browse files Browse the repository at this point in the history
This PR adds a simple validation on the request body of discord's
webhook and makes `tagUser` param optional. I've made it default to
`"0"` because I'm assuming n8n sends `"0"` since there's a check for
that, but if that's not the case we could change this just to check if
the value is a string, to not blow up while calling `.split` which I've
also changed the placement, bc `memberIds` was not used anywhere else,
so it makes more sense to move it.

Also it updates the admin list.

Not sure why tests are failing, seems a port issue, maybe it's the
worker ?
  • Loading branch information
Shadowfiend authored Jan 2, 2024
2 parents f5d3b2c + b5c3b6d commit 6f935aa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
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

0 comments on commit 6f935aa

Please sign in to comment.