Merge pull request #444 from mchangrh/full-tests

Tests for full video locks
This commit is contained in:
Ajay Ramachandran
2022-01-16 13:17:19 -05:00
committed by GitHub
10 changed files with 193 additions and 71 deletions

View File

@@ -1,30 +1,32 @@
import { db } from "../databases/databases";
import { Logger } from "../utils/logger";
import { Request, Response } from "express";
import { Category, VideoID } from "../types/segments.model";
import { ActionType, Category, VideoID } from "../types/segments.model";
import { getService } from "../utils/getService";
export async function getLockCategories(req: Request, res: Response): Promise<Response> {
const videoID = req.query.videoID as VideoID;
const service = getService(req.query.service as string);
const actionTypes = req.query.actionTypes as ActionType[] || [ActionType.Skip, ActionType.Mute];
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" from "lockCategories" where "videoID" = ? AND "service" = ?', [videoID, service]) as {category: Category, reason: string}[];
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}[];
const actionTypeMatches = row.filter((lock) => actionTypes.includes(lock.actionType));
// map categories to array in JS becaues of SQL incompatibilities
const categories = row.map(item => item.category);
const categories = actionTypeMatches.map(item => item.category);
if (categories.length === 0 || !categories[0]) return res.sendStatus(404);
// Get longest lock reason
const reason = row.map(item => item.reason)
const reason = actionTypeMatches.map(item => item.reason)
.reduce((a,b) => (a.length > b.length) ? a : b);
return res.send({
reason,
categories
categories,
actionTypes
});
} catch (err) {
Logger.error(err as string);

View File

@@ -2,7 +2,7 @@ import { db } from "../databases/databases";
import { Logger } from "../utils/logger";
import { Request, Response } from "express";
import { hashPrefixTester } from "../utils/hashPrefixTester";
import { Category, VideoID, VideoIDHash } from "../types/segments.model";
import { ActionType, Category, VideoID, VideoIDHash } from "../types/segments.model";
interface LockResultByHash {
videoID: VideoID,
@@ -16,11 +16,13 @@ interface DBLock {
hash: VideoIDHash,
category: Category,
reason: string,
actionType: ActionType,
}
const mergeLocks = (source: DBLock[]) => {
const mergeLocks = (source: DBLock[], actionTypes: ActionType[]) => {
const dest: LockResultByHash[] = [];
for (const obj of source) {
if (!actionTypes.includes(obj.actionType)) continue;
// videoID already exists
const destMatch = dest.find(s => s.videoID === obj.videoID);
if (destMatch) {
@@ -42,6 +44,7 @@ const mergeLocks = (source: DBLock[]) => {
export async function getLockCategoriesByHash(req: Request, res: Response): Promise<Response> {
let hashPrefix = req.params.prefix as VideoIDHash;
const actionTypes = req.query.actionTypes as ActionType[] || [ActionType.Mute, ActionType.Skip];
if (!hashPrefixTester(req.params.prefix)) {
return res.status(400).send("Hash prefix does not match format requirements."); // Exit early on faulty prefix
}
@@ -49,10 +52,10 @@ export async function getLockCategoriesByHash(req: Request, res: Response): Prom
try {
// Get existing lock categories markers
const lockedRows = await db.prepare("all", 'SELECT "videoID", "hashedVideoID" as "hash", "category", "reason" from "lockCategories" where "hashedVideoID" LIKE ?', [`${hashPrefix}%`]) as DBLock[];
const lockedRows = await db.prepare("all", 'SELECT "videoID", "hashedVideoID" as "hash", "category", "reason", "actionType" from "lockCategories" where "hashedVideoID" LIKE ?', [`${hashPrefix}%`]) as DBLock[];
if (lockedRows.length === 0 || !lockedRows[0]) return res.sendStatus(404);
// merge all locks
return res.send(mergeLocks(lockedRows));
return res.send(mergeLocks(lockedRows, actionTypes));
} catch (err) {
Logger.error(err as string);
return res.sendStatus(500);

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

@@ -389,7 +389,7 @@ async function checkEachSegmentValid(rawIP: IPAddress, paramUserID: UserID, user
|| (getCategoryActionType(segments[i].category) === CategoryActionType.POI && startTime !== endTime)
|| (segments[i].actionType === ActionType.Full && (startTime !== 0 || endTime !== 0))) {
//invalid request
return { pass: false, errorMessage: "One of your segments times are invalid (too short, startTime before endTime, etc.)", errorCode: 400 };
return { pass: false, errorMessage: "One of your segments times are invalid (too short, endTime before startTime, etc.)", errorCode: 400 };
}
// Check for POI segments before some seconds