diff --git a/databases/_sponsorTimes_indexes.sql b/databases/_sponsorTimes_indexes.sql index afcd16f..35c0964 100644 --- a/databases/_sponsorTimes_indexes.sql +++ b/databases/_sponsorTimes_indexes.sql @@ -124,6 +124,11 @@ CREATE INDEX IF NOT EXISTS "titles_timeSubmitted" ("timeSubmitted" ASC NULLS LAST) TABLESPACE pg_default; +CREATE INDEX IF NOT EXISTS "titles_votes_timeSubmitted" + ON public."titles" USING btree + ("videoID" COLLATE pg_catalog."default" ASC NULLS LAST, "service" COLLATE pg_catalog."default" ASC NULLS LAST, "votes" DESC NULLS LAST, "timeSubmitted" DESC NULLS LAST) + TABLESPACE pg_default; + CREATE INDEX IF NOT EXISTS "titles_videoID" ON public."titles" USING btree ("videoID" COLLATE pg_catalog."default" ASC NULLS LAST, "service" COLLATE pg_catalog."default" ASC NULLS LAST) @@ -141,6 +146,11 @@ CREATE INDEX IF NOT EXISTS "thumbnails_timeSubmitted" ("timeSubmitted" ASC NULLS LAST) TABLESPACE pg_default; +CREATE INDEX IF NOT EXISTS "thumbnails_votes_timeSubmitted" + ON public."thumbnails" USING btree + ("videoID" COLLATE pg_catalog."default" ASC NULLS LAST, "service" COLLATE pg_catalog."default" ASC NULLS LAST, "votes" DESC NULLS LAST, "timeSubmitted" DESC NULLS LAST) + TABLESPACE pg_default; + CREATE INDEX IF NOT EXISTS "thumbnails_videoID" ON public."thumbnails" USING btree ("videoID" COLLATE pg_catalog."default" ASC NULLS LAST, "service" COLLATE pg_catalog."default" ASC NULLS LAST) diff --git a/src/routes/getBranding.ts b/src/routes/getBranding.ts index fd99638..1499837 100644 --- a/src/routes/getBranding.ts +++ b/src/routes/getBranding.ts @@ -26,7 +26,9 @@ export async function getVideoBranding(res: Response, videoID: VideoID, service: "all", `SELECT "titles"."title", "titles"."original", "titleVotes"."votes", "titleVotes"."locked", "titleVotes"."shadowHidden", "titles"."UUID", "titles"."videoID", "titles"."hashedVideoID", "titleVotes"."verification", "titles"."userID" FROM "titles" JOIN "titleVotes" ON "titles"."UUID" = "titleVotes"."UUID" - WHERE "titles"."videoID" = ? AND "titles"."service" = ? AND "titleVotes"."votes" > -2`, + WHERE "titles"."videoID" = ? AND "titles"."service" = ? AND "titleVotes"."votes" > -2 + GROUP BY "titles"."userID" + ORDER BY "titleVotes"."votes", "titles"."timeSubmitted" DESC`, [videoID, service], { useReplica: true } ) as Promise; @@ -35,7 +37,9 @@ export async function getVideoBranding(res: Response, videoID: VideoID, service: "all", `SELECT "thumbnailTimestamps"."timestamp", "thumbnails"."original", "thumbnailVotes"."votes", "thumbnailVotes"."locked", "thumbnailVotes"."shadowHidden", "thumbnails"."UUID", "thumbnails"."videoID", "thumbnails"."hashedVideoID", "thumbnails"."userID" FROM "thumbnails" LEFT JOIN "thumbnailVotes" ON "thumbnails"."UUID" = "thumbnailVotes"."UUID" LEFT JOIN "thumbnailTimestamps" ON "thumbnails"."UUID" = "thumbnailTimestamps"."UUID" - WHERE "thumbnails"."videoID" = ? AND "thumbnails"."service" = ? AND "thumbnailVotes"."votes" > -2`, + WHERE "thumbnails"."videoID" = ? AND "thumbnails"."service" = ? AND "thumbnailVotes"."votes" > -2 + GROUP BY "thumbnails"."userID" + ORDER BY "thumbnailVotes"."votes", "thumbnails"."timeSubmitted" DESC`, [videoID, service], { useReplica: true } ) as Promise; diff --git a/test/cases/getBranding.ts b/test/cases/getBranding.ts index 1758cd4..7ee4b42 100644 --- a/test/cases/getBranding.ts +++ b/test/cases/getBranding.ts @@ -45,6 +45,8 @@ describe("getBranding", () => { db.prepare("run", titleQuery, [videoID1, "title1", 0, "userID1", Service.YouTube, videoID1Hash, 1, "UUID1"]), db.prepare("run", titleQuery, [videoID1, "title2", 0, "userID2", Service.YouTube, videoID1Hash, 1, "UUID2"]), db.prepare("run", titleQuery, [videoID1, "title3", 1, "userID3", Service.YouTube, videoID1Hash, 1, "UUID3"]), + // Will be ignored since it has the same userID, but less votes + db.prepare("run", titleQuery, [videoID1, "some badly written title", 0, "userID3", Service.YouTube, videoID1Hash, 1, "UUID4"]), db.prepare("run", thumbnailQuery, [videoID1, 0, "userID1", Service.YouTube, videoID1Hash, 1, "UUID1T"]), db.prepare("run", thumbnailQuery, [videoID1, 1, "userID2", Service.YouTube, videoID1Hash, 1, "UUID2T"]), db.prepare("run", thumbnailQuery, [videoID1, 0, "userID3", Service.YouTube, videoID1Hash, 1, "UUID3T"]),