mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-24 00:18:23 +03:00
Don't allow multiple downvotes on one submission
This commit is contained in:
@@ -183,51 +183,31 @@ export async function postBranding(req: Request, res: Response) {
|
||||
async function handleExistingVotes(type: BrandingType, videoID: VideoID,
|
||||
hashedUserID: HashedUserID, UUID: BrandingUUID, hashedIP: HashedIP, voteType: BrandingVoteType) {
|
||||
const table = type === BrandingType.Title ? `"titleVotes"` : `"thumbnailVotes"`;
|
||||
const table2 = type === BrandingType.Title ? `"titles"` : `"thumbnails"`;
|
||||
|
||||
const isUsersSubmission = (await db.prepare("get", `SELECT "userID" FROM ${table2} WHERE "UUID" = ?`, [UUID]))?.userID === hashedUserID;
|
||||
const idsDealtWith: BrandingUUID[] = [];
|
||||
|
||||
// Either votes of the same type, or on the same submission (undo a downvote)
|
||||
const existingVotes = await privateDB.prepare("all", `SELECT "id", "UUID", "type" from ${table} where "videoID" = ? AND "userID" = ? AND ("type" = ? OR "UUID" = ?)`, [videoID, hashedUserID, voteType, UUID]) as ExistingVote[];
|
||||
if (existingVotes.length > 0) {
|
||||
// Only one upvote per video
|
||||
if (voteType === BrandingVoteType.Upvote) {
|
||||
for (const existingVote of existingVotes) {
|
||||
switch (existingVote.type) {
|
||||
case BrandingVoteType.Upvote:
|
||||
// Old case where there are duplicate rows in private db
|
||||
if (!idsDealtWith.includes(existingVote.UUID)) {
|
||||
idsDealtWith.push(existingVote.UUID);
|
||||
await db.prepare("run", `UPDATE ${table} SET "votes" = "votes" - 1 WHERE "UUID" = ?`, [existingVote.UUID]);
|
||||
}
|
||||
for (const existingVote of existingVotes) {
|
||||
// For downvotes, only undo for this specific submission (multiple downvotes on one submission not allowed)
|
||||
if (voteType === BrandingVoteType.Downvote && existingVote.UUID !== UUID) continue;
|
||||
|
||||
await privateDB.prepare("run", `DELETE FROM ${table} WHERE "id" = ?`, [existingVote.id]);
|
||||
break;
|
||||
case BrandingVoteType.Downvote: {
|
||||
// Undoing a downvote now that it is being upvoted
|
||||
|
||||
// Only undo downvote if it is not their submission
|
||||
if (!isUsersSubmission) {
|
||||
await db.prepare("run", `UPDATE ${table} SET "downvotes" = "downvotes" - 1 WHERE "UUID" = ?`, [existingVote.UUID]);
|
||||
}
|
||||
|
||||
await privateDB.prepare("run", `DELETE FROM ${table} WHERE "id" = ?`, [existingVote.id]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (isUsersSubmission) {
|
||||
// Treat like upvoting another submission (undoing upvote)
|
||||
let dealtWith = false;
|
||||
for (const existingVote of existingVotes) {
|
||||
if (existingVote.type === BrandingVoteType.Upvote && existingVote.UUID === UUID) {
|
||||
if (!dealtWith) {
|
||||
dealtWith = true;
|
||||
await db.prepare("run", `UPDATE ${table} SET "votes" = "votes" - 1 WHERE "UUID" = ?`, [UUID]);
|
||||
switch (existingVote.type) {
|
||||
case BrandingVoteType.Upvote:
|
||||
// Old case where there are duplicate rows in private db
|
||||
if (!idsDealtWith.includes(existingVote.UUID)) {
|
||||
idsDealtWith.push(existingVote.UUID);
|
||||
await db.prepare("run", `UPDATE ${table} SET "votes" = "votes" - 1 WHERE "UUID" = ?`, [existingVote.UUID]);
|
||||
}
|
||||
|
||||
await privateDB.prepare("run", `DELETE FROM ${table} WHERE "id" = ?`, [existingVote.id]);
|
||||
break;
|
||||
case BrandingVoteType.Downvote: {
|
||||
await db.prepare("run", `UPDATE ${table} SET "downvotes" = "downvotes" - 1 WHERE "UUID" = ?`, [existingVote.UUID]);
|
||||
|
||||
await privateDB.prepare("run", `DELETE FROM ${table} WHERE "id" = ?`, [existingVote.id]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user