mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-08 04:27:09 +03:00
Write to private db first when rating
This commit is contained in:
@@ -34,8 +34,8 @@ export async function postRating(req: Request, res: Response): Promise<Response>
|
|||||||
const existingVote = await privateDB.prepare("get", `SELECT count(*) as "count" FROM "ratings" WHERE "videoID" = ? AND "service" = ? AND "type" = ? AND "userID" = ?`, [videoID, service, type, hashedUserID]);
|
const existingVote = await privateDB.prepare("get", `SELECT count(*) as "count" FROM "ratings" WHERE "videoID" = ? AND "service" = ? AND "type" = ? AND "userID" = ?`, [videoID, service, type, hashedUserID]);
|
||||||
if (existingVote.count > 0 && !enabled) {
|
if (existingVote.count > 0 && !enabled) {
|
||||||
// Undo the vote
|
// Undo the vote
|
||||||
await db.prepare("run", `UPDATE "ratings" SET "count" = "count" - 1 WHERE "videoID" = ? AND "service" = ? AND type = ?`, [videoID, service, type]);
|
|
||||||
await privateDB.prepare("run", `DELETE FROM "ratings" WHERE "videoID" = ? AND "service" = ? AND "type" = ? AND "userID" = ?`, [videoID, service, type, hashedUserID]);
|
await privateDB.prepare("run", `DELETE FROM "ratings" WHERE "videoID" = ? AND "service" = ? AND "type" = ? AND "userID" = ?`, [videoID, service, type, hashedUserID]);
|
||||||
|
await db.prepare("run", `UPDATE "ratings" SET "count" = "count" - 1 WHERE "videoID" = ? AND "service" = ? AND type = ?`, [videoID, service, type]);
|
||||||
} else if (existingVote.count === 0 && enabled) {
|
} else if (existingVote.count === 0 && enabled) {
|
||||||
// Make sure there hasn't been another vote from this IP
|
// Make sure there hasn't been another vote from this IP
|
||||||
const existingIPVote = (await privateDB.prepare("get", `SELECT count(*) as "count" FROM "ratings" WHERE "videoID" = ? AND "service" = ? AND "type" = ? AND "hashedIP" = ?`, [videoID, service, type, hashedIP]))
|
const existingIPVote = (await privateDB.prepare("get", `SELECT count(*) as "count" FROM "ratings" WHERE "videoID" = ? AND "service" = ? AND "type" = ? AND "hashedIP" = ?`, [videoID, service, type, hashedIP]))
|
||||||
@@ -43,6 +43,10 @@ export async function postRating(req: Request, res: Response): Promise<Response>
|
|||||||
if (existingIPVote) { // if exisiting vote, exit early instead
|
if (existingIPVote) { // if exisiting vote, exit early instead
|
||||||
return res.sendStatus(200);
|
return res.sendStatus(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create entry in privateDB
|
||||||
|
await privateDB.prepare("run", `INSERT INTO "ratings" ("videoID", "service", "type", "userID", "timeSubmitted", "hashedIP") VALUES (?, ?, ?, ?, ?, ?)`, [videoID, service, type, hashedUserID, Date.now(), hashedIP]);
|
||||||
|
|
||||||
// Check if general rating already exists, if so increase it
|
// Check if general rating already exists, if so increase it
|
||||||
const rating = await db.prepare("get", `SELECT count(*) as "count" FROM "ratings" WHERE "videoID" = ? AND "service" = ? AND type = ?`, [videoID, service, type]);
|
const rating = await db.prepare("get", `SELECT count(*) as "count" FROM "ratings" WHERE "videoID" = ? AND "service" = ? AND type = ?`, [videoID, service, type]);
|
||||||
if (rating.count > 0) {
|
if (rating.count > 0) {
|
||||||
@@ -50,9 +54,6 @@ export async function postRating(req: Request, res: Response): Promise<Response>
|
|||||||
} else {
|
} else {
|
||||||
await db.prepare("run", `INSERT INTO "ratings" ("videoID", "service", "type", "count", "hashedVideoID") VALUES (?, ?, ?, 1, ?)`, [videoID, service, type, hashedVideoID]);
|
await db.prepare("run", `INSERT INTO "ratings" ("videoID", "service", "type", "count", "hashedVideoID") VALUES (?, ?, ?, 1, ?)`, [videoID, service, type, hashedVideoID]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create entry in privateDB
|
|
||||||
await privateDB.prepare("run", `INSERT INTO "ratings" ("videoID", "service", "type", "userID", "timeSubmitted", "hashedIP") VALUES (?, ?, ?, ?, ?, ?)`, [videoID, service, type, hashedUserID, Date.now(), hashedIP]);
|
|
||||||
}
|
}
|
||||||
// clear rating cache
|
// clear rating cache
|
||||||
QueryCacher.clearRatingCache({ hashedVideoID, service });
|
QueryCacher.clearRatingCache({ hashedVideoID, service });
|
||||||
|
|||||||
Reference in New Issue
Block a user