update getLockReason

This commit is contained in:
Michael C
2022-01-07 03:04:47 -05:00
parent 0584492b8c
commit 6ec80df8f4
7 changed files with 74 additions and 31 deletions

View File

@@ -13,7 +13,6 @@ export async function getLockCategories(req: Request, res: Response): Promise<Re
//invalid request
return res.sendStatus(400);
}
try {
// Get existing lock categories markers
const row = await db.prepare("all", 'SELECT "category", "reason", "actionType" from "lockCategories" where "videoID" = ? AND "service" = ?', [videoID, service]) as {category: Category, reason: string, actionType: ActionType}[];

View File

@@ -1,10 +1,10 @@
import { db } from "../databases/databases";
import { Logger } from "../utils/logger";
import { Request, Response } from "express";
import { Category, VideoID } from "../types/segments.model";
import { Category, VideoID, ActionType } from "../types/segments.model";
import { config } from "../config";
const possibleCategoryList = config.categoryList;
const categorySupportList = config.categorySupport;
interface lockArray {
category: Category;
locked: number,
@@ -13,9 +13,23 @@ interface lockArray {
userName: string,
}
const filterActionType = (actionTypes: ActionType[]) => {
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<Response> {
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<Respon
return res.status(400).send("Bad parameter: categories (invalid JSON)");
}
// only take valid categories
const searchCategories = (categories.length === 0 ) ? possibleCategoryList : categories.filter(x => 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

View File

@@ -28,6 +28,7 @@ export interface SBSConfig {
readOnly: boolean;
webhooks: WebhookConfig[];
categoryList: string[];
categorySupport: Record<string, string[]>;
getTopUsersCacheTimeMinutes: number;
maxNumberOfActiveWarnings: number;
hoursAfterWarningExpires: number;

View File

@@ -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 {

View File

@@ -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));

View File

@@ -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));
});
});

View File

@@ -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<LockCategory[]> => db.prepare("all", 'SELECT * FROM "lockCategories" WHERE "videoID" = ?', [videoID]);