mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2026-01-30 06:10:52 +03:00
catch all error in endpoint
This commit is contained in:
@@ -7,7 +7,7 @@ import {postWarning} from "./routes/postWarning";
|
|||||||
import {getIsUserVIP} from "./routes/getIsUserVIP";
|
import {getIsUserVIP} from "./routes/getIsUserVIP";
|
||||||
import {deleteLockCategoriesEndpoint} from "./routes/deleteLockCategories";
|
import {deleteLockCategoriesEndpoint} from "./routes/deleteLockCategories";
|
||||||
import {postLockCategories} from "./routes/postLockCategories";
|
import {postLockCategories} from "./routes/postLockCategories";
|
||||||
import {getUserInfo} from "./routes/getUserInfo";
|
import {endpoint as getUserInfo} from "./routes/getUserInfo";
|
||||||
import {getDaysSavedFormatted} from "./routes/getDaysSavedFormatted";
|
import {getDaysSavedFormatted} from "./routes/getDaysSavedFormatted";
|
||||||
import {getTotalStats} from "./routes/getTotalStats";
|
import {getTotalStats} from "./routes/getTotalStats";
|
||||||
import {getTopUsers} from "./routes/getTopUsers";
|
import {getTopUsers} from "./routes/getTopUsers";
|
||||||
|
|||||||
@@ -8,23 +8,19 @@ import { getReputation } from "../utils/reputation";
|
|||||||
import { SegmentUUID } from "../types/segments.model";
|
import { SegmentUUID } from "../types/segments.model";
|
||||||
|
|
||||||
async function dbGetSubmittedSegmentSummary(userID: HashedUserID): Promise<{ minutesSaved: number, segmentCount: number }> {
|
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",
|
||||||
const row = await db.prepare("get", `SELECT SUM((("endTime" - "startTime") / 60) * "views") as "minutesSaved",
|
count(*) as "segmentCount" FROM "sponsorTimes"
|
||||||
count(*) as "segmentCount" FROM "sponsorTimes"
|
WHERE "userID" = ? AND "votes" > -2 AND "shadowHidden" != 1`, [userID]);
|
||||||
WHERE "userID" = ? AND "votes" > -2 AND "shadowHidden" != 1`, [userID]);
|
if (row.minutesSaved != null) {
|
||||||
if (row.minutesSaved != null) {
|
return {
|
||||||
return {
|
minutesSaved: row.minutesSaved,
|
||||||
minutesSaved: row.minutesSaved,
|
segmentCount: row.segmentCount,
|
||||||
segmentCount: row.segmentCount,
|
};
|
||||||
};
|
} else {
|
||||||
} else {
|
return {
|
||||||
return {
|
minutesSaved: 0,
|
||||||
minutesSaved: 0,
|
segmentCount: 0,
|
||||||
segmentCount: 0,
|
};
|
||||||
};
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +130,7 @@ const dbGetValue = async (userID: HashedUserID, property: string): Promise<strin
|
|||||||
})("")(property);
|
})("")(property);
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function getUserInfo(req: Request, res: Response): Promise<Response> {
|
async function getUserInfo(req: Request, res: Response): Promise<Response> {
|
||||||
const userID = req.query.userID as UserID;
|
const userID = req.query.userID as UserID;
|
||||||
const hashedUserID: HashedUserID = userID ? getHash(userID) : req.query.publicUserID as HashedUserID;
|
const hashedUserID: HashedUserID = userID ? getHash(userID) : req.query.publicUserID as HashedUserID;
|
||||||
const defaultProperties: string[] = ["userID", "userName", "minutesSaved", "segmentCount", "ignoredSegmentCount",
|
const defaultProperties: string[] = ["userID", "userName", "minutesSaved", "segmentCount", "ignoredSegmentCount",
|
||||||
@@ -149,7 +145,7 @@ export async function getUserInfo(req: Request, res: Response): Promise<Response
|
|||||||
: [req.query.value]
|
: [req.query.value]
|
||||||
: defaultProperties;
|
: defaultProperties;
|
||||||
if (!Array.isArray(paramValues)) {
|
if (!Array.isArray(paramValues)) {
|
||||||
return res.status(400).send("Invalid values JSON");
|
return res.status(400).send("Invalid values");
|
||||||
}
|
}
|
||||||
// filter array to only include from allProperties
|
// filter array to only include from allProperties
|
||||||
paramValues = paramValues.filter(param => allProperties.includes(param));
|
paramValues = paramValues.filter(param => allProperties.includes(param));
|
||||||
@@ -165,15 +161,21 @@ export async function getUserInfo(req: Request, res: Response): Promise<Response
|
|||||||
|
|
||||||
const segmentsSummary = await dbGetSubmittedSegmentSummary(hashedUserID);
|
const segmentsSummary = await dbGetSubmittedSegmentSummary(hashedUserID);
|
||||||
const responseObj = {} as Record<string, string|SegmentUUID|number>;
|
const responseObj = {} as Record<string, string|SegmentUUID|number>;
|
||||||
if (segmentsSummary) {
|
for (const property of paramValues) {
|
||||||
for (const property of paramValues) {
|
responseObj[property] = await dbGetValue(hashedUserID, property);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
// 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;
|
||||||
|
return res.send(responseObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function endpoint(req: Request, res: Response): Promise<Response> {
|
||||||
|
try {
|
||||||
|
return await getUserInfo(req, res);
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof SyntaxError) { // catch JSON.parse error
|
||||||
|
return res.status(400).send("Invalid values JSON");
|
||||||
|
} else return res.sendStatus(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -146,7 +146,7 @@ describe("getSegmentInfo", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("Should be able to retreive hidden segment", (done: Done) => {
|
it("Should be able to retreive hidden segment", (done: Done) => {
|
||||||
fetch(`${getbaseURL() }/api/segmentInfo?UUID=${hiddenID}`)
|
fetch(`${getbaseURL()}/api/segmentInfo?UUID=${hiddenID}`)
|
||||||
.then(async res => {
|
.then(async res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
|
|||||||
@@ -250,7 +250,7 @@ describe("getUserInfo", () => {
|
|||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should get ban data for unbanned user (only appears when specifically requested)", (done: Done) => {
|
it("Should get ban data for unbanned user (only appears when specifically requested)", (done: Done) => {
|
||||||
fetch(`${getbaseURL()}/api/userInfo?userID=getuserinfo_notban_01&value=banned`)
|
fetch(`${getbaseURL()}/api/userInfo?userID=getuserinfo_notban_01&value=banned`)
|
||||||
.then(async res => {
|
.then(async res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
@@ -263,4 +263,35 @@ describe("getUserInfo", () => {
|
|||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Should throw 400 on bad json in values", (done: Done) => {
|
||||||
|
fetch(`${getbaseURL()}/api/userInfo?userID=x&values=[userID]`)
|
||||||
|
.then(async res => {
|
||||||
|
assert.strictEqual(res.status, 400);
|
||||||
|
done(); // pass
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should return 200 on userID not found", (done: Done) => {
|
||||||
|
fetch(`${getbaseURL()}/api/userInfo?userID=notused-userid`)
|
||||||
|
.then(async res => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
const data = await res.json();
|
||||||
|
const expected = {
|
||||||
|
minutesSaved: 0,
|
||||||
|
segmentCount: 0,
|
||||||
|
ignoredSegmentCount: 0,
|
||||||
|
viewCount: 0,
|
||||||
|
ignoredViewCount: 0,
|
||||||
|
warnings: 0,
|
||||||
|
warningReason: "",
|
||||||
|
reputation: 0,
|
||||||
|
vip: false,
|
||||||
|
};
|
||||||
|
assert.ok(partialDeepEquals(data, expected));
|
||||||
|
done(); // pass
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user