Add option to submit without locking

Also fixes voting for an existing thumbnail not unlocking other thumbnails
This commit is contained in:
Ajay 2024-01-02 19:12:55 -05:00
parent ad439fd368
commit 33dad0a5e4
3 changed files with 50 additions and 10 deletions

View file

@ -31,7 +31,7 @@ interface ExistingVote {
} }
export async function postBranding(req: Request, res: Response) { export async function postBranding(req: Request, res: Response) {
const { videoID, userID, title, thumbnail } = req.body as BrandingSubmission; const { videoID, userID, title, thumbnail, autoLock } = req.body as BrandingSubmission;
const service = getService(req.body.service); const service = getService(req.body.service);
if (!videoID || !userID || userID.length < 30 || !service if (!videoID || !userID || userID.length < 30 || !service
@ -45,6 +45,7 @@ export async function postBranding(req: Request, res: Response) {
try { try {
const hashedUserID = await getHashCache(userID); const hashedUserID = await getHashCache(userID);
const isVip = await isUserVIP(hashedUserID); const isVip = await isUserVIP(hashedUserID);
const shouldLock = isVip && autoLock !== false;
const hashedVideoID = await getHashCache(videoID, 1); const hashedVideoID = await getHashCache(videoID, 1);
const hashedIP = await getHashCache(getIP(req) + config.globalSalt as IPAddress); const hashedIP = await getHashCache(getIP(req) + config.globalSalt as IPAddress);
const isBanned = await checkBanStatus(hashedUserID, hashedIP); const isBanned = await checkBanStatus(hashedUserID, hashedIP);
@ -75,21 +76,25 @@ export async function postBranding(req: Request, res: Response) {
const existingVote = await handleExistingVotes(BrandingType.Title, videoID, hashedUserID, UUID, hashedIP, voteType); const existingVote = await handleExistingVotes(BrandingType.Title, videoID, hashedUserID, UUID, hashedIP, voteType);
if (existingUUID) { if (existingUUID) {
await updateVoteTotals(BrandingType.Title, existingVote, UUID, isVip); await updateVoteTotals(BrandingType.Title, existingVote, UUID, shouldLock);
} else { } else {
await db.prepare("run", `INSERT INTO "titles" ("videoID", "title", "original", "userID", "service", "hashedVideoID", "timeSubmitted", "UUID") VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, await db.prepare("run", `INSERT INTO "titles" ("videoID", "title", "original", "userID", "service", "hashedVideoID", "timeSubmitted", "UUID") VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
[videoID, title.title, title.original ? 1 : 0, hashedUserID, service, hashedVideoID, now, UUID]); [videoID, title.title, title.original ? 1 : 0, hashedUserID, service, hashedVideoID, now, UUID]);
const verificationValue = await getVerificationValue(hashedUserID, isVip); const verificationValue = await getVerificationValue(hashedUserID, isVip);
await db.prepare("run", `INSERT INTO "titleVotes" ("UUID", "votes", "locked", "shadowHidden", "verification") VALUES (?, 0, ?, ?, ?);`, await db.prepare("run", `INSERT INTO "titleVotes" ("UUID", "votes", "locked", "shadowHidden", "verification") VALUES (?, 0, ?, ?, ?);`,
[UUID, isVip ? 1 : 0, isBanned ? 1 : 0, verificationValue]); [UUID, shouldLock ? 1 : 0, isBanned ? 1 : 0, verificationValue]);
await verifyOldSubmissions(hashedUserID, verificationValue); await verifyOldSubmissions(hashedUserID, verificationValue);
} }
if (isVip) { if (isVip) {
// unlock all other titles // unlock all other titles
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]); if (shouldLock) {
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]);
} else {
await db.prepare("run", `UPDATE "titleVotes" as tv SET "locked" = 0 FROM "titles" t WHERE t."videoID" = ?`, [videoID]);
}
} }
sendWebhooks(videoID, UUID).catch((e) => Logger.error(e)); sendWebhooks(videoID, UUID).catch((e) => Logger.error(e));
@ -108,22 +113,26 @@ export async function postBranding(req: Request, res: Response) {
const existingVote = await handleExistingVotes(BrandingType.Thumbnail, videoID, hashedUserID, UUID, hashedIP, voteType); const existingVote = await handleExistingVotes(BrandingType.Thumbnail, videoID, hashedUserID, UUID, hashedIP, voteType);
if (existingUUID) { if (existingUUID) {
await updateVoteTotals(BrandingType.Thumbnail, existingVote, UUID, isVip); await updateVoteTotals(BrandingType.Thumbnail, existingVote, UUID, shouldLock);
} else { } else {
await db.prepare("run", `INSERT INTO "thumbnails" ("videoID", "original", "userID", "service", "hashedVideoID", "timeSubmitted", "UUID") VALUES (?, ?, ?, ?, ?, ?, ?)`, await db.prepare("run", `INSERT INTO "thumbnails" ("videoID", "original", "userID", "service", "hashedVideoID", "timeSubmitted", "UUID") VALUES (?, ?, ?, ?, ?, ?, ?)`,
[videoID, thumbnail.original ? 1 : 0, hashedUserID, service, hashedVideoID, now, UUID]); [videoID, thumbnail.original ? 1 : 0, hashedUserID, service, hashedVideoID, now, UUID]);
await db.prepare("run", `INSERT INTO "thumbnailVotes" ("UUID", "votes", "locked", "shadowHidden") VALUES (?, 0, ?, ?)`, await db.prepare("run", `INSERT INTO "thumbnailVotes" ("UUID", "votes", "locked", "shadowHidden") VALUES (?, 0, ?, ?)`,
[UUID, isVip ? 1 : 0, isBanned ? 1 : 0]); [UUID, shouldLock ? 1 : 0, isBanned ? 1 : 0]);
if (!thumbnail.original) { if (!thumbnail.original) {
await db.prepare("run", `INSERT INTO "thumbnailTimestamps" ("UUID", "timestamp") VALUES (?, ?)`, await db.prepare("run", `INSERT INTO "thumbnailTimestamps" ("UUID", "timestamp") VALUES (?, ?)`,
[UUID, (thumbnail as TimeThumbnailSubmission).timestamp]); [UUID, (thumbnail as TimeThumbnailSubmission).timestamp]);
} }
}
if (isVip) { if (isVip) {
// unlock all other titles // unlock all other titles
if (shouldLock) {
await db.prepare("run", `UPDATE "thumbnailVotes" as tv SET "locked" = 0 FROM "thumbnails" t WHERE tv."UUID" = t."UUID" AND tv."UUID" != ? AND t."videoID" = ?`, [UUID, videoID]); await db.prepare("run", `UPDATE "thumbnailVotes" as tv SET "locked" = 0 FROM "thumbnails" t WHERE tv."UUID" = t."UUID" AND tv."UUID" != ? AND t."videoID" = ?`, [UUID, videoID]);
} else {
await db.prepare("run", `UPDATE "thumbnailVotes" as tv SET "locked" = 0 FROM "thumbnails" t WHERE t."videoID" = ?`, [videoID]);
} }
} }
} }
@ -165,7 +174,7 @@ async function handleExistingVotes(type: BrandingType, videoID: VideoID,
* Only called if an existing vote exists. * Only called if an existing vote exists.
* Will update public vote totals and locked status. * Will update public vote totals and locked status.
*/ */
async function updateVoteTotals(type: BrandingType, existingVote: ExistingVote, UUID: BrandingUUID, isVip: boolean): Promise<void> { async function updateVoteTotals(type: BrandingType, existingVote: ExistingVote, UUID: BrandingUUID, shouldLock: boolean): Promise<void> {
const table = type === BrandingType.Title ? `"titleVotes"` : `"thumbnailVotes"`; const table = type === BrandingType.Title ? `"titleVotes"` : `"thumbnailVotes"`;
// Don't upvote if we vote on the same submission // Don't upvote if we vote on the same submission
@ -173,7 +182,7 @@ async function updateVoteTotals(type: BrandingType, existingVote: ExistingVote,
await db.prepare("run", `UPDATE ${table} SET "votes" = "votes" + 1 WHERE "UUID" = ?`, [UUID]); await db.prepare("run", `UPDATE ${table} SET "votes" = "votes" + 1 WHERE "UUID" = ?`, [UUID]);
} }
if (isVip) { if (shouldLock) {
await db.prepare("run", `UPDATE ${table} SET "locked" = 1 WHERE "UUID" = ?`, [UUID]); await db.prepare("run", `UPDATE ${table} SET "locked" = 1 WHERE "UUID" = ?`, [UUID]);
} }
} }

View file

@ -83,6 +83,7 @@ export interface BrandingSubmission {
videoID: VideoID; videoID: VideoID;
userID: UserID; userID: UserID;
service: Service; service: Service;
autoLock: boolean | undefined;
} }
export interface BrandingSegmentDBResult { export interface BrandingSegmentDBResult {

View file

@ -320,6 +320,36 @@ describe("postBranding", () => {
assert.strictEqual(otherSegmentThumbnailVotes2.locked, 1); assert.strictEqual(otherSegmentThumbnailVotes2.locked, 1);
}); });
it("Submit title and thumbnail as VIP without locking", async () => {
const videoID = "postBrand6";
const title = {
title: "Some title",
original: false
};
const thumbnail = {
timestamp: 12.42,
original: false
};
const res = await postBranding({
title,
thumbnail,
userID: vipUser,
service: Service.YouTube,
videoID,
autoLock: false
});
assert.strictEqual(res.status, 200);
const dbTitle = await queryTitleByVideo(videoID);
const dbTitleVotes = await queryTitleVotesByUUID(dbTitle.UUID);
const dbThumbnail = await queryThumbnailByVideo(videoID);
const dbThumbnailVotes = await queryThumbnailVotesByUUID(dbThumbnail.UUID);
assert.strictEqual(dbTitleVotes.locked, 0);
assert.strictEqual(dbThumbnailVotes.locked, 0);
});
it("Vote the same title again", async () => { it("Vote the same title again", async () => {
const videoID = "postBrand1"; const videoID = "postBrand1";
const title = { const title = {