mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2026-01-31 14:51:09 +03:00
Merge pull request #275 from HaiDang666/193_log-username-change
Add log when update username
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
[vipUsers](###vipUsers)
|
[vipUsers](###vipUsers)
|
||||||
[sponsorTimes](###sponsorTimes)
|
[sponsorTimes](###sponsorTimes)
|
||||||
[userNames](###userNames)
|
[userNames](###userNames)
|
||||||
|
[userNameLogs](###userNameLogs)
|
||||||
[categoryVotes](###categoryVotes)
|
[categoryVotes](###categoryVotes)
|
||||||
[lockCategories](###lockCategories)
|
[lockCategories](###lockCategories)
|
||||||
[warnings](###warnings)
|
[warnings](###warnings)
|
||||||
@@ -61,6 +62,16 @@
|
|||||||
| -- | :--: |
|
| -- | :--: |
|
||||||
| userNames_userID | userID |
|
| userNames_userID | userID |
|
||||||
|
|
||||||
|
### userNameLogs
|
||||||
|
|
||||||
|
| Name | Type | |
|
||||||
|
| -- | :--: | -- |
|
||||||
|
| userID | TEXT | not null |
|
||||||
|
| newUserName | TEXT | not null |
|
||||||
|
| oldUserName | TEXT | not null |
|
||||||
|
| updatedByAdmin | BOOLEAN | not null |
|
||||||
|
| updatedAt | INTEGER | not null |
|
||||||
|
|
||||||
### categoryVotes
|
### categoryVotes
|
||||||
|
|
||||||
| Name | Type | |
|
| Name | Type | |
|
||||||
|
|||||||
13
databases/_upgrade_private_2.sql
Normal file
13
databases/_upgrade_private_2.sql
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
BEGIN TRANSACTION;
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS "userNameLogs" (
|
||||||
|
"userID" TEXT NOT NULL,
|
||||||
|
"newUserName" TEXT NOT NULL,
|
||||||
|
"oldUserName" TEXT NOT NULL,
|
||||||
|
"updatedByAdmin" BOOLEAN NOT NULL,
|
||||||
|
"updatedAt" INTEGER NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
UPDATE "config" SET value = 2 WHERE key = 'version';
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -1,9 +1,16 @@
|
|||||||
import {config} from '../config';
|
import {config} from '../config';
|
||||||
import {Logger} from '../utils/logger';
|
import {Logger} from '../utils/logger';
|
||||||
import {db} from '../databases/databases';
|
import {db, privateDB} from '../databases/databases';
|
||||||
import {getHash} from '../utils/getHash';
|
import {getHash} from '../utils/getHash';
|
||||||
import {Request, Response} from 'express';
|
import {Request, Response} from 'express';
|
||||||
|
|
||||||
|
async function logUserNameChange(userID: string, newUserName: string, oldUserName: string, updatedByAdmin: boolean): Promise<void> {
|
||||||
|
return privateDB.prepare('run',
|
||||||
|
`INSERT INTO "userNameLogs"("userID", "newUserName", "oldUserName", "updatedByAdmin", "updatedAt") VALUES(?, ?, ?, ?, ?)`,
|
||||||
|
[userID, newUserName, oldUserName, + updatedByAdmin, new Date().getTime()]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export async function setUsername(req: Request, res: Response) {
|
export async function setUsername(req: Request, res: Response) {
|
||||||
let userID = req.query.userID as string;
|
let userID = req.query.userID as string;
|
||||||
let userName = req.query.username as string;
|
let userName = req.query.username as string;
|
||||||
@@ -55,16 +62,20 @@ export async function setUsername(req: Request, res: Response) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
//check if username is already set
|
//check if username is already set
|
||||||
let row = await db.prepare('get', `SELECT count(*) as count FROM "userNames" WHERE "userID" = ?`, [userID]);
|
let row = await db.prepare('get', `SELECT "userName" FROM "userNames" WHERE "userID" = ? LIMIT 1`, [userID]);
|
||||||
|
let oldUserName = '';
|
||||||
|
|
||||||
if (row.count > 0) {
|
if (row.userName && row.userName.length !== 0) {
|
||||||
//already exists, update this row
|
//already exists, update this row
|
||||||
|
oldUserName = row.userName;
|
||||||
await db.prepare('run', `UPDATE "userNames" SET "userName" = ? WHERE "userID" = ?`, [userName, userID]);
|
await db.prepare('run', `UPDATE "userNames" SET "userName" = ? WHERE "userID" = ?`, [userName, userID]);
|
||||||
} else {
|
} else {
|
||||||
//add to the db
|
//add to the db
|
||||||
await db.prepare('run', `INSERT INTO "userNames"("userID", "userName") VALUES(?, ?)`, [userID, userName]);
|
await db.prepare('run', `INSERT INTO "userNames"("userID", "userName") VALUES(?, ?)`, [userID, userName]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await logUserNameChange(userID, userName, oldUserName, adminUserIDInput !== undefined);
|
||||||
|
|
||||||
res.sendStatus(200);
|
res.sendStatus(200);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Logger.error(err);
|
Logger.error(err);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
import { Done, getbaseURL } from '../utils';
|
import { Done, getbaseURL } from '../utils';
|
||||||
import { db } from '../../src/databases/databases';
|
import { db, privateDB } from '../../src/databases/databases';
|
||||||
import { getHash } from '../../src/utils/getHash';
|
import { getHash } from '../../src/utils/getHash';
|
||||||
|
|
||||||
const adminPrivateUserID = 'testUserId';
|
const adminPrivateUserID = 'testUserId';
|
||||||
@@ -21,6 +21,7 @@ const username07 = 'Username 07';
|
|||||||
|
|
||||||
async function addUsername(userID: string, userName: string, locked = 0) {
|
async function addUsername(userID: string, userName: string, locked = 0) {
|
||||||
await db.prepare('run', 'INSERT INTO "userNames" ("userID", "userName", "locked") VALUES(?, ?, ?)', [userID, userName, locked]);
|
await db.prepare('run', 'INSERT INTO "userNames" ("userID", "userName", "locked") VALUES(?, ?, ?)', [userID, userName, locked]);
|
||||||
|
await addLogUserNameChange(userID, userName);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getUsername(userID: string) {
|
async function getUsername(userID: string) {
|
||||||
@@ -31,6 +32,40 @@ async function getUsername(userID: string) {
|
|||||||
return row.userName;
|
return row.userName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function addLogUserNameChange(userID: string, newUserName: string, oldUserName: string = '') {
|
||||||
|
privateDB.prepare('run',
|
||||||
|
`INSERT INTO "userNameLogs"("userID", "newUserName", "oldUserName", "updatedAt", "updatedByAdmin") VALUES(?, ?, ?, ?, ?)`,
|
||||||
|
[getHash(userID), newUserName, oldUserName, new Date().getTime(), + true]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getLastLogUserNameChange(userID: string) {
|
||||||
|
return privateDB.prepare('get', `SELECT * FROM "userNameLogs" WHERE "userID" = ? ORDER BY "updatedAt" DESC LIMIT 1`, [getHash(userID)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function wellFormatUserName(userName: string) {
|
||||||
|
return userName.replace(/[\u0000-\u001F\u007F-\u009F]/g, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
async function testUserNameChangelog(userID: string, newUserName: string, oldUserName: string, byAdmin: boolean, done: Done) {
|
||||||
|
|
||||||
|
const log = await getLastLogUserNameChange(userID);
|
||||||
|
|
||||||
|
if (newUserName !== log.newUserName) {
|
||||||
|
return done(`UserID '${userID}' incorrect log on newUserName: ${newUserName} !== ${log.newUserName}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (oldUserName !== log.oldUserName) {
|
||||||
|
return done(`UserID '${userID}' incorrect log on oldUserName: ${oldUserName} !== ${log.oldUserName}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (byAdmin !== Boolean(log.updatedByAdmin)) {
|
||||||
|
return done(`UserID '${userID}' incorrect log on updatedByAdmin: ${byAdmin} !== ${log.updatedByAdmin}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return done();
|
||||||
|
}
|
||||||
|
|
||||||
describe('setUsername', () => {
|
describe('setUsername', () => {
|
||||||
before(async () => {
|
before(async () => {
|
||||||
await addUsername(getHash(user01PrivateUserID), username01, 0);
|
await addUsername(getHash(user01PrivateUserID), username01, 0);
|
||||||
@@ -46,9 +81,11 @@ describe('setUsername', () => {
|
|||||||
fetch(`${getbaseURL()}/api/setUsername?userID=${user01PrivateUserID}&username=Changed%20Username`, {
|
fetch(`${getbaseURL()}/api/setUsername?userID=${user01PrivateUserID}&username=Changed%20Username`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
})
|
})
|
||||||
.then(res => {
|
.then(async res => {
|
||||||
if (res.status !== 200) done(`Status code was ${res.status}`);
|
if (res.status !== 200) done(`Status code was ${res.status}`);
|
||||||
else done(); // pass
|
else {
|
||||||
|
testUserNameChangelog(user01PrivateUserID, decodeURIComponent('Changed%20Username'), username01, false, done);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch(err => done(`couldn't call endpoint`));
|
.catch(err => done(`couldn't call endpoint`));
|
||||||
});
|
});
|
||||||
@@ -113,7 +150,9 @@ describe('setUsername', () => {
|
|||||||
.then(async res => {
|
.then(async res => {
|
||||||
const username = await getUsername(getHash(user03PrivateUserID));
|
const username = await getUsername(getHash(user03PrivateUserID));
|
||||||
if (username !== newUsername) done(`Username did not change`);
|
if (username !== newUsername) done(`Username did not change`);
|
||||||
else done();
|
else {
|
||||||
|
testUserNameChangelog(user03PrivateUserID, newUsername, username03, false, done);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch(err => done(`couldn't call endpoint`));
|
.catch(err => done(`couldn't call endpoint`));
|
||||||
});
|
});
|
||||||
@@ -139,7 +178,7 @@ describe('setUsername', () => {
|
|||||||
.then(async res => {
|
.then(async res => {
|
||||||
const username = await getUsername(getHash(user05PrivateUserID));
|
const username = await getUsername(getHash(user05PrivateUserID));
|
||||||
if (username === newUsername) done(`Username contains unicode control characters`);
|
if (username === newUsername) done(`Username contains unicode control characters`);
|
||||||
else done();
|
else testUserNameChangelog(user05PrivateUserID, wellFormatUserName(newUsername), username05, false, done);
|
||||||
})
|
})
|
||||||
.catch(err => done(`couldn't call endpoint`));
|
.catch(err => done(`couldn't call endpoint`));
|
||||||
});
|
});
|
||||||
@@ -164,7 +203,7 @@ describe('setUsername', () => {
|
|||||||
.then(async res => {
|
.then(async res => {
|
||||||
const username = await getUsername(getHash(user06PrivateUserID));
|
const username = await getUsername(getHash(user06PrivateUserID));
|
||||||
if (username !== newUsername) done(`Failed to change username from '${username06}' to '${newUsername}'`);
|
if (username !== newUsername) done(`Failed to change username from '${username06}' to '${newUsername}'`);
|
||||||
else done();
|
else testUserNameChangelog(user06PrivateUserID, newUsername, username06, true, done);
|
||||||
})
|
})
|
||||||
.catch(err => done(`couldn't call endpoint`));
|
.catch(err => done(`couldn't call endpoint`));
|
||||||
});
|
});
|
||||||
@@ -175,9 +214,9 @@ describe('setUsername', () => {
|
|||||||
method: 'POST',
|
method: 'POST',
|
||||||
})
|
})
|
||||||
.then(async res => {
|
.then(async res => {
|
||||||
const username = await getUsername(getHash(user06PrivateUserID));
|
const username = await getUsername(getHash(user07PrivateUserID));
|
||||||
if (username !== newUsername) done(`Failed to change username from '${username06}' to '${newUsername}'`);
|
if (username !== newUsername) done(`Failed to change username from '${username07}' to '${newUsername}'`);
|
||||||
else done();
|
else testUserNameChangelog(user07PrivateUserID, newUsername, username07, true, done);
|
||||||
})
|
})
|
||||||
.catch(err => done(`couldn't call endpoint`));
|
.catch(err => done(`couldn't call endpoint`));
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user