mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2024-11-10 01:02:30 +01:00
Only send low voted segments when asked for
This commit is contained in:
parent
47c109f012
commit
c19d6fe97a
2 changed files with 18 additions and 14 deletions
|
@ -21,7 +21,7 @@ enum BrandingSubmissionType {
|
||||||
Thumbnail = "thumbnail"
|
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(
|
const getTitles = () => db.prepare(
|
||||||
"all",
|
"all",
|
||||||
`SELECT "titles"."title", "titles"."original", "titleVotes"."votes", "titleVotes"."downvotes", "titleVotes"."locked", "titleVotes"."shadowHidden", "titles"."UUID", "titles"."videoID", "titles"."hashedVideoID", "titleVotes"."verification", "titles"."userID"
|
`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
|
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(
|
const getTitles = () => db.prepare(
|
||||||
"all",
|
"all",
|
||||||
`SELECT "titles"."title", "titles"."original", "titleVotes"."votes", "titleVotes"."downvotes", "titleVotes"."locked", "titleVotes"."shadowHidden", "titles"."UUID", "titles"."videoID", "titles"."hashedVideoID", "titleVotes"."verification"
|
`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> = {};
|
const processedResult: Record<VideoID, BrandingResult> = {};
|
||||||
await Promise.all(Object.keys(branding).map(async (key) => {
|
await Promise.all(Object.keys(branding).map(async (key) => {
|
||||||
const castedKey = key as VideoID;
|
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);
|
branding[castedKey].thumbnails, branding[castedKey].segments, ip, cache);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return processedResult;
|
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[],
|
dbThumbnails: ThumbnailDBResult[], dbSegments: BrandingSegmentDBResult[],
|
||||||
ip: IPAddress, cache: { currentIP: Promise<HashedIP> | null }): Promise<BrandingResult> {
|
ip: IPAddress, cache: { currentIP: Promise<HashedIP> | null }): Promise<BrandingResult> {
|
||||||
|
|
||||||
|
@ -181,6 +181,7 @@ async function filterAndSortBranding(videoID: VideoID, returnUserID: boolean, db
|
||||||
UUID: r.UUID,
|
UUID: r.UUID,
|
||||||
userID: returnUserID ? r.userID : undefined
|
userID: returnUserID ? r.userID : undefined
|
||||||
}))
|
}))
|
||||||
|
.filter((a) => fetchAll || a.votes > 0 || a.locked)
|
||||||
.sort((a, b) => b.votes - a.votes)
|
.sort((a, b) => b.votes - a.votes)
|
||||||
.sort((a, b) => +b.locked - +a.locked) as TitleResult[];
|
.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,
|
locked: r.locked === 1,
|
||||||
UUID: r.UUID,
|
UUID: r.UUID,
|
||||||
userID: returnUserID ? r.userID : undefined
|
userID: returnUserID ? r.userID : undefined
|
||||||
})) as ThumbnailResult[];
|
}))
|
||||||
|
.filter((a) => fetchAll || a.votes > 0 || a.locked) as ThumbnailResult[];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
titles,
|
titles,
|
||||||
|
@ -280,6 +282,7 @@ export async function getBranding(req: Request, res: Response) {
|
||||||
const videoID: VideoID = req.query.videoID as VideoID;
|
const videoID: VideoID = req.query.videoID as VideoID;
|
||||||
const service: Service = getService(req.query.service as string);
|
const service: Service = getService(req.query.service as string);
|
||||||
const returnUserID = req.query.returnUserID === "true";
|
const returnUserID = req.query.returnUserID === "true";
|
||||||
|
const fetchAll = req.query.fetchAll === "true";
|
||||||
|
|
||||||
if (!videoID) {
|
if (!videoID) {
|
||||||
return res.status(400).send("Missing parameter: 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);
|
const ip = getIP(req);
|
||||||
try {
|
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;
|
const status = result.titles.length > 0 || result.thumbnails.length > 0 ? 200 : 404;
|
||||||
return res.status(status).json(result);
|
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 service: Service = getService(req.query.service as string);
|
||||||
const ip = getIP(req);
|
const ip = getIP(req);
|
||||||
const returnUserID = req.query.returnUserID === "true";
|
const returnUserID = req.query.returnUserID === "true";
|
||||||
|
const fetchAll = req.query.fetchAll === "true";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await getVideoBrandingByHash(hashPrefix, service, ip, returnUserID);
|
const result = await getVideoBrandingByHash(hashPrefix, service, ip, returnUserID, fetchAll);
|
||||||
|
|
||||||
const status = !isEmpty(result) ? 200 : 404;
|
const status = !isEmpty(result) ? 200 : 404;
|
||||||
return res.status(status).json(result);
|
return res.status(status).json(result);
|
||||||
|
|
|
@ -247,8 +247,8 @@ describe("getBranding", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should get 404 when nothing", async () => {
|
it("should get 404 when nothing", async () => {
|
||||||
const result1 = await getBranding({ videoID: videoIDEmpty });
|
const result1 = await getBranding({ videoID: videoIDEmpty, fetchAll: true });
|
||||||
const result2 = await getBrandingByHash(videoIDEmptyHash, {});
|
const result2 = await getBrandingByHash(videoIDEmptyHash, { fetchAll: true });
|
||||||
|
|
||||||
assert.strictEqual(result1.status, 404);
|
assert.strictEqual(result1.status, 404);
|
||||||
assert.strictEqual(result2.status, 404);
|
assert.strictEqual(result2.status, 404);
|
||||||
|
@ -257,8 +257,8 @@ describe("getBranding", () => {
|
||||||
it("should get correct random time", async () => {
|
it("should get correct random time", async () => {
|
||||||
const videoDuration = 100;
|
const videoDuration = 100;
|
||||||
|
|
||||||
const result1 = await getBranding({ videoID: videoIDRandomTime });
|
const result1 = await getBranding({ videoID: videoIDRandomTime, fetchAll: true });
|
||||||
const result2 = await getBrandingByHash(videoIDRandomTimeHash, {});
|
const result2 = await getBrandingByHash(videoIDRandomTimeHash, { fetchAll: true });
|
||||||
|
|
||||||
const randomTime = result1.data.randomTime;
|
const randomTime = result1.data.randomTime;
|
||||||
assert.strictEqual(randomTime, result2.data[videoIDRandomTime].randomTime);
|
assert.strictEqual(randomTime, result2.data[videoIDRandomTime].randomTime);
|
||||||
|
@ -316,8 +316,8 @@ describe("getBranding", () => {
|
||||||
titles: TitleResult[],
|
titles: TitleResult[],
|
||||||
thumbnails: ThumbnailResult[]
|
thumbnails: ThumbnailResult[]
|
||||||
}) {
|
}) {
|
||||||
const result1 = await getBranding({ videoID });
|
const result1 = await getBranding({ videoID, fetchAll: true });
|
||||||
const result2 = await getBrandingByHash(videoIDHash, {});
|
const result2 = await getBrandingByHash(videoIDHash, { fetchAll: true });
|
||||||
|
|
||||||
assert.strictEqual(result1.status, 200);
|
assert.strictEqual(result1.status, 200);
|
||||||
assert.strictEqual(result2.status, 200);
|
assert.strictEqual(result2.status, 200);
|
||||||
|
|
Loading…
Reference in a new issue