mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2024-11-10 01:02:30 +01:00
Merge branch 'master' of https://github.com/ajayyy/SponsorBlockServer into no-console
This commit is contained in:
commit
5743ed5434
16 changed files with 235 additions and 88 deletions
|
@ -38,5 +38,6 @@ CREATE TABLE IF NOT EXISTS "config" (
|
|||
);
|
||||
|
||||
CREATE EXTENSION IF NOT EXISTS pgcrypto; --!sqlite-ignore
|
||||
CREATE EXTENSION IF NOT EXISTS pg_trgm; --!sqlite-ignore
|
||||
|
||||
COMMIT;
|
|
@ -4,7 +4,6 @@ import { Mysql } from "./Mysql";
|
|||
import { Postgres } from "./Postgres";
|
||||
import { IDatabase } from "./IDatabase";
|
||||
|
||||
|
||||
let db: IDatabase;
|
||||
let privateDB: IDatabase;
|
||||
if (config.mysql) {
|
||||
|
@ -68,6 +67,15 @@ async function initDb(): Promise<void> {
|
|||
// Attach private db to main db
|
||||
(db as Sqlite).attachDatabase(config.privateDB, "privateDB");
|
||||
}
|
||||
|
||||
if (config.mode === "mirror" && db instanceof Postgres) {
|
||||
const tables = config?.dumpDatabase?.tables ?? [];
|
||||
const tableNames = tables.map(table => table.name);
|
||||
for (const table of tableNames) {
|
||||
const filePath = `${config?.dumpDatabase?.postgresExportPath}/${table}.csv`;
|
||||
await db.prepare("run", `COPY "${table}" FROM '${filePath}' WITH (FORMAT CSV, HEADER true);`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
|
|
|
@ -12,6 +12,9 @@ async function init() {
|
|||
});
|
||||
|
||||
await initDb();
|
||||
// edge case clause for creating compatible .db files, do not enable
|
||||
if (config.mode === "init-db-and-exit") process.exit(0);
|
||||
// do not enable init-db-only mode for usage.
|
||||
(global as any).HEADCOMMIT = config.mode === "development" ? "development"
|
||||
: config.mode === "test" ? "test"
|
||||
: getCommit() as string;
|
||||
|
|
|
@ -184,7 +184,7 @@ export async function redirectLink(req: Request, res: Response): Promise<void> {
|
|||
res.sendStatus(404);
|
||||
}
|
||||
|
||||
await queueDump();
|
||||
if (req.query.generate !== "false") await queueDump();
|
||||
}
|
||||
|
||||
function updateQueueTime(): void {
|
||||
|
|
|
@ -8,7 +8,9 @@ const possibleCategoryList = config.categoryList;
|
|||
interface lockArray {
|
||||
category: Category;
|
||||
locked: number,
|
||||
reason: string
|
||||
reason: string,
|
||||
userID: string,
|
||||
userName: string,
|
||||
}
|
||||
|
||||
export async function getLockReason(req: Request, res: Response): Promise<Response> {
|
||||
|
@ -38,31 +40,49 @@ export async function getLockReason(req: Request, res: Response): Promise<Respon
|
|||
|
||||
try {
|
||||
// Get existing lock categories markers
|
||||
const row = await db.prepare("all", 'SELECT "category", "reason" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category, reason: string}[];
|
||||
const row = await db.prepare("all", 'SELECT "category", "reason", "userID" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category, reason: string, userID: string }[];
|
||||
// map to object array
|
||||
const locks = [];
|
||||
const lockedCategories = [] as string[];
|
||||
const userIDs = new Set();
|
||||
// get all locks for video, check if requested later
|
||||
for (const lock of row) {
|
||||
locks.push({
|
||||
category: lock.category,
|
||||
locked: 1,
|
||||
reason: lock.reason
|
||||
reason: lock.reason,
|
||||
userID: lock?.userID || "",
|
||||
userName: "",
|
||||
} as lockArray);
|
||||
lockedCategories.push(lock.category);
|
||||
userIDs.add(lock.userID);
|
||||
}
|
||||
// all userName from userIDs
|
||||
const userNames = await db.prepare(
|
||||
"all",
|
||||
`SELECT "userName", "userID" FROM "userNames" WHERE "userID" IN (${Array.from("?".repeat(userIDs.size)).join()}) LIMIT ?`,
|
||||
[...userIDs, userIDs.size]
|
||||
) as { userName: string, userID: string }[];
|
||||
|
||||
const results = [];
|
||||
for (const category of searchCategories) {
|
||||
const lock = locks.find(l => l.category === category);
|
||||
if (lock?.userID) {
|
||||
// mapping userName to locks
|
||||
const user = userNames.find(u => u.userID === lock.userID);
|
||||
lock.userName = user?.userName || "";
|
||||
results.push(lock);
|
||||
} else {
|
||||
// add empty locks for categories requested but not locked
|
||||
const noLockCategories = searchCategories.filter(x => !lockedCategories.includes(x));
|
||||
for (const noLock of noLockCategories) {
|
||||
locks.push({
|
||||
category: noLock,
|
||||
results.push({
|
||||
category,
|
||||
locked: 0,
|
||||
reason: ""
|
||||
reason: "",
|
||||
userID: "",
|
||||
userName: "",
|
||||
} as lockArray);
|
||||
}
|
||||
// return real and fake locks that were requested
|
||||
const filtered = locks.filter(lock => searchCategories.includes(lock.category));
|
||||
return res.send(filtered);
|
||||
}
|
||||
|
||||
return res.send(results);
|
||||
} catch (err) {
|
||||
Logger.error(err as string);
|
||||
return res.sendStatus(500);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { db } from "../databases/databases";
|
||||
import { Logger } from "../utils/logger";
|
||||
import { Request, Response } from "express";
|
||||
import os from "os";
|
||||
|
||||
export async function getStatus(req: Request, res: Response): Promise<Response> {
|
||||
const startTime = Date.now();
|
||||
|
@ -14,8 +15,9 @@ export async function getStatus(req: Request, res: Response): Promise<Response>
|
|||
db: Number(dbVersion),
|
||||
startTime,
|
||||
processTime: Date.now() - startTime,
|
||||
loadavg: os.loadavg().slice(1) // only return 5 & 15 minute load average
|
||||
};
|
||||
return value ? res.send(String(statusValues[value])) : res.send(statusValues);
|
||||
return value ? res.send(JSON.stringify(statusValues[value])) : res.send(statusValues);
|
||||
} catch (err) {
|
||||
Logger.error(err as string);
|
||||
return res.sendStatus(500);
|
||||
|
|
|
@ -2,6 +2,7 @@ import { Request, Response } from "express";
|
|||
import { Logger } from "../utils/logger";
|
||||
import { isUserVIP } from "../utils/isUserVIP";
|
||||
import { getMaxResThumbnail, YouTubeAPI } from "../utils/youtubeApi";
|
||||
import { APIVideoInfo } from "../types/youtubeApi.model";
|
||||
import { db, privateDB } from "../databases/databases";
|
||||
import { dispatchEvent, getVoteAuthor, getVoteAuthorRaw } from "../utils/webhookUtils";
|
||||
import { getFormattedTime } from "../utils/getFormattedTime";
|
||||
|
@ -9,7 +10,7 @@ import { getIP } from "../utils/getIP";
|
|||
import { getHash } from "../utils/getHash";
|
||||
import { config } from "../config";
|
||||
import { UserID } from "../types/user.model";
|
||||
import { Category, CategoryActionType, HashedIP, IPAddress, SegmentUUID, Service, VideoID, VideoIDHash, Visibility } from "../types/segments.model";
|
||||
import { Category, CategoryActionType, HashedIP, IPAddress, SegmentUUID, Service, VideoID, VideoIDHash, Visibility, VideoDuration } from "../types/segments.model";
|
||||
import { getCategoryActionType } from "../utils/categoryInfo";
|
||||
import { QueryCacher } from "../utils/queryCacher";
|
||||
import axios from "axios";
|
||||
|
@ -48,6 +49,30 @@ interface VoteData {
|
|||
finalResponse: FinalResponse;
|
||||
}
|
||||
|
||||
function getYouTubeVideoInfo(videoID: VideoID, ignoreCache = false): Promise<APIVideoInfo> {
|
||||
if (config.newLeafURLs !== null) {
|
||||
return YouTubeAPI.listVideos(videoID, ignoreCache);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const videoDurationChanged = (segmentDuration: number, APIDuration: number) => (APIDuration > 0 && Math.abs(segmentDuration - APIDuration) > 2);
|
||||
|
||||
async function checkVideoDurationChange(UUID: SegmentUUID) {
|
||||
const { videoDuration, videoID, service } = await db.prepare("get", `select "videoDuration", "videoID", "service" from "sponsorTimes" where "UUID" = ?`, [UUID]);
|
||||
let apiVideoInfo: APIVideoInfo = null;
|
||||
if (service == Service.YouTube) {
|
||||
// don't use cache since we have no information about the video length
|
||||
apiVideoInfo = await getYouTubeVideoInfo(videoID);
|
||||
}
|
||||
const apiVideoDuration = apiVideoInfo?.data?.lengthSeconds as VideoDuration;
|
||||
if (videoDurationChanged(videoDuration, apiVideoDuration)) {
|
||||
Logger.info(`Video duration changed for ${videoID} from ${videoDuration} to ${apiVideoDuration}`);
|
||||
await db.prepare("run", `UPDATE "sponsorTimes" SET "videoDuration" = ? WHERE "UUID" = ?`, [apiVideoDuration, UUID]);
|
||||
}
|
||||
}
|
||||
|
||||
async function sendWebhooks(voteData: VoteData) {
|
||||
const submissionInfoRow = await db.prepare("get", `SELECT "s"."videoID", "s"."userID", s."startTime", s."endTime", s."category", u."userName",
|
||||
(select count(1) from "sponsorTimes" where "userID" = s."userID") count,
|
||||
|
@ -436,6 +461,8 @@ export async function voteOnSponsorTime(req: Request, res: Response): Promise<Re
|
|||
//oldIncrementAmount will be zero is row is null
|
||||
await db.prepare("run", `UPDATE "sponsorTimes" SET "${columnName}" = "${columnName}" + ? WHERE "UUID" = ?`, [incrementAmount - oldIncrementAmount, UUID]);
|
||||
if (isVIP && incrementAmount > 0 && voteTypeEnum === voteTypes.normal) {
|
||||
// check for video duration change
|
||||
await checkVideoDurationChange(UUID);
|
||||
// Unhide and Lock this submission
|
||||
await db.prepare("run", 'UPDATE "sponsorTimes" SET locked = 1, hidden = 0, "shadowHidden" = 0 WHERE "UUID" = ?', [UUID]);
|
||||
|
||||
|
|
|
@ -64,6 +64,7 @@ export interface DBSegment {
|
|||
hashedVideoID: VideoIDHash;
|
||||
timeSubmitted: number;
|
||||
userAgent: string;
|
||||
service: Service;
|
||||
}
|
||||
|
||||
export interface OverlappingSegmentGroup {
|
||||
|
|
|
@ -2,6 +2,7 @@ import { getHash } from "../../src/utils/getHash";
|
|||
import { db } from "../../src/databases/databases";
|
||||
import assert from "assert";
|
||||
import { client } from "../utils/httpClient";
|
||||
import { mixedDeepEquals } from "../utils/partialDeepEquals";
|
||||
const endpoint = "/api/lockCategories";
|
||||
const getLockCategories = (videoID: string) => client.get(endpoint, { params: { videoID } });
|
||||
const getLockCategoriesWithService = (videoID: string, service: string) => client.get(endpoint, { params: { videoID, service } });
|
||||
|
@ -12,13 +13,13 @@ describe("getLockCategories", () => {
|
|||
await db.prepare("run", insertVipUserQuery, [getHash("getLockCategoriesVIP")]);
|
||||
|
||||
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason", "service") VALUES (?, ?, ?, ?, ?)';
|
||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLock1", "sponsor", "1-short", "YouTube"]);
|
||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLock1", "interaction", "1-longer-reason", "YouTube"]);
|
||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory1", "sponsor", "1-short", "YouTube"]);
|
||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory1", "interaction", "1-longer-reason", "YouTube"]);
|
||||
|
||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLock2", "preview", "2-reason", "YouTube"]);
|
||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory2", "preview", "2-reason", "YouTube"]);
|
||||
|
||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLock3", "nonmusic", "3-reason", "PeerTube"]);
|
||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLock3", "sponsor", "3-reason", "YouTube"]);
|
||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory3", "nonmusic", "3-reason", "PeerTube"]);
|
||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory3", "sponsor", "3-reason", "YouTube"]);
|
||||
});
|
||||
|
||||
it("Should update the database version when starting the application", async () => {
|
||||
|
@ -27,7 +28,7 @@ describe("getLockCategories", () => {
|
|||
});
|
||||
|
||||
it("Should be able to get multiple locks", (done) => {
|
||||
getLockCategories("getLock1")
|
||||
getLockCategories("getLockCategory1")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = {
|
||||
|
@ -37,14 +38,14 @@ describe("getLockCategories", () => {
|
|||
],
|
||||
reason: "1-longer-reason"
|
||||
};
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
assert.ok(mixedDeepEquals(res.data, expected));
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get single locks", (done) => {
|
||||
getLockCategories("getLock2")
|
||||
getLockCategories("getLockCategory2")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = {
|
||||
|
@ -60,7 +61,7 @@ describe("getLockCategories", () => {
|
|||
});
|
||||
|
||||
it("should return 404 if no lock exists", (done) => {
|
||||
getLockCategories("getLockNull")
|
||||
getLockCategories("getLockCategoryNull")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 404);
|
||||
done();
|
||||
|
@ -78,7 +79,7 @@ describe("getLockCategories", () => {
|
|||
});
|
||||
|
||||
it("Should be able to get multiple locks with service", (done) => {
|
||||
getLockCategoriesWithService("getLock1", "YouTube")
|
||||
getLockCategoriesWithService("getLockCategory1", "YouTube")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = {
|
||||
|
@ -88,14 +89,14 @@ describe("getLockCategories", () => {
|
|||
],
|
||||
reason: "1-longer-reason"
|
||||
};
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
assert.ok(mixedDeepEquals(res.data, expected));
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get single locks with service", (done) => {
|
||||
getLockCategoriesWithService("getLock3", "PeerTube")
|
||||
getLockCategoriesWithService("getLockCategory3", "PeerTube")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = {
|
||||
|
@ -111,7 +112,7 @@ describe("getLockCategories", () => {
|
|||
});
|
||||
|
||||
it("Should be able to get single locks with service", (done) => {
|
||||
getLockCategoriesWithService("getLock3", "Youtube")
|
||||
getLockCategoriesWithService("getLockCategory3", "Youtube")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = {
|
||||
|
@ -127,7 +128,7 @@ describe("getLockCategories", () => {
|
|||
});
|
||||
|
||||
it("should return result from Youtube service if service not match", (done) => {
|
||||
getLockCategoriesWithService("getLock3", "Dailymotion")
|
||||
getLockCategoriesWithService("getLockCategory3", "Dailymotion")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = {
|
||||
|
|
|
@ -5,19 +5,33 @@ import { client } from "../utils/httpClient";
|
|||
|
||||
const endpoint = "/api/lockReason";
|
||||
|
||||
const vipUserName1 = "getLockReason-vipUserName_1";
|
||||
const vipUserID1 = getHash("getLockReason-vipUserID_1");
|
||||
const vipUserName2 = "getLockReason-vipUserName_2";
|
||||
const vipUserID2 = getHash("getLockReason-vipUserID_2");
|
||||
|
||||
describe("getLockReason", () => {
|
||||
before(async () => {
|
||||
const vipUserID = "getLockReasonVIP";
|
||||
const vipUserHash = getHash(vipUserID);
|
||||
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
|
||||
await db.prepare("run", insertVipUserQuery, [vipUserHash]);
|
||||
await db.prepare("run", insertVipUserQuery, [vipUserHash]);
|
||||
await db.prepare("run", insertVipUserQuery, [vipUserID1]);
|
||||
await db.prepare("run", insertVipUserQuery, [vipUserID2]);
|
||||
|
||||
const insertVipUserNameQuery = 'INSERT INTO "userNames" ("userID", "userName") VALUES (?, ?)';
|
||||
await db.prepare("run", insertVipUserNameQuery, [vipUserID1, vipUserName1]);
|
||||
await db.prepare("run", insertVipUserNameQuery, [vipUserID2, vipUserName2]);
|
||||
|
||||
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason") VALUES (?, ?, ?, ?)';
|
||||
await db.prepare("run", insertLockCategoryQuery, [vipUserHash, "getLockReason", "sponsor", "sponsor-reason"]);
|
||||
await db.prepare("run", insertLockCategoryQuery, [vipUserHash, "getLockReason", "interaction", "interaction-reason"]);
|
||||
await db.prepare("run", insertLockCategoryQuery, [vipUserHash, "getLockReason", "preview", "preview-reason"]);
|
||||
await db.prepare("run", insertLockCategoryQuery, [vipUserHash, "getLockReason", "music_offtopic", "nonmusic-reason"]);
|
||||
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "sponsor", "sponsor-reason"]);
|
||||
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "interaction", "interaction-reason"]);
|
||||
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "preview", "preview-reason"]);
|
||||
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "music_offtopic", "nonmusic-reason"]);
|
||||
await db.prepare("run", insertLockCategoryQuery, [vipUserID2, "getLockReason", "outro", "outro-reason"]);
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
const deleteUserNameQuery = 'DELETE FROM "userNames" WHERE "userID" = ? AND "userName" = ? LIMIT 1';
|
||||
await db.prepare("run", deleteUserNameQuery, [vipUserID1, vipUserName1]);
|
||||
await db.prepare("run", deleteUserNameQuery, [vipUserID2, vipUserName2]);
|
||||
});
|
||||
|
||||
it("Should update the database version when starting the application", async () => {
|
||||
|
@ -31,7 +45,7 @@ describe("getLockReason", () => {
|
|||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [
|
||||
{ category: "sponsor", locked: 1, reason: "sponsor-reason" }
|
||||
{ category: "sponsor", locked: 1, reason: "sponsor-reason", userID: vipUserID1, userName: vipUserName1 }
|
||||
];
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
|
@ -44,7 +58,7 @@ describe("getLockReason", () => {
|
|||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [
|
||||
{ category: "intro", locked: 0, reason: "" }
|
||||
{ category: "intro", locked: 0, reason: "", userID: "", userName: "" }
|
||||
];
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
|
@ -53,12 +67,13 @@ describe("getLockReason", () => {
|
|||
});
|
||||
|
||||
it("should get multiple locks with array", (done) => {
|
||||
client.get(endpoint, { params: { videoID: "getLockReason", categories: `["intro","sponsor"]` } })
|
||||
client.get(endpoint, { params: { videoID: "getLockReason", categories: `["intro","sponsor","outro"]` } })
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [
|
||||
{ category: "sponsor", locked: 1, reason: "sponsor-reason" },
|
||||
{ category: "intro", locked: 0, reason: "" }
|
||||
{ category: "intro", locked: 0, reason: "", userID: "", userName: "" },
|
||||
{ category: "sponsor", locked: 1, reason: "sponsor-reason", userID: vipUserID1, userName: vipUserName1 },
|
||||
{ category: "outro", locked: 1, reason: "outro-reason", userID: vipUserID2, userName: vipUserName2 }
|
||||
];
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
|
@ -71,9 +86,9 @@ describe("getLockReason", () => {
|
|||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [
|
||||
{ category: "interaction", locked: 1, reason: "interaction-reason" },
|
||||
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason" },
|
||||
{ category: "intro", locked: 0, reason: "" }
|
||||
{ category: "interaction", locked: 1, reason: "interaction-reason", userID: vipUserID1, userName: vipUserName1 },
|
||||
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason", userID: vipUserID1, userName: vipUserName1 },
|
||||
{ category: "intro", locked: 0, reason: "", userID: "", userName: "" }
|
||||
];
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
|
@ -86,14 +101,14 @@ describe("getLockReason", () => {
|
|||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const expected = [
|
||||
{ category: "sponsor", locked: 1, reason: "sponsor-reason" },
|
||||
{ category: "interaction", locked: 1, reason: "interaction-reason" },
|
||||
{ category: "preview", locked: 1, reason: "preview-reason" },
|
||||
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason" },
|
||||
{ category: "selfpromo", locked: 0, reason: "" },
|
||||
{ category: "intro", locked: 0, reason: "" },
|
||||
{ category: "outro", locked: 0, reason: "" },
|
||||
{ category: "poi_highlight", locked: 0, reason: "" }
|
||||
{ category: "sponsor", locked: 1, reason: "sponsor-reason", userID: vipUserID1, userName: vipUserName1 },
|
||||
{ category: "selfpromo", locked: 0, reason: "", userID: "", userName: "" },
|
||||
{ category: "interaction", locked: 1, reason: "interaction-reason", userID: vipUserID1, userName: vipUserName1 },
|
||||
{ category: "intro", locked: 0, reason: "", userID: "", userName: "" },
|
||||
{ category: "outro", locked: 1, reason: "outro-reason", userID: vipUserID2, userName: vipUserName2 },
|
||||
{ category: "preview", locked: 1, reason: "preview-reason", userID: vipUserID1, userName: vipUserName1 },
|
||||
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason", userID: vipUserID1, userName: vipUserName1 },
|
||||
{ category: "poi_highlight", locked: 0, reason: "", userID: "", userName: "" }
|
||||
];
|
||||
assert.deepStrictEqual(res.data, expected);
|
||||
done();
|
||||
|
|
|
@ -19,6 +19,7 @@ describe("getStatus", () => {
|
|||
assert.strictEqual(data.db, Number(dbVersion));
|
||||
assert.ok(data.startTime);
|
||||
assert.ok(data.processTime >= 0);
|
||||
assert.ok(data.loadavg.length == 2);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
|
@ -74,4 +75,15 @@ describe("getStatus", () => {
|
|||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get loadavg only", (done) => {
|
||||
client.get(`${endpoint}/loadavg`)
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
assert.ok(Number(res.data[0]) >= 0);
|
||||
assert.ok(Number(res.data[1]) >= 0);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { config } from "../../src/config";
|
||||
import { getHash } from "../../src/utils/getHash";
|
||||
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
||||
import { partialDeepEquals, arrayDeepEquals } from "../utils/partialDeepEquals";
|
||||
import { db } from "../../src/databases/databases";
|
||||
import { ImportMock } from "ts-mock-imports";
|
||||
import * as YouTubeAPIModule from "../../src/utils/youtubeApi";
|
||||
|
@ -29,7 +29,7 @@ describe("postSkipSegments", () => {
|
|||
|
||||
const submitUserOneHash = getHash(submitUserOne);
|
||||
const submitVIPuser = `VIPPostSkipUser${".".repeat(16)}`;
|
||||
const warnVideoID = "dQw4w9WgXcF";
|
||||
const warnVideoID = "postSkip2";
|
||||
const badInputVideoID = "dQw4w9WgXcQ";
|
||||
|
||||
const queryDatabase = (videoID: string) => db.prepare("get", `SELECT "startTime", "endTime", "locked", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, [videoID]);
|
||||
|
@ -91,7 +91,7 @@ describe("postSkipSegments", () => {
|
|||
});
|
||||
|
||||
it("Should be able to submit a single time (Params method)", (done) => {
|
||||
const videoID = "dQw4w9WgXcR";
|
||||
const videoID = "postSkip1";
|
||||
postSkipSegmentParam({
|
||||
videoID,
|
||||
startTime: 2,
|
||||
|
@ -125,7 +125,7 @@ describe("postSkipSegments", () => {
|
|||
});
|
||||
|
||||
it("Should be able to submit a single time (JSON method)", (done) => {
|
||||
const videoID = "dQw4w9WgXcF";
|
||||
const videoID = "postSkip2";
|
||||
postSkipSegmentJSON({
|
||||
userID: submitUserOne,
|
||||
videoID,
|
||||
|
@ -150,7 +150,7 @@ describe("postSkipSegments", () => {
|
|||
});
|
||||
|
||||
it("Should be able to submit a single time with an action type (JSON method)", (done) => {
|
||||
const videoID = "dQw4w9WgXcV";
|
||||
const videoID = "postSkip3";
|
||||
postSkipSegmentJSON({
|
||||
userID: submitUserOne,
|
||||
videoID,
|
||||
|
@ -176,7 +176,7 @@ describe("postSkipSegments", () => {
|
|||
});
|
||||
|
||||
it("Should not be able to submit an intro with mute action type (JSON method)", (done) => {
|
||||
const videoID = "dQw4w9WgXpQ";
|
||||
const videoID = "postSkip4";
|
||||
postSkipSegmentJSON({
|
||||
userID: submitUserOne,
|
||||
videoID,
|
||||
|
@ -196,7 +196,7 @@ describe("postSkipSegments", () => {
|
|||
});
|
||||
|
||||
it("Should be able to submit a single time with a duration from the YouTube API (JSON method)", (done) => {
|
||||
const videoID = "dQw4w9WgXZX";
|
||||
const videoID = "postSkip5";
|
||||
postSkipSegmentJSON({
|
||||
userID: submitUserOne,
|
||||
videoID,
|
||||
|
@ -222,7 +222,7 @@ describe("postSkipSegments", () => {
|
|||
});
|
||||
|
||||
it("Should be able to submit a single time with a precise duration close to the one from the YouTube API (JSON method)", (done) => {
|
||||
const videoID = "dQw4w9WgXZH";
|
||||
const videoID = "postSkip6";
|
||||
postSkipSegmentJSON({
|
||||
userID: submitUserOne,
|
||||
videoID,
|
||||
|
@ -331,7 +331,7 @@ describe("postSkipSegments", () => {
|
|||
});
|
||||
|
||||
it("Should be able to submit a single time under a different service (JSON method)", (done) => {
|
||||
const videoID = "dQw4w9WgXcG";
|
||||
const videoID = "postSkip7";
|
||||
postSkipSegmentJSON({
|
||||
userID: submitUserOne,
|
||||
videoID,
|
||||
|
@ -383,7 +383,7 @@ describe("postSkipSegments", () => {
|
|||
});
|
||||
|
||||
it("Should be able to submit multiple times (JSON method)", (done) => {
|
||||
const videoID = "dQw4w9WgXcT";
|
||||
const videoID = "postSkip11";
|
||||
postSkipSegmentJSON({
|
||||
userID: submitUserOne,
|
||||
videoID,
|
||||
|
@ -407,14 +407,14 @@ describe("postSkipSegments", () => {
|
|||
endTime: 60,
|
||||
category: "intro"
|
||||
}];
|
||||
assert.deepStrictEqual(rows, expected);
|
||||
assert.ok(arrayDeepEquals(rows, expected));
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
}).timeout(5000);
|
||||
|
||||
it("Should allow multiple times if total is under 80% of video(JSON method)", (done) => {
|
||||
const videoID = "L_jWHffIx5E";
|
||||
const videoID = "postSkip9";
|
||||
postSkipSegmentJSON({
|
||||
userID: submitUserOne,
|
||||
videoID,
|
||||
|
@ -452,7 +452,7 @@ describe("postSkipSegments", () => {
|
|||
endTime: 170,
|
||||
category: "sponsor"
|
||||
}];
|
||||
assert.deepStrictEqual(rows, expected);
|
||||
assert.ok(arrayDeepEquals(rows, expected));
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
|
@ -505,20 +505,20 @@ describe("postSkipSegments", () => {
|
|||
.then(async res => {
|
||||
assert.strictEqual(res.status, 403);
|
||||
const expected = [{
|
||||
category: "sponsor",
|
||||
startTime: 2000,
|
||||
endTime: 4000
|
||||
category: "interaction",
|
||||
startTime: 0,
|
||||
endTime: 1000
|
||||
}, {
|
||||
category: "sponsor",
|
||||
startTime: 1500,
|
||||
endTime: 2750
|
||||
category: "interaction",
|
||||
startTime: 1001,
|
||||
endTime: 1005
|
||||
}, {
|
||||
category: "sponsor",
|
||||
startTime: 4050,
|
||||
endTime: 4750
|
||||
category: "interaction",
|
||||
startTime: 0,
|
||||
endTime: 5000
|
||||
}];
|
||||
const rows = await queryDatabase(videoID);
|
||||
assert.notDeepStrictEqual(rows, expected);
|
||||
const rows = await db.prepare("all", `SELECT "category", "startTime", "endTime" FROM "sponsorTimes" WHERE "videoID" = ?`, [videoID]);
|
||||
assert.ok(arrayDeepEquals(rows, expected));
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
|
|
|
@ -92,7 +92,6 @@ describe("setUsername", () => {
|
|||
it("Should be able to set username that has never been set", (done) => {
|
||||
postSetUserName(user00PrivateUserID, username00)
|
||||
.then(async res => {
|
||||
console.log(res.data);
|
||||
const usernameInfo = await getUsernameInfo(getHash(user00PrivateUserID));
|
||||
assert.strictEqual(res.status, 200);
|
||||
assert.strictEqual(usernameInfo.userName, username00);
|
||||
|
|
|
@ -56,6 +56,7 @@ describe("voteOnSponsorTime", () => {
|
|||
await db.prepare("run", insertSponsorTimeQuery, ["category-change-test-1", 8, 12, 0, 1, "category-change-uuid-6", categoryChangeUserHash, 0, 50, "intro", 0, 0]);
|
||||
await db.prepare("run", insertSponsorTimeQuery, ["category-change-test-1", 9, 14, 0, 0, "category-change-uuid-7", categoryChangeUserHash, 0, 50, "intro", 0, 0]);
|
||||
await db.prepare("run", insertSponsorTimeQuery, ["category-change-test-1", 7, 12, 0, 1, "category-change-uuid-8", categoryChangeUserHash, 0, 50, "intro", 0, 0]);
|
||||
await db.prepare("run", insertSponsorTimeQuery, ["duration-update", 1, 10, 0, 0, "duration-update-uuid-1", "testman", 0, 0, "intro", 0, 0]);
|
||||
|
||||
const insertWarningQuery = 'INSERT INTO "warnings" ("userID", "issueTime", "issuerUserID", "enabled") VALUES(?, ?, ?, ?)';
|
||||
await db.prepare("run", insertWarningQuery, [warnUser01Hash, now, warnVip01Hash, 1]);
|
||||
|
@ -299,7 +300,6 @@ describe("voteOnSponsorTime", () => {
|
|||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const row = await getSegmentCategory(UUID);
|
||||
console.log(row.category);
|
||||
assert.strictEqual(row.category, category);
|
||||
done();
|
||||
})
|
||||
|
@ -314,7 +314,6 @@ describe("voteOnSponsorTime", () => {
|
|||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const row = await getSegmentCategory(UUID);
|
||||
console.log(row.category);
|
||||
assert.strictEqual(row.category, "intro");
|
||||
done();
|
||||
})
|
||||
|
@ -564,4 +563,15 @@ describe("voteOnSponsorTime", () => {
|
|||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to update stored videoDuration with VIP upvote", (done) => {
|
||||
const UUID = "duration-update-uuid-1";
|
||||
postVote(vipUser, UUID, 1)
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const { videoDuration } = await db.prepare("get", `SELECT "videoDuration" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]);
|
||||
assert.strictEqual(videoDuration, 500);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -22,3 +22,34 @@ export const partialDeepEquals = (actual: Record<string, any>, expected: Record<
|
|||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
export const arrayDeepEquals = (actual: Record<string, any>, expected: Record<string, any>, print = true): boolean => {
|
||||
if (actual.length !== expected.length) return false;
|
||||
let flag = true;
|
||||
const actualString = JSON.stringify(actual);
|
||||
const expectedString = JSON.stringify(expected);
|
||||
// check every value in arr1 for match in arr2
|
||||
actual.every((value: any) => { if (flag && !expectedString.includes(JSON.stringify(value))) flag = false; });
|
||||
// check arr2 for match in arr1
|
||||
expected.every((value: any) => { if (flag && !actualString.includes(JSON.stringify(value))) flag = false; });
|
||||
|
||||
if (!flag && print) printActualExpected(actual, expected);
|
||||
return flag;
|
||||
};
|
||||
|
||||
export const mixedDeepEquals = (actual: Record<string, any>, expected: Record<string, any>, print = true): boolean => {
|
||||
for (const [ key, value ] of Object.entries(expected)) {
|
||||
// if value is object or array, recurse
|
||||
if (Array.isArray(value)) {
|
||||
if (!arrayDeepEquals(actual?.[key], value, false)) {
|
||||
if (print) printActualExpected(actual, expected);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (actual?.[key] !== value) {
|
||||
if (print) printActualExpected(actual, expected);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
|
|
@ -30,6 +30,23 @@ export class YouTubeApiMock {
|
|||
]
|
||||
} as APIVideoData
|
||||
};
|
||||
} else if (obj.id === "duration-update") {
|
||||
return {
|
||||
err: null,
|
||||
data: {
|
||||
title: "Example Title",
|
||||
lengthSeconds: 500,
|
||||
videoThumbnails: [
|
||||
{
|
||||
quality: "maxres",
|
||||
url: "https://sponsor.ajay.app/LogoSponsorBlockSimple256px.png",
|
||||
second__originalUrl:"https://sponsor.ajay.app/LogoSponsorBlockSimple256px.png",
|
||||
width: 1280,
|
||||
height: 720
|
||||
},
|
||||
]
|
||||
} as APIVideoData
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
err: null,
|
||||
|
|
Loading…
Reference in a new issue