diff --git a/databases/_upgrade_sponsorTimes_35.sql b/databases/_upgrade_sponsorTimes_35.sql new file mode 100644 index 0000000..eb10ee5 --- /dev/null +++ b/databases/_upgrade_sponsorTimes_35.sql @@ -0,0 +1,7 @@ +BEGIN TRANSACTION; + +ALTER TABLE "titleVotes" ADD "verification" INTEGER default 0; + +UPDATE "config" SET value = 35 WHERE key = 'version'; + +COMMIT; \ No newline at end of file diff --git a/src/routes/getBranding.ts b/src/routes/getBranding.ts index 0acb6c8..b6a2131 100644 --- a/src/routes/getBranding.ts +++ b/src/routes/getBranding.ts @@ -24,7 +24,7 @@ enum BrandingSubmissionType { export async function getVideoBranding(res: Response, videoID: VideoID, service: Service, ip: IPAddress): Promise { const getTitles = () => db.prepare( "all", - `SELECT "titles"."title", "titles"."original", "titleVotes"."votes", "titleVotes"."locked", "titleVotes"."shadowHidden", "titles"."UUID", "titles"."videoID", "titles"."hashedVideoID" + `SELECT "titles"."title", "titles"."original", "titleVotes"."votes", "titleVotes"."locked", "titleVotes"."shadowHidden", "titles"."UUID", "titles"."videoID", "titles"."hashedVideoID", "titleVotes"."verification" FROM "titles" JOIN "titleVotes" ON "titles"."UUID" = "titleVotes"."UUID" WHERE "titles"."videoID" = ? AND "titles"."service" = ? AND "titleVotes"."votes" > -2`, [videoID, service], @@ -84,7 +84,7 @@ export async function getVideoBranding(res: Response, videoID: VideoID, service: export async function getVideoBrandingByHash(videoHashPrefix: VideoIDHash, service: Service, ip: IPAddress): Promise> { const getTitles = () => db.prepare( "all", - `SELECT "titles"."title", "titles"."original", "titleVotes"."votes", "titleVotes"."locked", "titleVotes"."shadowHidden", "titles"."UUID", "titles"."videoID", "titles"."hashedVideoID" + `SELECT "titles"."title", "titles"."original", "titleVotes"."votes", "titleVotes"."locked", "titleVotes"."shadowHidden", "titles"."UUID", "titles"."videoID", "titles"."hashedVideoID", "titleVotes"."verification" FROM "titles" JOIN "titleVotes" ON "titles"."UUID" = "titleVotes"."UUID" WHERE "titles"."hashedVideoID" LIKE ? AND "titles"."service" = ? AND "titleVotes"."votes" > -2`, [`${videoHashPrefix}%`, service], @@ -165,15 +165,15 @@ async function filterAndSortBranding(videoID: VideoID, dbTitles: TitleDBResult[] const shouldKeepThumbnails = shouldKeepSubmission(dbThumbnails, BrandingSubmissionType.Thumbnail, ip, cache); const titles = shuffleArray(dbTitles.filter(await shouldKeepTitles)) - .sort((a, b) => b.votes - a.votes) - .sort((a, b) => b.locked - a.locked) .map((r) => ({ title: r.title, original: r.original === 1, - votes: r.votes, + votes: r.votes + r.verification, locked: r.locked === 1, UUID: r.UUID, - })) as TitleResult[]; + })) + .sort((a, b) => b.votes - a.votes) + .sort((a, b) => +b.locked - +a.locked) as TitleResult[]; const thumbnails = shuffleArray(dbThumbnails.filter(await shouldKeepThumbnails)) .sort((a, b) => b.votes - a.votes) diff --git a/src/routes/postBranding.ts b/src/routes/postBranding.ts index 0b5901d..75dc51e 100644 --- a/src/routes/postBranding.ts +++ b/src/routes/postBranding.ts @@ -57,8 +57,8 @@ export async function postBranding(req: Request, res: Response) { 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]); - await db.prepare("run", `INSERT INTO "titleVotes" ("UUID", "votes", "locked", "shadowHidden") VALUES (?, 0, ?, 0);`, - [UUID, isVip ? 1 : 0]); + await db.prepare("run", `INSERT INTO "titleVotes" ("UUID", "votes", "locked", "shadowHidden", "verification") VALUES (?, 0, ?, 0, ?);`, + [UUID, isVip ? 1 : 0, await getVerificationValue(hashedUserID, isVip)]); } if (isVip) { @@ -145,4 +145,15 @@ async function updateVoteTotals(type: BrandingType, existingVote: ExistingVote, if (isVip) { await db.prepare("run", `UPDATE ${table} SET "locked" = 1 WHERE "UUID" = ?`, [UUID]); } +} + +async function getVerificationValue(hashedUserID: HashedUserID, isVip: boolean): Promise { + const voteSum = await db.prepare("get", `SELECT SUM("titleVotes"."votes") as "voteSum" FROM "titles" JOIN "titleVotes" ON "titles"."UUID" = "titleVotes"."UUID" WHERE "titles"."userID" = ?`, [hashedUserID]); + const sbSubmissions = () => db.prepare("get", `SELECT COUNT(*) as count FROM "sponsorTimes" WHERE "userID" = ? AND "votes" > 0 LIMIT 3`, [hashedUserID]); + + if (voteSum.voteSum > 3 || isVip || (await sbSubmissions()).count > 2) { + return 0; + } else { + return -1; + } } \ No newline at end of file diff --git a/src/types/branding.model.ts b/src/types/branding.model.ts index e6bd8c0..4e5febf 100644 --- a/src/types/branding.model.ts +++ b/src/types/branding.model.ts @@ -17,7 +17,8 @@ export interface TitleDBResult extends BrandingDBSubmission { title: string, original: number, votes: number, - locked: number + locked: number, + verification: number } export interface TitleResult { diff --git a/test/cases/getBranding.ts b/test/cases/getBranding.ts index e7e075a..1758cd4 100644 --- a/test/cases/getBranding.ts +++ b/test/cases/getBranding.ts @@ -12,12 +12,14 @@ describe("getBranding", () => { const videoID2ShadowHide = "videoID3"; const videoIDEmpty = "videoID4"; const videoIDRandomTime = "videoID5"; + const videoIDUnverified = "videoID6"; const videoID1Hash = getHash(videoID1, 1).slice(0, 4); const videoID2LockedHash = getHash(videoID2Locked, 1).slice(0, 4); const videoID2ShadowHideHash = getHash(videoID2ShadowHide, 1).slice(0, 4); const videoIDEmptyHash = "aaaa"; const videoIDRandomTimeHash = getHash(videoIDRandomTime, 1).slice(0, 4); + const videoIDUnverifiedHash = getHash(videoIDUnverified, 1).slice(0, 4); const endpoint = "/api/branding"; const getBranding = (params: Record) => client({ @@ -34,7 +36,7 @@ describe("getBranding", () => { before(async () => { const titleQuery = `INSERT INTO "titles" ("videoID", "title", "original", "userID", "service", "hashedVideoID", "timeSubmitted", "UUID") VALUES (?, ?, ?, ?, ?, ?, ?, ?)`; - const titleVotesQuery = `INSERT INTO "titleVotes" ("UUID", "votes", "locked", "shadowHidden") VALUES (?, ?, ?, ?)`; + const titleVotesQuery = `INSERT INTO "titleVotes" ("UUID", "votes", "locked", "shadowHidden", "verification") VALUES (?, ?, ?, ?, ?)`; const thumbnailQuery = `INSERT INTO "thumbnails" ("videoID", "original", "userID", "service", "hashedVideoID", "timeSubmitted", "UUID") VALUES (?, ?, ?, ?, ?, ?, ?)`; const thumbnailTimestampsQuery = `INSERT INTO "thumbnailTimestamps" ("UUID", "timestamp") VALUES (?, ?)`; const thumbnailVotesQuery = `INSERT INTO "thumbnailVotes" ("UUID", "votes", "locked", "shadowHidden") VALUES (?, ?, ?, ?)`; @@ -49,9 +51,9 @@ describe("getBranding", () => { ]); await Promise.all([ - db.prepare("run", titleVotesQuery, ["UUID1", 3, 0, 0]), - db.prepare("run", titleVotesQuery, ["UUID2", 2, 0, 0]), - db.prepare("run", titleVotesQuery, ["UUID3", 1, 0, 0]), + db.prepare("run", titleVotesQuery, ["UUID1", 3, 0, 0, 0]), + db.prepare("run", titleVotesQuery, ["UUID2", 2, 0, 0, 0]), + db.prepare("run", titleVotesQuery, ["UUID3", 1, 0, 0, 0]), db.prepare("run", thumbnailTimestampsQuery, ["UUID1T", 1]), db.prepare("run", thumbnailTimestampsQuery, ["UUID3T", 3]), db.prepare("run", thumbnailVotesQuery, ["UUID1T", 3, 0, 0]), @@ -69,9 +71,9 @@ describe("getBranding", () => { ]); await Promise.all([ - db.prepare("run", titleVotesQuery, ["UUID11", 3, 0, 0]), - db.prepare("run", titleVotesQuery, ["UUID21", 2, 0, 0]), - db.prepare("run", titleVotesQuery, ["UUID31", 1, 1, 0]), + db.prepare("run", titleVotesQuery, ["UUID11", 3, 0, 0, 0]), + db.prepare("run", titleVotesQuery, ["UUID21", 2, 0, 0, 0]), + db.prepare("run", titleVotesQuery, ["UUID31", 1, 1, 0, 0]), db.prepare("run", thumbnailTimestampsQuery, ["UUID11T", 1]), db.prepare("run", thumbnailTimestampsQuery, ["UUID31T", 3]), @@ -90,9 +92,9 @@ describe("getBranding", () => { ]); await Promise.all([ - db.prepare("run", titleVotesQuery, ["UUID12", 3, 0, 0]), - db.prepare("run", titleVotesQuery, ["UUID22", 2, 0, 0]), - db.prepare("run", titleVotesQuery, ["UUID32", 1, 0, 1]), + db.prepare("run", titleVotesQuery, ["UUID12", 3, 0, 0, 0]), + db.prepare("run", titleVotesQuery, ["UUID22", 2, 0, 0, 0]), + db.prepare("run", titleVotesQuery, ["UUID32", 1, 0, 1, 0]), db.prepare("run", thumbnailTimestampsQuery, ["UUID12T", 1]), db.prepare("run", thumbnailTimestampsQuery, ["UUID32T", 3]), db.prepare("run", thumbnailVotesQuery, ["UUID12T", 3, 0, 0]), @@ -103,6 +105,26 @@ describe("getBranding", () => { const query = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "service", "videoDuration", "hidden", "shadowHidden", "description", "hashedVideoID") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; await db.prepare("run", query, [videoIDRandomTime, 1, 11, 1, 0, "uuidbranding1", "testman", 0, 50, "sponsor", "skip", "YouTube", 100, 0, 0, "", videoIDRandomTimeHash]); await db.prepare("run", query, [videoIDRandomTime, 20, 33, 2, 0, "uuidbranding2", "testman", 0, 50, "intro", "skip", "YouTube", 100, 0, 0, "", videoIDRandomTimeHash]); + + await Promise.all([ + db.prepare("run", titleQuery, [videoIDUnverified, "title1", 0, "userID1", Service.YouTube, videoIDUnverifiedHash, 1, "UUID-uv-1"]), + db.prepare("run", titleQuery, [videoIDUnverified, "title2", 0, "userID2", Service.YouTube, videoIDUnverifiedHash, 1, "UUID-uv-2"]), + db.prepare("run", titleQuery, [videoIDUnverified, "title3", 1, "userID3", Service.YouTube, videoIDUnverifiedHash, 1, "UUID-uv-3"]), + db.prepare("run", thumbnailQuery, [videoIDUnverified, 0, "userID1", Service.YouTube, videoIDUnverifiedHash, 1, "UUID-uv-1T"]), + db.prepare("run", thumbnailQuery, [videoIDUnverified, 1, "userID2", Service.YouTube, videoIDUnverifiedHash, 1, "UUID-uv-2T"]), + db.prepare("run", thumbnailQuery, [videoIDUnverified, 0, "userID3", Service.YouTube, videoIDUnverifiedHash, 1, "UUID-uv-3T"]), + ]); + + await Promise.all([ + db.prepare("run", titleVotesQuery, ["UUID-uv-1", 3, 0, 0, -1]), + db.prepare("run", titleVotesQuery, ["UUID-uv-2", 2, 0, 0, -1]), + db.prepare("run", titleVotesQuery, ["UUID-uv-3", 0, 0, 0, -1]), + db.prepare("run", thumbnailTimestampsQuery, ["UUID-uv-1T", 1]), + db.prepare("run", thumbnailTimestampsQuery, ["UUID-uv-3T", 3]), + db.prepare("run", thumbnailVotesQuery, ["UUID-uv-1T", 3, 0, 0]), + db.prepare("run", thumbnailVotesQuery, ["UUID-uv-2T", 2, 0, 0]), + db.prepare("run", thumbnailVotesQuery, ["UUID-uv-3T", 1, 0, 0]) + ]); }); it("should get top titles and thumbnails", async () => { @@ -243,6 +265,48 @@ describe("getBranding", () => { assert.strictEqual(result1.data.videoDuration, 100); }); + it("should get top titles and thumbnails that are unverified", async () => { + await checkVideo(videoIDUnverified, videoIDUnverifiedHash, { + titles: [{ + title: "title1", + original: false, + votes: 2, + locked: false, + UUID: "UUID-uv-1" as BrandingUUID + }, { + title: "title2", + original: false, + votes: 1, + locked: false, + UUID: "UUID-uv-2" as BrandingUUID + }, { + title: "title3", + original: true, + votes: -1, + locked: false, + UUID: "UUID-uv-3" as BrandingUUID + }], + thumbnails: [{ + timestamp: 1, + original: false, + votes: 3, + locked: false, + UUID: "UUID-uv-1T" as BrandingUUID + }, { + original: true, + votes: 2, + locked: false, + UUID: "UUID-uv-2T" as BrandingUUID + }, { + timestamp: 3, + original: false, + votes: 1, + locked: false, + UUID: "UUID-uv-3T" as BrandingUUID + }] + }); + }); + async function checkVideo(videoID: string, videoIDHash: string, expected: { titles: TitleResult[], thumbnails: ThumbnailResult[] diff --git a/test/cases/postBranding.ts b/test/cases/postBranding.ts index 88ab6d6..5e35038 100644 --- a/test/cases/postBranding.ts +++ b/test/cases/postBranding.ts @@ -12,7 +12,11 @@ describe("postBranding", () => { const userID2 = `PostBrandingUser2${".".repeat(16)}`; const userID3 = `PostBrandingUser3${".".repeat(16)}`; const userID4 = `PostBrandingUser4${".".repeat(16)}`; - const userID5 = `PostBrandingUser4${".".repeat(16)}`; + const userID5 = `PostBrandingUser5${".".repeat(16)}`; + const userID6 = `PostBrandingUser6${".".repeat(16)}`; + const userID7 = `PostBrandingUser7${".".repeat(16)}`; + const userID8 = `PostBrandingUser8${".".repeat(16)}`; + const endpoint = "/api/branding"; const postBranding = (data: Record) => client({ @@ -35,9 +39,9 @@ describe("postBranding", () => { const insertTitleQuery = 'INSERT INTO "titles" ("videoID", "title", "original", "userID", "service", "hashedVideoID", "timeSubmitted", "UUID") VALUES (?, ?, ?, ?, ?, ?, ?, ?)'; await db.prepare("run", insertTitleQuery, ["postBrandLocked1", "Some title", 0, getHash(userID1), Service.YouTube, getHash("postBrandLocked1"), Date.now(), "postBrandLocked1"]); await db.prepare("run", insertTitleQuery, ["postBrandLocked2", "Some title", 1, getHash(userID2), Service.YouTube, getHash("postBrandLocked2"), Date.now(), "postBrandLocked2"]); - const insertTitleVotesQuery = 'INSERT INTO "titleVotes" ("UUID", "votes", "locked", "shadowHidden") VALUES (?, ?, ?, ?);'; - await db.prepare("run", insertTitleVotesQuery, ["postBrandLocked1", 0, 1, 0]); - await db.prepare("run", insertTitleVotesQuery, ["postBrandLocked2", 0, 1, 0]); + const insertTitleVotesQuery = 'INSERT INTO "titleVotes" ("UUID", "votes", "locked", "shadowHidden", "verification") VALUES (?, ?, ?, ?, ?);'; + await db.prepare("run", insertTitleVotesQuery, ["postBrandLocked1", 0, 1, 0, 0]); + await db.prepare("run", insertTitleVotesQuery, ["postBrandLocked2", 0, 1, 0, 0]); const insertThumbnailQuery = 'INSERT INTO "thumbnails" ("videoID", "original", "userID", "service", "hashedVideoID", "timeSubmitted", "UUID") VALUES (?, ?, ?, ?, ?, ?, ?)'; await db.prepare("run", insertThumbnailQuery, ["postBrandLocked1", 0, getHash(userID3), Service.YouTube, getHash("postBrandLocked1"), Date.now(), "postBrandLocked1"]); @@ -45,6 +49,18 @@ describe("postBranding", () => { const insertThumbnailVotesQuery = 'INSERT INTO "thumbnailVotes" ("UUID", "votes", "locked", "shadowHidden") VALUES (?, ?, ?, ?);'; await db.prepare("run", insertThumbnailVotesQuery, ["postBrandLocked1", 0, 1, 0]); await db.prepare("run", insertThumbnailVotesQuery, ["postBrandLocked2", 0, 1, 0]); + + // Verified through title submissions + await db.prepare("run", insertTitleQuery, ["postBrandVerified1", "Some title", 0, getHash(userID7), Service.YouTube, getHash("postBrandVerified1"), Date.now(), "postBrandVerified1"]); + await db.prepare("run", insertTitleQuery, ["postBrandVerified2", "Some title", 1, getHash(userID7), Service.YouTube, getHash("postBrandVerified2"), Date.now(), "postBrandVerified2"]); + await db.prepare("run", insertTitleVotesQuery, ["postBrandVerified1", 5, 0, 0, -1]); + await db.prepare("run", insertTitleVotesQuery, ["postBrandVerified2", -1, 0, 0, -1]); + + // Verified through SponsorBlock submissions + const insertSegment = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "service", "videoDuration", "hidden", "shadowHidden", "description") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; + await db.prepare("run", insertSegment, ["postBrandVerified3", 1, 11, 1, 0, "postBrandVerified3", getHash(userID8), 0, 50, "sponsor", "skip", "YouTube", 100, 0, 0, ""]); + await db.prepare("run", insertSegment, ["postBrandVerified3", 11, 21, 1, 0, "postBrandVerified32", getHash(userID8), 0, 50, "sponsor", "skip", "YouTube", 100, 0, 0, ""]); + await db.prepare("run", insertSegment, ["postBrandVerified3", 21, 31, 1, 0, "postBrandVerified33", getHash(userID8), 0, 50, "sponsor", "skip", "YouTube", 100, 0, 0, ""]); }); it("Submit only title", async () => { @@ -431,4 +447,76 @@ describe("postBranding", () => { assert.strictEqual(oldDBVotes.locked, 0); assert.strictEqual(oldDBVotes.shadowHidden, 0); }); + + it("Submit from unverified user", async () => { + const videoID = "postBrandUnverified"; + const title = { + title: "Some title", + original: false + }; + + const res = await postBranding({ + title, + userID: userID6, + service: Service.YouTube, + videoID + }); + + assert.strictEqual(res.status, 200); + const dbTitle = await queryTitleByVideo(videoID); + const dbVotes = await queryTitleVotesByUUID(dbTitle.UUID); + + assert.strictEqual(dbTitle.title, title.title); + assert.strictEqual(dbTitle.original, title.original ? 1 : 0); + + assert.strictEqual(dbVotes.verification, -1); + }); + + it("Submit from verified user from title submissions", async () => { + const videoID = "postBrandVerified"; + const title = { + title: "Some title", + original: false + }; + + const res = await postBranding({ + title, + userID: userID7, + service: Service.YouTube, + videoID + }); + + assert.strictEqual(res.status, 200); + const dbTitle = await queryTitleByVideo(videoID); + const dbVotes = await queryTitleVotesByUUID(dbTitle.UUID); + + assert.strictEqual(dbTitle.title, title.title); + assert.strictEqual(dbTitle.original, title.original ? 1 : 0); + + assert.strictEqual(dbVotes.verification, 0); + }); + + it("Submit from verified user from SponsorBlock submissions", async () => { + const videoID = "postBrandVerified2-2"; + const title = { + title: "Some title", + original: false + }; + + const res = await postBranding({ + title, + userID: userID8, + service: Service.YouTube, + videoID + }); + + assert.strictEqual(res.status, 200); + const dbTitle = await queryTitleByVideo(videoID); + const dbVotes = await queryTitleVotesByUUID(dbTitle.UUID); + + assert.strictEqual(dbTitle.title, title.title); + assert.strictEqual(dbTitle.original, title.original ? 1 : 0); + + assert.strictEqual(dbVotes.verification, 0); + }); }); \ No newline at end of file