mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-09 21:17:15 +03:00
Add getService helper function
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { Request, Response } from "express";
|
||||
import { db } from "../databases/databases";
|
||||
import { ActionType, Category, DBSegment, Service, VideoID } from "../types/segments.model";
|
||||
import { getService } from "../utils/getService";
|
||||
const segmentsPerPage = 10;
|
||||
|
||||
type searchSegmentResponse = {
|
||||
@@ -59,10 +60,7 @@ async function handleGetSegments(req: Request, res: Response): Promise<searchSeg
|
||||
return false;
|
||||
}
|
||||
|
||||
let service: Service = req.query.service ?? req.body.service ?? Service.YouTube;
|
||||
if (!Object.values(Service).some((val) => val == service)) {
|
||||
service = Service.YouTube;
|
||||
}
|
||||
const service = getService(req.query.service, req.body.service);
|
||||
|
||||
let page: number = req.query.page ?? req.body.page ?? 0;
|
||||
page = Number(page);
|
||||
|
||||
@@ -10,6 +10,7 @@ import { getIP } from "../utils/getIP";
|
||||
import { Logger } from "../utils/logger";
|
||||
import { QueryCacher } from "../utils/queryCacher";
|
||||
import { getReputation } from "../utils/reputation";
|
||||
import { getService } from "../utils/getService";
|
||||
|
||||
|
||||
async function prepareCategorySegments(req: Request, videoID: VideoID, category: Category, segments: DBSegment[], cache: SegmentCache = {shadowHiddenSegmentIPs: {}}): Promise<Segment[]> {
|
||||
@@ -317,10 +318,7 @@ async function handleGetSegments(req: Request, res: Response): Promise<Segment[]
|
||||
return false;
|
||||
}
|
||||
|
||||
let service: Service = req.query.service ?? req.body.service ?? Service.YouTube;
|
||||
if (!Object.values(Service).some((val) => val == service)) {
|
||||
service = Service.YouTube;
|
||||
}
|
||||
const service = getService(req.query.service, req.body.service);
|
||||
|
||||
const segments = await getSegmentsByVideoID(req, videoID, categories, actionTypes, requiredSegments, service);
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import {hashPrefixTester} from "../utils/hashPrefixTester";
|
||||
import {getSegmentsByHash} from "./getSkipSegments";
|
||||
import {Request, Response} from "express";
|
||||
import { ActionType, Category, SegmentUUID, Service, VideoIDHash } from "../types/segments.model";
|
||||
import { getService } from "../utils/getService";
|
||||
|
||||
export async function getSkipSegmentsByHash(req: Request, res: Response): Promise<Response> {
|
||||
let hashPrefix = req.params.prefix as VideoIDHash;
|
||||
@@ -58,10 +59,7 @@ export async function getSkipSegmentsByHash(req: Request, res: Response): Promis
|
||||
return res.status(400).send("Bad parameter: requiredSegments (invalid JSON)");
|
||||
}
|
||||
|
||||
let service: Service = req.query.service ?? req.body.service ?? Service.YouTube;
|
||||
if (!Object.values(Service).some((val) => val == service)) {
|
||||
service = Service.YouTube;
|
||||
}
|
||||
const service = getService(req.query.service, req.body.service);
|
||||
|
||||
// filter out none string elements, only flat array with strings is valid
|
||||
categories = categories.filter((item: any) => typeof item === "string");
|
||||
|
||||
@@ -6,11 +6,12 @@ import { Service, VideoID } from "../types/segments.model";
|
||||
import { QueryCacher } from "../utils/queryCacher";
|
||||
import { isUserVIP } from "../utils/isUserVIP";
|
||||
import { VideoIDHash } from "../types/segments.model";
|
||||
import { getService } from "../utils/getService";
|
||||
|
||||
export async function postClearCache(req: Request, res: Response): Promise<Response> {
|
||||
const videoID = req.query.videoID as VideoID;
|
||||
const userID = req.query.userID as UserID;
|
||||
const service = req.query.service as Service ?? Service.YouTube;
|
||||
const service = getService(req.query.service as Service);
|
||||
|
||||
const invalidFields = [];
|
||||
if (typeof videoID !== "string") {
|
||||
|
||||
@@ -19,6 +19,7 @@ import { APIVideoData, APIVideoInfo } from "../types/youtubeApi.model";
|
||||
import { UserID } from "../types/user.model";
|
||||
import { isUserVIP } from "../utils/isUserVIP";
|
||||
import { parseUserAgent } from "../utils/userAgent";
|
||||
import { getService } from "../utils/getService";
|
||||
|
||||
type CheckResult = {
|
||||
pass: boolean,
|
||||
@@ -545,10 +546,7 @@ function proxySubmission(req: Request) {
|
||||
function preprocessInput(req: Request) {
|
||||
const videoID = req.query.videoID || req.body.videoID;
|
||||
const userID = req.query.userID || req.body.userID;
|
||||
let service: Service = req.query.service ?? req.body.service ?? Service.YouTube;
|
||||
if (!Object.values(Service).some((val) => val === service)) {
|
||||
service = Service.YouTube;
|
||||
}
|
||||
const service = getService(req.query.service, req.body.service);
|
||||
const videoDurationParam: VideoDuration = (parseFloat(req.query.videoDuration || req.body.videoDuration) || 0) as VideoDuration;
|
||||
const videoDuration = videoDurationParam;
|
||||
|
||||
|
||||
16
src/utils/getService.ts
Normal file
16
src/utils/getService.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Service } from "../types/segments.model";
|
||||
|
||||
export function getService<T extends string>(...value: T[]): Service {
|
||||
for (const name of value) {
|
||||
if (name) {
|
||||
const service = Object.values(Service).find(
|
||||
(val) => val.toLowerCase() === name.trim().toLowerCase()
|
||||
);
|
||||
if (service) {
|
||||
return service;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Service.YouTube;
|
||||
}
|
||||
29
test/cases/getService.ts
Normal file
29
test/cases/getService.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { getService } from "../../src/utils/getService";
|
||||
import { Service } from "../../src/types/segments.model";
|
||||
|
||||
import assert from "assert";
|
||||
|
||||
describe("getService", () => {
|
||||
it("Should return youtube if not match", () => {
|
||||
assert.strictEqual(getService(), Service.YouTube);
|
||||
assert.strictEqual(getService(""), Service.YouTube);
|
||||
assert.strictEqual(getService("test", "not exist"), Service.YouTube);
|
||||
assert.strictEqual(getService(null, null), Service.YouTube);
|
||||
assert.strictEqual(getService(undefined, undefined), Service.YouTube);
|
||||
assert.strictEqual(getService(undefined), Service.YouTube);
|
||||
});
|
||||
|
||||
it("Should return Youtube", () => {
|
||||
assert.strictEqual(getService("youtube"), Service.YouTube);
|
||||
assert.strictEqual(getService(" Youtube "), Service.YouTube);
|
||||
assert.strictEqual(getService(" YouTube "), Service.YouTube);
|
||||
assert.strictEqual(getService(undefined, " YouTube "), Service.YouTube);
|
||||
});
|
||||
|
||||
it("Should return PeerTube", () => {
|
||||
assert.strictEqual(getService("PeerTube"), Service.PeerTube);
|
||||
assert.strictEqual(getService(" PeerTube "), Service.PeerTube);
|
||||
assert.strictEqual(getService(" peertube "), Service.PeerTube);
|
||||
assert.strictEqual(getService(undefined, " PeerTube "), Service.PeerTube);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user