mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-06 11:36:58 +03:00
add username in get lock reason route
This commit is contained in:
@@ -8,7 +8,9 @@ const possibleCategoryList = config.categoryList;
|
|||||||
interface lockArray {
|
interface lockArray {
|
||||||
category: Category;
|
category: Category;
|
||||||
locked: number,
|
locked: number,
|
||||||
reason: string
|
reason: string,
|
||||||
|
userID: string,
|
||||||
|
userName: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getLockReason(req: Request, res: Response): Promise<Response> {
|
export async function getLockReason(req: Request, res: Response): Promise<Response> {
|
||||||
@@ -38,31 +40,51 @@ export async function getLockReason(req: Request, res: Response): Promise<Respon
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Get existing lock categories markers
|
// 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
|
// map to object array
|
||||||
const locks = [];
|
const locks = [];
|
||||||
const lockedCategories = [] as string[];
|
const lockedCategories = [] as string[];
|
||||||
|
const userIDs = new Set();
|
||||||
// get all locks for video, check if requested later
|
// get all locks for video, check if requested later
|
||||||
for (const lock of row) {
|
for (const lock of row) {
|
||||||
locks.push({
|
locks.push({
|
||||||
category: lock.category,
|
category: lock.category,
|
||||||
locked: 1,
|
locked: 1,
|
||||||
reason: lock.reason
|
reason: lock.reason,
|
||||||
|
userID: lock?.userID || "",
|
||||||
|
userName: "",
|
||||||
} as lockArray);
|
} as lockArray);
|
||||||
lockedCategories.push(lock.category);
|
lockedCategories.push(lock.category);
|
||||||
|
userIDs.add(lock.userID);
|
||||||
}
|
}
|
||||||
// add empty locks for categories requested but not locked
|
// all userName from userIDs
|
||||||
const noLockCategories = searchCategories.filter(x => !lockedCategories.includes(x));
|
const userNames = await db.prepare(
|
||||||
for (const noLock of noLockCategories) {
|
"all",
|
||||||
locks.push({
|
`SELECT "userName", "userID" FROM "userNames" WHERE "userID" IN (${Array.from("?".repeat(userIDs.size)).join()}) LIMIT ?`,
|
||||||
category: noLock,
|
[...userIDs, userIDs.size]
|
||||||
locked: 0,
|
) as { userName: string, userID: string }[];
|
||||||
reason: ""
|
|
||||||
} as lockArray);
|
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
|
||||||
|
results.push({
|
||||||
|
category,
|
||||||
|
locked: 0,
|
||||||
|
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(results);
|
||||||
return res.send(filtered);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Logger.error(err as string);
|
Logger.error(err as string);
|
||||||
return res.sendStatus(500);
|
return res.sendStatus(500);
|
||||||
|
|||||||
@@ -5,19 +5,33 @@ import { client } from "../utils/httpClient";
|
|||||||
|
|
||||||
const endpoint = "/api/lockReason";
|
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", () => {
|
describe("getLockReason", () => {
|
||||||
before(async () => {
|
before(async () => {
|
||||||
const vipUserID = "getLockReasonVIP";
|
|
||||||
const vipUserHash = getHash(vipUserID);
|
|
||||||
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
|
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
|
||||||
await db.prepare("run", insertVipUserQuery, [vipUserHash]);
|
await db.prepare("run", insertVipUserQuery, [vipUserID1]);
|
||||||
await db.prepare("run", insertVipUserQuery, [vipUserHash]);
|
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 (?, ?, ?, ?)';
|
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, [vipUserID1, "getLockReason", "sponsor", "sponsor-reason"]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [vipUserHash, "getLockReason", "interaction", "interaction-reason"]);
|
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "interaction", "interaction-reason"]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [vipUserHash, "getLockReason", "preview", "preview-reason"]);
|
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "preview", "preview-reason"]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [vipUserHash, "getLockReason", "music_offtopic", "nonmusic-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 () => {
|
it("Should update the database version when starting the application", async () => {
|
||||||
@@ -31,7 +45,7 @@ describe("getLockReason", () => {
|
|||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const expected = [
|
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);
|
assert.deepStrictEqual(res.data, expected);
|
||||||
done();
|
done();
|
||||||
@@ -44,7 +58,7 @@ describe("getLockReason", () => {
|
|||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const expected = [
|
const expected = [
|
||||||
{ category: "intro", locked: 0, reason: "" }
|
{ category: "intro", locked: 0, reason: "", userID: "", userName: "" }
|
||||||
];
|
];
|
||||||
assert.deepStrictEqual(res.data, expected);
|
assert.deepStrictEqual(res.data, expected);
|
||||||
done();
|
done();
|
||||||
@@ -53,12 +67,13 @@ describe("getLockReason", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should get multiple locks with array", (done) => {
|
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 => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const expected = [
|
const expected = [
|
||||||
{ category: "sponsor", locked: 1, reason: "sponsor-reason" },
|
{ category: "intro", locked: 0, reason: "", userID: "", userName: "" },
|
||||||
{ category: "intro", locked: 0, reason: "" }
|
{ 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);
|
assert.deepStrictEqual(res.data, expected);
|
||||||
done();
|
done();
|
||||||
@@ -71,9 +86,9 @@ describe("getLockReason", () => {
|
|||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const expected = [
|
const expected = [
|
||||||
{ category: "interaction", locked: 1, reason: "interaction-reason" },
|
{ category: "interaction", locked: 1, reason: "interaction-reason", userID: vipUserID1, userName: vipUserName1 },
|
||||||
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason" },
|
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason", userID: vipUserID1, userName: vipUserName1 },
|
||||||
{ category: "intro", locked: 0, reason: "" }
|
{ category: "intro", locked: 0, reason: "", userID: "", userName: "" }
|
||||||
];
|
];
|
||||||
assert.deepStrictEqual(res.data, expected);
|
assert.deepStrictEqual(res.data, expected);
|
||||||
done();
|
done();
|
||||||
@@ -86,14 +101,14 @@ describe("getLockReason", () => {
|
|||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const expected = [
|
const expected = [
|
||||||
{ category: "sponsor", locked: 1, reason: "sponsor-reason" },
|
{ category: "sponsor", locked: 1, reason: "sponsor-reason", userID: vipUserID1, userName: vipUserName1 },
|
||||||
{ category: "interaction", locked: 1, reason: "interaction-reason" },
|
{ category: "selfpromo", locked: 0, reason: "", userID: "", userName: "" },
|
||||||
{ category: "preview", locked: 1, reason: "preview-reason" },
|
{ category: "interaction", locked: 1, reason: "interaction-reason", userID: vipUserID1, userName: vipUserName1 },
|
||||||
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason" },
|
{ category: "intro", locked: 0, reason: "", userID: "", userName: "" },
|
||||||
{ category: "selfpromo", locked: 0, reason: "" },
|
{ category: "outro", locked: 1, reason: "outro-reason", userID: vipUserID2, userName: vipUserName2 },
|
||||||
{ category: "intro", locked: 0, reason: "" },
|
{ category: "preview", locked: 1, reason: "preview-reason", userID: vipUserID1, userName: vipUserName1 },
|
||||||
{ category: "outro", locked: 0, reason: "" },
|
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason", userID: vipUserID1, userName: vipUserName1 },
|
||||||
{ category: "poi_highlight", locked: 0, reason: "" }
|
{ category: "poi_highlight", locked: 0, reason: "", userID: "", userName: "" }
|
||||||
];
|
];
|
||||||
assert.deepStrictEqual(res.data, expected);
|
assert.deepStrictEqual(res.data, expected);
|
||||||
done();
|
done();
|
||||||
|
|||||||
Reference in New Issue
Block a user