Skip to content

Commit

Permalink
start converting to modern async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Mar 8, 2024
1 parent 94588f1 commit 141587e
Show file tree
Hide file tree
Showing 38 changed files with 577 additions and 677 deletions.
63 changes: 13 additions & 50 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"nodemon": "^3.1.0",
"prettier": "^3.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
"typescript": "^5.4.2"
},
"dependencies": {
"@tsoa/runtime": "^6.0.0",
Expand All @@ -44,9 +44,9 @@
"body-parser": "^1.20.2",
"cloudflare": "^2.9.1",
"cors": "^2.8.5",
"discord-api-types": "^0.37.70",
"discord-api-types": "^0.37.73",
"dotenv": "^16.4.5",
"express": "^4.18.2",
"express": "^4.18.3",
"firestorm-db": "^1.12.0",
"form-data": "^4.0.0",
"isomorphic-dompurify": "^2.4.0",
Expand Down
91 changes: 41 additions & 50 deletions src/v2/controller/addon.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export class AddonController extends Controller {

return f;
});

case "all":
default:
return this.service.getAll(id).then((addon) => {
Expand Down Expand Up @@ -80,15 +79,15 @@ export class AddonController extends Controller {
@Security("discord", ["administrator"])
@Security("bot")
@Get("raw")
public async getRaw(): Promise<Record<string, Addon>> {
public getRaw(): Promise<Record<string, Addon>> {
return this.service.getRaw();
}

/**
* Get all add-ons matching the given status
* @param status Add-on status
*/
public async getAddonsByStatus(status: AddonStatus): Promise<Addons> {
public getAddonsByStatus(status: AddonStatus): Promise<Addons> {
return this.service.getAddonByStatus(status);
}

Expand All @@ -97,7 +96,7 @@ export class AddonController extends Controller {
*/
@Response<NotAvailableError>(408)
@Get("stats")
public async getStats(): Promise<AddonStats> {
public getStats(): Promise<AddonStats> {
return cache.handle("addon-stats", () =>
this.service.getStats(false).then((res) =>
extract<AddonStats>({
Expand All @@ -115,7 +114,7 @@ export class AddonController extends Controller {
@Response<PermissionError>(403)
@Security("discord", ["administrator"])
@Get("stats-admin")
public async getStatsAdmin(): Promise<AddonStatsAdmin> {
public getStatsAdmin(): Promise<AddonStatsAdmin> {
return cache.handle("addon-stats-admin", () => this.service.getStats(true));
}

Expand All @@ -129,7 +128,8 @@ export class AddonController extends Controller {
public async getAddon(@Path() id_or_slug: string): Promise<Addon | Addons> {
if (AddonStatusValues.includes(id_or_slug as AddonStatus))
return this.getAddonsByStatus(id_or_slug as AddonStatus);
return this.service.getAddonFromSlugOrId(id_or_slug).then((r) => r[1]);
const [, addon] = await this.service.getAddonFromSlugOrId(id_or_slug);
return addon;
}

/**
Expand All @@ -145,8 +145,8 @@ export class AddonController extends Controller {
@Path() id_or_slug: string,
@Request() request: ExRequest,
): Promise<void> {
const id = (await this.service.getAddonFromSlugOrId(id_or_slug))[0];
let headerFileURL = await this.service.getHeaderFileURL(id);
const [addonID] = await this.service.getAddonFromSlugOrId(id_or_slug);
let headerFileURL = await this.service.getHeaderFileURL(addonID);
if (headerFileURL.startsWith("/")) headerFileURL = process.env.DB_IMAGE_ROOT + headerFileURL;

const response = (<any>request).res as ExResponse;
Expand All @@ -161,7 +161,7 @@ export class AddonController extends Controller {
@Response<PermissionError>(403)
@Security("discord", ["addon:approved", "administrator"])
@Get("{id_or_slug}/authors")
public async getAddonAuthorsProfiles(@Path() id_or_slug: string): Promise<UserProfile[]> {
public getAddonAuthorsProfiles(@Path() id_or_slug: string): Promise<UserProfile[]> {
return this.service.getAddonAuthorsProfiles(id_or_slug);
}

Expand All @@ -176,9 +176,8 @@ export class AddonController extends Controller {
@Path() id_or_slug: string,
@Path() property: AddonProperty,
): Promise<Addon | Files> {
return this.service
.getAddonFromSlugOrId(id_or_slug)
.then((value) => this.getAddonProperty(value[0], property));
const [addonID] = await this.service.getAddonFromSlugOrId(id_or_slug);
return this.getAddonProperty(addonID, property);
}

/**
Expand All @@ -190,12 +189,9 @@ export class AddonController extends Controller {
@Security("discord", ["addon:approved", "administrator"])
@Get("{id_or_slug}/files/screenshots")
public async getScreenshots(@Path() id_or_slug: string): Promise<Array<string>> {
return this.service
.getAddonFromSlugOrId(id_or_slug)
.then((value) => this.service.getScreenshots(value[0]))
.then((screens) =>
screens.map((s) => (s.startsWith("/") ? process.env.DB_IMAGE_ROOT + s : s)),
);
const [addonID] = await this.service.getAddonFromSlugOrId(id_or_slug);
const screens = await this.service.getScreenshots(addonID);
return screens.map((s) => (s.startsWith("/") ? process.env.DB_IMAGE_ROOT + s : s));
}

/**
Expand All @@ -207,9 +203,8 @@ export class AddonController extends Controller {
@Security("discord", ["addon:own", "administrator"])
@Get("{id_or_slug}/files/screenshots-ids")
public async getScreenshotsIds(@Path() id_or_slug: string): Promise<Array<string>> {
return this.service
.getAddonFromSlugOrId(id_or_slug)
.then((value) => this.service.getScreenshotsIds(value[0]));
const [addonID] = await this.service.getAddonFromSlugOrId(id_or_slug);
return this.service.getScreenshotsIds(addonID);
}

/**
Expand All @@ -221,31 +216,27 @@ export class AddonController extends Controller {
@Security("discord", ["addon:approved", "administrator"])
@Get("{id_or_slug}/files/downloads")
public async getDownloads(@Path() id_or_slug: string): Promise<Array<AddonDownload>> {
return this.service
.getAddonFromSlugOrId(id_or_slug)
.then((res) => this.service.getFiles(res[0]))
.then((files) =>
Object.values(
files
.filter((f) => f.use === "download")
.map((f) => {
if (!f.source.startsWith("https://") && !f.source.startsWith("http://"))
f.source = `http://${f.source}`;
return f;
})
.reduce((acc, file) => {
if (acc[file.name] === undefined) {
acc[file.name] = {
key: file.name,
links: [],
};
}
acc[file.name].links.push(file.source);

return acc;
}, {}),
),
);
const [addonID] = await this.service.getAddonFromSlugOrId(id_or_slug);
const files = await this.service.getFiles(addonID);
return Object.values(
files
.filter((f) => f.use === "download")
.map((file) => {
if (!file.source.startsWith("https://") && !file.source.startsWith("http://"))
file.source = `http://${file.source}`;
return file;
})
.reduce((acc, file) => {
if (acc[file.name] === undefined) {
acc[file.name] = {
key: file.name,
links: [],
};
}
acc[file.name].links.push(file.source);
return acc;
}, {}),
);
}

/**
Expand All @@ -263,8 +254,8 @@ export class AddonController extends Controller {
@Path() index: number,
@Request() request: ExRequest,
) {
const id = (await this.service.getAddonFromSlugOrId(id_or_slug))[0];
const screenshotURL = await this.service.getScreenshotURL(id, index);
const [addonID] = await this.service.getAddonFromSlugOrId(id_or_slug);
const screenshotURL = await this.service.getScreenshotURL(addonID, index);
const response = (<any>request).res as ExResponse;
response.redirect(screenshotURL);
}
Expand All @@ -279,8 +270,8 @@ export class AddonController extends Controller {
@Security("discord", ["addon:approved", "administrator"])
@Get("{id_or_slug}/files/header")
public async getHeaderURL(@Path() id_or_slug: string): Promise<string> {
const id = (await this.service.getAddonFromSlugOrId(id_or_slug))[0];
let headerFileURL = await this.service.getHeaderFileURL(id);
const [addonID] = await this.service.getAddonFromSlugOrId(id_or_slug);
let headerFileURL = await this.service.getHeaderFileURL(addonID);
if (headerFileURL.startsWith("/")) headerFileURL = process.env.DB_IMAGE_ROOT + headerFileURL;

return headerFileURL;
Expand Down
Loading

0 comments on commit 141587e

Please sign in to comment.