mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-11 05:57:04 +03:00
Add reputation to user info
This commit is contained in:
@@ -4,6 +4,7 @@ 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';
|
||||
|
||||
async function dbGetSubmittedSegmentSummary(userID: HashedUserID): Promise<{ minutesSaved: number, segmentCount: number }> {
|
||||
try {
|
||||
@@ -60,16 +61,15 @@ async function dbGetWarningsForUser(userID: HashedUserID): Promise<number> {
|
||||
}
|
||||
|
||||
export async function getUserInfo(req: Request, res: Response) {
|
||||
let userID = req.query.userID as UserID;
|
||||
const userID = req.query.userID as UserID;
|
||||
const hashedUserID: HashedUserID = userID ? getHash(userID) : req.query.publicUserID as HashedUserID;
|
||||
|
||||
if (userID == undefined) {
|
||||
if (hashedUserID == undefined) {
|
||||
//invalid request
|
||||
res.status(400).send('Parameters are not valid');
|
||||
return;
|
||||
}
|
||||
|
||||
//hash the userID
|
||||
const hashedUserID: HashedUserID = getHash(userID);
|
||||
|
||||
const segmentsSummary = await dbGetSubmittedSegmentSummary(hashedUserID);
|
||||
if (segmentsSummary) {
|
||||
@@ -80,6 +80,7 @@ export async function getUserInfo(req: Request, res: Response) {
|
||||
segmentCount: segmentsSummary.segmentCount,
|
||||
viewCount: await dbGetViewsForUser(hashedUserID),
|
||||
warnings: await dbGetWarningsForUser(hashedUserID),
|
||||
reputation: await getReputation(hashedUserID),
|
||||
vip: await isUserVIP(hashedUserID),
|
||||
});
|
||||
} else {
|
||||
|
||||
@@ -41,7 +41,7 @@ describe('getUserInfo', () => {
|
||||
.catch(err => done('couldn\'t call endpoint'));
|
||||
});
|
||||
|
||||
it('Should done(info', (done: Done) => {
|
||||
it('Should be able to get user info', (done: Done) => {
|
||||
fetch(getbaseURL() + '/api/getUserInfo?userID=getuserinfo_user_01')
|
||||
.then(async res => {
|
||||
if (res.status !== 200) {
|
||||
@@ -56,6 +56,8 @@ describe('getUserInfo', () => {
|
||||
done('Returned incorrect viewCount "' + data.viewCount + '"');
|
||||
} else if (data.segmentCount !== 3) {
|
||||
done('Returned incorrect segmentCount "' + data.segmentCount + '"');
|
||||
} else if (Math.abs(data.reputation - -0.928) > 0.001) {
|
||||
done('Returned incorrect reputation "' + data.reputation + '"');
|
||||
} else {
|
||||
done(); // pass
|
||||
}
|
||||
@@ -78,6 +80,21 @@ describe('getUserInfo', () => {
|
||||
.catch(err => ("couldn't call endpoint"));
|
||||
});
|
||||
|
||||
it('Should get warning data with public ID', async () => {
|
||||
try {
|
||||
const res = await fetch(getbaseURL() + '/api/getUserInfo?userID=' + await getHash("getuserinfo_warning_0"))
|
||||
|
||||
if (res.status !== 200) {
|
||||
return 'non 200 (' + res.status + ')';
|
||||
} else {
|
||||
const data = await res.json();;
|
||||
if (data.warnings !== 1) return 'wrong number of warnings: ' + data.warnings + ', not ' + 1;
|
||||
}
|
||||
} catch (err) {
|
||||
return "couldn't call endpoint";
|
||||
}
|
||||
});
|
||||
|
||||
it('Should get multiple warnings', (done: Done) => {
|
||||
fetch(getbaseURL() + '/api/getUserInfo?userID=getuserinfo_warning_1')
|
||||
.then(async res => {
|
||||
|
||||
Reference in New Issue
Block a user