mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-12 14:37:17 +03:00
169 lines
6.6 KiB
TypeScript
169 lines
6.6 KiB
TypeScript
import {db} from '../databases/databases';
|
|
import {getHash} from '../utils/getHash';
|
|
import {isUserVIP} from '../utils/isUserVIP';
|
|
import {Request, Response} from 'express';
|
|
import {Logger} from '../utils/logger';
|
|
import { HashedUserID, UserID } from '../types/user.model';
|
|
import { getReputation } from '../utils/reputation';
|
|
import { SegmentUUID } from "../types/segments.model";
|
|
|
|
async function dbGetSubmittedSegmentSummary(userID: HashedUserID): Promise<{ minutesSaved: number, segmentCount: number }> {
|
|
try {
|
|
const row = await db.prepare("get", `SELECT SUM((("endTime" - "startTime") / 60) * "views") as "minutesSaved",
|
|
count(*) as "segmentCount" FROM "sponsorTimes"
|
|
WHERE "userID" = ? AND "votes" > -2 AND "shadowHidden" != 1`, [userID]);
|
|
if (row.minutesSaved != null) {
|
|
return {
|
|
minutesSaved: row.minutesSaved,
|
|
segmentCount: row.segmentCount,
|
|
};
|
|
} else {
|
|
return {
|
|
minutesSaved: 0,
|
|
segmentCount: 0,
|
|
};
|
|
}
|
|
} catch (err) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function dbGetIgnoredSegmentCount(userID: HashedUserID): Promise<number> {
|
|
try {
|
|
const row = await db.prepare("get", `SELECT COUNT(*) as "ignoredSegmentCount" FROM "sponsorTimes" WHERE "userID" = ? AND ( "votes" <= -2 OR "shadowHidden" = 1 )`, [userID]);
|
|
return row?.ignoredSegmentCount ?? 0;
|
|
} catch (err) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function dbGetUsername(userID: HashedUserID) {
|
|
try {
|
|
const row = await db.prepare('get', `SELECT "userName" FROM "userNames" WHERE "userID" = ?`, [userID]);
|
|
if (row !== undefined) {
|
|
return row.userName;
|
|
} else {
|
|
//no username yet, just send back the userID
|
|
return userID;
|
|
}
|
|
} catch (err) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function dbGetViewsForUser(userID: HashedUserID) {
|
|
try {
|
|
const row = await db.prepare('get', `SELECT SUM("views") as "viewCount" FROM "sponsorTimes" WHERE "userID" = ? AND "votes" > -2 AND "shadowHidden" != 1`, [userID]);
|
|
return row?.viewCount ?? 0;
|
|
} catch (err) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function dbGetIgnoredViewsForUser(userID: HashedUserID) {
|
|
try {
|
|
const row = await db.prepare('get', `SELECT SUM("views") as "ignoredViewCount" FROM "sponsorTimes" WHERE "userID" = ? AND ( "votes" <= -2 OR "shadowHidden" = 1 )`, [userID]);
|
|
return row?.ignoredViewCount ?? 0;
|
|
} catch (err) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function dbGetWarningsForUser(userID: HashedUserID): Promise<number> {
|
|
try {
|
|
const row = await db.prepare('get', `SELECT COUNT(*) as total FROM "warnings" WHERE "userID" = ? AND "enabled" = 1`, [userID]);
|
|
return row?.total ?? 0;
|
|
} catch (err) {
|
|
Logger.error('Couldn\'t get warnings for user ' + userID + '. returning 0');
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
async function dbGetLastSegmentForUser(userID: HashedUserID): Promise<SegmentUUID> {
|
|
try {
|
|
const row = await db.prepare('get', `SELECT "UUID" FROM "sponsorTimes" WHERE "userID" = ? ORDER BY "timeSubmitted" DESC LIMIT 1`, [userID]);
|
|
return row?.UUID ?? null;
|
|
} catch (err) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function dbGetActiveWarningReasonForUser(userID: HashedUserID): Promise<string> {
|
|
try {
|
|
const row = await db.prepare('get', `SELECT reason FROM "warnings" WHERE "userID" = ? AND "enabled" = 1 ORDER BY "issueTime" DESC LIMIT 1`, [userID]);
|
|
return row?.reason ?? '';
|
|
} catch (err) {
|
|
Logger.error('Couldn\'t get reason for user ' + userID + '. returning blank');
|
|
return '';
|
|
}
|
|
}
|
|
|
|
type cases = Record<string, any>
|
|
|
|
const executeIfFunction = (f: any) =>
|
|
typeof f === 'function' ? f() : f;
|
|
|
|
const objSwitch = (cases: cases) => (defaultCase: string) => (key: string) =>
|
|
Object.prototype.hasOwnProperty.call(cases, key) ? cases[key] : defaultCase;
|
|
|
|
const functionSwitch = (cases: cases) => (defaultCase: string) => (key: string) =>
|
|
executeIfFunction(objSwitch(cases)(defaultCase)(key));
|
|
|
|
const dbGetValue = async (userID: HashedUserID, property: string): Promise<string|SegmentUUID|number> => {
|
|
return functionSwitch({
|
|
userID,
|
|
userName: dbGetUsername(userID),
|
|
ignoredSegmentCount: dbGetIgnoredSegmentCount(userID),
|
|
viewCount: dbGetViewsForUser(userID),
|
|
ignoredViewCount: dbGetIgnoredViewsForUser(userID),
|
|
warnings: dbGetWarningsForUser(userID),
|
|
warningReason: dbGetActiveWarningReasonForUser(userID),
|
|
reputation: getReputation(userID),
|
|
vip: isUserVIP(userID),
|
|
lastSegmentID: dbGetLastSegmentForUser(userID),
|
|
})("")(property);
|
|
};
|
|
|
|
export async function getUserInfo(req: Request, res: Response): Promise<Response> {
|
|
const userID = req.query.userID as UserID;
|
|
const hashedUserID: HashedUserID = userID ? getHash(userID) : req.query.publicUserID as HashedUserID;
|
|
const allProperties: string[] = ["userID", "userName", "minutesSaved", "segmentCount", "ignoredSegmentCount",
|
|
"viewCount", "ignoredViewCount", "warnings", "warningReason", "reputation",
|
|
"vip", "lastSegmentID"];
|
|
let paramValues: string[] = req.query.values
|
|
? JSON.parse(req.query.values as string)
|
|
: req.query.value
|
|
? Array.isArray(req.query.value)
|
|
? req.query.value
|
|
: [req.query.value]
|
|
: allProperties;
|
|
if (!Array.isArray(paramValues)) {
|
|
return res.status(400).send("Invalid values JSON");
|
|
}
|
|
// filter array to only include from allProperties
|
|
paramValues = paramValues.filter(param => allProperties.includes(param));
|
|
if (paramValues.length === 0) {
|
|
// invalid values
|
|
return res.status(400).send("No valid values specified");
|
|
}
|
|
|
|
if (hashedUserID == undefined) {
|
|
//invalid request
|
|
return res.status(400).send('Invalid userID or publicUserID parameter');
|
|
}
|
|
|
|
const segmentsSummary = await dbGetSubmittedSegmentSummary(hashedUserID);
|
|
const responseObj = {} as Record<string, string|SegmentUUID|number>;
|
|
if (segmentsSummary) {
|
|
for (const property of paramValues) {
|
|
responseObj[property] = await dbGetValue(hashedUserID, property);
|
|
}
|
|
// add minutesSaved and segmentCount after to avoid getting overwritten
|
|
if (paramValues.includes("minutesSaved")) { responseObj["minutesSaved"] = segmentsSummary.minutesSaved; }
|
|
if (paramValues.includes("segmentCount")) responseObj["segmentCount"] = segmentsSummary.segmentCount;
|
|
res.send(responseObj);
|
|
} else {
|
|
return res.sendStatus(404);
|
|
}
|
|
}
|