mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-06 03:26:59 +03:00
add warning webhook
This commit is contained in:
@@ -5,6 +5,7 @@ import { isUserVIP } from "../utils/isUserVIP";
|
|||||||
import { getHashCache } from "../utils/getHashCache";
|
import { getHashCache } from "../utils/getHashCache";
|
||||||
import { HashedUserID, UserID } from "../types/user.model";
|
import { HashedUserID, UserID } from "../types/user.model";
|
||||||
import { config } from "../config";
|
import { config } from "../config";
|
||||||
|
import { generateWarningDiscord, warningData, dispatchEvent } from "../utils/webhookUtils";
|
||||||
|
|
||||||
type warningEntry = {
|
type warningEntry = {
|
||||||
userID: HashedUserID,
|
userID: HashedUserID,
|
||||||
@@ -21,6 +22,8 @@ function checkExpiredWarning(warning: warningEntry): boolean {
|
|||||||
return warning.issueTime > expiry && !warning.enabled;
|
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<Response> {
|
export async function postWarning(req: Request, res: Response): Promise<Response> {
|
||||||
if (!req.body.userID) return res.status(400).json({ "message": "Missing parameters" });
|
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<Response
|
|||||||
resultStatus = "removed from";
|
resultStatus = "removed from";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const targetUsername = await getUsername(userID) ?? null;
|
||||||
|
const issuerUsername = await getUsername(issuerUserID) ?? null;
|
||||||
|
const webhookData = {
|
||||||
|
target: {
|
||||||
|
userID,
|
||||||
|
username: targetUsername
|
||||||
|
},
|
||||||
|
issuer: {
|
||||||
|
userID: issuerUserID,
|
||||||
|
username: issuerUsername
|
||||||
|
},
|
||||||
|
reason
|
||||||
|
} as warningData;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const warning = generateWarningDiscord(webhookData);
|
||||||
|
dispatchEvent("warning", warning);
|
||||||
|
} catch /* istanbul ignore next */ (err) {
|
||||||
|
Logger.error(`Error sending warning to Discord ${err}`);
|
||||||
|
}
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
message: `Warning ${resultStatus} user '${userID}'.`,
|
message: `Warning ${resultStatus} user '${userID}'.`,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { config } from "../config";
|
import { config } from "../config";
|
||||||
import { Logger } from "../utils/logger";
|
import { Logger } from "../utils/logger";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
import { HashedUserID } from "../types/user.model";
|
||||||
|
|
||||||
function getVoteAuthorRaw(submissionCount: number, isTempVIP: boolean, isVIP: boolean, isOwnSubmission: boolean): string {
|
function getVoteAuthorRaw(submissionCount: number, isTempVIP: boolean, isVIP: boolean, isOwnSubmission: boolean): string {
|
||||||
if (isOwnSubmission) {
|
if (isOwnSubmission) {
|
||||||
@@ -57,8 +58,35 @@ function dispatchEvent(scope: string, data: Record<string, unknown>): 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 {
|
export {
|
||||||
getVoteAuthorRaw,
|
getVoteAuthorRaw,
|
||||||
getVoteAuthor,
|
getVoteAuthor,
|
||||||
dispatchEvent,
|
dispatchEvent,
|
||||||
|
generateWarningDiscord,
|
||||||
|
warningData
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -42,6 +42,12 @@
|
|||||||
"vote.up",
|
"vote.up",
|
||||||
"vote.down"
|
"vote.down"
|
||||||
]
|
]
|
||||||
|
}, {
|
||||||
|
"url": "http://127.0.0.1:8081/WarningWebhook",
|
||||||
|
"key": "superSecretKey",
|
||||||
|
"scopes": [
|
||||||
|
"warning"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"maxNumberOfActiveWarnings": 3,
|
"maxNumberOfActiveWarnings": 3,
|
||||||
|
|||||||
@@ -13,6 +13,10 @@ app.post("/webhook/FirstTimeSubmissions", (req, res) => {
|
|||||||
res.sendStatus(200);
|
res.sendStatus(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.post("/webhook/WarningWebhook", (req, res) => {
|
||||||
|
res.sendStatus(200);
|
||||||
|
});
|
||||||
|
|
||||||
app.post("/webhook/CompletelyIncorrectReport", (req, res) => {
|
app.post("/webhook/CompletelyIncorrectReport", (req, res) => {
|
||||||
res.sendStatus(200);
|
res.sendStatus(200);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user