From 6ec80df8f45a0eaf78a3fe191817c92ce484e893 Mon Sep 17 00:00:00 2001 From: Michael C Date: Fri, 7 Jan 2022 03:04:47 -0500 Subject: [PATCH] update getLockReason --- src/routes/getLockCategories.ts | 1 - src/routes/getLockReason.ts | 42 ++++++++++++++++++++--------- src/types/config.model.ts | 1 + src/types/segments.model.ts | 13 +++++---- test/cases/getLockCategories.ts | 4 +-- test/cases/getLockReason.ts | 34 ++++++++++++++++------- test/cases/lockCategoriesRecords.ts | 10 ++++++- 7 files changed, 74 insertions(+), 31 deletions(-) diff --git a/src/routes/getLockCategories.ts b/src/routes/getLockCategories.ts index 245d882..26ed81a 100644 --- a/src/routes/getLockCategories.ts +++ b/src/routes/getLockCategories.ts @@ -13,7 +13,6 @@ export async function getLockCategories(req: Request, res: Response): Promise { + const filterCategories = new Set(); + for (const [key, value] of Object.entries(categorySupportList)) { + for (const type of actionTypes) { + if (value.includes(type)) { + filterCategories.add(key as Category); + } + } + } + return [...filterCategories]; +}; + export async function getLockReason(req: Request, res: Response): Promise { const videoID = req.query.videoID as VideoID; let categories: Category[] = []; + const actionTypes = req.query.actionTypes as ActionType[] || [ActionType.Skip, ActionType.Mute]; + const possibleCategories = filterActionType(actionTypes); try { categories = req.query.categories ? JSON.parse(req.query.categories as string) @@ -31,28 +45,32 @@ export async function getLockReason(req: Request, res: Response): Promise possibleCategoryList.includes(x)); + const searchCategories = (categories.length === 0 ) + ? possibleCategories + : categories.filter(x => + possibleCategories.includes(x)); - if (videoID == undefined) { + if (!videoID || !Array.isArray(actionTypes)) { //invalid request return res.sendStatus(400); } try { // Get existing lock categories markers - const row = await db.prepare("all", 'SELECT "category", "reason", "userID" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category, reason: string, userID: string }[]; + const row = await db.prepare("all", 'SELECT "category", "reason", "actionType", "userID" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category, reason: string, actionType: ActionType, userID: string }[]; // map to object array const locks = []; 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, - userID: lock?.userID || "", - userName: "", - } as lockArray); + if (actionTypes.includes(lock.actionType)) + locks.push({ + category: lock.category, + locked: 1, + reason: lock.reason, + userID: lock?.userID || "", + userName: "", + } as lockArray); userIDs.add(lock.userID); } // all userName from userIDs diff --git a/src/types/config.model.ts b/src/types/config.model.ts index bc74a27..1a5a22f 100644 --- a/src/types/config.model.ts +++ b/src/types/config.model.ts @@ -28,6 +28,7 @@ export interface SBSConfig { readOnly: boolean; webhooks: WebhookConfig[]; categoryList: string[]; + categorySupport: Record; getTopUsersCacheTimeMinutes: number; maxNumberOfActiveWarnings: number; hoursAfterWarningExpires: number; diff --git a/src/types/segments.model.ts b/src/types/segments.model.ts index b3c4356..04c5d1a 100644 --- a/src/types/segments.model.ts +++ b/src/types/segments.model.ts @@ -1,6 +1,6 @@ import { HashedValue } from "./hash.model"; import { SBRecord } from "./lib.model"; -import { UserID } from "./user.model"; +import { HashedUserID, UserID } from "./user.model"; export type SegmentUUID = string & { __segmentUUIDBrand: unknown }; export type VideoID = string & { __videoIDBrand: unknown }; @@ -107,11 +107,14 @@ export enum CategoryActionType { POI } -export interface LockCategory { - category: Category, - reason: string, +export interface DBLock { videoID: VideoID, - userID: UserID + userID: HashedUserID, + actionType: ActionType, + category: Category, + hashedVideoID: VideoIDHash, + reason: string, + service: Service, } export enum SortableFields { diff --git a/test/cases/getLockCategories.ts b/test/cases/getLockCategories.ts index f05a821..a84e405 100644 --- a/test/cases/getLockCategories.ts +++ b/test/cases/getLockCategories.ts @@ -171,7 +171,7 @@ describe("getLockCategories", () => { reason: "1-longer-reason", actionTypes: ["mute"] }; - assert.deepStrictEqual(res.data, expected); + mixedDeepEquals(res.data, expected); done(); }) .catch(err => done(err)); @@ -190,7 +190,7 @@ describe("getLockCategories", () => { reason: "3-longer-reason", actionTypes }; - assert.deepStrictEqual(res.data, expected); + mixedDeepEquals(res.data, expected); done(); }) .catch(err => done(err)); diff --git a/test/cases/getLockReason.ts b/test/cases/getLockReason.ts index c5bdb89..d2292d4 100644 --- a/test/cases/getLockReason.ts +++ b/test/cases/getLockReason.ts @@ -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 { partialDeepEquals } from "../utils/partialDeepEquals"; const endpoint = "/api/lockReason"; @@ -20,12 +21,13 @@ describe("getLockReason", () => { 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, [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"]); + const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "actionType", "category", "reason") VALUES (?, ?, ?, ?, ?)'; + await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "skip", "sponsor", "sponsor-reason"]); + await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "skip", "interaction", "interaction-reason"]); + await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "skip", "preview", "preview-reason"]); + await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "mute", "music_offtopic", "nonmusic-reason"]); + await db.prepare("run", insertLockCategoryQuery, [vipUserID2, "getLockReason", "mute", "outro", "outro-reason"]); + await db.prepare("run", insertLockCategoryQuery, [vipUserID2, "getLockReason", "full", "selfpromo", "selfpromo-reason"]); }); after(async () => { @@ -96,7 +98,7 @@ describe("getLockReason", () => { .catch(err => done(err)); }); - it("should return all categories if none specified", (done) => { + it("should return all skip + mute categories if none specified", (done) => { client.get(endpoint, { params: { videoID: "getLockReason" } }) .then(res => { assert.strictEqual(res.status, 200); @@ -109,10 +111,8 @@ describe("getLockReason", () => { { category: "preview", locked: 1, reason: "preview-reason", userID: vipUserID1, userName: vipUserName1 }, { category: "music_offtopic", locked: 1, reason: "nonmusic-reason", userID: vipUserID1, userName: vipUserName1 }, { category: "filler", locked: 0, reason: "", userID: "", userName: "" }, - { category: "poi_highlight", locked: 0, reason: "", userID: "", userName: "" }, - { category: "chapter", locked: 0, reason: "", userID: "", userName: "" } ]; - assert.deepStrictEqual(res.data, expected); + partialDeepEquals(res.data, expected, false); done(); }) .catch(err => done(err)); @@ -126,4 +126,18 @@ describe("getLockReason", () => { }) .catch(err => done(err)); }); + + it("should be able to get by actionType", (done) => { + client.get(endpoint, { params: { videoID: "getLockReason", actionTypes: ["full"] } }) + .then(res => { + assert.strictEqual(res.status, 200); + const expected = [ + { category: "selfpromo", locked: 1, reason: "sponsor-reason", userID: vipUserID2, userName: vipUserName2 }, + { category: "sponsor", locked: 0, reason: "", userID: "", userName: "" } + ]; + partialDeepEquals(res.data, expected); + done(); + }) + .catch(err => done(err)); + }); }); diff --git a/test/cases/lockCategoriesRecords.ts b/test/cases/lockCategoriesRecords.ts index 5632be6..dbc4969 100644 --- a/test/cases/lockCategoriesRecords.ts +++ b/test/cases/lockCategoriesRecords.ts @@ -1,7 +1,8 @@ import { getHash } from "../../src/utils/getHash"; import { db } from "../../src/databases/databases"; import assert from "assert"; -import { LockCategory } from "../../src/types/segments.model"; +import { UserID } from "../../src/types/user.model"; +import { Category, VideoID } from "../../src/types/segments.model"; import { client } from "../utils/httpClient"; import { partialDeepEquals } from "../utils/partialDeepEquals"; @@ -13,6 +14,13 @@ const stringDeepEquals = (a: string[], b: string[]): boolean => { return result; }; +interface LockCategory { + category: Category, + reason: string, + videoID: VideoID, + userID: UserID +} + const endpoint = "/api/lockCategories"; const submitEndpoint = "/api/skipSegments"; const checkLockCategories = (videoID: string): Promise => db.prepare("all", 'SELECT * FROM "lockCategories" WHERE "videoID" = ?', [videoID]);