Files
SponsorBlockServer/src/routes/getViewsForUser.ts
2022-09-25 03:29:31 -04:00

33 lines
1011 B
TypeScript

import { db } from "../databases/databases";
import { Request, Response } from "express";
import { getHashCache } from "../utils/getHashCache";
import { Logger } from "../utils/logger";
export async function getViewsForUser(req: Request, res: Response): Promise<Response> {
let userID = req.query.userID as string;
if (userID == undefined) {
//invalid request
return res.sendStatus(400);
}
//hash the userID
userID = await getHashCache(userID);
try {
const row = await db.prepare("get", `SELECT SUM("views") as "viewCount" FROM "sponsorTimes" WHERE "userID" = ?`, [userID], { useReplica: true });
//increase the view count by one
if (row.viewCount != null) {
return res.send({
viewCount: row.viewCount,
});
} else {
return res.sendStatus(404);
}
} catch (err) /* istanbul ignore next */ {
Logger.error(err as string);
return res.sendStatus(500);
}
}