Only send low voted segments when asked for

This commit is contained in:
Ajay 2024-03-22 18:37:39 -04:00
parent 47c109f012
commit c19d6fe97a
2 changed files with 18 additions and 14 deletions

View file

@ -21,7 +21,7 @@ enum BrandingSubmissionType {
Thumbnail = "thumbnail"
}
export async function getVideoBranding(res: Response, videoID: VideoID, service: Service, ip: IPAddress, returnUserID: boolean): Promise<BrandingResult> {
export async function getVideoBranding(res: Response, videoID: VideoID, service: Service, ip: IPAddress, returnUserID: boolean, fetchAll: boolean): Promise<BrandingResult> {
const getTitles = () => db.prepare(
"all",
`SELECT "titles"."title", "titles"."original", "titleVotes"."votes", "titleVotes"."downvotes", "titleVotes"."locked", "titleVotes"."shadowHidden", "titles"."UUID", "titles"."videoID", "titles"."hashedVideoID", "titleVotes"."verification", "titles"."userID"
@ -83,10 +83,10 @@ export async function getVideoBranding(res: Response, videoID: VideoID, service:
currentIP: null as Promise<HashedIP> | null
};
return filterAndSortBranding(videoID, returnUserID, branding.titles, branding.thumbnails, branding.segments, ip, cache);
return filterAndSortBranding(videoID, returnUserID, fetchAll, branding.titles, branding.thumbnails, branding.segments, ip, cache);
}
export async function getVideoBrandingByHash(videoHashPrefix: VideoIDHash, service: Service, ip: IPAddress, returnUserID: boolean): Promise<Record<VideoID, BrandingResult>> {
export async function getVideoBrandingByHash(videoHashPrefix: VideoIDHash, service: Service, ip: IPAddress, returnUserID: boolean, fetchAll: boolean): Promise<Record<VideoID, BrandingResult>> {
const getTitles = () => db.prepare(
"all",
`SELECT "titles"."title", "titles"."original", "titleVotes"."votes", "titleVotes"."downvotes", "titleVotes"."locked", "titleVotes"."shadowHidden", "titles"."UUID", "titles"."videoID", "titles"."hashedVideoID", "titleVotes"."verification"
@ -158,14 +158,14 @@ export async function getVideoBrandingByHash(videoHashPrefix: VideoIDHash, servi
const processedResult: Record<VideoID, BrandingResult> = {};
await Promise.all(Object.keys(branding).map(async (key) => {
const castedKey = key as VideoID;
processedResult[castedKey] = await filterAndSortBranding(castedKey, returnUserID, branding[castedKey].titles,
processedResult[castedKey] = await filterAndSortBranding(castedKey, returnUserID, fetchAll, branding[castedKey].titles,
branding[castedKey].thumbnails, branding[castedKey].segments, ip, cache);
}));
return processedResult;
}
async function filterAndSortBranding(videoID: VideoID, returnUserID: boolean, dbTitles: TitleDBResult[],
async function filterAndSortBranding(videoID: VideoID, returnUserID: boolean, fetchAll: boolean, dbTitles: TitleDBResult[],
dbThumbnails: ThumbnailDBResult[], dbSegments: BrandingSegmentDBResult[],
ip: IPAddress, cache: { currentIP: Promise<HashedIP> | null }): Promise<BrandingResult> {
@ -181,6 +181,7 @@ async function filterAndSortBranding(videoID: VideoID, returnUserID: boolean, db
UUID: r.UUID,
userID: returnUserID ? r.userID : undefined
}))
.filter((a) => fetchAll || a.votes > 0 || a.locked)
.sort((a, b) => b.votes - a.votes)
.sort((a, b) => +b.locked - +a.locked) as TitleResult[];
@ -195,7 +196,8 @@ async function filterAndSortBranding(videoID: VideoID, returnUserID: boolean, db
locked: r.locked === 1,
UUID: r.UUID,
userID: returnUserID ? r.userID : undefined
})) as ThumbnailResult[];
}))
.filter((a) => fetchAll || a.votes > 0 || a.locked) as ThumbnailResult[];
return {
titles,
@ -280,6 +282,7 @@ export async function getBranding(req: Request, res: Response) {
const videoID: VideoID = req.query.videoID as VideoID;
const service: Service = getService(req.query.service as string);
const returnUserID = req.query.returnUserID === "true";
const fetchAll = req.query.fetchAll === "true";
if (!videoID) {
return res.status(400).send("Missing parameter: videoID");
@ -287,7 +290,7 @@ export async function getBranding(req: Request, res: Response) {
const ip = getIP(req);
try {
const result = await getVideoBranding(res, videoID, service, ip, returnUserID);
const result = await getVideoBranding(res, videoID, service, ip, returnUserID, fetchAll);
const status = result.titles.length > 0 || result.thumbnails.length > 0 ? 200 : 404;
return res.status(status).json(result);
@ -307,9 +310,10 @@ export async function getBrandingByHashEndpoint(req: Request, res: Response) {
const service: Service = getService(req.query.service as string);
const ip = getIP(req);
const returnUserID = req.query.returnUserID === "true";
const fetchAll = req.query.fetchAll === "true";
try {
const result = await getVideoBrandingByHash(hashPrefix, service, ip, returnUserID);
const result = await getVideoBrandingByHash(hashPrefix, service, ip, returnUserID, fetchAll);
const status = !isEmpty(result) ? 200 : 404;
return res.status(status).json(result);

View file

@ -247,8 +247,8 @@ describe("getBranding", () => {
});
it("should get 404 when nothing", async () => {
const result1 = await getBranding({ videoID: videoIDEmpty });
const result2 = await getBrandingByHash(videoIDEmptyHash, {});
const result1 = await getBranding({ videoID: videoIDEmpty, fetchAll: true });
const result2 = await getBrandingByHash(videoIDEmptyHash, { fetchAll: true });
assert.strictEqual(result1.status, 404);
assert.strictEqual(result2.status, 404);
@ -257,8 +257,8 @@ describe("getBranding", () => {
it("should get correct random time", async () => {
const videoDuration = 100;
const result1 = await getBranding({ videoID: videoIDRandomTime });
const result2 = await getBrandingByHash(videoIDRandomTimeHash, {});
const result1 = await getBranding({ videoID: videoIDRandomTime, fetchAll: true });
const result2 = await getBrandingByHash(videoIDRandomTimeHash, { fetchAll: true });
const randomTime = result1.data.randomTime;
assert.strictEqual(randomTime, result2.data[videoIDRandomTime].randomTime);
@ -316,8 +316,8 @@ describe("getBranding", () => {
titles: TitleResult[],
thumbnails: ThumbnailResult[]
}) {
const result1 = await getBranding({ videoID });
const result2 = await getBrandingByHash(videoIDHash, {});
const result1 = await getBranding({ videoID, fetchAll: true });
const result2 = await getBrandingByHash(videoIDHash, { fetchAll: true });
assert.strictEqual(result1.status, 200);
assert.strictEqual(result2.status, 200);