Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: MyMICDS CI

on:
push:
branches: master
branches: [master]
pull_request:

jobs:
Expand All @@ -14,7 +14,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: 14.x
node-version: 16.x
- name: Install dependencies
run: npm ci
- name: Create fake config
Expand All @@ -30,7 +30,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: 14.x
node-version: 16.x
- name: Install dependencies
run: npm ci
- name: Decrypt secret files
Expand Down
15,992 changes: 15,025 additions & 967 deletions package-lock.json

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"@types/jsonwebtoken": "^7.2.7",
"@types/later": "^1.2.4",
"@types/lodash": "^4.14.149",
"@types/mongodb": "^3.5.26",
"@types/multer": "^1.3.7",
"@types/node": "^14.14.32",
"@types/nodemailer": "^4.6.1",
Expand All @@ -91,26 +90,23 @@
"lodash": "^4.17.21",
"mjml": "^4.11.0",
"moment": "^2.29.3",
"mongodb": "^3.6.0",
"mongodb": "^4.13.0",
"multer": "^1.2.0",
"nodemailer": "^6.7.2",
"object-assign-deep": "^0.4.0",
"p-all": "^2.1.0",
"pm2": "^4.5.6",
"random-js": "^1.0.8",
"socket.io": "^2.4.0",
"ts-node": "^9.1.1",
"ttypescript": "^1.5.12",
"typescript": "^4.2.3",
"typescript-is": "^0.17.1"
"typia": "^3.4.20"
},
"devDependencies": {
"@mymicds/eslint-config": "^1.0.0",
"@types/chai": "^4.2.7",
"@types/chai-subset": "^1.3.3",
"@types/mocha": "^5.2.7",
"@types/supertest": "^2.0.8",
"@typescript-eslint/eslint-plugin": "^3.6.1",
"@typescript-eslint/eslint-plugin": "^4.16",
"chai": "^4.2.0",
"chai-subset": "^1.6.0",
"eslint": "^7.4.0",
Expand All @@ -122,6 +118,9 @@
"prettier": "^2.0.5",
"pretty-quick": "^2.0.1",
"supertest": "^4.0.2",
"typedoc": "^0.22.15"
"ts-node": "^10.9.1",
"ttypescript": "^1.5.15",
"typedoc": "^0.23.24",
"typescript": "^4.9.4"
}
}
4 changes: 2 additions & 2 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function initAPI(dbUri: string) {
});

// Connect to database
const client = await MongoClient.connect(dbUri, { useUnifiedTopology: true });
const client = await MongoClient.connect(dbUri);
const db = client.db();

// Enable JWT authentication middleware
Expand Down Expand Up @@ -98,7 +98,7 @@ export async function initAPI(dbUri: string) {
if (res.headersSent) {
return next(err);
}
api.error(res, err);
api.error(res, err as Error);
}) as express.ErrorRequestHandler);

return [app, db, server] as const;
Expand Down
4 changes: 2 additions & 2 deletions src/libs/admins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function getAdmins(db: Db) {
try {
return await userdata.find({ scopes: ['admin'] }).toArray();
} catch (e) {
throw new InternalError('There was a problem querying the database!', e);
throw new InternalError('There was a problem querying the database!', e as Error);
}
}

Expand All @@ -30,7 +30,7 @@ async function sendAdminEmail(db: Db, message: mail.Message) {
try {
admins = await getAdmins(db);
} catch (e) {
throw new InternalError('Error getting list of admins!', e);
throw new InternalError('Error getting list of admins!', e as Error);
}

if (admins.length < 1) {
Expand Down
38 changes: 24 additions & 14 deletions src/libs/aliases.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AliasType, PortalClass } from '@mymicds/sdk';
import { Db, ObjectID } from 'mongodb';
import { Db, ObjectId } from 'mongodb';
import { InputError, InternalError } from './errors';
import * as classes from './classes';
import * as users from './users';
Expand Down Expand Up @@ -53,7 +53,8 @@ async function addAlias(
user: userDoc!._id,
type,
classNative: validClassObject._id,
classRemote: classString
classRemote: classString,
_id: new ObjectId()
};

// Insert into database
Expand All @@ -63,10 +64,13 @@ async function addAlias(
try {
results = await aliasdata.insertOne(insertAlias);
} catch (e) {
throw new InternalError('There was a problem inserting the alias into the database!', e);
throw new InternalError(
'There was a problem inserting the alias into the database!',
e as Error
);
}

return results.ops[0]._id;
return results.insertedId;
}

/**
Expand All @@ -89,7 +93,7 @@ async function listAliases(db: Db, user: string) {
try {
aliases = await aliasdata.find({ user: userDoc!._id }).toArray();
} catch (e) {
throw new InternalError('There was a problem querying the database!', e);
throw new InternalError('There was a problem querying the database!', e as Error);
}

// Add array for all alias types
Expand Down Expand Up @@ -157,7 +161,7 @@ async function deleteAlias(db: Db, user: string, type: AliasType, aliasId: strin
// Make sure valid alias
const aliases = await listAliases(db, user);

let validAliasId: ObjectID | null = null;
let validAliasId: ObjectId | null = null;
for (const alias of aliases[type]) {
if (aliasId === alias._id.toHexString()) {
validAliasId = alias._id;
Expand All @@ -174,7 +178,10 @@ async function deleteAlias(db: Db, user: string, type: AliasType, aliasId: strin
try {
await aliasdata.deleteOne({ _id: validAliasId });
} catch (e) {
throw new InternalError('There was a problem deleting the alias from the database!', e);
throw new InternalError(
'There was a problem deleting the alias from the database!',
e as Error
);
}
}

Expand Down Expand Up @@ -202,7 +209,7 @@ async function getAliasClass(db: Db, user: string, type: AliasType, classInput:
.find({ user: userDoc!._id, type, classRemote: classInput })
.toArray();
} catch (e) {
throw new InternalError('There was a problem querying the database!', e);
throw new InternalError('There was a problem querying the database!', e as Error);
}

if (aliases.length === 0) {
Expand Down Expand Up @@ -236,7 +243,7 @@ export async function deleteClasslessAliases(db: Db) {

try {
classless = await aliasdata
.aggregate([
.aggregate<AliasWithIDs>([
// Stage 1
{
$lookup: {
Expand All @@ -257,23 +264,26 @@ export async function deleteClasslessAliases(db: Db) {
])
.toArray();
} catch (e) {
throw new InternalError('There was a problem querying the database!', e);
throw new InternalError('There was a problem querying the database!', e as Error);
}

try {
await Promise.all(
classless.map(alias => aliasdata.deleteOne({ _id: alias._id, user: alias.user }))
);
} catch (e) {
throw new InternalError('There was a problem deleting aliases in the database!', e);
throw new InternalError(
'There was a problem deleting aliases in the database!',
e as Error
);
}
}

export interface AliasWithIDs {
_id: ObjectID;
user: ObjectID;
_id: ObjectId;
user: ObjectId;
type: AliasType;
classNative: ObjectID;
classNative: ObjectId;
classRemote: string;
}

Expand Down
2 changes: 1 addition & 1 deletion src/libs/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Action } from '@mymicds/sdk';
import { InputError, InternalError } from './errors';
import { NextFunction, Request, Response } from 'express';
import { TypeGuardError } from 'typescript-is';
import { TypeGuardError } from 'typia';
import * as Sentry from '@sentry/node';

declare global {
Expand Down
14 changes: 10 additions & 4 deletions src/libs/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ export async function register(db: Db, user: NewUserData) {
try {
confirmationBuf = await promisify(crypto.randomBytes)(16);
} catch (e) {
throw new InternalError('There was a problem generating a random confirmation hash!', e);
throw new InternalError(
'There was a problem generating a random confirmation hash!',
e as Error
);
}

const confirmationHash = confirmationBuf.toString('hex');
Expand All @@ -99,7 +102,7 @@ export async function register(db: Db, user: NewUserData) {
try {
unsubscribeBuf = await promisify(crypto.randomBytes)(16);
} catch (e) {
throw new InternalError('There was a problem generating a random email hash!', e);
throw new InternalError('There was a problem generating a random email hash!', e as Error);
}

const unsubscribeHash = unsubscribeBuf.toString('hex');
Expand All @@ -123,7 +126,10 @@ export async function register(db: Db, user: NewUserData) {
try {
await userdata.updateOne({ user: newUser.user }, { $set: newUser }, { upsert: true });
} catch (e) {
throw new InternalError('There was a problem inserting the account into the database!', e);
throw new InternalError(
'There was a problem inserting the account into the database!',
e as Error
);
}

const email = newUser.user + '@micds.org';
Expand Down Expand Up @@ -179,7 +185,7 @@ export async function confirm(db: Db, user: string, hash: string) {
try {
await userdata.updateOne({ user }, { $set: { confirmed: true } });
} catch (e) {
throw new InternalError('There was a problem updating the database!', e);
throw new InternalError('There was a problem updating the database!', e as Error);
}
} else {
// Hash does not match
Expand Down
25 changes: 17 additions & 8 deletions src/libs/backgrounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function uploadBackground() {
try {
await deleteBackground(req.apiUser!);
} catch (err) {
cb(err, '');
cb(err as Error, '');
return;
}

Expand Down Expand Up @@ -104,7 +104,10 @@ async function getCurrentFiles(user: string) {
try {
userDirs = await fs.readdir(userBackgroundsDir);
} catch (e) {
throw new InternalError('There was a problem reading the user backgrounds directory!', e);
throw new InternalError(
'There was a problem reading the user backgrounds directory!',
e as Error
);
}

// Look through all the directories
Expand Down Expand Up @@ -161,7 +164,7 @@ async function deleteBackground(user: string) {
try {
await fs.rename(currentPath, deletedPath);
} catch (e) {
throw new InternalError('There was a problem deleting the directory!', e);
throw new InternalError('There was a problem deleting the directory!', e as Error);
}
}

Expand Down Expand Up @@ -201,7 +204,10 @@ async function getAllBackgrounds(db: Db) {
try {
userDirs = await fs.readdir(userBackgroundsDir);
} catch (e) {
throw new InternalError('There was a problem reading the user backgrounds directory!', e);
throw new InternalError(
'There was a problem reading the user backgrounds directory!',
e as Error
);
}

const userdata = db.collection<UserDoc>('users');
Expand All @@ -210,7 +216,7 @@ async function getAllBackgrounds(db: Db) {
try {
users = await userdata.find({ confirmed: true }).toArray();
} catch (e) {
throw new InternalError('There was a problem querying the database!', e);
throw new InternalError('There was a problem querying the database!', e as Error);
}

const remainingUsers = users.map(u => u.user);
Expand Down Expand Up @@ -267,7 +273,10 @@ async function getDirExtension(userDir: string) {
try {
userImages = await fs.readdir(userBackgroundsDir + '/' + userDir);
} catch (e) {
throw new InternalError("There was a problem reading the user's background directory!", e);
throw new InternalError(
"There was a problem reading the user's background directory!",
e as Error
);
}

// Loop through all valid files until there's either a .png or .jpg extention
Expand Down Expand Up @@ -297,13 +306,13 @@ async function addBlur(fromPath: string, toPath: string, blurRadius: number) {
try {
image = await Jimp.read(fromPath);
} catch (e) {
throw new InternalError('There was a problem reading the image!', e);
throw new InternalError('There was a problem reading the image!', e as Error);
}

try {
await promisify(image.blur(blurRadius).write.bind(image))(toPath);
} catch (e) {
throw new InternalError('There was a problem saving the image!', e);
throw new InternalError('There was a problem saving the image!', e as Error);
}
}

Expand Down
Loading