Skip to content

Commit c19d6fe

Browse files
committed
Only send low voted segments when asked for
1 parent 47c109f commit c19d6fe

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/routes/getBranding.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ enum BrandingSubmissionType {
2121
Thumbnail = "thumbnail"
2222
}
2323

24-
export async function getVideoBranding(res: Response, videoID: VideoID, service: Service, ip: IPAddress, returnUserID: boolean): Promise<BrandingResult> {
24+
export async function getVideoBranding(res: Response, videoID: VideoID, service: Service, ip: IPAddress, returnUserID: boolean, fetchAll: boolean): Promise<BrandingResult> {
2525
const getTitles = () => db.prepare(
2626
"all",
2727
`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:
8383
currentIP: null as Promise<HashedIP> | null
8484
};
8585

86-
return filterAndSortBranding(videoID, returnUserID, branding.titles, branding.thumbnails, branding.segments, ip, cache);
86+
return filterAndSortBranding(videoID, returnUserID, fetchAll, branding.titles, branding.thumbnails, branding.segments, ip, cache);
8787
}
8888

89-
export async function getVideoBrandingByHash(videoHashPrefix: VideoIDHash, service: Service, ip: IPAddress, returnUserID: boolean): Promise<Record<VideoID, BrandingResult>> {
89+
export async function getVideoBrandingByHash(videoHashPrefix: VideoIDHash, service: Service, ip: IPAddress, returnUserID: boolean, fetchAll: boolean): Promise<Record<VideoID, BrandingResult>> {
9090
const getTitles = () => db.prepare(
9191
"all",
9292
`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
158158
const processedResult: Record<VideoID, BrandingResult> = {};
159159
await Promise.all(Object.keys(branding).map(async (key) => {
160160
const castedKey = key as VideoID;
161-
processedResult[castedKey] = await filterAndSortBranding(castedKey, returnUserID, branding[castedKey].titles,
161+
processedResult[castedKey] = await filterAndSortBranding(castedKey, returnUserID, fetchAll, branding[castedKey].titles,
162162
branding[castedKey].thumbnails, branding[castedKey].segments, ip, cache);
163163
}));
164164

165165
return processedResult;
166166
}
167167

168-
async function filterAndSortBranding(videoID: VideoID, returnUserID: boolean, dbTitles: TitleDBResult[],
168+
async function filterAndSortBranding(videoID: VideoID, returnUserID: boolean, fetchAll: boolean, dbTitles: TitleDBResult[],
169169
dbThumbnails: ThumbnailDBResult[], dbSegments: BrandingSegmentDBResult[],
170170
ip: IPAddress, cache: { currentIP: Promise<HashedIP> | null }): Promise<BrandingResult> {
171171

@@ -181,6 +181,7 @@ async function filterAndSortBranding(videoID: VideoID, returnUserID: boolean, db
181181
UUID: r.UUID,
182182
userID: returnUserID ? r.userID : undefined
183183
}))
184+
.filter((a) => fetchAll || a.votes > 0 || a.locked)
184185
.sort((a, b) => b.votes - a.votes)
185186
.sort((a, b) => +b.locked - +a.locked) as TitleResult[];
186187

@@ -195,7 +196,8 @@ async function filterAndSortBranding(videoID: VideoID, returnUserID: boolean, db
195196
locked: r.locked === 1,
196197
UUID: r.UUID,
197198
userID: returnUserID ? r.userID : undefined
198-
})) as ThumbnailResult[];
199+
}))
200+
.filter((a) => fetchAll || a.votes > 0 || a.locked) as ThumbnailResult[];
199201

200202
return {
201203
titles,
@@ -280,14 +282,15 @@ export async function getBranding(req: Request, res: Response) {
280282
const videoID: VideoID = req.query.videoID as VideoID;
281283
const service: Service = getService(req.query.service as string);
282284
const returnUserID = req.query.returnUserID === "true";
285+
const fetchAll = req.query.fetchAll === "true";
283286

284287
if (!videoID) {
285288
return res.status(400).send("Missing parameter: videoID");
286289
}
287290

288291
const ip = getIP(req);
289292
try {
290-
const result = await getVideoBranding(res, videoID, service, ip, returnUserID);
293+
const result = await getVideoBranding(res, videoID, service, ip, returnUserID, fetchAll);
291294

292295
const status = result.titles.length > 0 || result.thumbnails.length > 0 ? 200 : 404;
293296
return res.status(status).json(result);
@@ -307,9 +310,10 @@ export async function getBrandingByHashEndpoint(req: Request, res: Response) {
307310
const service: Service = getService(req.query.service as string);
308311
const ip = getIP(req);
309312
const returnUserID = req.query.returnUserID === "true";
313+
const fetchAll = req.query.fetchAll === "true";
310314

311315
try {
312-
const result = await getVideoBrandingByHash(hashPrefix, service, ip, returnUserID);
316+
const result = await getVideoBrandingByHash(hashPrefix, service, ip, returnUserID, fetchAll);
313317

314318
const status = !isEmpty(result) ? 200 : 404;
315319
return res.status(status).json(result);

test/cases/getBranding.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ describe("getBranding", () => {
247247
});
248248

249249
it("should get 404 when nothing", async () => {
250-
const result1 = await getBranding({ videoID: videoIDEmpty });
251-
const result2 = await getBrandingByHash(videoIDEmptyHash, {});
250+
const result1 = await getBranding({ videoID: videoIDEmpty, fetchAll: true });
251+
const result2 = await getBrandingByHash(videoIDEmptyHash, { fetchAll: true });
252252

253253
assert.strictEqual(result1.status, 404);
254254
assert.strictEqual(result2.status, 404);
@@ -257,8 +257,8 @@ describe("getBranding", () => {
257257
it("should get correct random time", async () => {
258258
const videoDuration = 100;
259259

260-
const result1 = await getBranding({ videoID: videoIDRandomTime });
261-
const result2 = await getBrandingByHash(videoIDRandomTimeHash, {});
260+
const result1 = await getBranding({ videoID: videoIDRandomTime, fetchAll: true });
261+
const result2 = await getBrandingByHash(videoIDRandomTimeHash, { fetchAll: true });
262262

263263
const randomTime = result1.data.randomTime;
264264
assert.strictEqual(randomTime, result2.data[videoIDRandomTime].randomTime);
@@ -316,8 +316,8 @@ describe("getBranding", () => {
316316
titles: TitleResult[],
317317
thumbnails: ThumbnailResult[]
318318
}) {
319-
const result1 = await getBranding({ videoID });
320-
const result2 = await getBrandingByHash(videoIDHash, {});
319+
const result1 = await getBranding({ videoID, fetchAll: true });
320+
const result2 = await getBrandingByHash(videoIDHash, { fetchAll: true });
321321

322322
assert.strictEqual(result1.status, 200);
323323
assert.strictEqual(result2.status, 200);

0 commit comments

Comments
 (0)