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 {