Merge branch 'master' into pr/LoboMetalurgico/424

This commit is contained in:
Flam3rboy
2021-10-10 12:50:45 +02:00
48 changed files with 15902 additions and 17258 deletions
+57 -995
View File
File diff suppressed because it is too large Load Diff
+1 -9
View File
@@ -8,7 +8,7 @@
"start": "npm run build && node dist/",
"test": "npm run build && jest",
"postinstall": "npm run build",
"build": "npx tsc -b ."
"build": "npx tsc -p ."
},
"repository": {
"type": "git",
@@ -28,22 +28,15 @@
},
"homepage": "https://docs.fosscord.com/",
"devDependencies": {
"@swc/cli": "^0.1.51",
"@swc/core": "^1.2.93",
"@types/amqplib": "^0.8.1",
"@types/jsonwebtoken": "^8.5.0",
"@types/mongoose-autopopulate": "^0.10.1",
"@types/multer": "^1.4.7",
"@types/node": "^14.17.9",
"@types/node-fetch": "^2.5.12",
"jest": "^27.0.6"
},
"dependencies": {
"ajv": "^8.6.2",
"amqplib": "^0.8.0",
"class-validator": "^0.13.1",
"dot-prop": "^6.0.1",
"env-paths": "^2.2.1",
"jsonwebtoken": "^8.5.1",
"lambert-server": "^1.2.11",
"missing-native-js-functions": "^1.2.17",
@@ -54,7 +47,6 @@
"pg": "^8.7.1",
"reflect-metadata": "^0.1.13",
"sqlite3": "^5.0.2",
"tsconfig-paths": "^3.11.0",
"typeorm": "^0.2.37",
"typescript": "^4.4.2",
"typescript-json-schema": "^0.50.1"
+2 -55
View File
@@ -1,19 +1,8 @@
import "reflect-metadata";
import {
BaseEntity,
BeforeInsert,
BeforeUpdate,
EntityMetadata,
FindConditions,
ObjectIdColumn,
PrimaryColumn,
} from "typeorm";
import { BaseEntity, EntityMetadata, FindConditions, ObjectIdColumn, PrimaryColumn } from "typeorm";
import { Snowflake } from "../util/Snowflake";
import "missing-native-js-functions";
// TODO use class-validator https://typeorm.io/#/validation with class annotators (isPhone/isEmail) combined with types from typescript-json-schema
// btw. we don't use class-validator for everything, because we need to explicitly set the type instead of deriving it from typescript also it doesn't easily support nested objects
export class BaseClassWithoutId extends BaseEntity {
constructor(props?: any) {
super();
@@ -42,7 +31,7 @@ export class BaseClassWithoutId extends BaseEntity {
for (const key in props) {
if (!properties.has(key)) continue;
// @ts-ignore
const setter = this[`set${key.capitalize()}`];
const setter = this[`set${key.capitalize()}`]; // use setter function if it exists
if (setter) {
setter.call(this, props[key]);
@@ -53,12 +42,6 @@ export class BaseClassWithoutId extends BaseEntity {
}
}
@BeforeUpdate()
@BeforeInsert()
validate() {
return this;
}
toJSON(): any {
return Object.fromEntries(
this.metadata.columns // @ts-ignore
@@ -76,42 +59,6 @@ export class BaseClassWithoutId extends BaseEntity {
const repository = this.getRepository();
return repository.decrement(conditions, propertyPath, value);
}
// static async delete<T>(criteria: FindConditions<T>, options?: RemoveOptions) {
// if (!criteria) throw new Error("You need to specify delete criteria");
// const repository = this.getRepository();
// const promises = repository.metadata.relations.map(async (x) => {
// if (x.orphanedRowAction !== "delete") return;
// const foreignKey =
// x.foreignKeys.find((key) => key.entityMetadata === repository.metadata) ||
// x.inverseRelation?.foreignKeys[0]; // find foreign key for this entity
// if (!foreignKey) {
// throw new Error(
// `Foreign key not found for entity ${repository.metadata.name} in relation ${x.propertyName}`
// );
// }
// const id = (criteria as any)[foreignKey.referencedColumnNames[0]];
// if (!id) throw new Error("id missing in criteria options " + foreignKey.referencedColumnNames);
// if (x.relationType === "many-to-many") {
// return getConnection()
// .createQueryBuilder()
// .relation(this, x.propertyName)
// .of(id)
// .remove({ [foreignKey.columnNames[0]]: id });
// } else if (
// x.relationType === "one-to-one" ||
// x.relationType === "many-to-one" ||
// x.relationType === "one-to-many"
// ) {
// return (x.inverseEntityMetadata.target as any).delete({ [foreignKey.columnNames[0]]: id });
// }
// });
// await Promise.all(promises);
// return super.delete(criteria, options);
// }
}
export const PrimaryIdColumn = process.env.DATABASE?.startsWith("mongodb") ? ObjectIdColumn : PrimaryColumn;
-3
View File
@@ -46,9 +46,6 @@ export enum MessageType {
@Entity("messages")
export class Message extends BaseClass {
@Column()
id: string;
@Column({ nullable: true })
@RelationId((message: Message) => message.channel)
channel_id: string;
-3
View File
@@ -3,9 +3,6 @@ import { BaseClass } from "./BaseClass";
@Entity("rate_limits")
export class RateLimit extends BaseClass {
@Column()
id: "global" | "error" | string; // channel_239842397 | guild_238927349823 | webhook_238923423498
@Column() // no relation as it also
executor_id: string;
+6 -3
View File
@@ -198,7 +198,7 @@ export class User extends BaseClass {
// randomly generates a discriminator between 1 and 9999 and checks max five times if it already exists
// if it all five times already exists, abort with USERNAME_TOO_MANY_USERS error
// else just continue
// TODO: is there any better way to generate a random discriminator only once, without checking if it already exists in the mongodb database?
// TODO: is there any better way to generate a random discriminator only once, without checking if it already exists in the database?
for (let tries = 0; tries < 5; tries++) {
discriminator = Math.randomIntBetween(1, 9999).toString().padStart(4, "0");
exists = await User.findOne({ where: { discriminator, username: username }, select: ["id"] });
@@ -219,7 +219,7 @@ export class User extends BaseClass {
// if nsfw_allowed is null/undefined it'll require date_of_birth to set it to true/false
const language = req.language === "en" ? "en-US" : req.language || "en-US";
const user = await new User({
const user = new User({
created_at: new Date(),
username: username,
discriminator,
@@ -246,7 +246,10 @@ export class User extends BaseClass {
},
settings: { ...defaultSettings, locale: language },
fingerprints: [],
}).save();
});
console.log(user);
await user.save();
if (Config.get().guild.autoJoin.enabled) {
for (const guild of Config.get().guild.autoJoin.guilds || []) {
-3
View File
@@ -12,9 +12,6 @@ export enum WebhookType {
@Entity("webhooks")
export class Webhook extends BaseClass {
@Column()
id: string;
@Column({ type: "simple-enum", enum: WebhookType })
type: WebhookType;
-6
View File
@@ -1,12 +1,6 @@
import "reflect-metadata";
// export * as Constants from "../util/Constants";
export * from "./util/index";
export * from "./interfaces/index";
export * from "./entities/index";
export * from "./dtos/index";
// import Config from "../util/Config";
// import db, { MongooseCache, toObject } from "./util/Database";
// export { Config };
+10
View File
@@ -0,0 +1,10 @@
import { Server, traverseDirectory } from "lambert-server";
const DEFAULT_FILTER = /^([^\.].*)(?<!\.d)\.(js)$/;
export function registerRoutes(server: Server, root: string) {
return traverseDirectory(
{ dirname: root, recursive: true, filter: DEFAULT_FILTER },
server.registerRoute.bind(server, root)
);
}
+1
View File
@@ -17,3 +17,4 @@ export * from "./Rights";
export * from "./Snowflake";
export * from "./String";
export * from "./Array";
export * from "./TraverseDirectory";
+2 -2
View File
@@ -1,10 +1,10 @@
{
"include": ["src/**/*.ts", "tests/Test.ts"],
"include": ["src/**/*.ts"],
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
"incremental": true, /* Enable incremental compilation */
"incremental": true /* Enable incremental compilation */,
"target": "ES6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"lib": ["ES2021"] /* Specify library files to be included in the compilation. */,