mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-14 07:27:01 +03:00
add option to get ban status from user info
This commit is contained in:
@@ -98,6 +98,15 @@ async function dbGetActiveWarningReasonForUser(userID: HashedUserID): Promise<st
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function dbGetBanned(userID: HashedUserID): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
const row = await db.prepare("get", `SELECT count(*) as "userCount" FROM "shadowBannedUsers" WHERE "userID" = ? LIMIT 1`, [userID]);
|
||||||
|
return row?.userCount > 0 ?? false;
|
||||||
|
} catch (err) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type cases = Record<string, any>
|
type cases = Record<string, any>
|
||||||
|
|
||||||
const executeIfFunction = (f: any) =>
|
const executeIfFunction = (f: any) =>
|
||||||
@@ -118,6 +127,7 @@ const dbGetValue = async (userID: HashedUserID, property: string): Promise<strin
|
|||||||
ignoredViewCount: dbGetIgnoredViewsForUser(userID),
|
ignoredViewCount: dbGetIgnoredViewsForUser(userID),
|
||||||
warnings: dbGetWarningsForUser(userID),
|
warnings: dbGetWarningsForUser(userID),
|
||||||
warningReason: dbGetActiveWarningReasonForUser(userID),
|
warningReason: dbGetActiveWarningReasonForUser(userID),
|
||||||
|
banned: dbGetBanned(userID),
|
||||||
reputation: getReputation(userID),
|
reputation: getReputation(userID),
|
||||||
vip: isUserVIP(userID),
|
vip: isUserVIP(userID),
|
||||||
lastSegmentID: dbGetLastSegmentForUser(userID),
|
lastSegmentID: dbGetLastSegmentForUser(userID),
|
||||||
@@ -127,16 +137,17 @@ const dbGetValue = async (userID: HashedUserID, property: string): Promise<strin
|
|||||||
export async function getUserInfo(req: Request, res: Response): Promise<Response> {
|
export 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 allProperties: string[] = ["userID", "userName", "minutesSaved", "segmentCount", "ignoredSegmentCount",
|
const defaultProperties: string[] = ["userID", "userName", "minutesSaved", "segmentCount", "ignoredSegmentCount",
|
||||||
"viewCount", "ignoredViewCount", "warnings", "warningReason", "reputation",
|
"viewCount", "ignoredViewCount", "warnings", "warningReason", "reputation",
|
||||||
"vip", "lastSegmentID"];
|
"vip", "lastSegmentID"];
|
||||||
|
const allProperties: string[] = [...defaultProperties, "banned"];
|
||||||
let paramValues: string[] = req.query.values
|
let paramValues: string[] = req.query.values
|
||||||
? JSON.parse(req.query.values as string)
|
? JSON.parse(req.query.values as string)
|
||||||
: req.query.value
|
: req.query.value
|
||||||
? Array.isArray(req.query.value)
|
? Array.isArray(req.query.value)
|
||||||
? req.query.value
|
? req.query.value
|
||||||
: [req.query.value]
|
: [req.query.value]
|
||||||
: allProperties;
|
: defaultProperties;
|
||||||
if (!Array.isArray(paramValues)) {
|
if (!Array.isArray(paramValues)) {
|
||||||
return res.status(400).send("Invalid values JSON");
|
return res.status(400).send("Invalid values JSON");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -616,7 +616,7 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
//check to see if this user is shadowbanned
|
//check to see if this user is shadowbanned
|
||||||
const shadowBanRow = await db.prepare("get", `SELECT count(*) as "userCount" FROM "shadowBannedUsers" WHERE "userID" = ?`, [userID]);
|
const shadowBanRow = await db.prepare("get", `SELECT count(*) as "userCount" FROM "shadowBannedUsers" WHERE "userID" = ? LIMIT 1`, [userID]);
|
||||||
|
|
||||||
let shadowBanned = shadowBanRow.userCount;
|
let shadowBanned = shadowBanRow.userCount;
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,9 @@ describe("getUserInfo", () => {
|
|||||||
await db.prepare("run", insertWarningQuery, [getHash("getuserinfo_warning_2"), 40, "getuserinfo_vip", 0, "warning2-0"]);
|
await db.prepare("run", insertWarningQuery, [getHash("getuserinfo_warning_2"), 40, "getuserinfo_vip", 0, "warning2-0"]);
|
||||||
await db.prepare("run", insertWarningQuery, [getHash("getuserinfo_warning_3"), 50, "getuserinfo_vip", 1, "warning3-0"]);
|
await db.prepare("run", insertWarningQuery, [getHash("getuserinfo_warning_3"), 50, "getuserinfo_vip", 1, "warning3-0"]);
|
||||||
await db.prepare("run", insertWarningQuery, [getHash("getuserinfo_warning_3"), 60, "getuserinfo_vip", 0, "warning3-1"]);
|
await db.prepare("run", insertWarningQuery, [getHash("getuserinfo_warning_3"), 60, "getuserinfo_vip", 0, "warning3-1"]);
|
||||||
|
|
||||||
|
const insertBanQuery = 'INSERT INTO "shadowBannedUsers" ("userID") VALUES (?)';
|
||||||
|
await db.prepare("run", insertBanQuery, [getHash("getuserinfo_ban_01")]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should be able to get a 200", (done: Done) => {
|
it("Should be able to get a 200", (done: Done) => {
|
||||||
@@ -172,7 +175,7 @@ describe("getUserInfo", () => {
|
|||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
const expected = {
|
const expected = {
|
||||||
warningReason: "warning0-0"
|
warningReason: "warning0-0",
|
||||||
};
|
};
|
||||||
assert.ok(partialDeepEquals(data, expected));
|
assert.ok(partialDeepEquals(data, expected));
|
||||||
done(); // pass
|
done(); // pass
|
||||||
@@ -232,4 +235,32 @@ describe("getUserInfo", () => {
|
|||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Should get ban data for banned user (only appears when specifically requested)", (done: Done) => {
|
||||||
|
fetch(`${getbaseURL()}/api/userInfo?userID=getuserinfo_ban_01&value=banned`)
|
||||||
|
.then(async res => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
const data = await res.json();
|
||||||
|
const expected = {
|
||||||
|
banned: true
|
||||||
|
};
|
||||||
|
assert.ok(partialDeepEquals(data, expected));
|
||||||
|
done(); // pass
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
|
|
||||||
|
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`)
|
||||||
|
.then(async res => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
const data = await res.json();
|
||||||
|
const expected = {
|
||||||
|
banned: false
|
||||||
|
};
|
||||||
|
assert.ok(partialDeepEquals(data, expected));
|
||||||
|
done(); // pass
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user