Merge pull request #588 from mini-bomba/dearrow-locked-titles-downvotes

postBranding.ts webhook changes
This commit is contained in:
Ajay Ramachandran 2024-08-04 09:38:24 +02:00 committed by GitHub
commit 8da5de4d7b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -85,6 +85,7 @@ export async function postBranding(req: Request, res: Response) {
const existingIsLocked = !!existingUUID && (await db.prepare("get", `SELECT "locked" from "titleVotes" where "UUID" = ?`, [existingUUID]))?.locked;
if (existingUUID != undefined && isBanned) return; // ignore votes on existing details from banned users
if (downvote && existingIsLocked && !isVip) {
sendWebhooks(videoID, existingUUID, voteType).catch((e) => Logger.error(e));
errorCode = 403;
return;
}
@ -113,7 +114,7 @@ export async function postBranding(req: Request, res: Response) {
await db.prepare("run", `UPDATE "titleVotes" as tv SET "locked" = 0 FROM "titles" t WHERE tv."UUID" = t."UUID" AND tv."UUID" != ? AND t."videoID" = ?`, [UUID, videoID]);
}
sendWebhooks(videoID, UUID).catch((e) => Logger.error(e));
sendWebhooks(videoID, UUID, voteType).catch((e) => Logger.error(e));
}
})(), (async () => {
if (thumbnail) {
@ -290,14 +291,34 @@ export async function verifyOldSubmissions(hashedUserID: HashedUserID, verificat
}
}
async function sendWebhooks(videoID: VideoID, UUID: BrandingUUID) {
const lockedSubmission = await db.prepare("get", `SELECT "titleVotes"."votes", "titles"."title", "titles"."userID" FROM "titles" JOIN "titleVotes" ON "titles"."UUID" = "titleVotes"."UUID" WHERE "titles"."videoID" = ? AND "titles"."UUID" != ? AND "titleVotes"."locked" = 1`, [videoID, UUID]);
async function sendWebhooks(videoID: VideoID, UUID: BrandingUUID, voteType: BrandingVoteType) {
const currentSubmission = await db.prepare(
"get",
`SELECT
"titles"."title",
"titleVotes"."locked",
"titles"."userID",
"titleVotes"."votes"-"titleVotes"."downvotes"+"titleVotes"."verification" AS "score"
FROM "titles" JOIN "titleVotes" ON "titles"."UUID" = "titleVotes"."UUID"
WHERE "titles"."UUID" = ?`,
[UUID]);
if (lockedSubmission) {
const currentSubmission = await db.prepare("get", `SELECT "titleVotes"."votes", "titles"."title" FROM "titles" JOIN "titleVotes" ON "titles"."UUID" = "titleVotes"."UUID" WHERE "titles"."UUID" = ?`, [UUID]);
// Unlocked title getting more upvotes than the locked one
if (voteType === BrandingVoteType.Upvote) {
const lockedSubmission = await db.prepare(
"get",
`SELECT
"titles"."title",
"titles"."userID",
"titleVotes"."votes"-"titleVotes"."downvotes"+"titleVotes"."verification" AS "score"
FROM "titles" JOIN "titleVotes" ON "titles"."UUID" = "titleVotes"."UUID"
WHERE "titles"."videoID" = ?
AND "titles"."UUID" != ?
AND "titleVotes"."locked" = 1`,
[videoID, UUID]);
// Time to warn that there may be an issue
if (currentSubmission.votes - lockedSubmission.votes > 2) {
if (lockedSubmission && currentSubmission.score - lockedSubmission.score > 2) {
const usernameRow = await db.prepare("get", `SELECT "userName" FROM "userNames" WHERE "userID" = ?`, [lockedSubmission.userID]);
const data = await getVideoDetails(videoID);
@ -305,7 +326,7 @@ async function sendWebhooks(videoID: VideoID, UUID: BrandingUUID) {
"embeds": [{
"title": data?.title,
"url": `https://www.youtube.com/watch?v=${videoID}`,
"description": `**${lockedSubmission.votes}** Votes vs **${currentSubmission.votes}**\
"description": `**${lockedSubmission.score}** score vs **${currentSubmission.score}**\
\n\n**Locked title:** ${lockedSubmission.title}\
\n**New title:** ${currentSubmission.title}\
\n\n**Submitted by:** ${usernameRow?.userName ?? ""}\n${lockedSubmission.userID}`,
@ -329,6 +350,38 @@ async function sendWebhooks(videoID: VideoID, UUID: BrandingUUID) {
});
}
}
// Downvotes on locked title
if (voteType === BrandingVoteType.Downvote && currentSubmission.locked === 1) {
const usernameRow = await db.prepare("get", `SELECT "userName" FROM "userNames" WHERE "userID" = ?`, [currentSubmission.userID]);
const data = await getVideoDetails(videoID);
axios.post(config.discordDeArrowLockedWebhookURL, {
"embeds": [{
"title": data?.title,
"url": `https://www.youtube.com/watch?v=${videoID}`,
"description": `Locked title with **${currentSubmission.score}** score received a downvote\
\n\n**Locked title:** ${currentSubmission.title}\
\n**Submitted by:** ${usernameRow?.userName ?? ""}\n${currentSubmission.userID}`,
"color": 10813440,
"thumbnail": {
"url": getMaxResThumbnail(videoID),
},
}],
})
.then(res => {
if (res.status >= 400) {
Logger.error("Error sending reported submission Discord hook");
Logger.error(JSON.stringify((res.data)));
Logger.error("\n");
}
})
.catch(err => {
Logger.error("Failed to send reported submission Discord hook.");
Logger.error(JSON.stringify(err));
Logger.error("\n");
});
}
}
async function checkForWrongVideoDuration(videoID: VideoID, duration: number): Promise<boolean> {
@ -336,4 +389,4 @@ async function checkForWrongVideoDuration(videoID: VideoID, duration: number): P
const apiDuration = apiVideoDetails?.duration;
return apiDuration && apiDuration > 2 && duration && duration > 2 && Math.abs(apiDuration - duration) > 3;
}
}