diff --git a/src/routes/postWarning.ts b/src/routes/postWarning.ts index f9eaa4c..c3c2a0c 100644 --- a/src/routes/postWarning.ts +++ b/src/routes/postWarning.ts @@ -5,6 +5,7 @@ import { isUserVIP } from "../utils/isUserVIP"; import { getHashCache } from "../utils/getHashCache"; import { HashedUserID, UserID } from "../types/user.model"; import { config } from "../config"; +import { generateWarningDiscord, warningData, dispatchEvent } from "../utils/webhookUtils"; type warningEntry = { userID: HashedUserID, @@ -21,6 +22,8 @@ function checkExpiredWarning(warning: warningEntry): boolean { return warning.issueTime > expiry && !warning.enabled; } +const getUsername = (userID: HashedUserID) => db.prepare("get", `SELECT "userName" FROM "userNames" WHERE "userID" = ?`, [userID], { useReplica: true }); + export async function postWarning(req: Request, res: Response): Promise { if (!req.body.userID) return res.status(400).json({ "message": "Missing parameters" }); @@ -62,6 +65,27 @@ export async function postWarning(req: Request, res: Response): Promise): void { } } +interface warningData { + target: { + userID: HashedUserID + username: string | null + }, + issuer: { + userID: HashedUserID, + username: string | null + }, + reason: string +} + +function generateWarningDiscord(data: warningData) { + return { + embeds: [ + { + title: "Warning", + description: `**User:** ${data.target.username} (${data.target.userID})\n**Issuer:** ${data.issuer.username} (${data.issuer.userID})\n**Reason:** ${data.reason}`, + color: 0xff0000, + timestamp: new Date().toISOString() + } + ] + }; +} + export { getVoteAuthorRaw, getVoteAuthor, dispatchEvent, + generateWarningDiscord, + warningData }; diff --git a/test.json b/test.json index fef8052..0d38463 100644 --- a/test.json +++ b/test.json @@ -42,6 +42,12 @@ "vote.up", "vote.down" ] + }, { + "url": "http://127.0.0.1:8081/WarningWebhook", + "key": "superSecretKey", + "scopes": [ + "warning" + ] } ], "maxNumberOfActiveWarnings": 3, diff --git a/test/mocks.ts b/test/mocks.ts index d6673bf..f34a403 100644 --- a/test/mocks.ts +++ b/test/mocks.ts @@ -13,6 +13,10 @@ app.post("/webhook/FirstTimeSubmissions", (req, res) => { res.sendStatus(200); }); +app.post("/webhook/WarningWebhook", (req, res) => { + res.sendStatus(200); +}); + app.post("/webhook/CompletelyIncorrectReport", (req, res) => { res.sendStatus(200); });