Files
SponsorBlockServer/src/routes/getChapterNames.ts
mini-bomba 3f026409cd Don't show completely unrelated chapter suggestions
Chapter suggestions should be at least slightly related to what the user
has already typed.
This change stops the server from sending suggestions that postgresql
deems to be "less than 10% similar"

Also modified tests to reflect this change.
2024-07-29 02:26:53 +02:00

48 lines
1.6 KiB
TypeScript

import { Logger } from "../utils/logger";
import { Request, Response } from "express";
import { db } from "../databases/databases";
import { Postgres } from "../databases/Postgres";
export async function getChapterNames(req: Request, res: Response): Promise<Response> {
const description = req.query.description as string;
const channelID = req.query.channelID as string;
if (!description || typeof(description) !== "string"
|| !channelID || typeof(channelID) !== "string") {
return res.sendStatus(400);
}
if (!(db instanceof Postgres)) {
return res.sendStatus(500).json({
message: "Not supported on this instance"
});
}
try {
const descriptions = await db.prepare("all", `
SELECT "description"
FROM "sponsorTimes"
WHERE ("locked" = 1 OR "votes" >= 0) AND "videoID" IN (
SELECT "videoID"
FROM "videoInfo"
WHERE "channelID" = ?
) AND "description" != ''
AND similarity("description", ?) >= 0.1
GROUP BY "description"
ORDER BY SUM("votes"), similarity("description", ?) DESC
LIMIT 5;`
, [channelID, description, description]) as { description: string }[];
if (descriptions?.length > 0) {
return res.status(200).json(descriptions.map(d => ({
description: d.description
})));
}
} catch (err) {
Logger.error(err as string);
return res.sendStatus(500);
}
return res.status(404).json([]);
}