add reason to getLockCategories

This commit is contained in:
Michael C
2021-07-07 00:22:05 -04:00
parent e94d1d4bae
commit 3371c6a099
4 changed files with 47 additions and 22 deletions

View File

@@ -13,11 +13,17 @@ export async function getLockCategories(req: Request, res: Response): Promise<Re
try {
// Get existing lock categories markers
const lockedCategories = await db.prepare('all', 'SELECT "category" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category}[];
if (lockedCategories.length === 0 || !lockedCategories[0]) return res.sendStatus(404);
const row = await db.prepare('all', 'SELECT "category", "reason" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category, reason: string}[];
// map to array in JS becaues of SQL incompatibilities
const categories = Object.values(lockedCategories).map((entry) => entry.category);
const categories = row.map(item => item.category);
if (categories.length === 0 || !categories[0]) return res.sendStatus(404);
// Get existing lock categories markers
const reasons = row.map(item => item.reason);
let longReason = "";
// set longReason if current length is longer
reasons.forEach((e) => { if (e.length > longReason.length) longReason = e; });
return res.send({
reason: longReason,
categories
});
} catch (err) {

View File

@@ -7,13 +7,15 @@ import { Category, VideoID, VideoIDHash } from "../types/segments.model";
interface LockResultByHash {
videoID: VideoID,
hash: VideoIDHash,
reason: string,
categories: Category[]
}
interface DBLock {
videoID: VideoID,
hash: VideoIDHash,
category: Category
category: Category,
reason: string,
}
const mergeLocks = (source: DBLock[]) => {
@@ -22,12 +24,15 @@ const mergeLocks = (source: DBLock[]) => {
// videoID already exists
const destMatch = dest.find(s => s.videoID === obj.videoID);
if (destMatch) {
// override longer reason
if (destMatch.reason.length > obj.reason.length) destMatch.reason = obj.reason;
// push to categories
destMatch.categories.push(obj.category);
} else {
dest.push({
videoID: obj.videoID,
hash: obj.hash,
reason: obj.reason,
categories: [obj.category]
});
}
@@ -45,7 +50,7 @@ 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" from "lockCategories" where "hashedVideoID" LIKE ?', [hashPrefix + '%']) as DBLock[];
const lockedRows = await db.prepare('all', 'SELECT "videoID", "hashedVideoID" as "hash", "category", "reason" 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));