remove extra async and extra utils

This commit is contained in:
Michael C 2021-09-22 23:34:46 -04:00
parent 4e50f0ab4b
commit 28d637f620
No known key found for this signature in database
GPG key ID: FFB04FB3B878B7B4
21 changed files with 52 additions and 75 deletions

View file

@ -26,5 +26,6 @@ module.exports = {
"no-multiple-empty-lines": ["error", { max: 2, maxEOF: 0 }],
"indent": ["warn", 4, { "SwitchCase": 1 }],
"object-curly-spacing": ["warn", "always"],
"require-await": "warn",
},
};

View file

@ -10,6 +10,7 @@ export class Mysql implements IDatabase {
constructor(private config: unknown) {
}
// eslint-disable-next-line require-await
async init(): Promise<void> {
this.connection = new MysqlInterface(this.config);
}

View file

@ -12,6 +12,7 @@ export class Sqlite implements IDatabase {
{
}
// eslint-disable-next-line require-await
async prepare(type: QueryType, query: string, params: any[] = []): Promise<any[]> {
// Logger.debug(`prepare (sqlite): type: ${type}, query: ${query}, params: ${params}`);
const preparedQuery = this.db.prepare(query);
@ -30,6 +31,7 @@ export class Sqlite implements IDatabase {
}
}
// eslint-disable-next-line require-await
async init(): Promise<void> {
// Make dirs if required
if (!fs.existsSync(path.join(this.config.dbPath, "../"))) {

View file

@ -10,7 +10,7 @@ type searchSegmentResponse = {
segments: DBSegment[]
};
async function getSegmentsFromDBByVideoID(videoID: VideoID, service: Service): Promise<DBSegment[]> {
function getSegmentsFromDBByVideoID(videoID: VideoID, service: Service): Promise<DBSegment[]> {
return db.prepare(
"all",
`SELECT "UUID", "timeSubmitted", "startTime", "endTime", "category", "actionType", "votes", "views", "locked", "hidden", "shadowHidden" FROM "sponsorTimes"

View file

@ -1,7 +1,7 @@
import { hashPrefixTester } from "../utils/hashPrefixTester";
import { getSegmentsByHash } from "./getSkipSegments";
import { Request, Response } from "express";
import { ActionType, Category, SegmentUUID, Service, VideoIDHash } from "../types/segments.model";
import { ActionType, Category, SegmentUUID, VideoIDHash } from "../types/segments.model";
import { getService } from "../utils/getService";
export async function getSkipSegmentsByHash(req: Request, res: Response): Promise<Response> {

View file

@ -116,7 +116,7 @@ const objSwitch = (cases: cases) => (defaultCase: string) => (key: string) =>
const functionSwitch = (cases: cases) => (defaultCase: string) => (key: string) =>
executeIfFunction(objSwitch(cases)(defaultCase)(key));
const dbGetValue = async (userID: HashedUserID, property: string): Promise<string|SegmentUUID|number> => {
const dbGetValue = (userID: HashedUserID, property: string): Promise<string|SegmentUUID|number> => {
return functionSwitch({
userID,
userName: dbGetUsername(userID),

View file

@ -1,7 +1,7 @@
import { postSkipSegments } from "./postSkipSegments";
import { Request, Response } from "express";
export async function oldSubmitSponsorTimes(req: Request, res: Response): Promise<Response> {
export function oldSubmitSponsorTimes(req: Request, res: Response): Promise<Response> {
req.query.category = "sponsor";
return postSkipSegments(req, res);
}

View file

@ -274,7 +274,7 @@ async function autoModerateSubmission(apiVideoInfo: APIVideoInfo,
}
}
async function getYouTubeVideoInfo(videoID: VideoID, ignoreCache = false): Promise<APIVideoInfo> {
function getYouTubeVideoInfo(videoID: VideoID, ignoreCache = false): Promise<APIVideoInfo> {
if (config.newLeafURLs !== null) {
return YouTubeAPI.listVideos(videoID, ignoreCache);
} else {

View file

@ -4,7 +4,7 @@ import { db, privateDB } from "../databases/databases";
import { getHash } from "../utils/getHash";
import { Request, Response } from "express";
async function logUserNameChange(userID: string, newUserName: string, oldUserName: string, updatedByAdmin: boolean): Promise<Response> {
function logUserNameChange(userID: string, newUserName: string, oldUserName: string, updatedByAdmin: boolean): Promise<Response> {
return privateDB.prepare("run",
`INSERT INTO "userNameLogs"("userID", "newUserName", "oldUserName", "updatedByAdmin", "updatedAt") VALUES(?, ?, ?, ?, ?)`,
[userID, newUserName, oldUserName, + updatedByAdmin, new Date().getTime()]

View file

@ -1,11 +1,9 @@
import fetch from "node-fetch";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { getHash } from "../../src/utils/getHash";
import { db } from "../../src/databases/databases";
import assert from "assert";
import { client } from "../utils/httpClient";
const endpoint = `${getbaseURL()}/api/lockReason`;
const endpoint = "/api/lockReason";
describe("getLockReason", () => {
before(async () => {
@ -28,71 +26,65 @@ describe("getLockReason", () => {
else return `Version isn't greater than 20. Version is ${version}`;
});
it("Should be able to get single reason", (done: Done) => {
fetch(`${endpoint}?videoID=getLockReason&category=sponsor`)
.then(async res => {
it("Should be able to get single reason", (done) => {
client.get(endpoint, { params: { videoID: "getLockReason", category: "sponsor" } })
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = [
{ category: "sponsor", locked: 1, reason: "sponsor-reason" }
];
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("Should be able to get empty locks", (done: Done) => {
fetch(`${endpoint}?videoID=getLockReason&category=intro`)
.then(async res => {
it("Should be able to get empty locks", (done) => {
client.get(endpoint, { params: { videoID: "getLockReason", category: "intro" } })
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = [
{ category: "intro", locked: 0, reason: "" }
];
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("should get multiple locks with array", (done: Done) => {
fetch(`${endpoint}?videoID=getLockReason&categories=["intro","sponsor"]`)
.then(async res => {
it("should get multiple locks with array", (done) => {
client.get(endpoint, { params: { videoID: "getLockReason", categories: `["intro","sponsor"]` } })
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = [
{ category: "sponsor", locked: 1, reason: "sponsor-reason" },
{ category: "intro", locked: 0, reason: "" }
];
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("should get multiple locks with repeated category", (done: Done) => {
fetch(`${endpoint}?videoID=getLockReason&category=interaction&category=music_offtopic&category=intro`)
.then(async res => {
it("should get multiple locks with repeated category", (done) => {
client.get(`${endpoint}?videoID=getLockReason&category=interaction&category=music_offtopic&category=intro`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = [
{ category: "interaction", locked: 1, reason: "interaction-reason" },
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason" },
{ category: "intro", locked: 0, reason: "" }
];
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("should return all categories if none specified", (done: Done) => {
fetch(`${endpoint}?videoID=getLockReason`)
.then(async res => {
it("should return all categories if none specified", (done) => {
client.get(endpoint, { params: { videoID: "getLockReason" } })
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
console.log(data);
const expected = [
{ category: "sponsor", locked: 1, reason: "sponsor-reason" },
{ category: "interaction", locked: 1, reason: "interaction-reason" },
@ -103,14 +95,14 @@ describe("getLockReason", () => {
{ category: "outro", locked: 0, reason: "" },
{ category: "poi_highlight", locked: 0, reason: "" }
];
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("should return 400 if no videoID specified", (done: Done) => {
fetch(endpoint)
it("should return 400 if no videoID specified", (done) => {
client.get(endpoint)
.then(res => {
assert.strictEqual(res.status, 400);
done();

View file

@ -35,7 +35,7 @@ describe("postClearCache", () => {
it("Should get 403 as non-vip", (done) => {
postClearCache(regularUser, "clear-test")
.then(async res => {
.then(res => {
assert.strictEqual(res.status, 403);
done();
})
@ -44,7 +44,7 @@ describe("postClearCache", () => {
it("Should give 400 with missing videoID", (done) => {
client.post(endpoint, { params: { userID: VIPUser } })
.then(async res => {
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
@ -53,7 +53,7 @@ describe("postClearCache", () => {
it("Should give 400 with missing userID", (done) => {
client.post(endpoint, { params: { videoID: "clear-test" } })
.then(async res => {
.then(res => {
assert.strictEqual(res.status, 400);
done();
})

View file

@ -311,7 +311,7 @@ describe("postSkipSegments", () => {
category: "sponsor",
}],
})
.then(async res => {
.then(res => {
assert.strictEqual(res.status, 403);
done();
})
@ -632,7 +632,7 @@ describe("postSkipSegments", () => {
category: "sponsor",
}],
})
.then(async res => {
.then(res => {
assert.strictEqual(res.status, 403);
const errorMessage = res.data;
const reason = "Reason01";
@ -707,7 +707,7 @@ describe("postSkipSegments", () => {
category: "sponsor",
}],
})
.then(async res => {
.then(res => {
assert.strictEqual(res.status, 403);
const errorMessage = res.data;
assert.notStrictEqual(errorMessage, "");

View file

@ -90,7 +90,7 @@ describe("postWarning", () => {
it("Should return 400 if missing body", (done) => {
client.post(endpoint, {})
.then(async res => {
.then(res => {
assert.strictEqual(res.status, 400);
done();
})

View file

@ -41,7 +41,7 @@ describe("segmentShift", function () {
const privateVipUserID = "VIPUser-segmentShift";
const vipUserID = getHash(privateVipUserID);
const endpoint = "/api/segmentShift";
const postSegmentShift = async (data: Record<string, any>) => client({
const postSegmentShift = (data: Record<string, any>) => client({
method: "POST",
url: endpoint,
data,
@ -71,7 +71,7 @@ describe("segmentShift", function () {
startTime: 20,
endTime: 30,
})
.then(async res => {
.then(res => {
assert.strictEqual(res.status, 403);
done();
})

View file

@ -34,14 +34,14 @@ async function getUsernameInfo(userID: string): Promise<{ userName: string, lock
return row;
}
async function addLogUserNameChange(userID: string, newUserName: string, oldUserName = "") {
function addLogUserNameChange(userID: string, newUserName: string, oldUserName = "") {
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) {
function getLastLogUserNameChange(userID: string) {
return privateDB.prepare("get", `SELECT * FROM "userNameLogs" WHERE "userID" = ? ORDER BY "updatedAt" DESC LIMIT 1`, [getHash(userID)]);
}
@ -105,7 +105,7 @@ describe("setUsername", () => {
it("Should return 200", (done) => {
const username = "Changed%20Username";
postSetUserName(user01PrivateUserID, username)
.then(async res => {
.then(res => {
assert.strictEqual(res.status, 200);
testUserNameChangelog(user01PrivateUserID, username, username01, false, done);
})

View file

@ -2,7 +2,7 @@ import assert from "assert";
import { partialDeepEquals } from "../utils/partialDeepEquals";
describe("Test utils ", () => {
it("objectContain", async () => {
it("objectContain", () => {
assert(partialDeepEquals(
{
name: "John Wick",

View file

@ -310,7 +310,7 @@ describe("voteOnSponsorTime", () => {
it("Should not be able to category-vote on an invalid UUID submission", (done) => {
const UUID = "invalid-uuid";
postVoteCategory("randomID3", UUID, "intro")
.then(async res => {
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
@ -356,7 +356,7 @@ describe("voteOnSponsorTime", () => {
it("Should not be able to upvote a segment (Too many warning)", (done) => {
const UUID = "warnvote-uuid-0";
postVote("warn-voteuser01", UUID, 1)
.then(async res => {
.then(res => {
assert.strictEqual(res.status, 403);
done();
})

View file

@ -1,5 +0,0 @@
import { config } from "../../src/config";
export function getbaseURL(): string {
return `http://localhost:${config.port}`;
}

View file

@ -1,12 +1,8 @@
import { config } from "../../src/config";
import axios, { AxiosRequestConfig } from "axios";
export function getbaseURL(): string {
return `http://localhost:${config.port}`;
}
export const defaultConfig: AxiosRequestConfig = {
baseURL: getbaseURL(),
const defaultConfig: AxiosRequestConfig = {
baseURL: `http://localhost:${config.port}`,
validateStatus: (status) => status < 500
};

View file

@ -1,11 +0,0 @@
/**
* Duplicated from Mocha types. TypeScript doesn't infer that type by itself for some reason.
*/
export type Done = (err?: any) => void;
export const postJSON = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
};

View file

@ -1,6 +1,7 @@
import { APIVideoData, APIVideoInfo } from "../src/types/youtubeApi.model";
export class YouTubeApiMock {
// eslint-disable-next-line require-await
static async listVideos(videoID: string): Promise<APIVideoInfo> {
const obj = {
id: videoID