From 90af904661ac5af8db83dc2e34fb4f6f2d0c9275 Mon Sep 17 00:00:00 2001 From: Lobo Metalúrgico <43734867+LoboMetalurgico@users.noreply.github.com> Date: Fri, 8 Oct 2021 23:45:12 -0300 Subject: (api): inital emojis route implementation --- util/src/entities/Config.ts | 2 ++ 1 file changed, 2 insertions(+) (limited to 'util/src') diff --git a/util/src/entities/Config.ts b/util/src/entities/Config.ts index 921a12c2..ed2bcab3 100644 --- a/util/src/entities/Config.ts +++ b/util/src/entities/Config.ts @@ -64,6 +64,7 @@ export interface ConfigValue { }; guild: { maxRoles: number; + maxEmojis: number; maxMembers: number; maxChannels: number; maxChannelsInCategory: number; @@ -187,6 +188,7 @@ export const DefaultConfigOptions: ConfigValue = { }, guild: { maxRoles: 250, + maxEmojis: 50, // TODO: max emojis per guild per nitro level maxMembers: 250000, maxChannels: 500, maxChannelsInCategory: 50, -- cgit 1.5.1 From 74bd98737acf89d9d56ee66a68694d82f591d591 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 10 Oct 2021 11:03:32 +0200 Subject: :art: clean up imports + classes --- .gitignore | 4 +- api/src/Server.ts | 3 -- api/src/routes/channels/#channel_id/invites.ts | 2 +- .../routes/channels/#channel_id/messages/index.ts | 3 +- api/src/test/jwt.ts | 37 -------------- api/src/test/jwt2.ts | 13 ----- api/src/test/password_test.ts | 12 ----- gateway/src/events/Connection.ts | 3 +- gateway/src/opcodes/LazyRequest.ts | 10 +++- util/src/entities/BaseClass.ts | 58 ++-------------------- util/src/entities/Message.ts | 3 -- util/src/entities/RateLimit.ts | 3 -- util/src/entities/User.ts | 9 ++-- util/src/entities/Webhook.ts | 3 -- util/src/index.ts | 6 --- 15 files changed, 23 insertions(+), 146 deletions(-) delete mode 100644 api/src/test/jwt.ts delete mode 100644 api/src/test/jwt2.ts delete mode 100644 api/src/test/password_test.ts (limited to 'util/src') diff --git a/.gitignore b/.gitignore index f67723af..a09d215a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,6 @@ node_modules api/assets/*.js api/assets/*.css database.db -tsconfig.tsbuildinfo \ No newline at end of file +tsconfig.tsbuildinfo +files/ +.env \ No newline at end of file diff --git a/api/src/Server.ts b/api/src/Server.ts index 12c1d6b4..19e3d245 100644 --- a/api/src/Server.ts +++ b/api/src/Server.ts @@ -1,13 +1,10 @@ -import { OptionsJson } from "body-parser"; import "missing-native-js-functions"; -import { Connection } from "mongoose"; import { Server, ServerOptions } from "lambert-server"; import { Authentication, CORS } from "./middlewares/"; import { Config, initDatabase, initEvent } from "@fosscord/util"; import { ErrorHandler } from "./middlewares/ErrorHandler"; import { BodyParser } from "./middlewares/BodyParser"; import { Router, Request, Response, NextFunction } from "express"; -import mongoose from "mongoose"; import path from "path"; import { initRateLimits } from "./middlewares/RateLimit"; import TestClient from "./middlewares/TestClient"; diff --git a/api/src/routes/channels/#channel_id/invites.ts b/api/src/routes/channels/#channel_id/invites.ts index 22420983..6d2c625d 100644 --- a/api/src/routes/channels/#channel_id/invites.ts +++ b/api/src/routes/channels/#channel_id/invites.ts @@ -2,7 +2,7 @@ import { Router, Request, Response } from "express"; import { HTTPError } from "lambert-server"; import { route } from "@fosscord/api"; import { random } from "@fosscord/api"; -import { getPermission, Channel, Invite, InviteCreateEvent, emitEvent, User, Guild, PublicInviteRelation } from "@fosscord/util"; +import { Channel, Invite, InviteCreateEvent, emitEvent, User, Guild, PublicInviteRelation } from "@fosscord/util"; import { isTextChannel } from "./messages"; const router: Router = Router(); diff --git a/api/src/routes/channels/#channel_id/messages/index.ts b/api/src/routes/channels/#channel_id/messages/index.ts index 4ec31417..26bb9e5d 100644 --- a/api/src/routes/channels/#channel_id/messages/index.ts +++ b/api/src/routes/channels/#channel_id/messages/index.ts @@ -22,7 +22,7 @@ const router: Router = Router(); export default router; -function isTextChannel(type: ChannelType): boolean { +export function isTextChannel(type: ChannelType): boolean { switch (type) { case ChannelType.GUILD_STORE: case ChannelType.GUILD_VOICE: @@ -39,7 +39,6 @@ function isTextChannel(type: ChannelType): boolean { return true; } } -module.exports.isTextChannel = isTextChannel; export interface MessageCreateSchema { content?: string; diff --git a/api/src/test/jwt.ts b/api/src/test/jwt.ts deleted file mode 100644 index bdad513b..00000000 --- a/api/src/test/jwt.ts +++ /dev/null @@ -1,37 +0,0 @@ -const jwa = require("jwa"); - -var STR64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".split(""); - -function base64url(string: string, encoding: string) { - // @ts-ignore - return Buffer.from(string, encoding).toString("base64").replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_"); -} - -function to64String(input: number, current = ""): string { - if (input < 0 && current.length == 0) { - input = input * -1; - } - var modify = input % 64; - var remain = Math.floor(input / 64); - var result = STR64[modify] + current; - return remain <= 0 ? result : to64String(remain, result); -} - -function to64Parse(input: string) { - var result = 0; - var toProc = input.split(""); - var e; - for (e in toProc) { - result = result * 64 + STR64.indexOf(toProc[e]); - } - return result; -} - -// @ts-ignore -const start = `${base64url("311129357362135041")}.${to64String(Date.now())}`; -const signature = jwa("HS256").sign(start, `test`); -const token = `${start}.${signature}`; -console.log(token); - -// MzExMTI5MzU3MzYyMTM1MDQx.XdQb_rA.907VgF60kocnOTl32MSUWGSSzbAytQ0jbt36KjLaxuY -// MzExMTI5MzU3MzYyMTM1MDQx.XdQbaPy.4vGx4L7IuFJGsRe6IL3BeybLIvbx4Vauvx12pwNsy2U diff --git a/api/src/test/jwt2.ts b/api/src/test/jwt2.ts deleted file mode 100644 index e231233d..00000000 --- a/api/src/test/jwt2.ts +++ /dev/null @@ -1,13 +0,0 @@ -import jwt from "jsonwebtoken"; - -const algorithm = "HS256"; -const iat = Math.floor(Date.now() / 1000); - -// @ts-ignore -const token = jwt.sign({ id: "311129357362135041" }, "secret", { - algorithm, -}); -console.log(token); - -const decoded = jwt.verify(token, "secret", { algorithms: [algorithm] }); -console.log(decoded); diff --git a/api/src/test/password_test.ts b/api/src/test/password_test.ts deleted file mode 100644 index 983b18ae..00000000 --- a/api/src/test/password_test.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { checkPassword } from "@fosscord/api"; - -console.log(checkPassword("123456789012345")); -// -> 0.25 -console.log(checkPassword("ABCDEFGHIJKLMOPQ")); -// -> 0.25 -console.log(checkPassword("ABC123___...123")); -// -> -console.log(checkPassword("")); -// -> -// console.log(checkPassword("")); -// // -> diff --git a/gateway/src/events/Connection.ts b/gateway/src/events/Connection.ts index c1a6b618..2cf22f7d 100644 --- a/gateway/src/events/Connection.ts +++ b/gateway/src/events/Connection.ts @@ -24,6 +24,7 @@ export async function Connection( request: IncomingMessage ) { try { + // @ts-ignore socket.on("close", Close); // @ts-ignore socket.on("message", Message); @@ -68,12 +69,10 @@ export async function Connection( }); socket.readyTimeout = setTimeout(() => { - Session.delete({ session_id: socket.session_id }); //should we await? return socket.close(CLOSECODES.Session_timed_out); }, 1000 * 30); } catch (error) { console.error(error); - Session.delete({ session_id: socket.session_id }); //should we await? return socket.close(CLOSECODES.Unknown_error); } } diff --git a/gateway/src/opcodes/LazyRequest.ts b/gateway/src/opcodes/LazyRequest.ts index d37e32da..f5fd561a 100644 --- a/gateway/src/opcodes/LazyRequest.ts +++ b/gateway/src/opcodes/LazyRequest.ts @@ -41,6 +41,7 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) { const items = []; for (const role of roles) { + // @ts-ignore const [role_members, other_members] = partition(members, (m: Member) => m.roles.find((r) => r.id === role.id) ); @@ -53,9 +54,12 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) { groups.push(group); for (const member of role_members) { - member.roles = member.roles.filter((x) => x.id !== guild_id); + member.roles = member.roles.filter((x: Role) => x.id !== guild_id); items.push({ - member: { ...member, roles: member.roles.map((x) => x.id) }, + member: { + ...member, + roles: member.roles.map((x: Role) => x.id), + }, }); } members = other_members; @@ -84,7 +88,9 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) { } function partition(array: T[], isValid: Function) { + // @ts-ignore return array.reduce( + // @ts-ignore ([pass, fail], elem) => { return isValid(elem) ? [[...pass, elem], fail] diff --git a/util/src/entities/BaseClass.ts b/util/src/entities/BaseClass.ts index beccf04b..43b69c76 100644 --- a/util/src/entities/BaseClass.ts +++ b/util/src/entities/BaseClass.ts @@ -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(); @@ -38,11 +27,12 @@ export class BaseClassWithoutId extends BaseEntity { .concat(this.metadata.relations.map((x) => x.propertyName)) ); // will not include relational properties + console.log(properties); 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 +43,6 @@ export class BaseClassWithoutId extends BaseEntity { } } - @BeforeUpdate() - @BeforeInsert() - validate() { - return this; - } - toJSON(): any { return Object.fromEntries( this.metadata.columns // @ts-ignore @@ -76,42 +60,6 @@ export class BaseClassWithoutId extends BaseEntity { const repository = this.getRepository(); return repository.decrement(conditions, propertyPath, value); } - - // static async delete(criteria: FindConditions, 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; diff --git a/util/src/entities/Message.ts b/util/src/entities/Message.ts index 04c3c7aa..c7f7a1d1 100644 --- a/util/src/entities/Message.ts +++ b/util/src/entities/Message.ts @@ -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; diff --git a/util/src/entities/RateLimit.ts b/util/src/entities/RateLimit.ts index fa9c32c1..f5916f6b 100644 --- a/util/src/entities/RateLimit.ts +++ b/util/src/entities/RateLimit.ts @@ -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; diff --git a/util/src/entities/User.ts b/util/src/entities/User.ts index 97564af3..662ab031 100644 --- a/util/src/entities/User.ts +++ b/util/src/entities/User.ts @@ -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 || []) { diff --git a/util/src/entities/Webhook.ts b/util/src/entities/Webhook.ts index 8382435f..9d8609ae 100644 --- a/util/src/entities/Webhook.ts +++ b/util/src/entities/Webhook.ts @@ -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; diff --git a/util/src/index.ts b/util/src/index.ts index fc00d46b..ae0f7e54 100644 --- a/util/src/index.ts +++ b/util/src/index.ts @@ -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 }; -- cgit 1.5.1 From 3b4e026eadd1334f3f075774613ed7e8c4203b40 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 10 Oct 2021 12:35:46 +0200 Subject: :bug: fix tsc compiler --- api/src/Server.ts | 3 ++- bundle/scripts/build.js | 36 +++++++++++++++++++++++++----------- bundle/tsconfig.json | 21 ++++++--------------- cdn/src/Server.ts | 14 ++++++++++---- util/src/util/TraverseDirectory.ts | 10 ++++++++++ util/src/util/index.ts | 1 + 6 files changed, 54 insertions(+), 31 deletions(-) create mode 100644 util/src/util/TraverseDirectory.ts (limited to 'util/src') diff --git a/api/src/Server.ts b/api/src/Server.ts index 19e3d245..1f11a295 100644 --- a/api/src/Server.ts +++ b/api/src/Server.ts @@ -11,6 +11,7 @@ import TestClient from "./middlewares/TestClient"; import { initTranslation } from "./middlewares/Translation"; import morgan from "morgan"; import { initInstance } from "./util/Instance"; +import { registerRoutes } from "@fosscord/util"; export interface FosscordServerOptions extends ServerOptions {} @@ -72,7 +73,7 @@ export class FosscordServer extends Server { await initRateLimits(api); await initTranslation(api); - this.routes = await this.registerRoutes(path.join(__dirname, "routes", "/")); + this.routes = await registerRoutes(this, path.join(__dirname, "routes", "/")); api.use("*", (error: any, req: Request, res: Response, next: NextFunction) => { if (error) return next(error); diff --git a/bundle/scripts/build.js b/bundle/scripts/build.js index 2a82ec57..69111c88 100644 --- a/bundle/scripts/build.js +++ b/bundle/scripts/build.js @@ -2,18 +2,32 @@ const { execSync } = require("child_process"); const path = require("path"); const fse = require("fs-extra"); -const api = path.join(__dirname, "..", "..", "api"); -const dist = path.join(__dirname, "..", "dist"); +fse.copySync(path.join(__dirname, "..", "..", "api", "assets"), path.join(__dirname, "..", "dist", "api", "assets")); +fse.copySync( + path.join(__dirname, "..", "..", "api", "client_test"), + path.join(__dirname, "..", "dist", "api", "client_test") +); +fse.copySync(path.join(__dirname, "..", "..", "api", "locales"), path.join(__dirname, "..", "dist", "api", "locales")); +fse.copySync(path.join(__dirname, "..", "..", "api", "src"), path.join(__dirname, "..", "dist", "api", "src")); +fse.copySync(path.join(__dirname, "..", "..", "util", "src"), path.join(__dirname, "..", "dist", "util", "src")); +fse.copySync(path.join(__dirname, "..", "..", "cdn", "src"), path.join(__dirname, "..", "dist", "cdn", "src")); +fse.copySync(path.join(__dirname, "..", "..", "gateway", "src"), path.join(__dirname, "..", "dist", "gateway", "src")); +fse.copySync(path.join(__dirname, "..", "..", "bundle", "src"), path.join(__dirname, "..", "dist", "bundle", "src")); -fse.copySync(path.join(api, "assets"), path.join(dist, "api", "assets")); -fse.copySync(path.join(api, "client_test"), path.join(dist, "api", "client_test")); -fse.copySync(path.join(api, "locales"), path.join(dist, "api", "locales")); +console.log("Copying src files done"); +console.log("Compiling src files ..."); console.log( - execSync("node " + path.join(__dirname, "..", "node_modules", "typescript", "lib", "tsc.js") + " -p .", { - cwd: path.join(__dirname, ".."), - shell: true, - env: process.env, - encoding: "utf8", - }) + execSync( + "node " + + path.join(__dirname, "..", "node_modules", "typescript", "lib", "tsc.js") + + " -p " + + path.join(__dirname, ".."), + { + cwd: path.join(__dirname, ".."), + shell: true, + env: process.env, + encoding: "utf8", + } + ) ); diff --git a/bundle/tsconfig.json b/bundle/tsconfig.json index 1b2949da..4e8db342 100644 --- a/bundle/tsconfig.json +++ b/bundle/tsconfig.json @@ -1,16 +1,11 @@ { - "include": [ - "../api/src/**/*.ts", - "../gateway/src/**/*.ts", - "../cdn/src/**/*.ts", - "../util/src/**/*.ts", - "src/**/*.ts" - ], + "include": ["dist/**/*.ts"], + "exclude": [], "compilerOptions": { /* Visit https://aka.ms/tsconfig.json to read more about this file */ /* Basic Options */ - "incremental": true /* Enable incremental compilation */, + "incremental": false /* 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. */, @@ -22,7 +17,7 @@ "sourceMap": false /* Generates corresponding '.map' file. */, // "outFile": "./", /* Concatenate and emit output to single file. */ "outDir": "./dist/" /* Redirect output structure to the directory. */, - "rootDir": "../" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */, + "rootDir": "./dist/" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */, // "composite": true, /* Enable project compilation */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ // "removeComments": true, /* Do not emit comments to output. */ @@ -73,16 +68,12 @@ "emitDecoratorMetadata": true, "experimentalDecorators": true, "resolveJsonModule": true, - "baseUrl": "..", + "baseUrl": "./dist/", "paths": { "@fosscord/api": ["api/src/index"], - "@fosscord/api/*": ["api/src/*"], "@fosscord/gateway": ["gateway/src/index"], - "@fosscord/gateway/*": ["gateway/src/*"], "@fosscord/cdn": ["cdn/src/index"], - "@fosscord/cdn/*": ["cdn/src/*"], - "@fosscord/util": ["util/src/index"], - "@fosscord/util/*": ["util/src/*"] + "@fosscord/util": ["util/src/index"] }, "plugins": [{ "transform": "@zerollup/ts-transform-paths" }] } diff --git a/cdn/src/Server.ts b/cdn/src/Server.ts index 590eda6f..cac34a80 100644 --- a/cdn/src/Server.ts +++ b/cdn/src/Server.ts @@ -1,5 +1,5 @@ import { Server, ServerOptions } from "lambert-server"; -import { Config, initDatabase } from "@fosscord/util"; +import { Config, initDatabase, registerRoutes } from "@fosscord/util"; import path from "path"; import avatarsRoute from "./routes/avatars"; import bodyParser from "body-parser"; @@ -23,13 +23,19 @@ export class CDNServer extends Server { "Content-security-policy", "default-src * data: blob: filesystem: about: ws: wss: 'unsafe-inline' 'unsafe-eval'; script-src * data: blob: 'unsafe-inline' 'unsafe-eval'; connect-src * data: blob: 'unsafe-inline'; img-src * data: blob: 'unsafe-inline'; frame-src * data: blob: ; style-src * data: blob: 'unsafe-inline'; font-src * data: blob: 'unsafe-inline';" ); - res.set("Access-Control-Allow-Headers", req.header("Access-Control-Request-Headers") || "*"); - res.set("Access-Control-Allow-Methods", req.header("Access-Control-Request-Methods") || "*"); + res.set( + "Access-Control-Allow-Headers", + req.header("Access-Control-Request-Headers") || "*" + ); + res.set( + "Access-Control-Allow-Methods", + req.header("Access-Control-Request-Methods") || "*" + ); next(); }); this.app.use(bodyParser.json({ inflate: true, limit: "10mb" })); - await this.registerRoutes(path.join(__dirname, "routes/")); + await registerRoutes(this, path.join(__dirname, "routes/")); this.app.use("/icons/", avatarsRoute); this.log("verbose", "[Server] Route /icons registered"); diff --git a/util/src/util/TraverseDirectory.ts b/util/src/util/TraverseDirectory.ts new file mode 100644 index 00000000..275b7dcc --- /dev/null +++ b/util/src/util/TraverseDirectory.ts @@ -0,0 +1,10 @@ +import { Server, traverseDirectory } from "lambert-server"; + +const DEFAULT_FILTER = /^([^\.].*)(? Date: Sun, 10 Oct 2021 12:42:12 +0200 Subject: :see_no_evil: remove console.log --- util/src/entities/BaseClass.ts | 1 - 1 file changed, 1 deletion(-) (limited to 'util/src') diff --git a/util/src/entities/BaseClass.ts b/util/src/entities/BaseClass.ts index 43b69c76..d20078e5 100644 --- a/util/src/entities/BaseClass.ts +++ b/util/src/entities/BaseClass.ts @@ -27,7 +27,6 @@ export class BaseClassWithoutId extends BaseEntity { .concat(this.metadata.relations.map((x) => x.propertyName)) ); // will not include relational properties - console.log(properties); for (const key in props) { if (!properties.has(key)) continue; -- cgit 1.5.1 From 33103cc452cce0c7b91853d5f5466c1702ee576d Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 10 Oct 2021 14:08:52 +0200 Subject: :bug: fix cdn --- util/src/util/cdn.ts | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) (limited to 'util/src') diff --git a/util/src/util/cdn.ts b/util/src/util/cdn.ts index 8d45f85f..4dd0078a 100644 --- a/util/src/util/cdn.ts +++ b/util/src/util/cdn.ts @@ -25,30 +25,15 @@ export async function uploadFile(path: string, file: Express.Multer.File) { return result; } -export async function handleFile( - path: string, - body?: string -): Promise< - | (string & { - id: string; - content_type: string; - size: number; - url: string; - }) - | undefined -> { +export async function handleFile(path: string, body?: string): Promise { if (!body || !body.startsWith("data:")) return undefined; try { const mimetype = body.split(":")[1].split(";")[0]; const buffer = Buffer.from(body.split(",")[1], "base64"); // @ts-ignore - const file = await uploadFile(path, { buffer, mimetype, originalname: "banner" }); - const obj = file.id; - for (const key in file) { - obj[key] = file[key]; - } - return obj; + const { id } = await uploadFile(path, { buffer, mimetype, originalname: "banner" }); + return id; } catch (error) { console.error(error); throw new HTTPError("Invalid " + path); -- cgit 1.5.1 From 2f623b2a2276321b74c4601f67f5236be2ae5243 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 10 Oct 2021 14:09:54 +0200 Subject: :art: emoji db migration --- gateway/src/listener/listener.ts | 2 +- util/ormconfig.json | 9 ++ util/package-lock.json | 168 +++++++++++++++++++++++- util/package.json | 6 +- util/src/entities/Emoji.ts | 14 +- util/src/interfaces/Event.ts | 8 +- util/src/migrations/1633864260873-EmojiRoles.ts | 13 ++ util/src/migrations/1633864669243-EmojiUser.ts | 23 ++++ 8 files changed, 234 insertions(+), 9 deletions(-) create mode 100644 util/ormconfig.json create mode 100644 util/src/migrations/1633864260873-EmojiRoles.ts create mode 100644 util/src/migrations/1633864669243-EmojiUser.ts (limited to 'util/src') diff --git a/gateway/src/listener/listener.ts b/gateway/src/listener/listener.ts index ee640f38..c5b1a576 100644 --- a/gateway/src/listener/listener.ts +++ b/gateway/src/listener/listener.ts @@ -178,7 +178,7 @@ async function consume(this: WebSocket, opts: EventOpts) { case "CHANNEL_CREATE": case "CHANNEL_DELETE": case "CHANNEL_UPDATE": - case "GUILD_EMOJI_UPDATE": + case "GUILD_EMOJIS_UPDATE": case "READY": // will be sent by the gateway case "USER_UPDATE": case "APPLICATION_COMMAND_CREATE": diff --git a/util/ormconfig.json b/util/ormconfig.json new file mode 100644 index 00000000..c5587b8e --- /dev/null +++ b/util/ormconfig.json @@ -0,0 +1,9 @@ +{ + "type": "sqlite", + "database": "../bundle/database.db", + "migrations": ["src/migrations/*.ts"], + "entities": ["src/entities/*.ts"], + "cli": { + "migrationsDir": "src/migrations" + } +} diff --git a/util/package-lock.json b/util/package-lock.json index fa4549c6..5f136dbc 100644 --- a/util/package-lock.json +++ b/util/package-lock.json @@ -31,7 +31,8 @@ "@types/multer": "^1.4.7", "@types/node": "^14.17.9", "@types/node-fetch": "^2.5.12", - "jest": "^27.0.6" + "jest": "^27.0.6", + "ts-node": "^10.2.1" } }, "node_modules/@babel/code-frame": { @@ -666,6 +667,27 @@ "integrity": "sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==", "dev": true }, + "node_modules/@cspotcode/source-map-consumer": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-consumer/-/source-map-consumer-0.8.0.tgz", + "integrity": "sha512-41qniHzTU8yAGbCp04ohlmSrZf8bkf/iJsl3V0dRGsQN/5GFfx+LbCSsCpp2gqrqjTVg/K6O8ycoV35JIwAzAg==", + "dev": true, + "engines": { + "node": ">= 12" + } + }, + "node_modules/@cspotcode/source-map-support": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.6.1.tgz", + "integrity": "sha512-DX3Z+T5dt1ockmPdobJS/FAsQPW4V4SrWEhD2iYQT2Cb2tQsiMnYxrcUH9By/Z3B+v0S5LMBkQtV/XOBbpLEOg==", + "dev": true, + "dependencies": { + "@cspotcode/source-map-consumer": "0.8.0" + }, + "engines": { + "node": ">=12" + } + }, "node_modules/@istanbuljs/load-nyc-config": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz", @@ -1003,6 +1025,30 @@ "node": ">= 6" } }, + "node_modules/@tsconfig/node10": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.8.tgz", + "integrity": "sha512-6XFfSQmMgq0CFLY1MslA/CPUfhIL919M1rMsa5lP2P097N2Wd1sSX0tx1u4olM16fLNhtHZpRhedZJphNJqmZg==", + "dev": true + }, + "node_modules/@tsconfig/node12": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.9.tgz", + "integrity": "sha512-/yBMcem+fbvhSREH+s14YJi18sp7J9jpuhYByADT2rypfajMZZN4WQ6zBGgBKp53NKmqI36wFYDb3yaMPurITw==", + "dev": true + }, + "node_modules/@tsconfig/node14": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.1.tgz", + "integrity": "sha512-509r2+yARFfHHE7T6Puu2jjkoycftovhXRqW328PDXTVGKihlb1P8Z9mMZH04ebyajfRY7dedfGynlrFHJUQCg==", + "dev": true + }, + "node_modules/@tsconfig/node16": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.2.tgz", + "integrity": "sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==", + "dev": true + }, "node_modules/@types/amqplib": { "version": "0.8.2", "resolved": "https://registry.npmjs.org/@types/amqplib/-/amqplib-0.8.2.tgz", @@ -6431,6 +6477,59 @@ "node": ">=8" } }, + "node_modules/ts-node": { + "version": "10.2.1", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.2.1.tgz", + "integrity": "sha512-hCnyOyuGmD5wHleOQX6NIjJtYVIO8bPP8F2acWkB4W06wdlkgyvJtubO/I9NkI88hCFECbsEgoLc0VNkYmcSfw==", + "dev": true, + "dependencies": { + "@cspotcode/source-map-support": "0.6.1", + "@tsconfig/node10": "^1.0.7", + "@tsconfig/node12": "^1.0.7", + "@tsconfig/node14": "^1.0.0", + "@tsconfig/node16": "^1.0.2", + "acorn": "^8.4.1", + "acorn-walk": "^8.1.1", + "arg": "^4.1.0", + "create-require": "^1.1.0", + "diff": "^4.0.1", + "make-error": "^1.1.1", + "yn": "3.1.1" + }, + "bin": { + "ts-node": "dist/bin.js", + "ts-node-cwd": "dist/bin-cwd.js", + "ts-node-script": "dist/bin-script.js", + "ts-node-transpile-only": "dist/bin-transpile.js", + "ts-script": "dist/bin-script-deprecated.js" + }, + "engines": { + "node": ">=12.0.0" + }, + "peerDependencies": { + "@swc/core": ">=1.2.50", + "@swc/wasm": ">=1.2.50", + "@types/node": "*", + "typescript": ">=2.7" + }, + "peerDependenciesMeta": { + "@swc/core": { + "optional": true + }, + "@swc/wasm": { + "optional": true + } + } + }, + "node_modules/ts-node/node_modules/acorn-walk": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.2.0.tgz", + "integrity": "sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA==", + "dev": true, + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/tslib": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.1.tgz", @@ -7733,6 +7832,21 @@ "integrity": "sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==", "dev": true }, + "@cspotcode/source-map-consumer": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-consumer/-/source-map-consumer-0.8.0.tgz", + "integrity": "sha512-41qniHzTU8yAGbCp04ohlmSrZf8bkf/iJsl3V0dRGsQN/5GFfx+LbCSsCpp2gqrqjTVg/K6O8ycoV35JIwAzAg==", + "dev": true + }, + "@cspotcode/source-map-support": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.6.1.tgz", + "integrity": "sha512-DX3Z+T5dt1ockmPdobJS/FAsQPW4V4SrWEhD2iYQT2Cb2tQsiMnYxrcUH9By/Z3B+v0S5LMBkQtV/XOBbpLEOg==", + "dev": true, + "requires": { + "@cspotcode/source-map-consumer": "0.8.0" + } + }, "@istanbuljs/load-nyc-config": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz", @@ -8003,6 +8117,30 @@ "integrity": "sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==", "dev": true }, + "@tsconfig/node10": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.8.tgz", + "integrity": "sha512-6XFfSQmMgq0CFLY1MslA/CPUfhIL919M1rMsa5lP2P097N2Wd1sSX0tx1u4olM16fLNhtHZpRhedZJphNJqmZg==", + "dev": true + }, + "@tsconfig/node12": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.9.tgz", + "integrity": "sha512-/yBMcem+fbvhSREH+s14YJi18sp7J9jpuhYByADT2rypfajMZZN4WQ6zBGgBKp53NKmqI36wFYDb3yaMPurITw==", + "dev": true + }, + "@tsconfig/node14": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.1.tgz", + "integrity": "sha512-509r2+yARFfHHE7T6Puu2jjkoycftovhXRqW328PDXTVGKihlb1P8Z9mMZH04ebyajfRY7dedfGynlrFHJUQCg==", + "dev": true + }, + "@tsconfig/node16": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.2.tgz", + "integrity": "sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==", + "dev": true + }, "@types/amqplib": { "version": "0.8.2", "resolved": "https://registry.npmjs.org/@types/amqplib/-/amqplib-0.8.2.tgz", @@ -12301,6 +12439,34 @@ "punycode": "^2.1.1" } }, + "ts-node": { + "version": "10.2.1", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.2.1.tgz", + "integrity": "sha512-hCnyOyuGmD5wHleOQX6NIjJtYVIO8bPP8F2acWkB4W06wdlkgyvJtubO/I9NkI88hCFECbsEgoLc0VNkYmcSfw==", + "dev": true, + "requires": { + "@cspotcode/source-map-support": "0.6.1", + "@tsconfig/node10": "^1.0.7", + "@tsconfig/node12": "^1.0.7", + "@tsconfig/node14": "^1.0.0", + "@tsconfig/node16": "^1.0.2", + "acorn": "^8.4.1", + "acorn-walk": "^8.1.1", + "arg": "^4.1.0", + "create-require": "^1.1.0", + "diff": "^4.0.1", + "make-error": "^1.1.1", + "yn": "3.1.1" + }, + "dependencies": { + "acorn-walk": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.2.0.tgz", + "integrity": "sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA==", + "dev": true + } + } + }, "tslib": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.1.tgz", diff --git a/util/package.json b/util/package.json index 5efc16ae..e1003114 100644 --- a/util/package.json +++ b/util/package.json @@ -8,7 +8,8 @@ "start": "npm run build && node dist/", "test": "npm run build && jest", "postinstall": "npm run build", - "build": "npx tsc -p ." + "build": "npx tsc -p .", + "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js" }, "repository": { "type": "git", @@ -33,7 +34,8 @@ "@types/multer": "^1.4.7", "@types/node": "^14.17.9", "@types/node-fetch": "^2.5.12", - "jest": "^27.0.6" + "jest": "^27.0.6", + "ts-node": "^10.2.1" }, "dependencies": { "amqplib": "^0.8.0", diff --git a/util/src/entities/Emoji.ts b/util/src/entities/Emoji.ts index a252d9f4..03218375 100644 --- a/util/src/entities/Emoji.ts +++ b/util/src/entities/Emoji.ts @@ -1,4 +1,5 @@ -import { Column, Entity, JoinColumn, ManyToOne } from "typeorm"; +import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; +import { User } from "."; import { BaseClass } from "./BaseClass"; import { Guild } from "./Guild"; import { Role } from "./Role"; @@ -20,6 +21,14 @@ export class Emoji extends BaseClass { }) guild: Guild; + @Column({ nullable: true }) + @RelationId((emoji: Emoji) => emoji.user) + user_id: string; + + @JoinColumn({ name: "user_id" }) + @ManyToOne(() => User) + user: User; + @Column() managed: boolean; @@ -28,4 +37,7 @@ export class Emoji extends BaseClass { @Column() require_colons: boolean; + + @Column({ type: "simple-array" }) + roles: string[]; // roles this emoji is whitelisted to (new discord feature?) } diff --git a/util/src/interfaces/Event.ts b/util/src/interfaces/Event.ts index 03099bbb..3c8ab8ab 100644 --- a/util/src/interfaces/Event.ts +++ b/util/src/interfaces/Event.ts @@ -185,8 +185,8 @@ export interface GuildBanRemoveEvent extends Event { }; } -export interface GuildEmojiUpdateEvent extends Event { - event: "GUILD_EMOJI_UPDATE"; +export interface GuildEmojisUpdateEvent extends Event { + event: "GUILD_EMOJIS_UPDATE"; data: { guild_id: string; emojis: Emoji[]; @@ -459,7 +459,7 @@ export type EventData = | GuildDeleteEvent | GuildBanAddEvent | GuildBanRemoveEvent - | GuildEmojiUpdateEvent + | GuildEmojisUpdateEvent | GuildIntegrationUpdateEvent | GuildMemberAddEvent | GuildMemberRemoveEvent @@ -552,7 +552,7 @@ export type EVENT = | "GUILD_DELETE" | "GUILD_BAN_ADD" | "GUILD_BAN_REMOVE" - | "GUILD_EMOJI_UPDATE" + | "GUILD_EMOJIS_UPDATE" | "GUILD_INTEGRATIONS_UPDATE" | "GUILD_MEMBER_ADD" | "GUILD_MEMBER_REMOVE" diff --git a/util/src/migrations/1633864260873-EmojiRoles.ts b/util/src/migrations/1633864260873-EmojiRoles.ts new file mode 100644 index 00000000..f0d709f2 --- /dev/null +++ b/util/src/migrations/1633864260873-EmojiRoles.ts @@ -0,0 +1,13 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class EmojiRoles1633864260873 implements MigrationInterface { + name = "EmojiRoles1633864260873"; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "emojis" ADD "roles" text NOT NULL DEFAULT ''`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "emojis" DROP COLUMN column_name "roles"`); + } +} diff --git a/util/src/migrations/1633864669243-EmojiUser.ts b/util/src/migrations/1633864669243-EmojiUser.ts new file mode 100644 index 00000000..982405d7 --- /dev/null +++ b/util/src/migrations/1633864669243-EmojiUser.ts @@ -0,0 +1,23 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class EmojiUser1633864669243 implements MigrationInterface { + name = "EmojiUser1633864669243"; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "emojis" ADD "user_id" varchar`); + try { + await queryRunner.query( + `ALTER TABLE "emojis" ADD CONSTRAINT FK_fa7ddd5f9a214e28ce596548421 FOREIGN KEY (user_id) REFERENCES users(id)` + ); + } catch (error) { + console.error( + "sqlite doesn't support altering foreign keys: https://stackoverflow.com/questions/1884818/how-do-i-add-a-foreign-key-to-an-existing-sqlite-table" + ); + } + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "emojis" DROP COLUMN column_name "user_id"`); + await queryRunner.query(`ALTER TABLE "emojis" DROP CONSTRAINT FK_fa7ddd5f9a214e28ce596548421`); + } +} -- cgit 1.5.1 From 82205520ed302aa5a91fc6a770e7ecf2b6ccc043 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 10 Oct 2021 14:31:13 +0200 Subject: :bug: fix Emoji missing in identify --- bundle/package.json | 2 +- gateway/src/opcodes/Identify.ts | 1 + util/src/entities/User.ts | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) (limited to 'util/src') diff --git a/bundle/package.json b/bundle/package.json index 3d41f7c2..eedbdd8c 100644 --- a/bundle/package.json +++ b/bundle/package.json @@ -9,7 +9,7 @@ "start": "node scripts/build.js && node dist/bundle/src/start.js", "start:bundle": "node dist/bundle/src/start.js", "test": "echo \"Error: no test specified\" && exit 1", - "migrate": "node node_modules/typeorm/cli.js -f ../util/ormconfig.json migration:run" + "migrate": "node --require ts-node/register node_modules/typeorm/cli.js -f ../util/ormconfig.json migration:run" }, "repository": { "type": "git", diff --git a/gateway/src/opcodes/Identify.ts b/gateway/src/opcodes/Identify.ts index 16ea1904..5950105a 100644 --- a/gateway/src/opcodes/Identify.ts +++ b/gateway/src/opcodes/Identify.ts @@ -65,6 +65,7 @@ export async function onIdentify(this: WebSocket, data: Payload) { "guild", "guild.channels", "guild.emojis", + "guild.emojis.user", "guild.roles", "guild.stickers", "user", diff --git a/util/src/entities/User.ts b/util/src/entities/User.ts index 662ab031..04f1e9cb 100644 --- a/util/src/entities/User.ts +++ b/util/src/entities/User.ts @@ -248,7 +248,6 @@ export class User extends BaseClass { fingerprints: [], }); - console.log(user); await user.save(); if (Config.get().guild.autoJoin.enabled) { -- cgit 1.5.1 From 79aee5145b6b289a001caae516477c8ab641bb8f Mon Sep 17 00:00:00 2001 From: The Arcane Brony Date: Sun, 10 Oct 2021 17:55:48 +0200 Subject: Fix duplicate key --- api/assets/schemas.json | 2 +- api/src/routes/guilds/#guild_id/vanity-url.ts | 9 +++------ api/src/routes/invites/index.ts | 1 - bundle/scripts/build.js | 27 +++++++++++++++++++++------ util/src/entities/Guild.ts | 8 -------- util/src/entities/Invite.ts | 3 +++ 6 files changed, 28 insertions(+), 22 deletions(-) (limited to 'util/src') diff --git a/api/assets/schemas.json b/api/assets/schemas.json index 4f1ab9a8..a12925e0 100644 --- a/api/assets/schemas.json +++ b/api/assets/schemas.json @@ -4744,7 +4744,7 @@ "type": "string" }, "permissions": { - "type": "bigint" + "type": "array" }, "color": { "type": "integer" diff --git a/api/src/routes/guilds/#guild_id/vanity-url.ts b/api/src/routes/guilds/#guild_id/vanity-url.ts index 7f2cea9e..061b317c 100644 --- a/api/src/routes/guilds/#guild_id/vanity-url.ts +++ b/api/src/routes/guilds/#guild_id/vanity-url.ts @@ -10,10 +10,10 @@ const InviteRegex = /\W/g; router.get("/", route({ permission: "MANAGE_GUILD" }), async (req: Request, res: Response) => { const { guild_id } = req.params; - const guild = await Guild.findOneOrFail({ where: { id: guild_id }, relations: ["vanity_url"] }); - if (!guild.vanity_url) return res.json({ code: null }); + const invite = await Invite.findOne({ where: {guild_id: guild_id, vanity_url: true} }); + if (!invite) return res.json({ code: null }); - return res.json({ code: guild.vanity_url_code, uses: guild.vanity_url.uses }); + return res.json({ code: invite.code, uses: invite.uses }); }); export interface VanityUrlSchema { @@ -33,12 +33,9 @@ router.patch("/", route({ body: "VanityUrlSchema", permission: "MANAGE_GUILD" }) const invite = await Invite.findOne({ code }); if (invite) throw new HTTPError("Invite already exists"); - const guild = await Guild.findOneOrFail({ id: guild_id }); const { id } = await Channel.findOneOrFail({ guild_id, type: ChannelType.GUILD_TEXT }); Promise.all([ - Guild.update({ id: guild_id }, { vanity_url_code: code }), - Invite.delete({ code: guild.vanity_url_code }), new Invite({ code: code, uses: 0, diff --git a/api/src/routes/invites/index.ts b/api/src/routes/invites/index.ts index 0fcf7c86..185311bc 100644 --- a/api/src/routes/invites/index.ts +++ b/api/src/routes/invites/index.ts @@ -33,7 +33,6 @@ router.delete("/:code", route({}), async (req: Request, res: Response) => { await Promise.all([ Invite.delete({ code }), - Guild.update({ vanity_url_code: code }, { vanity_url_code: undefined }), emitEvent({ event: "INVITE_DELETE", guild_id: guild_id, diff --git a/bundle/scripts/build.js b/bundle/scripts/build.js index 69111c88..a9798eff 100644 --- a/bundle/scripts/build.js +++ b/bundle/scripts/build.js @@ -1,6 +1,22 @@ const { execSync } = require("child_process"); const path = require("path"); const fse = require("fs-extra"); +const { getSystemErrorMap } = require("util"); +const { argv } = require("process"); + +const dirs = ["api", "util", "cdn", "gateway", "bundle"]; + +const verbose = argv.includes("verbose") || argv.includes("v"); + +if(argv.includes("clean")){ + dirs.forEach(a=>{ + var d = "../"+a+"/dist"; + if(fse.existsSync(d)) { + fse.rmSync(d,{recursive: true}); + if(verbose) console.log(`Deleted ${d}!`); + } + }); +} fse.copySync(path.join(__dirname, "..", "..", "api", "assets"), path.join(__dirname, "..", "dist", "api", "assets")); fse.copySync( @@ -8,13 +24,12 @@ fse.copySync( path.join(__dirname, "..", "dist", "api", "client_test") ); fse.copySync(path.join(__dirname, "..", "..", "api", "locales"), path.join(__dirname, "..", "dist", "api", "locales")); -fse.copySync(path.join(__dirname, "..", "..", "api", "src"), path.join(__dirname, "..", "dist", "api", "src")); -fse.copySync(path.join(__dirname, "..", "..", "util", "src"), path.join(__dirname, "..", "dist", "util", "src")); -fse.copySync(path.join(__dirname, "..", "..", "cdn", "src"), path.join(__dirname, "..", "dist", "cdn", "src")); -fse.copySync(path.join(__dirname, "..", "..", "gateway", "src"), path.join(__dirname, "..", "dist", "gateway", "src")); -fse.copySync(path.join(__dirname, "..", "..", "bundle", "src"), path.join(__dirname, "..", "dist", "bundle", "src")); +dirs.forEach(a=>{ + fse.copySync("../"+a+"/src", "dist/"+a+"/src"); + if(verbose) console.log(`Copied ${"../"+a+"/dist"} -> ${"dist/"+a+"/src"}!`); +}); -console.log("Copying src files done"); +console.log("Copying src files done"); console.log("Compiling src files ..."); console.log( diff --git a/util/src/entities/Guild.ts b/util/src/entities/Guild.ts index 35595191..157f0921 100644 --- a/util/src/entities/Guild.ts +++ b/util/src/entities/Guild.ts @@ -257,14 +257,6 @@ export class Guild extends BaseClass { @Column({ nullable: true }) unavailable?: boolean; - @Column({ nullable: true }) - @RelationId((guild: Guild) => guild.vanity_url) - vanity_url_code?: string; - - @JoinColumn({ name: "vanity_url_code" }) - @ManyToOne(() => Invite) - vanity_url?: Invite; - @Column({ nullable: true }) verification_level?: number; diff --git a/util/src/entities/Invite.ts b/util/src/entities/Invite.ts index 82556fab..d6b8f2f8 100644 --- a/util/src/entities/Invite.ts +++ b/util/src/entities/Invite.ts @@ -71,6 +71,9 @@ export class Invite extends BaseClass { @Column({ nullable: true }) target_user_type?: number; + @Column({ nullable: true}) + vanity_url?: boolean; + static async joinGuild(user_id: string, code: string) { const invite = await Invite.findOneOrFail({ code }); if (invite.uses++ >= invite.max_uses && invite.max_uses !== 0) await Invite.delete({ code }); -- cgit 1.5.1 From d5fcad37389544d418c1894b796fdf66f3955e87 Mon Sep 17 00:00:00 2001 From: The Arcane Brony Date: Sun, 10 Oct 2021 18:12:51 +0200 Subject: forgot to fix primary columns on invites --- util/src/entities/Invite.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'util/src') diff --git a/util/src/entities/Invite.ts b/util/src/entities/Invite.ts index d6b8f2f8..b3e00957 100644 --- a/util/src/entities/Invite.ts +++ b/util/src/entities/Invite.ts @@ -1,6 +1,6 @@ -import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; +import { Column, Entity, JoinColumn, ManyToOne, RelationId, PrimaryColumn } from "typeorm"; import { Member } from "./Member"; -import { BaseClass, PrimaryIdColumn } from "./BaseClass"; +import { BaseClassWithoutId } from "./BaseClass"; import { Channel } from "./Channel"; import { Guild } from "./Guild"; import { User } from "./User"; @@ -8,8 +8,8 @@ import { User } from "./User"; export const PublicInviteRelation = ["inviter", "guild", "channel"]; @Entity("invites") -export class Invite extends BaseClass { - @PrimaryIdColumn() +export class Invite extends BaseClassWithoutId { + @PrimaryColumn() code: string; @Column() -- cgit 1.5.1 From fd152e8b6df18c9d98d02fed9e446de9690650cd Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 10 Oct 2021 18:28:50 +0200 Subject: added vanity db migration --- bundle/package.json | 2 +- util/src/migrations/1633881705509-VanityInvite.ts | 17 +++++++++++++++++ util/src/migrations/migrate_db_engine.ts | 21 ++++++++++----------- 3 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 util/src/migrations/1633881705509-VanityInvite.ts (limited to 'util/src') diff --git a/bundle/package.json b/bundle/package.json index eedbdd8c..404c6758 100644 --- a/bundle/package.json +++ b/bundle/package.json @@ -9,7 +9,7 @@ "start": "node scripts/build.js && node dist/bundle/src/start.js", "start:bundle": "node dist/bundle/src/start.js", "test": "echo \"Error: no test specified\" && exit 1", - "migrate": "node --require ts-node/register node_modules/typeorm/cli.js -f ../util/ormconfig.json migration:run" + "migrate": "cd ../util/ && npm i && node --require ts-node/register node_modules/typeorm/cli.js -f ../util/ormconfig.json migration:run" }, "repository": { "type": "git", diff --git a/util/src/migrations/1633881705509-VanityInvite.ts b/util/src/migrations/1633881705509-VanityInvite.ts new file mode 100644 index 00000000..af9b98ae --- /dev/null +++ b/util/src/migrations/1633881705509-VanityInvite.ts @@ -0,0 +1,17 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class VanityInvite1633881705509 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + try { + await queryRunner.query(`ALTER TABLE "emojis" DROP COLUMN vanity_url_code`); + await queryRunner.query(`ALTER TABLE "emojis" DROP CONSTRAINT FK_c2c1809d79eb120ea0cb8d342ad`); + } catch (error) {} + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "emojis" ADD vanity_url_code varchar`); + await queryRunner.query( + `ALTER TABLE "emojis" ADD CONSTRAINT FK_c2c1809d79eb120ea0cb8d342ad FOREIGN KEY ("vanity_url_code") REFERENCES "invites"("code") ON DELETE NO ACTION ON UPDATE NO ACTION` + ); + } +} diff --git a/util/src/migrations/migrate_db_engine.ts b/util/src/migrations/migrate_db_engine.ts index 33024a8d..cd578fb2 100644 --- a/util/src/migrations/migrate_db_engine.ts +++ b/util/src/migrations/migrate_db_engine.ts @@ -1,6 +1,6 @@ import { config } from "dotenv"; config(); -import { BaseEntity, createConnection, EntityTarget } from "typeorm"; +import { createConnection, EntityTarget } from "typeorm"; import { initDatabase } from "../util/Database"; import "missing-native-js-functions"; import { @@ -15,7 +15,6 @@ import { Invite, Member, Message, - RateLimit, ReadState, Recipient, Relationship, @@ -30,9 +29,9 @@ import { } from ".."; async function main() { - if (!process.env.FROM) throw new Error("FROM database env connection string not set"); + if (!process.env.TO) throw new Error("TO database env connection string not set"); - // manually arrange them because of foreign key + // manually arrange them because of foreign keys const entities = [ User, Guild, @@ -57,12 +56,12 @@ async function main() { Attachment, ]; - const newDB = await initDatabase(); + const oldDB = await initDatabase(); // @ts-ignore - const oldDB = await createConnection({ - type: process.env.FROM.split(":")[0]?.replace("+srv", ""), - url: process.env.FROM, + const newDB = await createConnection({ + type: process.env.TO.split(":")[0]?.replace("+srv", ""), + url: process.env.TO, entities, name: "old", }); @@ -73,13 +72,12 @@ async function main() { const entity = e as EntityTarget; const entries = await oldDB.manager.find(entity); //@ts-ignore - console.log("migrated " + entries.length + " " + entity.name); + console.log("migrating " + entries.length + " " + entity.name + " ..."); for (const entry of entries) { console.log(i++); if (entry instanceof User) { - console.log("instance of User"); if (entry.bio == null) entry.bio = ""; if (entry.rights == null) entry.rights = "0"; if (entry.disabled == null) entry.disabled = false; @@ -115,8 +113,9 @@ async function main() { // await newDB.manager.update(entity, { id: entry.id }, entry); // } } + // @ts-ignore - console.log("migrated all " + entity.name); + console.log("migrating " + entries.length + " " + entity.name + " ..."); } } catch (error) { console.error((error as any).message); -- cgit 1.5.1 From 0cabe8ed4c4cf8ce5ff1bca5bac1454fecfde77e Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 10 Oct 2021 18:31:04 +0200 Subject: :art: update migration script --- util/src/migrations/migrate_db_engine.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'util/src') diff --git a/util/src/migrations/migrate_db_engine.ts b/util/src/migrations/migrate_db_engine.ts index 33024a8d..527e7b53 100644 --- a/util/src/migrations/migrate_db_engine.ts +++ b/util/src/migrations/migrate_db_engine.ts @@ -15,7 +15,6 @@ import { Invite, Member, Message, - RateLimit, ReadState, Recipient, Relationship, @@ -30,7 +29,7 @@ import { } from ".."; async function main() { - if (!process.env.FROM) throw new Error("FROM database env connection string not set"); + if (!process.env.TO) throw new Error("TO database env connection string not set"); // manually arrange them because of foreign key const entities = [ @@ -59,10 +58,14 @@ async function main() { const newDB = await initDatabase(); + const type = process.env.TO.includes("://") ? process.env.TO.split(":")[0]?.replace("+srv", "") : "sqlite"; + const isSqlite = type.includes("sqlite"); + // @ts-ignore const oldDB = await createConnection({ - type: process.env.FROM.split(":")[0]?.replace("+srv", ""), - url: process.env.FROM, + type, + url: isSqlite ? undefined : process.env.TO, + database: isSqlite ? process.env.TO : undefined, entities, name: "old", }); @@ -72,14 +75,13 @@ async function main() { for (const e of entities) { const entity = e as EntityTarget; const entries = await oldDB.manager.find(entity); - //@ts-ignore - console.log("migrated " + entries.length + " " + entity.name); + // @ts-ignore + console.log("migrating " + entries.length + " " + entity.name + " ..."); for (const entry of entries) { console.log(i++); if (entry instanceof User) { - console.log("instance of User"); if (entry.bio == null) entry.bio = ""; if (entry.rights == null) entry.rights = "0"; if (entry.disabled == null) entry.disabled = false; @@ -116,7 +118,7 @@ async function main() { // } } // @ts-ignore - console.log("migrated all " + entity.name); + console.log("migrated " + entries.length + " " + entity.name); } } catch (error) { console.error((error as any).message); -- cgit 1.5.1 From aa25dac99f9ab2c595d9deeb4067f753796438a1 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 10 Oct 2021 19:00:50 +0200 Subject: :bug: fix modify role.permissions --- api/assets/schemas.json | 5 +- api/src/routes/guilds/#guild_id/roles.ts | 6 +- util/src/migrations/migrate_db_engine.js | 104 ++++++++++++++++++++++++ util/src/migrations/migrate_db_engine.ts | 131 ------------------------------- 4 files changed, 111 insertions(+), 135 deletions(-) create mode 100644 util/src/migrations/migrate_db_engine.js delete mode 100644 util/src/migrations/migrate_db_engine.ts (limited to 'util/src') diff --git a/api/assets/schemas.json b/api/assets/schemas.json index 4f1ab9a8..2ceaa923 100644 --- a/api/assets/schemas.json +++ b/api/assets/schemas.json @@ -2909,6 +2909,9 @@ } } }, + "required": [ + "image" + ], "definitions": { "ChannelPermissionOverwriteType": { "enum": [ @@ -4744,7 +4747,7 @@ "type": "string" }, "permissions": { - "type": "bigint" + "type": "string" }, "color": { "type": "integer" diff --git a/api/src/routes/guilds/#guild_id/roles.ts b/api/src/routes/guilds/#guild_id/roles.ts index 0a57c6a2..b1875598 100644 --- a/api/src/routes/guilds/#guild_id/roles.ts +++ b/api/src/routes/guilds/#guild_id/roles.ts @@ -17,7 +17,7 @@ const router: Router = Router(); export interface RoleModifySchema { name?: string; - permissions?: bigint; + permissions?: string; color?: number; hoist?: boolean; // whether the role should be displayed separately in the sidebar mentionable?: boolean; // whether the role should be mentionable @@ -57,7 +57,7 @@ router.post("/", route({ body: "RoleModifySchema", permission: "MANAGE_ROLES" }) ...body, guild_id: guild_id, managed: false, - permissions: String(req.permission!.bitfield & (body.permissions || BigInt("0"))), + permissions: String(req.permission!.bitfield & BigInt(body.permissions || "0")), tags: undefined }); @@ -109,7 +109,7 @@ router.patch("/:role_id", route({ body: "RoleModifySchema", permission: "MANAGE_ ...body, id: role_id, guild_id, - permissions: String(req.permission!.bitfield & (body.permissions || BigInt("0"))) + permissions: String(req.permission!.bitfield & BigInt(body.permissions || "0")) }); await Promise.all([ diff --git a/util/src/migrations/migrate_db_engine.js b/util/src/migrations/migrate_db_engine.js new file mode 100644 index 00000000..7b8b5784 --- /dev/null +++ b/util/src/migrations/migrate_db_engine.js @@ -0,0 +1,104 @@ +const { config } = require("dotenv"); +config(); +const { createConnection } = require("typeorm"); +const { initDatabase } = require("../../dist/util/Database"); +require("missing-native-js-functions"); +const { + Application, + Attachment, + Ban, + Channel, + ConnectedAccount, + Emoji, + Guild, + Invite, + Member, + Message, + ReadState, + Recipient, + Relationship, + Role, + Sticker, + Team, + TeamMember, + Template, + User, + VoiceState, + Webhook, +} = require("../../dist/entities/index"); + +async function main() { + if (!process.env.TO) throw new Error("TO database env connection string not set"); + + // manually arrange them because of foreign key + const entities = [ + User, + Guild, + Channel, + Invite, + Role, + Ban, + Application, + Emoji, + ConnectedAccount, + Member, + ReadState, + Recipient, + Relationship, + Sticker, + Team, + TeamMember, + Template, + VoiceState, + Webhook, + Message, + Attachment, + ]; + + const newDB = await initDatabase(); + + const type = process.env.TO.includes("://") ? process.env.TO.split(":")[0]?.replace("+srv", "") : "sqlite"; + const isSqlite = type.includes("sqlite"); + + // @ts-ignore + const oldDB = await createConnection({ + type, + url: isSqlite ? undefined : process.env.TO, + database: isSqlite ? process.env.TO : undefined, + entities, + name: "old", + }); + let i = 0; + + try { + for (const entity of entities) { + const entries = await oldDB.manager.find(entity); + // @ts-ignore + console.log("migrating " + entries.length + " " + entity.name + " ..."); + + for (const entry of entries) { + console.log(i++); + + try { + await newDB.manager.insert(entity, entry); + } catch (error) { + try { + if (!entry.id) throw new Error("object doesn't have a unique id: " + entry); + await newDB.manager.update(entity, { id: entry.id }, entry); + } catch (error) { + console.error("couldn't migrate " + i + " " + entity.name, error); + } + } + } + // @ts-ignore + console.log("migrated " + entries.length + " " + entity.name); + } + } catch (error) { + console.error(error.message); + } + + console.log("SUCCESS migrated all data"); + await newDB.close(); +} + +main().caught(); diff --git a/util/src/migrations/migrate_db_engine.ts b/util/src/migrations/migrate_db_engine.ts deleted file mode 100644 index 527e7b53..00000000 --- a/util/src/migrations/migrate_db_engine.ts +++ /dev/null @@ -1,131 +0,0 @@ -import { config } from "dotenv"; -config(); -import { BaseEntity, createConnection, EntityTarget } from "typeorm"; -import { initDatabase } from "../util/Database"; -import "missing-native-js-functions"; -import { - Application, - Attachment, - Ban, - Channel, - ConnectedAccount, - defaultSettings, - Emoji, - Guild, - Invite, - Member, - Message, - ReadState, - Recipient, - Relationship, - Role, - Sticker, - Team, - TeamMember, - Template, - User, - VoiceState, - Webhook, -} from ".."; - -async function main() { - if (!process.env.TO) throw new Error("TO database env connection string not set"); - - // manually arrange them because of foreign key - const entities = [ - User, - Guild, - Channel, - Invite, - Role, - Ban, - Application, - Emoji, - ConnectedAccount, - Member, - ReadState, - Recipient, - Relationship, - Sticker, - Team, - TeamMember, - Template, - VoiceState, - Webhook, - Message, - Attachment, - ]; - - const newDB = await initDatabase(); - - const type = process.env.TO.includes("://") ? process.env.TO.split(":")[0]?.replace("+srv", "") : "sqlite"; - const isSqlite = type.includes("sqlite"); - - // @ts-ignore - const oldDB = await createConnection({ - type, - url: isSqlite ? undefined : process.env.TO, - database: isSqlite ? process.env.TO : undefined, - entities, - name: "old", - }); - let i = 0; - - try { - for (const e of entities) { - const entity = e as EntityTarget; - const entries = await oldDB.manager.find(entity); - // @ts-ignore - console.log("migrating " + entries.length + " " + entity.name + " ..."); - - for (const entry of entries) { - console.log(i++); - - if (entry instanceof User) { - if (entry.bio == null) entry.bio = ""; - if (entry.rights == null) entry.rights = "0"; - if (entry.disabled == null) entry.disabled = false; - if (entry.fingerprints == null) entry.fingerprints = []; - if (entry.deleted == null) entry.deleted = false; - if (entry.data == null) { - entry.data = { - valid_tokens_since: new Date(0), - hash: undefined, - }; - // @ts-ignore - if (entry.user_data) { - // TODO: relationships - entry.data = { - // @ts-ignore - valid_tokens_since: entry.user_data.valid_tokens_since, // @ts-ignore - hash: entry.user_data.hash, - }; - } - } - // @ts-ignore - if (entry.settings == null) { - entry.settings = defaultSettings; - // @ts-ignore - if (entry.user_data) entry.settings = entry.user_settings; - } - } - - // try { - await newDB.manager.insert(entity, entry); - // } catch (error) { - // if (!entry.id) throw new Error("object doesn't have a unique id: " + entry); - // await newDB.manager.update(entity, { id: entry.id }, entry); - // } - } - // @ts-ignore - console.log("migrated " + entries.length + " " + entity.name); - } - } catch (error) { - console.error((error as any).message); - } - - console.log("SUCCESS migrated all data"); - await newDB.close(); -} - -main().caught(); -- cgit 1.5.1 From a11d693f434eb772836f93e6b5bcaac3cc409ec5 Mon Sep 17 00:00:00 2001 From: The Arcane Brony Date: Sun, 10 Oct 2021 20:34:45 +0200 Subject: Change enums to numbers --- util/src/entities/AuditLog.ts | 7 ++----- util/src/entities/Channel.ts | 4 ++-- util/src/entities/Message.ts | 4 ++-- util/src/entities/Relationship.ts | 4 ++-- util/src/entities/Role.ts | 2 +- util/src/entities/Sticker.ts | 8 ++++---- util/src/entities/TeamMember.ts | 4 ++-- util/src/entities/Webhook.ts | 4 ++-- 8 files changed, 17 insertions(+), 20 deletions(-) (limited to 'util/src') diff --git a/util/src/entities/AuditLog.ts b/util/src/entities/AuditLog.ts index ae9feb76..4b3536b6 100644 --- a/util/src/entities/AuditLog.ts +++ b/util/src/entities/AuditLog.ts @@ -55,11 +55,8 @@ export class AuditLog extends BaseClass { @ManyToOne(() => User, (user: User) => user.id) user: User; - @Column({ - type: "simple-enum", - enum: AuditLogEvents, - }) - action_type: AuditLogEvents; + @Column() + action_type: number; @Column({ type: "simple-json", nullable: true }) options?: { diff --git a/util/src/entities/Channel.ts b/util/src/entities/Channel.ts index 51d8b026..e10bd8ed 100644 --- a/util/src/entities/Channel.ts +++ b/util/src/entities/Channel.ts @@ -39,8 +39,8 @@ export class Channel extends BaseClass { @Column({ type: "text", nullable: true }) icon?: string | null; - @Column({ type: "simple-enum", enum: ChannelType }) - type: ChannelType; + @Column() + type: number; @OneToMany(() => Recipient, (recipient: Recipient) => recipient.channel, { cascade: true, diff --git a/util/src/entities/Message.ts b/util/src/entities/Message.ts index c7f7a1d1..e364c57b 100644 --- a/util/src/entities/Message.ts +++ b/util/src/entities/Message.ts @@ -148,8 +148,8 @@ export class Message extends BaseClass { @Column({ nullable: true }) pinned?: boolean; - @Column({ type: "simple-enum", enum: MessageType }) - type: MessageType; + @Column() + type: number; @Column({ type: "simple-json", nullable: true }) activity?: { diff --git a/util/src/entities/Relationship.ts b/util/src/entities/Relationship.ts index e016b36b..2e886e71 100644 --- a/util/src/entities/Relationship.ts +++ b/util/src/entities/Relationship.ts @@ -35,8 +35,8 @@ export class Relationship extends BaseClass { @Column({ nullable: true }) nickname?: string; - @Column({ type: "simple-enum", enum: RelationshipType }) - type: RelationshipType; + @Column() + type: number; toPublicRelationship() { return { diff --git a/util/src/entities/Role.ts b/util/src/entities/Role.ts index 9fca99a5..4a3dae98 100644 --- a/util/src/entities/Role.ts +++ b/util/src/entities/Role.ts @@ -36,7 +36,7 @@ export class Role extends BaseClass { @Column() position: number; - @Column({ type: "simple-json", nullable: true }) + @Column({ type: "simple-json", nullable: true, }) tags?: { bot_id?: string; integration_id?: string; diff --git a/util/src/entities/Sticker.ts b/util/src/entities/Sticker.ts index ab224d1d..8956616e 100644 --- a/util/src/entities/Sticker.ts +++ b/util/src/entities/Sticker.ts @@ -36,9 +36,9 @@ export class Sticker extends BaseClass { }) guild?: Guild; - @Column({ type: "simple-enum", enum: StickerType }) - type: StickerType; + @Column() + type: number; - @Column({ type: "simple-enum", enum: StickerFormatType }) - format_type: StickerFormatType; + @Column() + format_type: number; } diff --git a/util/src/entities/TeamMember.ts b/util/src/entities/TeamMember.ts index bdfdccf0..5642a211 100644 --- a/util/src/entities/TeamMember.ts +++ b/util/src/entities/TeamMember.ts @@ -9,8 +9,8 @@ export enum TeamMemberState { @Entity("team_members") export class TeamMember extends BaseClass { - @Column({ type: "simple-enum", enum: TeamMemberState }) - membership_state: TeamMemberState; + @Column() + membership_state: number; @Column({ type: "simple-array" }) permissions: string[]; diff --git a/util/src/entities/Webhook.ts b/util/src/entities/Webhook.ts index 9d8609ae..0d500962 100644 --- a/util/src/entities/Webhook.ts +++ b/util/src/entities/Webhook.ts @@ -12,8 +12,8 @@ export enum WebhookType { @Entity("webhooks") export class Webhook extends BaseClass { - @Column({ type: "simple-enum", enum: WebhookType }) - type: WebhookType; + @Column() + type: number; @Column({ nullable: true }) name?: string; -- cgit 1.5.1 From 7d36d3d62c856c23a9f45624d7ed26d8c5ff7dbc Mon Sep 17 00:00:00 2001 From: The Arcane Brony Date: Sun, 10 Oct 2021 21:30:46 +0200 Subject: Put local types back --- util/src/entities/AuditLog.ts | 4 ++-- util/src/entities/Channel.ts | 4 ++-- util/src/entities/Message.ts | 4 ++-- util/src/entities/Relationship.ts | 4 ++-- util/src/entities/Role.ts | 2 +- util/src/entities/Sticker.ts | 8 ++++---- util/src/entities/TeamMember.ts | 4 ++-- util/src/entities/Webhook.ts | 4 ++-- 8 files changed, 17 insertions(+), 17 deletions(-) (limited to 'util/src') diff --git a/util/src/entities/AuditLog.ts b/util/src/entities/AuditLog.ts index 4b3536b6..4b81ed6a 100644 --- a/util/src/entities/AuditLog.ts +++ b/util/src/entities/AuditLog.ts @@ -55,8 +55,8 @@ export class AuditLog extends BaseClass { @ManyToOne(() => User, (user: User) => user.id) user: User; - @Column() - action_type: number; + @Column({ type: "int" }) + action_type: AuditLogEvents; @Column({ type: "simple-json", nullable: true }) options?: { diff --git a/util/src/entities/Channel.ts b/util/src/entities/Channel.ts index e10bd8ed..bd2e5a58 100644 --- a/util/src/entities/Channel.ts +++ b/util/src/entities/Channel.ts @@ -39,8 +39,8 @@ export class Channel extends BaseClass { @Column({ type: "text", nullable: true }) icon?: string | null; - @Column() - type: number; + @Column({ type: "int" }) + type: ChannelType; @OneToMany(() => Recipient, (recipient: Recipient) => recipient.channel, { cascade: true, diff --git a/util/src/entities/Message.ts b/util/src/entities/Message.ts index e364c57b..63cd6ad3 100644 --- a/util/src/entities/Message.ts +++ b/util/src/entities/Message.ts @@ -148,8 +148,8 @@ export class Message extends BaseClass { @Column({ nullable: true }) pinned?: boolean; - @Column() - type: number; + @Column({ type: "int" }) + type: MessageType; @Column({ type: "simple-json", nullable: true }) activity?: { diff --git a/util/src/entities/Relationship.ts b/util/src/entities/Relationship.ts index 2e886e71..c3592c76 100644 --- a/util/src/entities/Relationship.ts +++ b/util/src/entities/Relationship.ts @@ -35,8 +35,8 @@ export class Relationship extends BaseClass { @Column({ nullable: true }) nickname?: string; - @Column() - type: number; + @Column({ type: "int" }) + type: RelationshipType; toPublicRelationship() { return { diff --git a/util/src/entities/Role.ts b/util/src/entities/Role.ts index 4a3dae98..9fca99a5 100644 --- a/util/src/entities/Role.ts +++ b/util/src/entities/Role.ts @@ -36,7 +36,7 @@ export class Role extends BaseClass { @Column() position: number; - @Column({ type: "simple-json", nullable: true, }) + @Column({ type: "simple-json", nullable: true }) tags?: { bot_id?: string; integration_id?: string; diff --git a/util/src/entities/Sticker.ts b/util/src/entities/Sticker.ts index 8956616e..036ff2d0 100644 --- a/util/src/entities/Sticker.ts +++ b/util/src/entities/Sticker.ts @@ -36,9 +36,9 @@ export class Sticker extends BaseClass { }) guild?: Guild; - @Column() - type: number; + @Column({ type: "int" }) + type: StickerType; - @Column() - format_type: number; + @Column({ type: "int" }) + format_type: StickerFormatType; } diff --git a/util/src/entities/TeamMember.ts b/util/src/entities/TeamMember.ts index 5642a211..b726e1e8 100644 --- a/util/src/entities/TeamMember.ts +++ b/util/src/entities/TeamMember.ts @@ -9,8 +9,8 @@ export enum TeamMemberState { @Entity("team_members") export class TeamMember extends BaseClass { - @Column() - membership_state: number; + @Column({ type: "int" }) + membership_state: TeamMemberState; @Column({ type: "simple-array" }) permissions: string[]; diff --git a/util/src/entities/Webhook.ts b/util/src/entities/Webhook.ts index 0d500962..89538417 100644 --- a/util/src/entities/Webhook.ts +++ b/util/src/entities/Webhook.ts @@ -12,8 +12,8 @@ export enum WebhookType { @Entity("webhooks") export class Webhook extends BaseClass { - @Column() - type: number; + @Column({ type: "int" }) + type: WebhookType; @Column({ nullable: true }) name?: string; -- cgit 1.5.1 From 601c850b66ea2b348fe27078d6efd9b706128702 Mon Sep 17 00:00:00 2001 From: Erkin Alp Güney Date: Sun, 10 Oct 2021 23:16:24 +0300 Subject: Added more rights, and explained existing ones --- util/src/util/Rights.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'util/src') diff --git a/util/src/util/Rights.ts b/util/src/util/Rights.ts index a266e4f7..3913e476 100644 --- a/util/src/util/Rights.ts +++ b/util/src/util/Rights.ts @@ -30,7 +30,7 @@ export class Rights extends BitField { MANAGE_MESSAGES: BitFlag(3), // Can't see other messages but delete/edit them in channels that they can see MANAGE_RATE_LIMITS: BitFlag(4), MANAGE_ROUTING: BitFlag(5), // can create custom message routes to any channel/guild - MANAGE_TICKETS: BitFlag(6), + MANAGE_TICKETS: BitFlag(6), // can respond to and resolve support tickets MANAGE_USERS: BitFlag(7), ADD_MEMBERS: BitFlag(8), // can manually add any members in their guilds BYPASS_RATE_LIMITS: BitFlag(9), @@ -39,7 +39,7 @@ export class Rights extends BitField { CREATE_DMS: BitFlag(12), CREATE_DM_GROUPS: BitFlag(13), CREATE_GUILDS: BitFlag(14), - CREATE_INVITES: BitFlag(15), + CREATE_INVITES: BitFlag(15), // can create mass invites in the guilds that they have CREATE_INSTANT_INVITE CREATE_ROLES: BitFlag(16), CREATE_TEMPLATES: BitFlag(17), CREATE_WEBHOOKS: BitFlag(18), @@ -50,9 +50,13 @@ export class Rights extends BitField { SELF_EDIT_MESSAGES: BitFlag(23), SELF_EDIT_NAME: BitFlag(24), SEND_MESSAGES: BitFlag(25), - USE_SCREEN: BitFlag(26), + USE_ACTIVITIES: BitFlag(26), USE_VIDEO: BitFlag(27), USE_VOICE: BitFlag(28), + INVITE_USERS: BitFlag(29), // can create user-specific invites in the guilds that they have INVITE_USERS + SELF_DELETE_DISABLE: BitFlag(30), // can disable/delete own account + DEBTABLE: BitFlag(31), // can use pay-to-use features + CREDITABLE: BitFlag(32) // can receive money from monetisation related features }; any(permission: RightResolvable, checkOperator = true) { -- cgit 1.5.1 From 207d9137c7c068a247fa55eef1bb98150e966567 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Mon, 11 Oct 2021 15:04:03 +0200 Subject: Update Rights.ts --- util/src/util/Rights.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util/src') diff --git a/util/src/util/Rights.ts b/util/src/util/Rights.ts index 3913e476..5edd9142 100644 --- a/util/src/util/Rights.ts +++ b/util/src/util/Rights.ts @@ -50,7 +50,7 @@ export class Rights extends BitField { SELF_EDIT_MESSAGES: BitFlag(23), SELF_EDIT_NAME: BitFlag(24), SEND_MESSAGES: BitFlag(25), - USE_ACTIVITIES: BitFlag(26), + USE_ACTIVITIES: BitFlag(26), // use (game) activities in voice channels (e.g. Watch together) USE_VIDEO: BitFlag(27), USE_VOICE: BitFlag(28), INVITE_USERS: BitFlag(29), // can create user-specific invites in the guilds that they have INVITE_USERS -- cgit 1.5.1 From 7053d9a8b8db54b349f39e3c19a38cee8e4fdb18 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Mon, 11 Oct 2021 16:34:18 +0200 Subject: :art: gifs --- api/src/routes/gifs/search.ts | 27 ++++--------- api/src/routes/gifs/trending-gifs.ts | 27 ++++--------- api/src/routes/gifs/trending.ts | 73 ++++++++++++++++++++---------------- api/src/util/Message.ts | 1 + util/src/entities/Config.ts | 21 +++++------ 5 files changed, 66 insertions(+), 83 deletions(-) (limited to 'util/src') diff --git a/api/src/routes/gifs/search.ts b/api/src/routes/gifs/search.ts index 3cbff64f..45b3ddca 100644 --- a/api/src/routes/gifs/search.ts +++ b/api/src/routes/gifs/search.ts @@ -1,37 +1,24 @@ import { Router, Response, Request } from "express"; import fetch from "node-fetch"; import { route } from "@fosscord/api"; +import { getGifApiKey, parseGifResult } from "./trending"; const router = Router(); router.get("/", route({}), async (req: Request, res: Response) => { - // TODO: Custom providers and code quality - const { q, media_format, locale, provider } = req.query; + // TODO: Custom providers + const { q, media_format, locale } = req.query; - const parseResult = (result: any) => { - return { - id: result.id, - title: result.title, - url: result.itemurl, - src: result.media[0].mp4.url, - gif_src: result.media[0].gif.url, - width: result.media[0].mp4.dims[0], - height: result.media[0].mp4.dims[1], - preview: result.media[0].mp4.preview - }; - }; + const apiKey = getGifApiKey(); - const response = await fetch(`https://g.tenor.com/v1/search?q=${q}&media_format=${media_format}&locale=${locale}&key=LIVDSRZULELA`, { + const response = await fetch(`https://g.tenor.com/v1/search?q=${q}&media_format=${media_format}&locale=${locale}&key=${apiKey}`, { method: "get", headers: { "Content-Type": "application/json" } }); const { results } = await response.json(); - let cache = new Array() as any[]; - results.forEach((result: any) => { - cache.push(parseResult(result)); - }); - res.json(cache).status(200); + + res.json(results.map(parseGifResult)).status(200); }); export default router; diff --git a/api/src/routes/gifs/trending-gifs.ts b/api/src/routes/gifs/trending-gifs.ts index 76e791fa..b5f87222 100644 --- a/api/src/routes/gifs/trending-gifs.ts +++ b/api/src/routes/gifs/trending-gifs.ts @@ -1,37 +1,24 @@ import { Router, Response, Request } from "express"; import fetch from "node-fetch"; import { route } from "@fosscord/api"; +import { getGifApiKey, parseGifResult } from "./trending"; const router = Router(); router.get("/", route({}), async (req: Request, res: Response) => { - // TODO: Custom providers and code quality - const { media_format, locale, provider } = req.query; + // TODO: Custom providers + const { media_format, locale } = req.query; - const parseResult = (result: any) => { - return { - id: result.id, - title: result.title, - url: result.itemurl, - src: result.media[0].mp4.url, - gif_src: result.media[0].gif.url, - width: result.media[0].mp4.dims[0], - height: result.media[0].mp4.dims[1], - preview: result.media[0].mp4.preview - }; - }; + const apiKey = getGifApiKey(); - const response = await fetch(`https://g.tenor.com/v1/trending?media_format=${media_format}&locale=${locale}&key=LIVDSRZULELA`, { + const response = await fetch(`https://g.tenor.com/v1/trending?media_format=${media_format}&locale=${locale}&key=${apiKey}`, { method: "get", headers: { "Content-Type": "application/json" } }); const { results } = await response.json(); - let cache = new Array() as any[]; - results.forEach((result: any) => { - cache.push(parseResult(result)); - }); - res.json(cache).status(200); + + res.json(results.map(parseGifResult)).status(200); }); export default router; diff --git a/api/src/routes/gifs/trending.ts b/api/src/routes/gifs/trending.ts index 3b91eb12..7ee9337e 100644 --- a/api/src/routes/gifs/trending.ts +++ b/api/src/routes/gifs/trending.ts @@ -1,48 +1,57 @@ import { Router, Response, Request } from "express"; import fetch from "node-fetch"; import { route } from "@fosscord/api"; +import { Config } from "@fosscord/util"; +import { HTTPError } from "lambert-server"; const router = Router(); -router.get("/", route({}), async (req: Request, res: Response) => { - // TODO: Custom providers and code quality - const { media_format, locale, provider } = req.query; - - const parseResult = (result: any) => { - return { - id: result.id, - title: result.title, - url: result.itemurl, - src: result.media[0].mp4.url, - gif_src: result.media[0].gif.url, - width: result.media[0].mp4.dims[0], - height: result.media[0].mp4.dims[1], - preview: result.media[0].mp4.preview - }; +export function parseGifResult(result: any) { + return { + id: result.id, + title: result.title, + url: result.itemurl, + src: result.media[0].mp4.url, + gif_src: result.media[0].gif.url, + width: result.media[0].mp4.dims[0], + height: result.media[0].mp4.dims[1], + preview: result.media[0].mp4.preview }; +} + +export function getGifApiKey() { + const { enabled, provider, apiKey } = Config.get().gif; + if (!enabled) throw new HTTPError(`Gifs are disabled`); + if (provider !== "tenor" || !apiKey) throw new HTTPError(`${provider} gif provider not supported`); - const responseSource = await fetch(`https://g.tenor.com/v1/categories?media_format=${media_format}&locale=${locale}&key=LIVDSRZULELA`, { - method: "get", - headers: { "Content-Type": "application/json" } - }); + return apiKey; +} - const trendGifSource = await fetch(`https://g.tenor.com/v1/trending?media_format=${media_format}&locale=${locale}&key=LIVDSRZULELA`, { - method: "get", - headers: { "Content-Type": "application/json" } - }); +router.get("/", route({}), async (req: Request, res: Response) => { + // TODO: Custom providers + // TODO: return gifs as mp4 + const { media_format, locale } = req.query; + + const apiKey = getGifApiKey(); + + const [responseSource, trendGifSource] = await Promise.all([ + fetch(`https://g.tenor.com/v1/categories?locale=${locale}&key=${apiKey}`, { + method: "get", + headers: { "Content-Type": "application/json" } + }), + fetch(`https://g.tenor.com/v1/trending?locale=${locale}&key=${apiKey}`, { + method: "get", + headers: { "Content-Type": "application/json" } + }) + ]); const { tags } = await responseSource.json(); const { results } = await trendGifSource.json(); - let cache = new Array() as any[]; - - tags.forEach((result: any) => { - cache.push({ - name: result.searchterm, - src: result.image - }); - }); - res.json({ categories: [cache], gifs: [parseResult(results[0])] }).status(200); + res.json({ + categories: tags.map((x: any) => ({ name: x.searchterm, src: x.image })), + gifs: [parseGifResult(results[0])] + }).status(200); }); export default router; diff --git a/api/src/util/Message.ts b/api/src/util/Message.ts index f8230124..40d96b42 100644 --- a/api/src/util/Message.ts +++ b/api/src/util/Message.ts @@ -25,6 +25,7 @@ import cheerio from "cheerio"; import { MessageCreateSchema } from "../routes/channels/#channel_id/messages"; // TODO: check webhook, application, system author +// TODO: embed gifs/videos/images const LINK_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g; diff --git a/util/src/entities/Config.ts b/util/src/entities/Config.ts index 6760187f..b3167ac7 100644 --- a/util/src/entities/Config.ts +++ b/util/src/entities/Config.ts @@ -51,11 +51,6 @@ export interface ConfigValue { general: { instanceId: string; }; - permissions: { - user: { - createGuilds: boolean; - }; - }; limits: { user: { maxGuilds: number; @@ -154,6 +149,11 @@ export interface ConfigValue { canLeave: boolean; }; }; + gif: { + enabled: boolean; + provider: "tenor"; // more coming soon + apiKey?: string; + }; rabbitmq: { host: string | null; }; @@ -176,11 +176,6 @@ export const DefaultConfigOptions: ConfigValue = { general: { instanceId: Snowflake.generate(), }, - permissions: { - user: { - createGuilds: true, - }, - }, limits: { user: { maxGuilds: 100, @@ -307,7 +302,6 @@ export const DefaultConfigOptions: ConfigValue = { }, ], }, - guild: { showAllGuildsInDiscovery: false, autoJoin: { @@ -316,6 +310,11 @@ export const DefaultConfigOptions: ConfigValue = { guilds: [], }, }, + gif: { + enabled: true, + provider: "tenor", + apiKey: "LIVDSRZULELA", + }, rabbitmq: { host: null, }, -- cgit 1.5.1 From f2e8e2e0311bc343a21c3de94f6a1e908be81c2c Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Tue, 12 Oct 2021 21:53:57 +0200 Subject: :zap: improve memory managment --- bundle/src/stats.ts | 3 ++- gateway/src/events/Close.ts | 9 ++++++--- gateway/src/util/Send.ts | 3 +++ util/src/util/Event.ts | 2 ++ 4 files changed, 13 insertions(+), 4 deletions(-) (limited to 'util/src') diff --git a/bundle/src/stats.ts b/bundle/src/stats.ts index 18bb85ca..7928de89 100644 --- a/bundle/src/stats.ts +++ b/bundle/src/stats.ts @@ -31,5 +31,6 @@ export function initStats() { process.memoryUsage().rss / 1024 / 1024 )}mb/${memory.totalMemMb.toFixed(0)}mb ${networkUsage}` ); - }, 1000 * 10); + // TODO: node-os-utils might have a memory leak, more investigation needed + }, 1000 * 60 * 5); } diff --git a/gateway/src/events/Close.ts b/gateway/src/events/Close.ts index 1299ad5c..5c1bd292 100644 --- a/gateway/src/events/Close.ts +++ b/gateway/src/events/Close.ts @@ -1,10 +1,13 @@ import { WebSocket } from "@fosscord/gateway"; -import { Message } from "./Message"; import { Session } from "@fosscord/util"; export async function Close(this: WebSocket, code: number, reason: string) { console.log("[WebSocket] closed", code, reason); if (this.session_id) await Session.delete({ session_id: this.session_id }); - // @ts-ignore - this.off("message", Message); + if (this.heartbeatTimeout) clearTimeout(this.heartbeatTimeout); + if (this.readyTimeout) clearTimeout(this.readyTimeout); + + this.deflate?.close(); + + this.removeAllListeners(); } diff --git a/gateway/src/util/Send.ts b/gateway/src/util/Send.ts index 4defa898..196d4205 100644 --- a/gateway/src/util/Send.ts +++ b/gateway/src/util/Send.ts @@ -18,6 +18,9 @@ export async function Send(socket: WebSocket, data: Payload) { } return new Promise((res, rej) => { + if (socket.readyState !== 1) { + return rej("socket not open"); + } socket.send(buffer, (err: any) => { if (err) return rej(err); return res(null); diff --git a/util/src/util/Event.ts b/util/src/util/Event.ts index bf9547b1..8ed009d5 100644 --- a/util/src/util/Event.ts +++ b/util/src/util/Event.ts @@ -46,7 +46,9 @@ export async function listenEvent(event: string, callback: (event: EventOpts) => } else { const cancel = () => { events.removeListener(event, callback); + events.setMaxListeners(events.getMaxListeners() - 1); }; + events.setMaxListeners(events.getMaxListeners() + 1); events.addListener(event, (opts) => callback({ ...opts, cancel })); return cancel; -- cgit 1.5.1 From 808bf0a74c974d365fb83d31de370dfebb6d0367 Mon Sep 17 00:00:00 2001 From: Maddy Date: Wed, 13 Oct 2021 16:07:33 +1100 Subject: added route /guilds/:id/prune GET count and POST prune. Edited Member entity to include last_message_id to avoid searching for every members last message when pruning When a message is sent through /channels/:id/messages last_message_id of sending Member is updated. --- .../routes/channels/#channel_id/messages/index.ts | 10 ++- api/src/routes/guilds/#guild_id/prune.ts | 74 ++++++++++++++++++++++ util/src/entities/Member.ts | 4 ++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 api/src/routes/guilds/#guild_id/prune.ts (limited to 'util/src') diff --git a/api/src/routes/channels/#channel_id/messages/index.ts b/api/src/routes/channels/#channel_id/messages/index.ts index 26bb9e5d..399fa7bb 100644 --- a/api/src/routes/channels/#channel_id/messages/index.ts +++ b/api/src/routes/channels/#channel_id/messages/index.ts @@ -10,7 +10,8 @@ import { getPermission, Message, MessageCreateEvent, - uploadFile + uploadFile, + Member, } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { handleMessage, postHandleMessage, route } from "@fosscord/api"; @@ -188,6 +189,13 @@ router.post( await channel.assign({ last_message_id: message.id }).save(); + //gosh + var member = await Member.findOneOrFail({ + where: { id: req.user_id }, + }); + await member.assign({ last_message_id: message.id }) + await member.save(); //why does member.assign here return void? + if (channel.isDm()) { const channel_dto = await DmChannelDTO.from(channel); diff --git a/api/src/routes/guilds/#guild_id/prune.ts b/api/src/routes/guilds/#guild_id/prune.ts new file mode 100644 index 00000000..cddbcee7 --- /dev/null +++ b/api/src/routes/guilds/#guild_id/prune.ts @@ -0,0 +1,74 @@ +import { Router, Request, Response } from "express"; +import { Guild, Member, Snowflake } from "@fosscord/util"; +import { HTTPError } from "lambert-server"; +import { LessThan } from "typeorm"; +import { route } from "@fosscord/api"; +const router = Router(); + +//Returns all inactive members, respecting role hierarchy +export const inactiveMembers = async (guild_id: string, user_id: string, days: number, roles: string[] = []) => { + var date = new Date(); + date.setDate(date.getDate() - days); + //Snowflake should have `generateFromTime` method? Or similar? + var minId = BigInt(date.valueOf() - Snowflake.EPOCH) << BigInt(22); + + var members = await Member.find({ + where: [ + { + guild_id, + last_message_id: LessThan(minId.toString()), + }, + ], + relations: ["roles"] + }); + if (!members.length) return []; + + //I'm sure I can do this in the above db query ( and it would probably be better to do so ), but oh well. + if (roles.length && members.length) + members = members.filter(user => user.roles?.some(role => roles.includes(role.id))); + + const me = await Member.findOne({ id: user_id, guild_id }, { relations: ["roles"] }); + if (!me) throw new HTTPError("You are not member of this guild", 403); + const myHighestRole = Math.max(...(me.roles?.map(x => x.position) || [])) + + const guild = await Guild.findOneOrFail({ where: { id: guild_id } }) + + members = members.filter(member => + member.id !== guild.owner_id && //can't kick owner + member.roles?.some(role => + role.position < myHighestRole || //roles higher than me can't be kicked + me.id === guild.owner_id //owner can kick anyone + ) + ) + + return members; +} + +router.get("/", async (req: Request, res: Response) => { + const days = parseInt(req.query.days as string); + + var roles = req.query.include_roles; + if (typeof roles === 'string') roles = [roles]; //express will return array otherwise + + const members = await inactiveMembers(req.params.guild_id, req.user_id, days, roles as string[]); + + res.send({ pruned: members.length }); +}); + +router.post("/", route({ permission: "KICK_MEMBERS" }), async (req: Request, res: Response) => { + const days = parseInt(req.body.days); + + var roles = req.query.include_roles; + if (typeof roles === 'string') roles = [roles]; + + const { guild_id } = req.params; + const members = await inactiveMembers(guild_id, req.user_id, days, roles as string[]); + + for (var curr of members) { + await Member.removeFromGuild(curr.id, guild_id); + } + + res.send({ purged: members.length }); +}) + +export default router; diff --git a/util/src/entities/Member.ts b/util/src/entities/Member.ts index 7d7ac40a..19747318 100644 --- a/util/src/entities/Member.ts +++ b/util/src/entities/Member.ts @@ -84,6 +84,9 @@ export class Member extends BaseClassWithoutId { @Column({ type: "simple-json" }) settings: UserGuildSettings; + @Column() + last_message_id: string; + // TODO: update // @Column({ type: "simple-json" }) // read_state: ReadState; @@ -230,6 +233,7 @@ export class Member extends BaseClassWithoutId { deaf: false, mute: false, pending: false, + last_message_id: "", }; await Promise.all([ -- cgit 1.5.1 From 8257e2631ae5c4237d08b48b365ce7f367de74c6 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 14 Oct 2021 00:32:15 +0200 Subject: :bug: fix migration + autojoin --- api/src/util/Instance.ts | 2 +- util/src/migrations/migrate_db_engine.js | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'util/src') diff --git a/api/src/util/Instance.ts b/api/src/util/Instance.ts index a7b3205a..d1d9e1ab 100644 --- a/api/src/util/Instance.ts +++ b/api/src/util/Instance.ts @@ -8,7 +8,7 @@ export async function initInstance() { // TODO: check if any current user is not part of autoJoinGuilds const { autoJoin } = Config.get().guild; - if (autoJoin.enabled && autoJoin.guilds?.length) { + if (autoJoin.enabled && !autoJoin.guilds?.length) { let guild = await Guild.findOne({}); if (!guild) guild = await Guild.createGuild({}); diff --git a/util/src/migrations/migrate_db_engine.js b/util/src/migrations/migrate_db_engine.js index eab30bc4..79e9d86f 100644 --- a/util/src/migrations/migrate_db_engine.js +++ b/util/src/migrations/migrate_db_engine.js @@ -8,6 +8,7 @@ const { Attachment, Ban, Channel, + ConfigEntity, ConnectedAccount, Emoji, Guild, @@ -32,6 +33,7 @@ async function main() { // manually arrange them because of foreign keys const entities = [ + ConfigEntity, User, Guild, Channel, @@ -61,12 +63,13 @@ async function main() { const isSqlite = type.includes("sqlite"); // @ts-ignore - const oldDB = await createConnection({ + const newDB = await createConnection({ type, url: isSqlite ? undefined : process.env.TO, database: isSqlite ? process.env.TO : undefined, entities, - name: "old", + name: "new", + synchronize: true, }); let i = 0; -- cgit 1.5.1 From 9200fb546c1e2c12e0403a1bbfb6360946a28b1c Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 14 Oct 2021 19:47:02 +0200 Subject: :sparkles: sticker db entities --- util/src/entities/Sticker.ts | 19 ++++++++++++++++++- util/src/entities/StickerPack.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 util/src/entities/StickerPack.ts (limited to 'util/src') diff --git a/util/src/entities/Sticker.ts b/util/src/entities/Sticker.ts index 036ff2d0..94761f60 100644 --- a/util/src/entities/Sticker.ts +++ b/util/src/entities/Sticker.ts @@ -1,4 +1,5 @@ -import { Column, Entity, JoinColumn, ManyToOne } from "typeorm"; +import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; +import { User } from "./User"; import { BaseClass } from "./BaseClass"; import { Guild } from "./Guild"; @@ -25,8 +26,15 @@ export class Sticker extends BaseClass { tags: string; @Column() + @RelationId((sticker: Sticker) => sticker.pack) pack_id: string; + @JoinColumn({ name: "pack_id" }) + @ManyToOne(() => require("./StickerPack").StickerPack, { + onDelete: "CASCADE", + }) + pack: import("./StickerPack").StickerPack; + @Column({ nullable: true }) guild_id?: string; @@ -36,6 +44,15 @@ export class Sticker extends BaseClass { }) guild?: Guild; + @Column({ nullable: true }) + user_id?: string; + + @JoinColumn({ name: "user_id" }) + @ManyToOne(() => User, { + onDelete: "CASCADE", + }) + user?: User; + @Column({ type: "int" }) type: StickerType; diff --git a/util/src/entities/StickerPack.ts b/util/src/entities/StickerPack.ts new file mode 100644 index 00000000..b80fa9c7 --- /dev/null +++ b/util/src/entities/StickerPack.ts @@ -0,0 +1,31 @@ +import { Column, Entity, JoinColumn, OneToMany, OneToOne, RelationId } from "typeorm"; +import { Sticker } from "."; +import { BaseClass } from "./BaseClass"; + +@Entity("stickers") +export class StickerPack extends BaseClass { + @Column() + name: string; + + @Column({ nullable: true }) + description?: string; + + @Column({ nullable: true }) + banner_asset_id?: string; + + @OneToMany(() => Sticker, (sticker: Sticker) => sticker.pack_id, { + cascade: true, + orphanedRowAction: "delete", + }) + stickers: Sticker[]; + + // sku_id: string + + @Column({ nullable: true }) + @RelationId((pack: StickerPack) => pack.cover_sticker) + cover_sticker_id?: string; + + @OneToOne(() => Sticker, { nullable: true }) + @JoinColumn() + cover_sticker?: Sticker; +} -- cgit 1.5.1 From d2df6f99bef7db6f5c20b822b274c6bcf664c7df Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Fri, 15 Oct 2021 00:02:23 +0200 Subject: :sparkles: sticker upload --- api/assets/schemas.json | 308 ++++++++++++++++++++++++++++ api/src/routes/guilds/#guild_id/stickers.ts | 49 ++++- util/src/entities/Sticker.ts | 13 +- 3 files changed, 359 insertions(+), 11 deletions(-) (limited to 'util/src') diff --git a/api/assets/schemas.json b/api/assets/schemas.json index 2ceaa923..eb97112c 100644 --- a/api/assets/schemas.json +++ b/api/assets/schemas.json @@ -514,6 +514,12 @@ "attachments": { "type": "array", "items": {} + }, + "sticker_ids": { + "type": "array", + "items": { + "type": "string" + } } }, "definitions": { @@ -5341,6 +5347,308 @@ }, "$schema": "http://json-schema.org/draft-07/schema#" }, + "ModifyGuildStickerSchema": { + "type": "object", + "properties": { + "name": { + "minLength": 2, + "maxLength": 30, + "type": "string" + }, + "description": { + "maxLength": 100, + "type": "string" + }, + "tags": { + "maxLength": 200, + "type": "string" + } + }, + "required": [ + "name", + "tags" + ], + "definitions": { + "ChannelPermissionOverwriteType": { + "enum": [ + 0, + 1 + ], + "type": "number" + }, + "Embed": { + "type": "object", + "properties": { + "title": { + "type": "string" + }, + "type": { + "enum": [ + "article", + "gifv", + "image", + "link", + "rich", + "video" + ], + "type": "string" + }, + "description": { + "type": "string" + }, + "url": { + "type": "string" + }, + "timestamp": { + "type": "string", + "format": "date-time" + }, + "color": { + "type": "integer" + }, + "footer": { + "type": "object", + "properties": { + "text": { + "type": "string" + }, + "icon_url": { + "type": "string" + }, + "proxy_icon_url": { + "type": "string" + } + }, + "required": [ + "text" + ] + }, + "image": { + "$ref": "#/definitions/EmbedImage" + }, + "thumbnail": { + "$ref": "#/definitions/EmbedImage" + }, + "video": { + "$ref": "#/definitions/EmbedImage" + }, + "provider": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "url": { + "type": "string" + } + } + }, + "author": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "url": { + "type": "string" + }, + "icon_url": { + "type": "string" + }, + "proxy_icon_url": { + "type": "string" + } + } + }, + "fields": { + "type": "array", + "items": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "value": { + "type": "string" + }, + "inline": { + "type": "boolean" + } + }, + "required": [ + "name", + "value" + ] + } + } + } + }, + "EmbedImage": { + "type": "object", + "properties": { + "url": { + "type": "string" + }, + "proxy_url": { + "type": "string" + }, + "height": { + "type": "integer" + }, + "width": { + "type": "integer" + } + } + }, + "ChannelModifySchema": { + "type": "object", + "properties": { + "name": { + "maxLength": 100, + "type": "string" + }, + "type": { + "enum": [ + 0, + 1, + 10, + 11, + 12, + 13, + 2, + 3, + 4, + 5, + 6 + ], + "type": "number" + }, + "topic": { + "type": "string" + }, + "icon": { + "type": [ + "null", + "string" + ] + }, + "bitrate": { + "type": "integer" + }, + "user_limit": { + "type": "integer" + }, + "rate_limit_per_user": { + "type": "integer" + }, + "position": { + "type": "integer" + }, + "permission_overwrites": { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "type": { + "$ref": "#/definitions/ChannelPermissionOverwriteType" + }, + "allow": { + "type": "string" + }, + "deny": { + "type": "string" + } + }, + "required": [ + "allow", + "deny", + "id", + "type" + ] + } + }, + "parent_id": { + "type": "string" + }, + "id": { + "type": "string" + }, + "nsfw": { + "type": "boolean" + }, + "rtc_region": { + "type": "string" + }, + "default_auto_archive_duration": { + "type": "integer" + } + } + }, + "UserPublic": { + "type": "object", + "properties": { + "username": { + "type": "string" + }, + "discriminator": { + "type": "string" + }, + "id": { + "type": "string" + }, + "public_flags": { + "type": "integer" + }, + "avatar": { + "type": "string" + }, + "accent_color": { + "type": "integer" + }, + "banner": { + "type": "string" + }, + "bio": { + "type": "string" + }, + "bot": { + "type": "boolean" + } + }, + "required": [ + "bio", + "bot", + "discriminator", + "id", + "public_flags", + "username" + ] + }, + "PublicConnectedAccount": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "type": { + "type": "string" + }, + "verifie": { + "type": "boolean" + } + }, + "required": [ + "name", + "type", + "verifie" + ] + } + }, + "$schema": "http://json-schema.org/draft-07/schema#" + }, "TemplateCreateSchema": { "type": "object", "properties": { diff --git a/api/src/routes/guilds/#guild_id/stickers.ts b/api/src/routes/guilds/#guild_id/stickers.ts index c122b72d..39095dc2 100644 --- a/api/src/routes/guilds/#guild_id/stickers.ts +++ b/api/src/routes/guilds/#guild_id/stickers.ts @@ -1,7 +1,8 @@ -import { Member, Sticker } from "@fosscord/util"; +import { handleFile, Member, Snowflake, Sticker, StickerFormatType, StickerType, uploadFile } from "@fosscord/util"; import { Router, Request, Response } from "express"; import { route } from "@fosscord/api"; import multer from "multer"; +import { HTTPError } from "lambert-server"; const router = Router(); router.get("/", route({}), async (req: Request, res: Response) => { @@ -20,12 +21,47 @@ const bodyParser = multer({ storage: multer.memoryStorage() }).single("file"); -router.post("/", bodyParser, route({ permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => { - const { guild_id } = req.params; - await Member.IsInGuildOrFail(req.user_id, guild_id); +router.post( + "/", + bodyParser, + route({ permission: "MANAGE_EMOJIS_AND_STICKERS", body: "ModifyGuildStickerSchema" }), + async (req: Request, res: Response) => { + if (!req.file) throw new HTTPError("missing file"); - res.json(await Sticker.find({ guild_id })); -}); + const { guild_id } = req.params; + const body = req.body as ModifyGuildStickerSchema; + const id = Snowflake.generate(); + + const [sticker] = await Promise.all([ + new Sticker({ + ...body, + guild_id, + id, + type: StickerType.GUILD, + format_type: getStickerFormat(req.file.mimetype), + available: true + }).save(), + uploadFile(`/stickers/${id}`, req.file) + ]); + + res.json(sticker); + } +); + +export function getStickerFormat(mime_type: string) { + switch (mime_type) { + case "image/apng": + return StickerFormatType.APNG; + case "application/json": + return StickerFormatType.LOTTIE; + case "image/png": + return StickerFormatType.PNG; + case "image/gif": + return StickerFormatType.GIF; + default: + throw new HTTPError("invalid sticker format: must be png, apng or lottie"); + } +} router.get("/:sticker_id", route({}), async (req: Request, res: Response) => { const { guild_id, sticker_id } = req.params; @@ -41,7 +77,6 @@ export interface ModifyGuildStickerSchema { */ name: string; /** - * @minLength 2 * @maxLength 100 */ description?: string; diff --git a/util/src/entities/Sticker.ts b/util/src/entities/Sticker.ts index 94761f60..37bc6fbe 100644 --- a/util/src/entities/Sticker.ts +++ b/util/src/entities/Sticker.ts @@ -9,6 +9,7 @@ export enum StickerType { } export enum StickerFormatType { + GIF = 0, // gif is a custom format type and not in discord spec PNG = 1, APNG = 2, LOTTIE = 3, @@ -22,16 +23,20 @@ export class Sticker extends BaseClass { @Column({ nullable: true }) description?: string; - @Column() - tags: string; + @Column({ nullable: true }) + available?: boolean; - @Column() + @Column({ nullable: true }) + tags?: string; + + @Column({ nullable: true }) @RelationId((sticker: Sticker) => sticker.pack) - pack_id: string; + pack_id?: string; @JoinColumn({ name: "pack_id" }) @ManyToOne(() => require("./StickerPack").StickerPack, { onDelete: "CASCADE", + nullable: true, }) pack: import("./StickerPack").StickerPack; -- cgit 1.5.1 From 0ccc478c54f6e6ae8c8e6574e24ab6796738c83a Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Fri, 15 Oct 2021 00:03:05 +0200 Subject: :bug: fix message sticker sending --- api/client_test/index.html | 4 +++- api/src/routes/channels/#channel_id/messages/index.ts | 1 + api/src/routes/guilds/#guild_id/premium.ts | 10 ++++++++++ api/src/util/Message.ts | 5 +++-- util/src/entities/Message.ts | 2 +- 5 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 api/src/routes/guilds/#guild_id/premium.ts (limited to 'util/src') diff --git a/api/client_test/index.html b/api/client_test/index.html index 20b431b8..5a795253 100644 --- a/api/client_test/index.html +++ b/api/client_test/index.html @@ -37,6 +37,7 @@ HTML_TIMESTAMP: Date.now(), ALGOLIA_KEY: "aca0d7082e4e63af5ba5917d5e96bed0" }; + GLOBAL_ENV.MEDIA_PROXY_ENDPOINT = location.protocol + "//" + GLOBAL_ENV.CDN_HOST; const localStorage = window.localStorage; // TODO: remote auth // window.GLOBAL_ENV.REMOTE_AUTH_ENDPOINT = window.GLOBAL_ENV.GATEWAY_ENDPOINT.replace(/wss?:/, ""); @@ -105,7 +106,8 @@ } const settings = JSON.parse(localStorage.getItem("UserSettingsStore")); - if (settings && settings.locale === "en") { + if (settings && settings.locale.length <= 2) { + // fix client locale wrong and client not loading at all settings.locale = "en-US"; localStorage.setItem("UserSettingsStore", JSON.stringify(settings)); } diff --git a/api/src/routes/channels/#channel_id/messages/index.ts b/api/src/routes/channels/#channel_id/messages/index.ts index 26bb9e5d..20c102ef 100644 --- a/api/src/routes/channels/#channel_id/messages/index.ts +++ b/api/src/routes/channels/#channel_id/messages/index.ts @@ -63,6 +63,7 @@ export interface MessageCreateSchema { payload_json?: string; file?: any; attachments?: any[]; //TODO we should create an interface for attachments + sticker_ids?: string[]; } // https://discord.com/developers/docs/resources/channel#create-message diff --git a/api/src/routes/guilds/#guild_id/premium.ts b/api/src/routes/guilds/#guild_id/premium.ts new file mode 100644 index 00000000..75361ac6 --- /dev/null +++ b/api/src/routes/guilds/#guild_id/premium.ts @@ -0,0 +1,10 @@ +import { Router, Request, Response } from "express"; +import { route } from "@fosscord/api"; +const router = Router(); + +router.get("/subscriptions", route({}), async (req: Request, res: Response) => { + // TODO: + res.json([]); +}); + +export default router; diff --git a/api/src/util/Message.ts b/api/src/util/Message.ts index 40d96b42..d14d3aa2 100644 --- a/api/src/util/Message.ts +++ b/api/src/util/Message.ts @@ -24,7 +24,7 @@ import fetch from "node-fetch"; import cheerio from "cheerio"; import { MessageCreateSchema } from "../routes/channels/#channel_id/messages"; -// TODO: check webhook, application, system author +// TODO: check webhook, application, system author, stickers // TODO: embed gifs/videos/images const LINK_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g; @@ -46,6 +46,7 @@ export async function handleMessage(opts: MessageOptions): Promise { const message = new Message({ ...opts, + sticker_items: opts.sticker_ids?.map((x) => ({ id: x })), guild_id: channel.guild_id, channel_id: opts.channel_id, attachments: opts.attachments || [], @@ -82,7 +83,7 @@ export async function handleMessage(opts: MessageOptions): Promise { } // TODO: stickers/activity - if (!opts.content && !opts.embeds?.length && !opts.attachments?.length) { + if (!opts.content && !opts.embeds?.length && !opts.attachments?.length && !opts.sticker_ids?.length) { throw new HTTPError("Empty messages are not allowed", 50006); } diff --git a/util/src/entities/Message.ts b/util/src/entities/Message.ts index 63cd6ad3..a4d38315 100644 --- a/util/src/entities/Message.ts +++ b/util/src/entities/Message.ts @@ -127,7 +127,7 @@ export class Message extends BaseClass { mention_channels: Channel[]; @JoinTable({ name: "message_stickers" }) - @ManyToMany(() => Sticker) + @ManyToMany(() => Sticker, { cascade: true, onDelete: "CASCADE" }) sticker_items?: Sticker[]; @OneToMany(() => Attachment, (attachment: Attachment) => attachment.message, { -- cgit 1.5.1 From c1d786b6e1a9b8562e7ea05ae08934d92abff270 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Fri, 15 Oct 2021 00:03:19 +0200 Subject: :bug: fix sticker packs --- api/src/routes/sticker-packs/index.ts | 6 ++++-- gateway/src/opcodes/Identify.ts | 2 ++ util/src/entities/StickerPack.ts | 8 ++++---- 3 files changed, 10 insertions(+), 6 deletions(-) (limited to 'util/src') diff --git a/api/src/routes/sticker-packs/index.ts b/api/src/routes/sticker-packs/index.ts index 89b91bdb..e6560d12 100644 --- a/api/src/routes/sticker-packs/index.ts +++ b/api/src/routes/sticker-packs/index.ts @@ -1,11 +1,13 @@ import { Request, Response, Router } from "express"; import { route } from "@fosscord/api"; -import { StickerPack } from "@fosscord/util/src/entities/StickerPack"; +import { StickerPack } from "@fosscord/util"; const router: Router = Router(); router.get("/", route({}), async (req: Request, res: Response) => { - res.json(await StickerPack.find({})); + const sticker_packs = await StickerPack.find({ relations: ["stickers"] }); + + res.json({ sticker_packs }); }); export default router; diff --git a/gateway/src/opcodes/Identify.ts b/gateway/src/opcodes/Identify.ts index c91ca5dd..2f9d4632 100644 --- a/gateway/src/opcodes/Identify.ts +++ b/gateway/src/opcodes/Identify.ts @@ -179,6 +179,8 @@ export async function onIdentify(this: WebSocket, data: Payload) { x.guild_hashes = {}; // @ts-ignore x.guild_scheduled_events = []; // @ts-ignore x.threads = []; + x.premium_subscription_count = 30; + x.premium_tier = 3; return x; }), guild_experiments: [], // TODO diff --git a/util/src/entities/StickerPack.ts b/util/src/entities/StickerPack.ts index b80fa9c7..ec8c69a2 100644 --- a/util/src/entities/StickerPack.ts +++ b/util/src/entities/StickerPack.ts @@ -1,8 +1,8 @@ -import { Column, Entity, JoinColumn, OneToMany, OneToOne, RelationId } from "typeorm"; +import { Column, Entity, JoinColumn, ManyToOne, OneToMany, OneToOne, RelationId } from "typeorm"; import { Sticker } from "."; import { BaseClass } from "./BaseClass"; -@Entity("stickers") +@Entity("sticker_packs") export class StickerPack extends BaseClass { @Column() name: string; @@ -13,7 +13,7 @@ export class StickerPack extends BaseClass { @Column({ nullable: true }) banner_asset_id?: string; - @OneToMany(() => Sticker, (sticker: Sticker) => sticker.pack_id, { + @OneToMany(() => Sticker, (sticker: Sticker) => sticker.pack, { cascade: true, orphanedRowAction: "delete", }) @@ -25,7 +25,7 @@ export class StickerPack extends BaseClass { @RelationId((pack: StickerPack) => pack.cover_sticker) cover_sticker_id?: string; - @OneToOne(() => Sticker, { nullable: true }) + @ManyToOne(() => Sticker, { nullable: true }) @JoinColumn() cover_sticker?: Sticker; } -- cgit 1.5.1 From 88425f87ecdc5e0d8e64a8e0ce44fea19daf4652 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Fri, 15 Oct 2021 00:03:35 +0200 Subject: :art: reformat --- api/src/Server.ts | 2 +- api/src/routes/guilds/#guild_id/emojis.ts | 5 +---- api/tsconfig.json | 3 +-- util/src/entities/ReadState.ts | 5 ----- util/src/entities/index.ts | 1 + util/src/util/cdn.ts | 4 +++- 6 files changed, 7 insertions(+), 13 deletions(-) (limited to 'util/src') diff --git a/api/src/Server.ts b/api/src/Server.ts index 1f11a295..a6887fd4 100644 --- a/api/src/Server.ts +++ b/api/src/Server.ts @@ -78,7 +78,7 @@ export class FosscordServer extends Server { api.use("*", (error: any, req: Request, res: Response, next: NextFunction) => { if (error) return next(error); res.status(404).json({ - message: "404: Not Found", + message: "404 endpoint not found", code: 0 }); next(); diff --git a/api/src/routes/guilds/#guild_id/emojis.ts b/api/src/routes/guilds/#guild_id/emojis.ts index ff565cd4..85d7ac05 100644 --- a/api/src/routes/guilds/#guild_id/emojis.ts +++ b/api/src/routes/guilds/#guild_id/emojis.ts @@ -40,17 +40,14 @@ router.post("/", route({ body: "EmojiCreateSchema", permission: "MANAGE_EMOJIS_A const { guild_id } = req.params; const body = req.body as EmojiCreateSchema; + const id = Snowflake.generate(); const emoji_count = await Emoji.count({ guild_id: guild_id }); const { maxEmojis } = Config.get().limits.guild; if (emoji_count >= maxEmojis) throw DiscordApiErrors.MAXIMUM_NUMBER_OF_EMOJIS_REACHED.withParams(maxEmojis); - - const id = Snowflake.generate(); - if (body.require_colons == null) body.require_colons = true; const user = await User.findOneOrFail({ id: req.user_id }); - body.image = (await handleFile(`/emojis/${id}`, body.image)) as string; const emoji = await new Emoji({ diff --git a/api/tsconfig.json b/api/tsconfig.json index 2cf4e4c1..80d7251f 100644 --- a/api/tsconfig.json +++ b/api/tsconfig.json @@ -67,8 +67,7 @@ "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */, "baseUrl": ".", "paths": { - "@fosscord/api": ["src/index"], - "@fosscord/api/*": ["src/*"] + "@fosscord/api": ["src/index"] }, "plugins": [{ "transform": "@zerollup/ts-transform-paths" }], "experimentalDecorators": true diff --git a/util/src/entities/ReadState.ts b/util/src/entities/ReadState.ts index 89480e83..ebef89be 100644 --- a/util/src/entities/ReadState.ts +++ b/util/src/entities/ReadState.ts @@ -32,13 +32,8 @@ export class ReadState extends BaseClass { user: User; @Column({ nullable: true }) - @RelationId((read_state: ReadState) => read_state.last_message) last_message_id: string; - @JoinColumn({ name: "last_message_id" }) - @ManyToOne(() => Message, { nullable: true }) - last_message?: Message; - @Column({ nullable: true }) last_pin_timestamp?: Date; diff --git a/util/src/entities/index.ts b/util/src/entities/index.ts index 7b1c9750..31afed88 100644 --- a/util/src/entities/index.ts +++ b/util/src/entities/index.ts @@ -18,6 +18,7 @@ export * from "./Relationship"; export * from "./Role"; export * from "./Session"; export * from "./Sticker"; +export * from "./StickerPack"; export * from "./Team"; export * from "./TeamMember"; export * from "./Template"; diff --git a/util/src/util/cdn.ts b/util/src/util/cdn.ts index 4dd0078a..ea950cd1 100644 --- a/util/src/util/cdn.ts +++ b/util/src/util/cdn.ts @@ -4,7 +4,9 @@ import fetch from "node-fetch"; import { Config } from "./Config"; import multer from "multer"; -export async function uploadFile(path: string, file: Express.Multer.File) { +export async function uploadFile(path: string, file?: Express.Multer.File) { + if (!file?.buffer) throw new HTTPError("Missing file in body"); + const form = new FormData(); form.append("file", file.buffer, { contentType: file.mimetype, -- cgit 1.5.1 From 0994b47a6702c8957ba8ffcfe1720d3c70fabebe Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Fri, 15 Oct 2021 00:35:18 +0200 Subject: :bug: fix prune --- api/src/routes/guilds/#guild_id/prune.ts | 56 ++++++++++++++++++-------------- util/src/entities/Member.ts | 5 ++- util/src/util/Snowflake.ts | 2 +- 3 files changed, 35 insertions(+), 28 deletions(-) (limited to 'util/src') diff --git a/api/src/routes/guilds/#guild_id/prune.ts b/api/src/routes/guilds/#guild_id/prune.ts index cddbcee7..92809985 100644 --- a/api/src/routes/guilds/#guild_id/prune.ts +++ b/api/src/routes/guilds/#guild_id/prune.ts @@ -1,7 +1,6 @@ import { Router, Request, Response } from "express"; import { Guild, Member, Snowflake } from "@fosscord/util"; -import { HTTPError } from "lambert-server"; -import { LessThan } from "typeorm"; +import { LessThan, IsNull } from "typeorm"; import { route } from "@fosscord/api"; const router = Router(); @@ -16,59 +15,68 @@ export const inactiveMembers = async (guild_id: string, user_id: string, days: n where: [ { guild_id, - last_message_id: LessThan(minId.toString()), + last_message_id: LessThan(minId.toString()) }, + { + last_message_id: IsNull() + } ], relations: ["roles"] }); + console.log(members); if (!members.length) return []; //I'm sure I can do this in the above db query ( and it would probably be better to do so ), but oh well. - if (roles.length && members.length) - members = members.filter(user => user.roles?.some(role => roles.includes(role.id))); + if (roles.length && members.length) members = members.filter((user) => user.roles?.some((role) => roles.includes(role.id))); - const me = await Member.findOne({ id: user_id, guild_id }, { relations: ["roles"] }); - if (!me) throw new HTTPError("You are not member of this guild", 403); - const myHighestRole = Math.max(...(me.roles?.map(x => x.position) || [])) + const me = await Member.findOneOrFail({ id: user_id, guild_id }, { relations: ["roles"] }); + const myHighestRole = Math.max(...(me.roles?.map((x) => x.position) || [])); - const guild = await Guild.findOneOrFail({ where: { id: guild_id } }) + const guild = await Guild.findOneOrFail({ where: { id: guild_id } }); - members = members.filter(member => - member.id !== guild.owner_id && //can't kick owner - member.roles?.some(role => - role.position < myHighestRole || //roles higher than me can't be kicked - me.id === guild.owner_id //owner can kick anyone - ) - ) + members = members.filter( + (member) => + member.id !== guild.owner_id && //can't kick owner + member.roles?.some( + (role) => + role.position < myHighestRole || //roles higher than me can't be kicked + me.id === guild.owner_id //owner can kick anyone + ) + ); return members; -} +}; -router.get("/", async (req: Request, res: Response) => { +router.get("/", route({ permission: "KICK_MEMBERS" }), async (req: Request, res: Response) => { const days = parseInt(req.query.days as string); var roles = req.query.include_roles; - if (typeof roles === 'string') roles = [roles]; //express will return array otherwise + if (typeof roles === "string") roles = [roles]; //express will return array otherwise const members = await inactiveMembers(req.params.guild_id, req.user_id, days, roles as string[]); res.send({ pruned: members.length }); }); +export interface PruneSchema { + /** + * @min 0 + */ + days: number; +} + router.post("/", route({ permission: "KICK_MEMBERS" }), async (req: Request, res: Response) => { const days = parseInt(req.body.days); var roles = req.query.include_roles; - if (typeof roles === 'string') roles = [roles]; + if (typeof roles === "string") roles = [roles]; const { guild_id } = req.params; const members = await inactiveMembers(guild_id, req.user_id, days, roles as string[]); - for (var curr of members) { - await Member.removeFromGuild(curr.id, guild_id); - } + await Promise.all(members.map((x) => Member.removeFromGuild(x.id, guild_id))); res.send({ purged: members.length }); -}) +}); export default router; diff --git a/util/src/entities/Member.ts b/util/src/entities/Member.ts index 19747318..12b0b49a 100644 --- a/util/src/entities/Member.ts +++ b/util/src/entities/Member.ts @@ -84,8 +84,8 @@ export class Member extends BaseClassWithoutId { @Column({ type: "simple-json" }) settings: UserGuildSettings; - @Column() - last_message_id: string; + @Column({ nullable: true }) + last_message_id?: string; // TODO: update // @Column({ type: "simple-json" }) @@ -233,7 +233,6 @@ export class Member extends BaseClassWithoutId { deaf: false, mute: false, pending: false, - last_message_id: "", }; await Promise.all([ diff --git a/util/src/util/Snowflake.ts b/util/src/util/Snowflake.ts index f7a13388..3f6e3c63 100644 --- a/util/src/util/Snowflake.ts +++ b/util/src/util/Snowflake.ts @@ -84,7 +84,7 @@ export class Snowflake { } static generate() { - var time = BigInt(Date.now() - Snowflake.EPOCH) << 22n; + var time = BigInt(Date.now() - Snowflake.EPOCH) << BigInt(22); var worker = Snowflake.workerId << 17n; var process = Snowflake.processId << 12n; var increment = Snowflake.INCREMENT++; -- cgit 1.5.1 From 34e2392b489d5e3250ceecc360f54104ec014f97 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Fri, 15 Oct 2021 18:39:19 +0200 Subject: :sparkles: automatically run db migrations --- util/scripts/migrate_db_engine.js | 109 ++++++++++++++++++++++ util/src/entities/Migration.ts | 18 ++++ util/src/entities/index.ts | 1 + util/src/migrations/1633881705509-VanityInvite.ts | 2 + util/src/migrations/1634308884591-Stickers.ts | 66 +++++++++++++ util/src/migrations/migrate_db_engine.js | 109 ---------------------- util/src/util/Database.ts | 20 +++- 7 files changed, 215 insertions(+), 110 deletions(-) create mode 100644 util/scripts/migrate_db_engine.js create mode 100644 util/src/entities/Migration.ts create mode 100644 util/src/migrations/1634308884591-Stickers.ts delete mode 100644 util/src/migrations/migrate_db_engine.js (limited to 'util/src') diff --git a/util/scripts/migrate_db_engine.js b/util/scripts/migrate_db_engine.js new file mode 100644 index 00000000..79e9d86f --- /dev/null +++ b/util/scripts/migrate_db_engine.js @@ -0,0 +1,109 @@ +const { config } = require("dotenv"); +config(); +const { createConnection } = require("typeorm"); +const { initDatabase } = require("../../dist/util/Database"); +require("missing-native-js-functions"); +const { + Application, + Attachment, + Ban, + Channel, + ConfigEntity, + ConnectedAccount, + Emoji, + Guild, + Invite, + Member, + Message, + ReadState, + Recipient, + Relationship, + Role, + Sticker, + Team, + TeamMember, + Template, + User, + VoiceState, + Webhook, +} = require("../../dist/entities/index"); + +async function main() { + if (!process.env.TO) throw new Error("TO database env connection string not set"); + + // manually arrange them because of foreign keys + const entities = [ + ConfigEntity, + User, + Guild, + Channel, + Invite, + Role, + Ban, + Application, + Emoji, + ConnectedAccount, + Member, + ReadState, + Recipient, + Relationship, + Sticker, + Team, + TeamMember, + Template, + VoiceState, + Webhook, + Message, + Attachment, + ]; + + const oldDB = await initDatabase(); + + const type = process.env.TO.includes("://") ? process.env.TO.split(":")[0]?.replace("+srv", "") : "sqlite"; + const isSqlite = type.includes("sqlite"); + + // @ts-ignore + const newDB = await createConnection({ + type, + url: isSqlite ? undefined : process.env.TO, + database: isSqlite ? process.env.TO : undefined, + entities, + name: "new", + synchronize: true, + }); + let i = 0; + + try { + for (const entity of entities) { + const entries = await oldDB.manager.find(entity); + + // @ts-ignore + console.log("migrating " + entries.length + " " + entity.name + " ..."); + + for (const entry of entries) { + console.log(i++); + + try { + await newDB.manager.insert(entity, entry); + } catch (error) { + try { + if (!entry.id) throw new Error("object doesn't have a unique id: " + entry); + await newDB.manager.update(entity, { id: entry.id }, entry); + } catch (error) { + console.error("couldn't migrate " + i + " " + entity.name, error); + } + } + } + + // @ts-ignore + console.log("migrated " + entries.length + " " + entity.name); + } + } catch (error) { + console.error(error.message); + } + + console.log("SUCCESS migrated all data"); + await newDB.close(); +} + +main().caught(); diff --git a/util/src/entities/Migration.ts b/util/src/entities/Migration.ts new file mode 100644 index 00000000..09df70fb --- /dev/null +++ b/util/src/entities/Migration.ts @@ -0,0 +1,18 @@ +import { Column, Entity, ObjectIdColumn, PrimaryGeneratedColumn } from "typeorm"; +import { BaseClassWithoutId } from "."; + +export const PrimaryIdAutoGenerated = process.env.DATABASE?.startsWith("mongodb") + ? ObjectIdColumn + : PrimaryGeneratedColumn; + +@Entity("migrations") +export class Migration extends BaseClassWithoutId { + @PrimaryIdAutoGenerated() + id: number; + + @Column() + timestamp: number; + + @Column() + name: string; +} diff --git a/util/src/entities/index.ts b/util/src/entities/index.ts index 31afed88..b52841c9 100644 --- a/util/src/entities/index.ts +++ b/util/src/entities/index.ts @@ -11,6 +11,7 @@ export * from "./Guild"; export * from "./Invite"; export * from "./Member"; export * from "./Message"; +export * from "./Migration"; export * from "./RateLimit"; export * from "./ReadState"; export * from "./Recipient"; diff --git a/util/src/migrations/1633881705509-VanityInvite.ts b/util/src/migrations/1633881705509-VanityInvite.ts index af9b98ae..45485310 100644 --- a/util/src/migrations/1633881705509-VanityInvite.ts +++ b/util/src/migrations/1633881705509-VanityInvite.ts @@ -1,6 +1,8 @@ import { MigrationInterface, QueryRunner } from "typeorm"; export class VanityInvite1633881705509 implements MigrationInterface { + name = "VanityInvite1633881705509"; + public async up(queryRunner: QueryRunner): Promise { try { await queryRunner.query(`ALTER TABLE "emojis" DROP COLUMN vanity_url_code`); diff --git a/util/src/migrations/1634308884591-Stickers.ts b/util/src/migrations/1634308884591-Stickers.ts new file mode 100644 index 00000000..fbc4649f --- /dev/null +++ b/util/src/migrations/1634308884591-Stickers.ts @@ -0,0 +1,66 @@ +import { MigrationInterface, QueryRunner, Table, TableColumn, TableForeignKey } from "typeorm"; + +export class Stickers1634308884591 implements MigrationInterface { + name = "Stickers1634308884591"; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.dropForeignKey("read_states", "FK_6f255d873cfbfd7a93849b7ff74"); + await queryRunner.changeColumn( + "stickers", + "tags", + new TableColumn({ name: "tags", type: "varchar", isNullable: true }) + ); + await queryRunner.changeColumn( + "stickers", + "pack_id", + new TableColumn({ name: "pack_id", type: "varchar", isNullable: true }) + ); + await queryRunner.changeColumn("stickers", "type", new TableColumn({ name: "type", type: "integer" })); + await queryRunner.changeColumn( + "stickers", + "format_type", + new TableColumn({ name: "format_type", type: "integer" }) + ); + await queryRunner.changeColumn( + "stickers", + "available", + new TableColumn({ name: "available", type: "boolean", isNullable: true }) + ); + await queryRunner.changeColumn( + "stickers", + "user_id", + new TableColumn({ name: "user_id", type: "boolean", isNullable: true }) + ); + await queryRunner.createForeignKey( + "stickers", + new TableForeignKey({ + name: "FK_8f4ee73f2bb2325ff980502e158", + columnNames: ["user_id"], + referencedColumnNames: ["id"], + referencedTableName: "users", + onDelete: "CASCADE", + }) + ); + await queryRunner.createTable( + new Table({ + name: "sticker_packs", + columns: [ + new TableColumn({ name: "id", type: "varchar", isPrimary: true }), + new TableColumn({ name: "name", type: "varchar" }), + new TableColumn({ name: "description", type: "varchar", isNullable: true }), + new TableColumn({ name: "banner_asset_id", type: "varchar", isNullable: true }), + new TableColumn({ name: "cover_sticker_id", type: "varchar", isNullable: true }), + ], + foreignKeys: [ + new TableForeignKey({ + columnNames: ["cover_sticker_id"], + referencedColumnNames: ["id"], + referencedTableName: "stickers", + }), + ], + }) + ); + } + + public async down(queryRunner: QueryRunner): Promise {} +} diff --git a/util/src/migrations/migrate_db_engine.js b/util/src/migrations/migrate_db_engine.js deleted file mode 100644 index 79e9d86f..00000000 --- a/util/src/migrations/migrate_db_engine.js +++ /dev/null @@ -1,109 +0,0 @@ -const { config } = require("dotenv"); -config(); -const { createConnection } = require("typeorm"); -const { initDatabase } = require("../../dist/util/Database"); -require("missing-native-js-functions"); -const { - Application, - Attachment, - Ban, - Channel, - ConfigEntity, - ConnectedAccount, - Emoji, - Guild, - Invite, - Member, - Message, - ReadState, - Recipient, - Relationship, - Role, - Sticker, - Team, - TeamMember, - Template, - User, - VoiceState, - Webhook, -} = require("../../dist/entities/index"); - -async function main() { - if (!process.env.TO) throw new Error("TO database env connection string not set"); - - // manually arrange them because of foreign keys - const entities = [ - ConfigEntity, - User, - Guild, - Channel, - Invite, - Role, - Ban, - Application, - Emoji, - ConnectedAccount, - Member, - ReadState, - Recipient, - Relationship, - Sticker, - Team, - TeamMember, - Template, - VoiceState, - Webhook, - Message, - Attachment, - ]; - - const oldDB = await initDatabase(); - - const type = process.env.TO.includes("://") ? process.env.TO.split(":")[0]?.replace("+srv", "") : "sqlite"; - const isSqlite = type.includes("sqlite"); - - // @ts-ignore - const newDB = await createConnection({ - type, - url: isSqlite ? undefined : process.env.TO, - database: isSqlite ? process.env.TO : undefined, - entities, - name: "new", - synchronize: true, - }); - let i = 0; - - try { - for (const entity of entities) { - const entries = await oldDB.manager.find(entity); - - // @ts-ignore - console.log("migrating " + entries.length + " " + entity.name + " ..."); - - for (const entry of entries) { - console.log(i++); - - try { - await newDB.manager.insert(entity, entry); - } catch (error) { - try { - if (!entry.id) throw new Error("object doesn't have a unique id: " + entry); - await newDB.manager.update(entity, { id: entry.id }, entry); - } catch (error) { - console.error("couldn't migrate " + i + " " + entity.name, error); - } - } - } - - // @ts-ignore - console.log("migrated " + entries.length + " " + entity.name); - } - } catch (error) { - console.error(error.message); - } - - console.log("SUCCESS migrated all data"); - await newDB.close(); -} - -main().caught(); diff --git a/util/src/util/Database.ts b/util/src/util/Database.ts index 8bce3a6f..6124ffab 100644 --- a/util/src/util/Database.ts +++ b/util/src/util/Database.ts @@ -2,6 +2,7 @@ import path from "path"; import "reflect-metadata"; import { Connection, createConnection } from "typeorm"; import * as Models from "../entities"; +import { Migration } from "../entities/Migration"; import { yellow, green } from "nanocolors"; // UUID extension option is only supported with postgres @@ -33,10 +34,27 @@ export function initDatabase(): Promise { bigNumberStrings: false, supportBigNumbers: true, name: "default", + migrations: [path.join(__dirname, "..", "migrations", "*.js")], }); - promise.then((connection) => { + promise.then(async (connection: Connection) => { dbConnection = connection; + + // run migrations, and if it is a new fresh database, set it to the last migration + if (connection.migrations.length) { + if (!(await Migration.findOne({}))) { + let i = 0; + + await Migration.insert( + connection.migrations.map((x) => ({ + id: i++, + name: x.name, + timestamp: Date.now(), + })) + ); + } + } + await connection.runMigrations(); console.log(`[Database] ${green("connected")}`); }); -- cgit 1.5.1 From a64f79afda319f7e75df1b705fa476131dfb5bbb Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Fri, 15 Oct 2021 18:39:28 +0200 Subject: :sparkles: sticker events --- api/src/routes/guilds/#guild_id/stickers.ts | 28 +++++++++++++++++++++++++++- util/src/interfaces/Event.ts | 10 ++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) (limited to 'util/src') diff --git a/api/src/routes/guilds/#guild_id/stickers.ts b/api/src/routes/guilds/#guild_id/stickers.ts index 39095dc2..4ea1dce1 100644 --- a/api/src/routes/guilds/#guild_id/stickers.ts +++ b/api/src/routes/guilds/#guild_id/stickers.ts @@ -1,4 +1,14 @@ -import { handleFile, Member, Snowflake, Sticker, StickerFormatType, StickerType, uploadFile } from "@fosscord/util"; +import { + emitEvent, + GuildStickersUpdateEvent, + handleFile, + Member, + Snowflake, + Sticker, + StickerFormatType, + StickerType, + uploadFile +} from "@fosscord/util"; import { Router, Request, Response } from "express"; import { route } from "@fosscord/api"; import multer from "multer"; @@ -44,6 +54,8 @@ router.post( uploadFile(`/stickers/${id}`, req.file) ]); + await sendStickerUpdateEvent(guild_id); + res.json(sticker); } ); @@ -94,14 +106,28 @@ router.patch( const body = req.body as ModifyGuildStickerSchema; const sticker = await new Sticker({ ...body, guild_id, id: sticker_id }).save(); + await sendStickerUpdateEvent(guild_id); + return res.json(sticker); } ); +async function sendStickerUpdateEvent(guild_id: string) { + return emitEvent({ + event: "GUILD_STICKERS_UPDATE", + guild_id: guild_id, + data: { + guild_id: guild_id, + stickers: await Sticker.find({ guild_id: guild_id }) + } + } as GuildStickersUpdateEvent); +} + router.delete("/:sticker_id", route({ permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => { const { guild_id, sticker_id } = req.params; await Sticker.delete({ guild_id, id: sticker_id }); + await sendStickerUpdateEvent(guild_id); return res.sendStatus(204); }); diff --git a/util/src/interfaces/Event.ts b/util/src/interfaces/Event.ts index 3c8ab8ab..13fd4b8b 100644 --- a/util/src/interfaces/Event.ts +++ b/util/src/interfaces/Event.ts @@ -12,6 +12,7 @@ import { Interaction } from "./Interaction"; import { ConnectedAccount } from "../entities/ConnectedAccount"; import { Relationship, RelationshipType } from "../entities/Relationship"; import { Presence } from "./Presence"; +import { Sticker } from ".."; export interface Event { guild_id?: string; @@ -193,6 +194,14 @@ export interface GuildEmojisUpdateEvent extends Event { }; } +export interface GuildStickersUpdateEvent extends Event { + event: "GUILD_STICKERS_UPDATE"; + data: { + guild_id: string; + stickers: Sticker[]; + }; +} + export interface GuildIntegrationUpdateEvent extends Event { event: "GUILD_INTEGRATIONS_UPDATE"; data: { @@ -553,6 +562,7 @@ export type EVENT = | "GUILD_BAN_ADD" | "GUILD_BAN_REMOVE" | "GUILD_EMOJIS_UPDATE" + | "GUILD_STICKERS_UPDATE" | "GUILD_INTEGRATIONS_UPDATE" | "GUILD_MEMBER_ADD" | "GUILD_MEMBER_REMOVE" -- cgit 1.5.1 From b9d852a4bdbb5c2478db744a91defeb8e944619b Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Fri, 15 Oct 2021 19:20:16 +0100 Subject: fix: make timestamp column a bigint --- util/src/entities/Migration.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util/src') diff --git a/util/src/entities/Migration.ts b/util/src/entities/Migration.ts index 09df70fb..7393496f 100644 --- a/util/src/entities/Migration.ts +++ b/util/src/entities/Migration.ts @@ -10,7 +10,7 @@ export class Migration extends BaseClassWithoutId { @PrimaryIdAutoGenerated() id: number; - @Column() + @Column({ type: 'bigint' }) timestamp: number; @Column() -- cgit 1.5.1 From 451e27998ffdcf63ad22aa4a8f59cfd1ecb6407a Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Fri, 15 Oct 2021 23:29:59 +0200 Subject: :bug: fix migration timestampe --- util/src/util/Database.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util/src') diff --git a/util/src/util/Database.ts b/util/src/util/Database.ts index 6124ffab..c71ebbd4 100644 --- a/util/src/util/Database.ts +++ b/util/src/util/Database.ts @@ -49,7 +49,7 @@ export function initDatabase(): Promise { connection.migrations.map((x) => ({ id: i++, name: x.name, - timestamp: Date.now(), + timestamp: Math.floor(Date.now() / 1000), })) ); } -- cgit 1.5.1 From bd0b941feaff6b414caf2c90cb1f1d0dce2e8106 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 17 Oct 2021 00:38:56 +0200 Subject: :sparkles: added session + memberlist event --- gateway/src/schema/Activity.ts | 39 +++------------------------------------ gateway/src/schema/Emoji.ts | 11 ----------- util/src/interfaces/Activity.ts | 37 +++++++++++++++++++------------------ util/src/interfaces/Event.ts | 37 +++++++++++++++++++++++++++++++++++++ util/src/interfaces/Presence.ts | 4 +++- 5 files changed, 62 insertions(+), 66 deletions(-) delete mode 100644 gateway/src/schema/Emoji.ts (limited to 'util/src') diff --git a/gateway/src/schema/Activity.ts b/gateway/src/schema/Activity.ts index f1665efd..e8763046 100644 --- a/gateway/src/schema/Activity.ts +++ b/gateway/src/schema/Activity.ts @@ -1,4 +1,4 @@ -import { EmojiSchema } from "./Emoji"; +import { Activity, Status } from "@fosscord/util"; export const ActivitySchema = { afk: Boolean, @@ -47,40 +47,7 @@ export const ActivitySchema = { export interface ActivitySchema { afk: boolean; - status: string; - activities?: [ - { - name: string; // the activity's name - type: number; // activity type // TODO: check if its between range 0-5 - url?: string; // stream url, is validated when type is 1 - created_at?: number; // unix timestamp of when the activity was added to the user's session - timestamps?: { - // unix timestamps for start and/or end of the game - start: number; - end: number; - }; - application_id?: string; // application id for the game - details?: string; - state?: string; - emoji?: EmojiSchema; - party?: { - id?: string; - size?: [number]; // used to show the party's current and maximum size // TODO: array length 2 - }; - assets?: { - large_image?: string; // the id for a large asset of the activity, usually a snowflake - large_text?: string; // text displayed when hovering over the large image of the activity - small_image?: string; // the id for a small asset of the activity, usually a snowflake - small_text?: string; // text displayed when hovering over the small image of the activity - }; - secrets?: { - join?: string; // the secret for joining a party - spectate?: string; // the secret for spectating a game - match?: string; // the secret for a specific instanced match - }; - instance?: boolean; - flags: string; // activity flags OR d together, describes what the payload includes - } - ]; + status: Status; + activities?: Activity[]; since?: number; // unix time (in milliseconds) of when the client went idle, or null if the client is not idle } diff --git a/gateway/src/schema/Emoji.ts b/gateway/src/schema/Emoji.ts deleted file mode 100644 index 413b8359..00000000 --- a/gateway/src/schema/Emoji.ts +++ /dev/null @@ -1,11 +0,0 @@ -export const EmojiSchema = { - name: String, // the name of the emoji - $id: String, // the id of the emoji - animated: Boolean, // whether this emoji is animated -}; - -export interface EmojiSchema { - name: string; - id?: string; - animated: Boolean; -} diff --git a/util/src/interfaces/Activity.ts b/util/src/interfaces/Activity.ts index f5a3c270..43984afd 100644 --- a/util/src/interfaces/Activity.ts +++ b/util/src/interfaces/Activity.ts @@ -1,37 +1,38 @@ export interface Activity { - name: string; - type: ActivityType; - url?: string; - created_at?: Date; + name: string; // the activity's name + type: ActivityType; // activity type // TODO: check if its between range 0-5 + url?: string; // stream url, is validated when type is 1 + created_at?: number; // unix timestamp of when the activity was added to the user's session timestamps?: { - start?: number; - end?: number; - }[]; - application_id?: string; + // unix timestamps for start and/or end of the game + start: number; + end: number; + }; + application_id?: string; // application id for the game details?: string; state?: string; emoji?: { name: string; id?: string; - amimated?: boolean; + animated: boolean; }; party?: { id?: string; - size?: [number, number]; + size?: [number]; // used to show the party's current and maximum size // TODO: array length 2 }; assets?: { - large_image?: string; - large_text?: string; - small_image?: string; - small_text?: string; + large_image?: string; // the id for a large asset of the activity, usually a snowflake + large_text?: string; // text displayed when hovering over the large image of the activity + small_image?: string; // the id for a small asset of the activity, usually a snowflake + small_text?: string; // text displayed when hovering over the small image of the activity }; secrets?: { - join?: string; - spectate?: string; - match?: string; + join?: string; // the secret for joining a party + spectate?: string; // the secret for spectating a game + match?: string; // the secret for a specific instanced match }; instance?: boolean; - flags?: bigint; + flags: string; // activity flags OR d together, describes what the payload includes } export enum ActivityType { diff --git a/util/src/interfaces/Event.ts b/util/src/interfaces/Event.ts index 13fd4b8b..a5253c09 100644 --- a/util/src/interfaces/Event.ts +++ b/util/src/interfaces/Event.ts @@ -13,6 +13,7 @@ import { ConnectedAccount } from "../entities/ConnectedAccount"; import { Relationship, RelationshipType } from "../entities/Relationship"; import { Presence } from "./Presence"; import { Sticker } from ".."; +import { Activity, Status } from "."; export interface Event { guild_id?: string; @@ -454,6 +455,37 @@ export interface RelationshipRemoveEvent extends Event { data: Omit; } +export interface SessionsReplace extends Event { + event: "SESSIONS_REPLACE"; + data: { + activities: Activity[]; + client_info: { + version: number; + os: string; + client: string; + }; + status: Status; + }[]; +} + +export interface GuildMemberListUpdate extends Event { + event: "GUILD_MEMBER_LIST_UPDATE"; + data: { + groups: { id: string; count: number }[]; + guild_id: string; + id: string; + member_count: number; + online_count: number; + ops: { + index: number; + item: { + member?: PublicMember & { presence: Presence }; + group?: { id: string; count: number }[]; + }; + }[]; + }; +} + export type EventData = | InvalidatedEvent | ReadyEvent @@ -474,6 +506,7 @@ export type EventData = | GuildMemberRemoveEvent | GuildMemberUpdateEvent | GuildMembersChunkEvent + | GuildMemberListUpdate | GuildRoleCreateEvent | GuildRoleUpdateEvent | GuildRoleDeleteEvent @@ -523,6 +556,7 @@ export enum EVENTEnum { GuildMemberUpdate = "GUILD_MEMBER_UPDATE", GuildMemberSpeaking = "GUILD_MEMBER_SPEAKING", GuildMembersChunk = "GUILD_MEMBERS_CHUNK", + GuildMemberListUpdate = "GUILD_MEMBER_LIST_UPDATE", GuildRoleCreate = "GUILD_ROLE_CREATE", GuildRoleDelete = "GUILD_ROLE_DELETE", GuildRoleUpdate = "GUILD_ROLE_UPDATE", @@ -546,6 +580,7 @@ export enum EVENTEnum { ApplicationCommandCreate = "APPLICATION_COMMAND_CREATE", ApplicationCommandUpdate = "APPLICATION_COMMAND_UPDATE", ApplicationCommandDelete = "APPLICATION_COMMAND_DELETE", + SessionsReplace = "SESSIONS_REPLACE", } export type EVENT = @@ -569,6 +604,7 @@ export type EVENT = | "GUILD_MEMBER_UPDATE" | "GUILD_MEMBER_SPEAKING" | "GUILD_MEMBERS_CHUNK" + | "GUILD_MEMBER_LIST_UPDATE" | "GUILD_ROLE_CREATE" | "GUILD_ROLE_DELETE" | "GUILD_ROLE_UPDATE" @@ -597,6 +633,7 @@ export type EVENT = | "MESSAGE_ACK" | "RELATIONSHIP_ADD" | "RELATIONSHIP_REMOVE" + | "SESSIONS_REPLACE" | CUSTOMEVENTS; export type CUSTOMEVENTS = "INVALIDATED" | "RATELIMIT"; diff --git a/util/src/interfaces/Presence.ts b/util/src/interfaces/Presence.ts index 4a1ff038..7663891a 100644 --- a/util/src/interfaces/Presence.ts +++ b/util/src/interfaces/Presence.ts @@ -1,10 +1,12 @@ import { ClientStatus, Status } from "./Status"; import { Activity } from "./Activity"; +import { PublicUser } from "../entities/User"; export interface Presence { - user_id: string; + user: PublicUser; guild_id?: string; status: Status; activities: Activity[]; client_status: ClientStatus; + // TODO: game } -- cgit 1.5.1 From 0ea7d5f35cba312545d56bee8abc878fe34383a0 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 17 Oct 2021 00:39:54 +0200 Subject: :sparkles: User presence/status --- gateway/src/events/Close.ts | 41 +++++++++-- gateway/src/events/Connection.ts | 2 +- gateway/src/opcodes/LazyRequest.ts | 132 ++++++++++++++++++++++++---------- gateway/src/opcodes/PresenceUpdate.ts | 24 ++++++- gateway/src/schema/LazyRequest.ts | 2 +- gateway/src/util/WebSocket.ts | 2 + gateway/tsconfig.json | 2 +- util/src/entities/Member.ts | 18 ++++- util/src/entities/Session.ts | 20 ++++-- 9 files changed, 193 insertions(+), 50 deletions(-) (limited to 'util/src') diff --git a/gateway/src/events/Close.ts b/gateway/src/events/Close.ts index 5c1bd292..5b7c512c 100644 --- a/gateway/src/events/Close.ts +++ b/gateway/src/events/Close.ts @@ -1,13 +1,46 @@ import { WebSocket } from "@fosscord/gateway"; -import { Session } from "@fosscord/util"; +import { + emitEvent, + PresenceUpdateEvent, + PrivateSessionProjection, + Session, + SessionsReplace, + User, +} from "@fosscord/util"; export async function Close(this: WebSocket, code: number, reason: string) { console.log("[WebSocket] closed", code, reason); - if (this.session_id) await Session.delete({ session_id: this.session_id }); if (this.heartbeatTimeout) clearTimeout(this.heartbeatTimeout); if (this.readyTimeout) clearTimeout(this.readyTimeout); - this.deflate?.close(); - this.removeAllListeners(); + + if (this.session_id) { + await Session.delete({ session_id: this.session_id }); + const sessions = await Session.find({ + where: { user_id: this.user_id }, + select: PrivateSessionProjection, + }); + await emitEvent({ + event: "SESSIONS_REPLACE", + user_id: this.user_id, + data: sessions, + } as SessionsReplace); + const session = sessions.first() || { + activities: [], + client_info: {}, + status: "offline", + }; + + await emitEvent({ + event: "PRESENCE_UPDATE", + user_id: this.user_id, + data: { + user: await User.getPublicUser(this.user_id), + activities: session.activities, + client_status: session?.client_info, + status: session.status, + }, + } as PresenceUpdateEvent); + } } diff --git a/gateway/src/events/Connection.ts b/gateway/src/events/Connection.ts index 9bb034f0..4954cd08 100644 --- a/gateway/src/events/Connection.ts +++ b/gateway/src/events/Connection.ts @@ -8,7 +8,6 @@ import { Close } from "./Close"; import { Message } from "./Message"; import { createDeflate } from "zlib"; import { URL } from "url"; -import { Session } from "@fosscord/util"; var erlpack: any; try { erlpack = require("@yukikaze-bot/erlpack"); @@ -57,6 +56,7 @@ export async function Connection( } socket.events = {}; + socket.member_events = {}; socket.permissions = {}; socket.sequence = 0; diff --git a/gateway/src/opcodes/LazyRequest.ts b/gateway/src/opcodes/LazyRequest.ts index f5fd561a..c304dfe7 100644 --- a/gateway/src/opcodes/LazyRequest.ts +++ b/gateway/src/opcodes/LazyRequest.ts @@ -1,46 +1,55 @@ import { + EVENTEnum, + EventOpts, getPermission, + listenEvent, Member, - PublicMemberProjection, Role, } from "@fosscord/util"; import { LazyRequest } from "../schema/LazyRequest"; import { Send } from "../util/Send"; import { OPCODES } from "../util/Constants"; -import { WebSocket, Payload } from "@fosscord/gateway"; +import { WebSocket, Payload, handlePresenceUpdate } from "@fosscord/gateway"; import { check } from "./instanceOf"; import "missing-native-js-functions"; +import { getRepository } from "typeorm"; +import "missing-native-js-functions"; -// TODO: check permission and only show roles/members that have access to this channel +// TODO: only show roles/members that have access to this channel // TODO: config: to list all members (even those who are offline) sorted by role, or just those who are online // TODO: rewrite typeorm -export async function onLazyRequest(this: WebSocket, { d }: Payload) { - // TODO: check data - check.call(this, LazyRequest, d); - const { guild_id, typing, channels, activities } = d as LazyRequest; - - const permissions = await getPermission(this.user_id, guild_id); - permissions.hasThrow("VIEW_CHANNEL"); - - var members = await Member.find({ - where: { guild_id: guild_id }, - relations: ["roles", "user"], - select: PublicMemberProjection, - }); +async function getMembers(guild_id: string, range: [number, number]) { + if (!Array.isArray(range) || range.length !== 2) { + throw new Error("range is not a valid array"); + } + // TODO: wait for typeorm to implement ordering for .find queries https://github.com/typeorm/typeorm/issues/2620 - const roles = await Role.find({ - where: { guild_id: guild_id }, - order: { - position: "DESC", - }, - }); + let members = await getRepository(Member) + .createQueryBuilder("member") + .where("member.guild_id = :guild_id", { guild_id }) + .leftJoinAndSelect("member.roles", "role") + .leftJoinAndSelect("member.user", "user") + .leftJoinAndSelect("user.sessions", "session") + .addSelect( + "CASE WHEN session.status = 'offline' THEN 0 ELSE 1 END", + "_status" + ) + .orderBy("role.position", "DESC") + .addOrderBy("_status", "DESC") + .addOrderBy("user.username", "ASC") + .offset(Number(range[0]) || 0) + .limit(Number(range[1]) || 100) + .getMany(); const groups = [] as any[]; - var member_count = 0; const items = []; + const member_roles = members + .map((m) => m.roles) + .flat() + .unique((r) => r.id); - for (const role of roles) { + for (const role of member_roles) { // @ts-ignore const [role_members, other_members] = partition(members, (m: Member) => m.roles.find((r) => r.id === role.id) @@ -54,35 +63,86 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) { groups.push(group); for (const member of role_members) { - member.roles = member.roles.filter((x: Role) => x.id !== guild_id); + const roles = member.roles + .filter((x: Role) => x.id !== guild_id) + .map((x: Role) => x.id); + + const session = member.user.sessions.first(); + + // TODO: properly mock/hide offline/invisible status items.push({ member: { ...member, - roles: member.roles.map((x: Role) => x.id), + roles, + user: { ...member.user, sessions: undefined }, + presence: { + ...session, + activities: session?.activities || [], + user: { id: member.user.id }, + }, }, }); } members = other_members; - member_count += role_members.length; } + return { + items, + groups, + range, + members: items.map((x) => x.member).filter((x) => x), + }; +} + +export async function onLazyRequest(this: WebSocket, { d }: Payload) { + // TODO: check data + check.call(this, LazyRequest, d); + const { guild_id, typing, channels, activities } = d as LazyRequest; + + const channel_id = Object.keys(channels || {}).first(); + if (!channel_id) return; + + const permissions = await getPermission(this.user_id, guild_id, channel_id); + permissions.hasThrow("VIEW_CHANNEL"); + + const ranges = channels![channel_id]; + if (!Array.isArray(ranges)) throw new Error("Not a valid Array"); + + const member_count = await Member.count({ guild_id }); + const ops = await Promise.all(ranges.map((x) => getMembers(guild_id, x))); + + // TODO: unsubscribe member_events that are not in op.members + + ops.forEach((op) => { + op.members.forEach(async (member) => { + if (this.events[member.user.id]) return; // already subscribed as friend + if (this.member_events[member.user.id]) return; // already subscribed in member list + this.member_events[member.user.id] = await listenEvent( + member.user.id, + handlePresenceUpdate.bind(this), + this.listen_options + ); + }); + }); + return Send(this, { op: OPCODES.Dispatch, s: this.sequence++, t: "GUILD_MEMBER_LIST_UPDATE", d: { - ops: [ - { - range: [0, 99], - op: "SYNC", - items, - }, - ], - online_count: member_count, // TODO count online count + ops: ops.map((x) => ({ + items: x.items, + op: "SYNC", + range: x.range, + })), + online_count: member_count, member_count, id: "everyone", guild_id, - groups, + groups: ops + .map((x) => x.groups) + .flat() + .unique(), }, }); } diff --git a/gateway/src/opcodes/PresenceUpdate.ts b/gateway/src/opcodes/PresenceUpdate.ts index 53d7b9d2..415df6ee 100644 --- a/gateway/src/opcodes/PresenceUpdate.ts +++ b/gateway/src/opcodes/PresenceUpdate.ts @@ -1,5 +1,25 @@ import { WebSocket, Payload } from "@fosscord/gateway"; +import { emitEvent, PresenceUpdateEvent, Session, User } from "@fosscord/util"; +import { ActivitySchema } from "../schema/Activity"; +import { check } from "./instanceOf"; -export function onPresenceUpdate(this: WebSocket, data: Payload) { - // return this.close(CLOSECODES.Unknown_error); +export async function onPresenceUpdate(this: WebSocket, { d }: Payload) { + check.call(this, ActivitySchema, d); + const presence = d as ActivitySchema; + + await Session.update( + { session_id: this.session_id }, + { status: presence.status, activities: presence.activities } + ); + + await emitEvent({ + event: "PRESENCE_UPDATE", + user_id: this.user_id, + data: { + user: await User.getPublicUser(this.user_id), + activities: presence.activities, + client_status: {}, // TODO: + status: presence.status, + }, + } as PresenceUpdateEvent); } diff --git a/gateway/src/schema/LazyRequest.ts b/gateway/src/schema/LazyRequest.ts index 7c828ac6..1fe658bb 100644 --- a/gateway/src/schema/LazyRequest.ts +++ b/gateway/src/schema/LazyRequest.ts @@ -1,6 +1,6 @@ export interface LazyRequest { guild_id: string; - channels?: Record; + channels?: Record; activities?: boolean; threads?: boolean; typing?: true; diff --git a/gateway/src/util/WebSocket.ts b/gateway/src/util/WebSocket.ts index 49626b2a..e3313f40 100644 --- a/gateway/src/util/WebSocket.ts +++ b/gateway/src/util/WebSocket.ts @@ -17,4 +17,6 @@ export interface WebSocket extends WS { sequence: number; permissions: Record; events: Record; + member_events: Record; + listen_options: any; } diff --git a/gateway/tsconfig.json b/gateway/tsconfig.json index 2ad38f93..b6ae9455 100644 --- a/gateway/tsconfig.json +++ b/gateway/tsconfig.json @@ -27,7 +27,7 @@ // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ /* Strict Type-Checking Options */ - "strict": false /* Enable all strict type-checking options. */, + "strict": true /* Enable all strict type-checking options. */, "noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */, "strictNullChecks": true /* Enable strict null checks. */, // "strictFunctionTypes": true, /* Enable strict checking of function types. */ diff --git a/util/src/entities/Member.ts b/util/src/entities/Member.ts index 12b0b49a..0f7be2a7 100644 --- a/util/src/entities/Member.ts +++ b/util/src/entities/Member.ts @@ -26,6 +26,22 @@ import { BaseClassWithoutId } from "./BaseClass"; import { Ban, PublicGuildRelations } from "."; import { DiscordApiErrors } from "../util/Constants"; +export const MemberPrivateProjection: (keyof Member)[] = [ + "id", + "guild", + "guild_id", + "deaf", + "joined_at", + "last_message_id", + "mute", + "nick", + "pending", + "premium_since", + "roles", + "settings", + "user", +]; + @Entity("members") @Index(["id", "guild_id"], { unique: true }) export class Member extends BaseClassWithoutId { @@ -81,7 +97,7 @@ export class Member extends BaseClassWithoutId { @Column() pending: boolean; - @Column({ type: "simple-json" }) + @Column({ type: "simple-json", select: false }) settings: UserGuildSettings; @Column({ nullable: true }) diff --git a/util/src/entities/Session.ts b/util/src/entities/Session.ts index 7cc325f5..ac5313f1 100644 --- a/util/src/entities/Session.ts +++ b/util/src/entities/Session.ts @@ -1,6 +1,8 @@ import { User } from "./User"; import { BaseClass } from "./BaseClass"; import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; +import { Status } from "../interfaces/Status"; +import { Activity } from "../interfaces/Activity"; //TODO we need to remove all sessions on server start because if the server crashes without closing websockets it won't delete them @@ -17,11 +19,13 @@ export class Session extends BaseClass { user: User; //TODO check, should be 32 char long hex string - @Column({ nullable: false }) + @Column({ nullable: false, select: false }) session_id: string; - activities: []; //TODO + @Column({ type: "simple-json", nullable: true }) + activities: Activity[] = []; + // TODO client_status @Column({ type: "simple-json", select: false }) client_info: { client: string; @@ -29,6 +33,14 @@ export class Session extends BaseClass { version: number; }; - @Column({ nullable: false }) - status: string; //TODO enum + @Column({ nullable: false, type: "varchar" }) + status: Status; //TODO enum } + +export const PrivateSessionProjection: (keyof Session)[] = [ + "user_id", + "session_id", + "activities", + "client_info", + "status", +]; -- cgit 1.5.1 From 5249e923a0614e83ac5a7ffb1885308b3f4a936e Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 17 Oct 2021 00:41:24 +0200 Subject: :art: reformatted --- api/tests/routes.test.ts | 2 -- util/src/entities/User.ts | 17 +++++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) (limited to 'util/src') diff --git a/api/tests/routes.test.ts b/api/tests/routes.test.ts index 2c265ee3..35d74a94 100644 --- a/api/tests/routes.test.ts +++ b/api/tests/routes.test.ts @@ -56,9 +56,7 @@ beforeAll(async (done) => { const response = await request("/auth/register", { body: { fingerprint: "805826570869932034.wR8vi8lGlFBJerErO9LG5NViJFw", - email: "test@example.com", username: "tester", - password: "wtp9gep9gw", invite: null, consent: true, date_of_birth: "2000-01-01", diff --git a/util/src/entities/User.ts b/util/src/entities/User.ts index 04f1e9cb..bc852616 100644 --- a/util/src/entities/User.ts +++ b/util/src/entities/User.ts @@ -4,7 +4,7 @@ import { BitField } from "../util/BitField"; import { Relationship } from "./Relationship"; import { ConnectedAccount } from "./ConnectedAccount"; import { Config, FieldErrors, Snowflake, trimSpecial } from ".."; -import { Member } from "."; +import { Member, Session } from "."; export enum PublicUserEnum { username, @@ -131,6 +131,9 @@ export class User extends BaseClass { @Column() rights: string; // Rights + @OneToMany(() => Session, (session: Session) => session.user) + sessions: Session[]; + @JoinColumn({ name: "relationship_ids" }) @OneToMany(() => Relationship, (relationship: Relationship) => relationship.from, { cascade: true, @@ -250,11 +253,13 @@ export class User extends BaseClass { await user.save(); - if (Config.get().guild.autoJoin.enabled) { - for (const guild of Config.get().guild.autoJoin.guilds || []) { - await Member.addToGuild(user.id, guild); + setImmediate(async () => { + if (Config.get().guild.autoJoin.enabled) { + for (const guild of Config.get().guild.autoJoin.guilds || []) { + await Member.addToGuild(user.id, guild).catch((e) => {}); + } } - } + }); return user; } @@ -293,7 +298,7 @@ export const defaultSettings: UserSettings = { render_reactions: true, restricted_guilds: [], show_current_game: true, - status: "offline", + status: "online", stream_notifications_enabled: true, theme: "dark", timezone_offset: 0, -- cgit 1.5.1 From f2cb03da4bcfd74e1082652f21bf182853d7b2e9 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 17 Oct 2021 00:51:51 +0200 Subject: :sparkles: add presence migration --- util/src/migrations/1634424361103-Presence.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 util/src/migrations/1634424361103-Presence.ts (limited to 'util/src') diff --git a/util/src/migrations/1634424361103-Presence.ts b/util/src/migrations/1634424361103-Presence.ts new file mode 100644 index 00000000..729955b8 --- /dev/null +++ b/util/src/migrations/1634424361103-Presence.ts @@ -0,0 +1,11 @@ +import { MigrationInterface, QueryRunner, TableColumn } from "typeorm"; + +export class Presence1634424361103 implements MigrationInterface { + name = "Presence1634424361103"; + + public async up(queryRunner: QueryRunner): Promise { + queryRunner.addColumn("sessions", new TableColumn({ name: "activites", type: "text" })); + } + + public async down(queryRunner: QueryRunner): Promise {} +} -- cgit 1.5.1 From 9fa1081803bb4e0cc043cd554b8c1d67f7ae5e68 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 17 Oct 2021 00:57:31 +0200 Subject: :bug: default session activites --- gateway/src/opcodes/Identify.ts | 1 + util/src/entities/Session.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'util/src') diff --git a/gateway/src/opcodes/Identify.ts b/gateway/src/opcodes/Identify.ts index bd7fc894..f39ac808 100644 --- a/gateway/src/opcodes/Identify.ts +++ b/gateway/src/opcodes/Identify.ts @@ -94,6 +94,7 @@ export async function onIdentify(this: WebSocket, data: Payload) { os: identify.properties?.os, version: 0, }, + activities: [], }).save(), Application.findOne({ id: this.user_id }), ]); diff --git a/util/src/entities/Session.ts b/util/src/entities/Session.ts index ac5313f1..969efa89 100644 --- a/util/src/entities/Session.ts +++ b/util/src/entities/Session.ts @@ -23,7 +23,7 @@ export class Session extends BaseClass { session_id: string; @Column({ type: "simple-json", nullable: true }) - activities: Activity[] = []; + activities: Activity[]; // TODO client_status @Column({ type: "simple-json", select: false }) -- cgit 1.5.1 From a8ba406222bbc8c862cab1b4e14b3be398d43371 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 17 Oct 2021 01:23:51 +0200 Subject: :sparkles: migration for timestamp migrations --- util/src/migrations/1634426540271-MigrationTimestamp.ts | 15 +++++++++++++++ util/src/util/Database.ts | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 util/src/migrations/1634426540271-MigrationTimestamp.ts (limited to 'util/src') diff --git a/util/src/migrations/1634426540271-MigrationTimestamp.ts b/util/src/migrations/1634426540271-MigrationTimestamp.ts new file mode 100644 index 00000000..3208b25b --- /dev/null +++ b/util/src/migrations/1634426540271-MigrationTimestamp.ts @@ -0,0 +1,15 @@ +import { MigrationInterface, QueryRunner, TableColumn } from "typeorm"; + +export class MigrationTimestamp1634426540271 implements MigrationInterface { + name = "MigrationTimestamp1634426540271"; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.changeColumn( + "migrations", + "timestamp", + new TableColumn({ name: "timestampe", type: "bigint", isNullable: false }) + ); + } + + public async down(queryRunner: QueryRunner): Promise {} +} diff --git a/util/src/util/Database.ts b/util/src/util/Database.ts index c71ebbd4..6124ffab 100644 --- a/util/src/util/Database.ts +++ b/util/src/util/Database.ts @@ -49,7 +49,7 @@ export function initDatabase(): Promise { connection.migrations.map((x) => ({ id: i++, name: x.name, - timestamp: Math.floor(Date.now() / 1000), + timestamp: Date.now(), })) ); } -- cgit 1.5.1