mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-07 12:07:07 +03:00
Don't allow multiple downvotes on one submission
This commit is contained in:
@@ -102,7 +102,9 @@ export class Sqlite implements IDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static processUpgradeQuery(query: string): string {
|
private static processUpgradeQuery(query: string): string {
|
||||||
return query.replace(/^.*--!sqlite-ignore/gm, "");
|
return query
|
||||||
|
.replace(/SERIAL PRIMARY KEY/gi, "INTEGER PRIMARY KEY AUTOINCREMENT")
|
||||||
|
.replace(/^.*--!sqlite-ignore/gm, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
highLoad() {
|
highLoad() {
|
||||||
|
|||||||
@@ -183,17 +183,16 @@ export async function postBranding(req: Request, res: Response) {
|
|||||||
async function handleExistingVotes(type: BrandingType, videoID: VideoID,
|
async function handleExistingVotes(type: BrandingType, videoID: VideoID,
|
||||||
hashedUserID: HashedUserID, UUID: BrandingUUID, hashedIP: HashedIP, voteType: BrandingVoteType) {
|
hashedUserID: HashedUserID, UUID: BrandingUUID, hashedIP: HashedIP, voteType: BrandingVoteType) {
|
||||||
const table = type === BrandingType.Title ? `"titleVotes"` : `"thumbnailVotes"`;
|
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[] = [];
|
const idsDealtWith: BrandingUUID[] = [];
|
||||||
|
|
||||||
// Either votes of the same type, or on the same submission (undo a downvote)
|
// 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[];
|
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) {
|
if (existingVotes.length > 0) {
|
||||||
// Only one upvote per video
|
// Only one upvote per video
|
||||||
if (voteType === BrandingVoteType.Upvote) {
|
|
||||||
for (const existingVote of existingVotes) {
|
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;
|
||||||
|
|
||||||
switch (existingVote.type) {
|
switch (existingVote.type) {
|
||||||
case BrandingVoteType.Upvote:
|
case BrandingVoteType.Upvote:
|
||||||
// Old case where there are duplicate rows in private db
|
// Old case where there are duplicate rows in private db
|
||||||
@@ -205,32 +204,13 @@ async function handleExistingVotes(type: BrandingType, videoID: VideoID,
|
|||||||
await privateDB.prepare("run", `DELETE FROM ${table} WHERE "id" = ?`, [existingVote.id]);
|
await privateDB.prepare("run", `DELETE FROM ${table} WHERE "id" = ?`, [existingVote.id]);
|
||||||
break;
|
break;
|
||||||
case BrandingVoteType.Downvote: {
|
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 db.prepare("run", `UPDATE ${table} SET "downvotes" = "downvotes" - 1 WHERE "UUID" = ?`, [existingVote.UUID]);
|
||||||
}
|
|
||||||
|
|
||||||
await privateDB.prepare("run", `DELETE FROM ${table} WHERE "id" = ?`, [existingVote.id]);
|
await privateDB.prepare("run", `DELETE FROM ${table} WHERE "id" = ?`, [existingVote.id]);
|
||||||
break;
|
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]);
|
|
||||||
}
|
|
||||||
|
|
||||||
await privateDB.prepare("run", `DELETE FROM ${table} WHERE "id" = ?`, [existingVote.id]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await privateDB.prepare("run", `INSERT INTO ${table} ("videoID", "UUID", "userID", "hashedIP", "type") VALUES (?, ?, ?, ?, ?)`,
|
await privateDB.prepare("run", `INSERT INTO ${table} ("videoID", "UUID", "userID", "hashedIP", "type") VALUES (?, ?, ?, ?, ?)`,
|
||||||
|
|||||||
@@ -373,6 +373,51 @@ describe("postBranding", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Double downvote title and thumbnail should only count once", async () => {
|
||||||
|
const videoID = "postBrand5";
|
||||||
|
const title = {
|
||||||
|
title: "Some other title",
|
||||||
|
original: false
|
||||||
|
};
|
||||||
|
const thumbnail = {
|
||||||
|
timestamp: 13.42,
|
||||||
|
original: false
|
||||||
|
};
|
||||||
|
|
||||||
|
const res = await postBranding({
|
||||||
|
title,
|
||||||
|
thumbnail,
|
||||||
|
userID: userID6,
|
||||||
|
service: Service.YouTube,
|
||||||
|
videoID,
|
||||||
|
downvote: true
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
const dbTitles = await queryTitleByVideo(videoID, true);
|
||||||
|
for (const dbTitle of dbTitles) {
|
||||||
|
if (dbTitle.title === title.title) {
|
||||||
|
const dbTitleVotes = await queryTitleVotesByUUID(dbTitle.UUID);
|
||||||
|
assert.strictEqual(dbTitleVotes.votes, 0);
|
||||||
|
assert.strictEqual(dbTitleVotes.downvotes, 1);
|
||||||
|
assert.strictEqual(dbTitleVotes.locked, 0);
|
||||||
|
assert.strictEqual(dbTitleVotes.shadowHidden, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const dbThumbnails = await queryThumbnailByVideo(videoID, true);
|
||||||
|
for (const dbThumbnail of dbThumbnails) {
|
||||||
|
if (dbThumbnail.timestamp === thumbnail.timestamp) {
|
||||||
|
const dbThumbnailVotes = await queryThumbnailVotesByUUID(dbThumbnail.UUID);
|
||||||
|
|
||||||
|
assert.strictEqual(dbThumbnailVotes.votes, 0);
|
||||||
|
assert.strictEqual(dbThumbnailVotes.downvotes, 1);
|
||||||
|
assert.strictEqual(dbThumbnailVotes.locked, 0);
|
||||||
|
assert.strictEqual(dbThumbnailVotes.shadowHidden, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
it("Downvote your own title and thumbnail", async () => {
|
it("Downvote your own title and thumbnail", async () => {
|
||||||
const videoID = "postBrand5";
|
const videoID = "postBrand5";
|
||||||
const title = {
|
const title = {
|
||||||
|
|||||||
Reference in New Issue
Block a user