Skip to content

Commit

Permalink
Merge pull request #391 from HaiDang666/388_lock-user-with-reason
Browse files Browse the repository at this point in the history
add username in get lock reason route
  • Loading branch information
ajayyy authored Oct 27, 2021
2 parents 393027c + 103280c commit 3877cd5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 40 deletions.
52 changes: 36 additions & 16 deletions src/routes/getLockReason.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const possibleCategoryList = config.categoryList;
interface lockArray {
category: Category;
locked: number,
reason: string
reason: string,
userID: string,
userName: string,
}

export async function getLockReason(req: Request, res: Response): Promise<Response> {
Expand Down Expand Up @@ -38,31 +40,49 @@ export async function getLockReason(req: Request, res: Response): Promise<Respon

try {
// Get existing lock categories markers
const row = await db.prepare("all", 'SELECT "category", "reason" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category, reason: string}[];
const row = await db.prepare("all", 'SELECT "category", "reason", "userID" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category, reason: string, userID: string }[];
// map to object array
const locks = [];
const lockedCategories = [] as string[];
const userIDs = new Set();
// get all locks for video, check if requested later
for (const lock of row) {
locks.push({
category: lock.category,
locked: 1,
reason: lock.reason
reason: lock.reason,
userID: lock?.userID || "",
userName: "",
} as lockArray);
lockedCategories.push(lock.category);
userIDs.add(lock.userID);
}
// add empty locks for categories requested but not locked
const noLockCategories = searchCategories.filter(x => !lockedCategories.includes(x));
for (const noLock of noLockCategories) {
locks.push({
category: noLock,
locked: 0,
reason: ""
} as lockArray);
// all userName from userIDs
const userNames = await db.prepare(
"all",
`SELECT "userName", "userID" FROM "userNames" WHERE "userID" IN (${Array.from("?".repeat(userIDs.size)).join()}) LIMIT ?`,
[...userIDs, userIDs.size]
) as { userName: string, userID: string }[];

const results = [];
for (const category of searchCategories) {
const lock = locks.find(l => l.category === category);
if (lock?.userID) {
// mapping userName to locks
const user = userNames.find(u => u.userID === lock.userID);
lock.userName = user?.userName || "";
results.push(lock);
} else {
// add empty locks for categories requested but not locked
results.push({
category,
locked: 0,
reason: "",
userID: "",
userName: "",
} as lockArray);
}
}
// return real and fake locks that were requested
const filtered = locks.filter(lock => searchCategories.includes(lock.category));
return res.send(filtered);

return res.send(results);
} catch (err) {
Logger.error(err as string);
return res.sendStatus(500);
Expand Down
63 changes: 39 additions & 24 deletions test/cases/getLockReason.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,33 @@ import { client } from "../utils/httpClient";

const endpoint = "/api/lockReason";

const vipUserName1 = "getLockReason-vipUserName_1";
const vipUserID1 = getHash("getLockReason-vipUserID_1");
const vipUserName2 = "getLockReason-vipUserName_2";
const vipUserID2 = getHash("getLockReason-vipUserID_2");

describe("getLockReason", () => {
before(async () => {
const vipUserID = "getLockReasonVIP";
const vipUserHash = getHash(vipUserID);
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
await db.prepare("run", insertVipUserQuery, [vipUserHash]);
await db.prepare("run", insertVipUserQuery, [vipUserHash]);
await db.prepare("run", insertVipUserQuery, [vipUserID1]);
await db.prepare("run", insertVipUserQuery, [vipUserID2]);

const insertVipUserNameQuery = 'INSERT INTO "userNames" ("userID", "userName") VALUES (?, ?)';
await db.prepare("run", insertVipUserNameQuery, [vipUserID1, vipUserName1]);
await db.prepare("run", insertVipUserNameQuery, [vipUserID2, vipUserName2]);

const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason") VALUES (?, ?, ?, ?)';
await db.prepare("run", insertLockCategoryQuery, [vipUserHash, "getLockReason", "sponsor", "sponsor-reason"]);
await db.prepare("run", insertLockCategoryQuery, [vipUserHash, "getLockReason", "interaction", "interaction-reason"]);
await db.prepare("run", insertLockCategoryQuery, [vipUserHash, "getLockReason", "preview", "preview-reason"]);
await db.prepare("run", insertLockCategoryQuery, [vipUserHash, "getLockReason", "music_offtopic", "nonmusic-reason"]);
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "sponsor", "sponsor-reason"]);
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "interaction", "interaction-reason"]);
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "preview", "preview-reason"]);
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "music_offtopic", "nonmusic-reason"]);
await db.prepare("run", insertLockCategoryQuery, [vipUserID2, "getLockReason", "outro", "outro-reason"]);
});

after(async () => {
const deleteUserNameQuery = 'DELETE FROM "userNames" WHERE "userID" = ? AND "userName" = ? LIMIT 1';
await db.prepare("run", deleteUserNameQuery, [vipUserID1, vipUserName1]);
await db.prepare("run", deleteUserNameQuery, [vipUserID2, vipUserName2]);
});

it("Should update the database version when starting the application", async () => {
Expand All @@ -31,7 +45,7 @@ describe("getLockReason", () => {
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [
{ category: "sponsor", locked: 1, reason: "sponsor-reason" }
{ category: "sponsor", locked: 1, reason: "sponsor-reason", userID: vipUserID1, userName: vipUserName1 }
];
assert.deepStrictEqual(res.data, expected);
done();
Expand All @@ -44,7 +58,7 @@ describe("getLockReason", () => {
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [
{ category: "intro", locked: 0, reason: "" }
{ category: "intro", locked: 0, reason: "", userID: "", userName: "" }
];
assert.deepStrictEqual(res.data, expected);
done();
Expand All @@ -53,12 +67,13 @@ describe("getLockReason", () => {
});

it("should get multiple locks with array", (done) => {
client.get(endpoint, { params: { videoID: "getLockReason", categories: `["intro","sponsor"]` } })
client.get(endpoint, { params: { videoID: "getLockReason", categories: `["intro","sponsor","outro"]` } })
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [
{ category: "sponsor", locked: 1, reason: "sponsor-reason" },
{ category: "intro", locked: 0, reason: "" }
{ category: "intro", locked: 0, reason: "", userID: "", userName: "" },
{ category: "sponsor", locked: 1, reason: "sponsor-reason", userID: vipUserID1, userName: vipUserName1 },
{ category: "outro", locked: 1, reason: "outro-reason", userID: vipUserID2, userName: vipUserName2 }
];
assert.deepStrictEqual(res.data, expected);
done();
Expand All @@ -71,9 +86,9 @@ describe("getLockReason", () => {
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [
{ category: "interaction", locked: 1, reason: "interaction-reason" },
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason" },
{ category: "intro", locked: 0, reason: "" }
{ category: "interaction", locked: 1, reason: "interaction-reason", userID: vipUserID1, userName: vipUserName1 },
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason", userID: vipUserID1, userName: vipUserName1 },
{ category: "intro", locked: 0, reason: "", userID: "", userName: "" }
];
assert.deepStrictEqual(res.data, expected);
done();
Expand All @@ -86,14 +101,14 @@ describe("getLockReason", () => {
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [
{ category: "sponsor", locked: 1, reason: "sponsor-reason" },
{ category: "interaction", locked: 1, reason: "interaction-reason" },
{ category: "preview", locked: 1, reason: "preview-reason" },
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason" },
{ category: "selfpromo", locked: 0, reason: "" },
{ category: "intro", locked: 0, reason: "" },
{ category: "outro", locked: 0, reason: "" },
{ category: "poi_highlight", locked: 0, reason: "" }
{ category: "sponsor", locked: 1, reason: "sponsor-reason", userID: vipUserID1, userName: vipUserName1 },
{ category: "selfpromo", locked: 0, reason: "", userID: "", userName: "" },
{ category: "interaction", locked: 1, reason: "interaction-reason", userID: vipUserID1, userName: vipUserName1 },
{ category: "intro", locked: 0, reason: "", userID: "", userName: "" },
{ category: "outro", locked: 1, reason: "outro-reason", userID: vipUserID2, userName: vipUserName2 },
{ category: "preview", locked: 1, reason: "preview-reason", userID: vipUserID1, userName: vipUserName1 },
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason", userID: vipUserID1, userName: vipUserName1 },
{ category: "poi_highlight", locked: 0, reason: "", userID: "", userName: "" }
];
assert.deepStrictEqual(res.data, expected);
done();
Expand Down

0 comments on commit 3877cd5

Please sign in to comment.