diff --git a/api/.gitignore b/api/.gitignore index 85ddc5c8..662816b9 100644 --- a/api/.gitignore +++ b/api/.gitignore @@ -109,4 +109,7 @@ src/ready.json # Docker .docker/config/* -!.docker/config/.keep \ No newline at end of file +!.docker/config/.keep + +# fosscord +*.db \ No newline at end of file diff --git a/api/__tests__/routes/auth/login.ts b/api/__tests__/routes/auth/login.ts deleted file mode 100644 index 536e03ee..00000000 --- a/api/__tests__/routes/auth/login.ts +++ /dev/null @@ -1 +0,0 @@ -it("works", () => {}); diff --git a/api/jest.config.ts b/api/jest.config.ts deleted file mode 100644 index 153a9b2b..00000000 --- a/api/jest.config.ts +++ /dev/null @@ -1,192 +0,0 @@ -/* - * For a detailed explanation regarding each configuration property and type check, visit: - * https://jestjs.io/docs/en/configuration.html - */ - -export default { - // All imported modules in your tests should be mocked automatically - // automock: false, - - // Stop running tests after `n` failures - // bail: 0, - - // The directory where Jest should store its cached dependency information - // cacheDirectory: "/private/var/folders/yw/1x3cqgtd4wvg2m2w4pjv5ctm0000gn/T/jest_dx", - - // Automatically clear mock calls and instances between every test - // clearMocks: false, - - // Indicates whether the coverage information should be collected while executing the test - // collectCoverage: false, - - // An array of glob patterns indicating a set of files for which coverage information should be collected - // collectCoverageFrom: undefined, - - // The directory where Jest should output its coverage files - coverageDirectory: "coverage", - - // An array of regexp pattern strings used to skip coverage collection - coveragePathIgnorePatterns: ["/node_modules/"], - - // Indicates which provider should be used to instrument code for coverage - coverageProvider: "v8", - - // A list of reporter names that Jest uses when writing coverage reports - // coverageReporters: [ - // "json", - // "text", - // "lcov", - // "clover" - // ], - - // An object that configures minimum threshold enforcement for coverage results - // coverageThreshold: undefined, - - // A path to a custom dependency extractor - // dependencyExtractor: undefined, - - // Make calling deprecated APIs throw helpful error messages - // errorOnDeprecated: false, - - // Force coverage collection from ignored files using an array of glob patterns - // forceCoverageMatch: [], - - // A path to a module which exports an async function that is triggered once before all test suites - // globalSetup: undefined, - - // A path to a module which exports an async function that is triggered once after all test suites - // globalTeardown: undefined, - - // A set of global variables that need to be available in all test environments - // globals: {}, - - // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers. - // maxWorkers: "50%", - - // An array of directory names to be searched recursively up from the requiring module's location - // moduleDirectories: [ - // "node_modules" - // ], - - // An array of file extensions your modules use - // moduleFileExtensions: [ - // "js", - // "json", - // "jsx", - // "ts", - // "tsx", - // "node" - // ], - - // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module - // moduleNameMapper: {}, - - // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader - // modulePathIgnorePatterns: [], - - // Activates notifications for test results - // notify: false, - - // An enum that specifies notification mode. Requires { notify: true } - // notifyMode: "failure-change", - - // A preset that is used as a base for Jest's configuration - // preset: undefined, - - // Run tests from one or more projects - // projects: undefined, - - // Use this configuration option to add custom reporters to Jest - // reporters: undefined, - - // Automatically reset mock state between every test - // resetMocks: false, - - // Reset the module registry before running each individual test - // resetModules: false, - - // A path to a custom resolver - // resolver: undefined, - - // Automatically restore mock state between every test - // restoreMocks: false, - - // The root directory that Jest should scan for tests and modules within - // rootDir: undefined, - - // A list of paths to directories that Jest should use to search for files in - // roots: [ - // "" - // ], - - // Allows you to use a custom runner instead of Jest's default test runner - // runner: "jest-runner", - - // The paths to modules that run some code to configure or set up the testing environment before each test - // setupFiles: [], - - // A list of paths to modules that run some code to configure or set up the testing framework before each test - // setupFilesAfterEnv: [], - - // The number of seconds after which a test is considered as slow and reported as such in the results. - // slowTestThreshold: 5, - - // A list of paths to snapshot serializer modules Jest should use for snapshot testing - // snapshotSerializers: [], - - // The test environment that will be used for testing - testEnvironment: "node", - - // Options that will be passed to the testEnvironment - // testEnvironmentOptions: {}, - - // Adds a location field to test results - // testLocationInResults: false, - - // The glob patterns Jest uses to detect test files - testMatch: [ - "**/__tests__/**/*.[jt]s?(x)", - // "**/?(*.)+(spec|test).[tj]s?(x)" - ], - - // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped - // testPathIgnorePatterns: [ - // "/node_modules/" - // ], - - // The regexp pattern or array of patterns that Jest uses to detect test files - // testRegex: [], - - // This option allows the use of a custom results processor - // testResultsProcessor: undefined, - - // This option allows use of a custom test runner - // testRunner: "jasmine2", - - // This option sets the URL for the jsdom environment. It is reflected in properties such as location.href - // testURL: "http://localhost", - - // Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout" - // timers: "real", - - // A map from regular expressions to paths to transformers - // transform: undefined, - - // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation - // transformIgnorePatterns: [ - // "/node_modules/", - // "\\.pnp\\.[^\\/]+$" - // ], - - // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them - // unmockedModulePathPatterns: undefined, - - // Indicates whether each individual test should be reported during the run - // verbose: undefined, - - // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode - // watchPathIgnorePatterns: [], - - // Whether to use watchman for file crawling - // watchman: true, -}; diff --git a/api/jest/setup.js b/api/jest/setup.js new file mode 100644 index 00000000..abc485ae --- /dev/null +++ b/api/jest/setup.js @@ -0,0 +1,2 @@ +jest.spyOn(global.console, "log").mockImplementation(() => jest.fn()); +jest.spyOn(global.console, "info").mockImplementation(() => jest.fn()); diff --git a/api/package-lock.json b/api/package-lock.json index 00af4b5b..63724688 100644 Binary files a/api/package-lock.json and b/api/package-lock.json differ diff --git a/api/package.json b/api/package.json index 7b2de011..37db6ff3 100644 --- a/api/package.json +++ b/api/package.json @@ -5,7 +5,7 @@ "main": "dist/Server.js", "types": "dist/Server.d.ts", "scripts": { - "test": "jest", + "test": "npm run build && jest --coverage --verbose ./tests", "test:watch": "jest --watch", "start": "npm run build && node dist/start", "build": "npx tsc -b .", @@ -35,6 +35,7 @@ "@types/bcrypt": "^5.0.0", "@types/express": "^4.17.9", "@types/i18next-node-fs-backend": "^2.1.0", + "@types/jest": "^27.0.1", "@types/jsonwebtoken": "^8.5.0", "@types/mongodb": "^3.6.9", "@types/mongoose": "^5.10.5", @@ -51,7 +52,7 @@ "saslprep": "^1.0.3", "ts-node": "^9.1.1", "ts-node-dev": "^1.1.6", - "typescript": "^4.1.2" + "typescript": "^4.4.2" }, "dependencies": { "@fosscord/util": "file:../util", @@ -73,12 +74,21 @@ "i18next-http-middleware": "^3.1.3", "i18next-node-fs-backend": "^2.1.3", "jsonwebtoken": "^8.5.1", - "lambert-server": "^1.2.8", - "missing-native-js-functions": "^1.2.10", + "lambert-server": "^1.2.10", + "missing-native-js-functions": "^1.2.11", "mongoose": "^5.12.3", "mongoose-autopopulate": "^0.12.3", "mongoose-long": "^0.3.2", "multer": "^1.4.2", - "node-fetch": "^2.6.1" + "node-fetch": "^2.6.1", + "supertest": "^6.1.6", + "typeorm": "^0.2.37" + }, + "jest": { + "setupFiles": [ + "/jest/setup.js", + "/scripts/setup_test.js" + ], + "verbose": true } } diff --git a/api/scripts/setup_test.js b/api/scripts/setup_test.js new file mode 100644 index 00000000..95bf2e40 --- /dev/null +++ b/api/scripts/setup_test.js @@ -0,0 +1,13 @@ +const fs = require("fs"); +const { FosscordServer } = require("../dist/Server"); +const Server = new FosscordServer({ port: 3001 }); +(async () => { + try { + fs.unlinkSync(`${__dirname}/database.db`); + } catch {} + return await Server.start(); +})(); + +// afterAll(async () => { +// return await Server.stop(); +// }); diff --git a/api/src/Server.ts b/api/src/Server.ts index adaf7f2c..0f444f86 100644 --- a/api/src/Server.ts +++ b/api/src/Server.ts @@ -2,7 +2,7 @@ import "missing-native-js-functions"; import { Connection } from "mongoose"; import { Server, ServerOptions } from "lambert-server"; import { Authentication, CORS } from "./middlewares/"; -import { Config, db, initEvent } from "@fosscord/util"; +import { Config, initDatabase, initEvent } from "@fosscord/util"; import { ErrorHandler } from "./middlewares/ErrorHandler"; import { BodyParser } from "./middlewares/BodyParser"; import { Router, Request, Response, NextFunction } from "express"; @@ -31,30 +31,13 @@ export class FosscordServer extends Server { super({ ...opts, errorHandler: false, jsonBody: false }); } - async setupSchema() { - return Promise.all([ - db.collection("users").createIndex({ id: 1 }, { unique: true }), - db.collection("messages").createIndex({ id: 1 }, { unique: true }), - db.collection("channels").createIndex({ id: 1 }, { unique: true }), - db.collection("guilds").createIndex({ id: 1 }, { unique: true }), - db.collection("members").createIndex({ id: 1, guild_id: 1 }, { unique: true }), - db.collection("roles").createIndex({ id: 1 }, { unique: true }), - db.collection("emojis").createIndex({ id: 1 }, { unique: true }), - db.collection("invites").createIndex({ code: 1 }, { unique: true }), - db.collection("invites").createIndex({ expires_at: 1 }, { expireAfterSeconds: 0 }), // after 0 seconds of expires_at the invite will get delete - db.collection("ratelimits").createIndex({ expires_at: 1 }, { expireAfterSeconds: 0 }) - ]); - } - async start() { - // @ts-ignore - await (db as Promise); - await this.setupSchema(); + await initDatabase(); await Config.init(); await initEvent(); this.app.use(CORS); - this.app.use(BodyParser({ inflate: true, limit: 1024 * 1024 * 10 })); // 2MB + this.app.use(BodyParser({ inflate: true, limit: 1024 * 1024 * 10 })); // 10MB const app = this.app; const api = Router(); // @ts-ignore diff --git a/api/src/index.ts b/api/src/index.ts index 73b9dbd2..0bba7f5e 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -8,8 +8,6 @@ export * from "./schema/Message"; export * from "./util/Constants"; export * from "./util/instanceOf"; export * from "./util/instanceOf"; -export * from "./util/Member"; export * from "./util/RandomInviteID"; export * from "./util/String"; -export * from "./util/User"; export { check as checkPassword } from "./util/passwordStrength"; diff --git a/api/src/middlewares/Authentication.ts b/api/src/middlewares/Authentication.ts index a8bfe196..a300c786 100644 --- a/api/src/middlewares/Authentication.ts +++ b/api/src/middlewares/Authentication.ts @@ -18,9 +18,9 @@ export const API_PREFIX_TRAILING_SLASH = /^\/api(\/v\d+)?\//; declare global { namespace Express { interface Request { - user_id: any; + user_id: string; user_bot: boolean; - token: any; + token: string; } } } @@ -28,7 +28,7 @@ declare global { export async function Authentication(req: Request, res: Response, next: NextFunction) { if (req.method === "OPTIONS") return res.sendStatus(204); const url = req.url.replace(API_PREFIX, ""); - if (url.startsWith("/invites") && req.method === "GET") return next(); // @ts-ignore + if (url.startsWith("/invites") && req.method === "GET") return next(); if ( NO_AUTHORIZATION_ROUTES.some((x) => { if (typeof x === "string") return url.startsWith(x); @@ -47,7 +47,7 @@ export async function Authentication(req: Request, res: Response, next: NextFunc req.user_id = decoded.id; req.user_bot = user.bot; return next(); - } catch (error) { - return next(new HTTPError(error.toString(), 400)); + } catch (error: any) { + return next(new HTTPError(error?.toString(), 400)); } } diff --git a/api/src/middlewares/ErrorHandler.ts b/api/src/middlewares/ErrorHandler.ts index d080e498..f061172a 100644 --- a/api/src/middlewares/ErrorHandler.ts +++ b/api/src/middlewares/ErrorHandler.ts @@ -1,9 +1,12 @@ import { NextFunction, Request, Response } from "express"; import { HTTPError } from "lambert-server"; +import { EntityNotFoundError } from "typeorm"; import { FieldError } from "../util/instanceOf"; +import {ApiError} from "../util/ApiError"; +// TODO: update with new body/typorm validation export function ErrorHandler(error: Error, req: Request, res: Response, next: NextFunction) { - if (!error) next(); + if (!error) return next(); try { let code = 400; @@ -12,13 +15,23 @@ export function ErrorHandler(error: Error, req: Request, res: Response, next: Ne let errors = undefined; if (error instanceof HTTPError && error.code) code = httpcode = error.code; - else if (error instanceof FieldError) { + else if (error instanceof ApiError) { + code = error.code; + message = error.message; + httpcode = error.httpStatus; + } + else if (error instanceof EntityNotFoundError) { + message = `${(error as any).stringifyTarget} can not be found`; + code = 404; + } else if (error instanceof FieldError) { code = Number(error.code); message = error.message; errors = error.errors; } else { - console.error(error); + console.error(`[Error] ${code} ${req.url}`, errors || error, "body:", req.body); + if (req.server?.options?.production) { + // don't expose internal errors to the user, instead human errors should be thrown as HTTPError message = "Internal Server Error"; } code = httpcode = 500; @@ -26,8 +39,6 @@ export function ErrorHandler(error: Error, req: Request, res: Response, next: Ne if (httpcode > 511) httpcode = 400; - console.error(`[Error] ${code} ${req.url} ${message}`, errors || error); - res.status(httpcode).json({ code: code, message, errors }); } catch (error) { console.error(`[Internal Server Error] 500`, error); diff --git a/api/src/middlewares/RateLimit.ts b/api/src/middlewares/RateLimit.ts index acf92606..dffbc0d9 100644 --- a/api/src/middlewares/RateLimit.ts +++ b/api/src/middlewares/RateLimit.ts @@ -1,11 +1,12 @@ -// @ts-nocheck -import { db, Bucket, Config, listenEvent, emitEvent } from "@fosscord/util"; +import { Config, listenEvent } from "@fosscord/util"; import { NextFunction, Request, Response, Router } from "express"; import { getIpAdress } from "../util/ipAddress"; import { API_PREFIX_TRAILING_SLASH } from "./Authentication"; // Docs: https://discord.com/developers/docs/topics/rate-limits +// TODO: use better caching (e.g. redis) as else it creates to much pressure on the database + /* ? bucket limit? Max actions/sec per bucket? @@ -18,10 +19,18 @@ TODO: different for methods (GET/POST) */ -var Cache = new Map(); -const EventRateLimit = "ratelimit"; +type RateLimit = { + id: "global" | "error" | string; + executor_id: string; + hits: number; + blocked: boolean; + expires_at: Date; +}; -export default function RateLimit(opts: { +var Cache = new Map(); +const EventRateLimit = "RATELIMIT"; + +export default function rateLimit(opts: { bucket?: string; window: number; count: number; @@ -36,23 +45,32 @@ export default function RateLimit(opts: { }): any { return async (req: Request, res: Response, next: NextFunction): Promise => { const bucket_id = opts.bucket || req.originalUrl.replace(API_PREFIX_TRAILING_SLASH, ""); - var user_id = getIpAdress(req); - if (!opts.onlyIp && req.user_id) user_id = req.user_id; + var executor_id = getIpAdress(req); + if (!opts.onlyIp && req.user_id) executor_id = req.user_id; var max_hits = opts.count; if (opts.bot && req.user_bot) max_hits = opts.bot; if (opts.GET && ["GET", "OPTIONS", "HEAD"].includes(req.method)) max_hits = opts.GET; else if (opts.MODIFY && ["POST", "DELETE", "PATCH", "PUT"].includes(req.method)) max_hits = opts.MODIFY; - const offender = Cache.get(user_id + bucket_id) as Bucket | null; + const offender = Cache.get(executor_id + bucket_id); - if (offender && offender.blocked) { + if (offender) { const reset = offender.expires_at.getTime(); const resetAfterMs = reset - Date.now(); const resetAfterSec = resetAfterMs / 1000; - const global = bucket_id === "global"; - if (resetAfterMs > 0) { + if (resetAfterMs <= 0) { + offender.hits = 0; + offender.expires_at = new Date(Date.now() + opts.window * 1000); + offender.blocked = false; + + Cache.delete(executor_id + bucket_id); + } + + if (offender.blocked) { + const global = bucket_id === "global"; + console.log("blocked bucket: " + bucket_id, { resetAfterMs }); return ( res @@ -67,17 +85,11 @@ export default function RateLimit(opts: { // TODO: error rate limit message translation .send({ message: "You are being rate limited.", retry_after: resetAfterSec, global }) ); - } else { - offender.hits = 0; - offender.expires_at = new Date(Date.now() + opts.window * 1000); - offender.blocked = false; - // mongodb ttl didn't update yet -> manually update/delete - db.collection("ratelimits").updateOne({ id: bucket_id, user_id }, { $set: offender }); - Cache.delete(user_id + bucket_id); } } + next(); - const hitRouteOpts = { bucket_id, user_id, max_hits, window: opts.window }; + const hitRouteOpts = { bucket_id, executor_id, max_hits, window: opts.window }; if (opts.error || opts.success) { res.once("finish", () => { @@ -97,69 +109,94 @@ export default function RateLimit(opts: { export async function initRateLimits(app: Router) { const { routes, global, ip, error } = Config.get().limits.rate; await listenEvent(EventRateLimit, (event) => { - Cache.set(event.channel_id, event.data); + Cache.set(event.channel_id as string, event.data); event.acknowledge?.(); }); + // await RateLimit.delete({ expires_at: LessThan(new Date().toISOString()) }); // cleans up if not already deleted, morethan -> older date + // const limits = await RateLimit.find({ blocked: true }); + // limits.forEach((limit) => { + // Cache.set(limit.executor_id, limit); + // }); setInterval(() => { Cache.forEach((x, key) => { - if (Date.now() > x.expires_at) Cache.delete(key); + if (new Date() > x.expires_at) { + Cache.delete(key); + // RateLimit.delete({ executor_id: key }); + } }); - }, 1000 * 60 * 10); + }, 1000 * 60); app.use( - RateLimit({ + rateLimit({ bucket: "global", onlyIp: true, ...ip }) ); - app.use(RateLimit({ bucket: "global", ...global })); + app.use(rateLimit({ bucket: "global", ...global })); app.use( - RateLimit({ + rateLimit({ bucket: "error", error: true, onlyIp: true, ...error }) ); - app.use("/guilds/:id", RateLimit(routes.guild)); - app.use("/webhooks/:id", RateLimit(routes.webhook)); - app.use("/channels/:id", RateLimit(routes.channel)); - app.use("/auth/login", RateLimit(routes.auth.login)); - app.use("/auth/register", RateLimit({ onlyIp: true, success: true, ...routes.auth.register })); + app.use("/guilds/:id", rateLimit(routes.guild)); + app.use("/webhooks/:id", rateLimit(routes.webhook)); + app.use("/channels/:id", rateLimit(routes.channel)); + app.use("/auth/login", rateLimit(routes.auth.login)); + app.use("/auth/register", rateLimit({ onlyIp: true, success: true, ...routes.auth.register })); } -async function hitRoute(opts: { user_id: string; bucket_id: string; max_hits: number; window: number }) { - const filter = { id: opts.bucket_id, user_id: opts.user_id }; - const { value } = await db.collection("ratelimits").findOneAndUpdate( - filter, - { - $setOnInsert: { - id: opts.bucket_id, - user_id: opts.user_id, - expires_at: new Date(Date.now() + opts.window * 1000) - }, - $inc: { - hits: 1 - } - // Conditionally update blocked doesn't work - }, - { upsert: true, returnDocument: "before" } - ); - if (!value) return; - const updateBlock = !value.blocked && value.hits >= opts.max_hits; +async function hitRoute(opts: { executor_id: string; bucket_id: string; max_hits: number; window: number }) { + const id = opts.executor_id + opts.bucket_id; + var limit = Cache.get(id); + if (!limit) { + limit = { + id: opts.bucket_id, + executor_id: opts.executor_id, + expires_at: new Date(Date.now() + opts.window * 1000), + hits: 0, + blocked: false + }; + Cache.set(id, limit); + } + + limit.hits++; + if (limit.hits >= opts.max_hits) { + limit.blocked = true; + } + + /* + var ratelimit = await RateLimit.findOne({ id: opts.bucket_id, executor_id: opts.executor_id }); + if (!ratelimit) { + ratelimit = new RateLimit({ + id: opts.bucket_id, + executor_id: opts.executor_id, + expires_at: new Date(Date.now() + opts.window * 1000), + hits: 0, + blocked: false + }); + } + + ratelimit.hits++; + + const updateBlock = !ratelimit.blocked && ratelimit.hits >= opts.max_hits; if (updateBlock) { - value.blocked = true; - Cache.set(opts.user_id + opts.bucket_id, value); + ratelimit.blocked = true; + Cache.set(opts.executor_id + opts.bucket_id, ratelimit); await emitEvent({ channel_id: EventRateLimit, event: EventRateLimit, - data: value + data: ratelimit }); - await db.collection("ratelimits").updateOne(filter, { $set: { blocked: true } }); } else { - Cache.delete(opts.user_id); + Cache.delete(opts.executor_id); } + + await ratelimit.save(); + */ } diff --git a/api/src/middlewares/Translation.ts b/api/src/middlewares/Translation.ts index edc14707..baabf221 100644 --- a/api/src/middlewares/Translation.ts +++ b/api/src/middlewares/Translation.ts @@ -19,7 +19,7 @@ export async function initTranslation(router: Router) { fallbackLng: "en", ns, backend: { - loadPath: __dirname + "/../locales/{{lng}}/{{ns}}.json" + loadPath: __dirname + "/../../locales/{{lng}}/{{ns}}.json" }, load: "all" }); diff --git a/api/src/routes/auth/login.ts b/api/src/routes/auth/login.ts index dc970e4c..7fd0f870 100644 --- a/api/src/routes/auth/login.ts +++ b/api/src/routes/auth/login.ts @@ -2,7 +2,7 @@ import { Request, Response, Router } from "express"; import { check, FieldErrors, Length } from "../../util/instanceOf"; import bcrypt from "bcrypt"; import jwt from "jsonwebtoken"; -import { Config, UserModel } from "@fosscord/util"; +import { Config, User } from "@fosscord/util"; import { adjustEmail } from "./register"; const router: Router = Router(); @@ -21,10 +21,7 @@ router.post( async (req: Request, res: Response) => { const { login, password, captcha_key, undelete } = req.body; const email = adjustEmail(login); - const query: any[] = [{ phone: login }]; - if (email) query.push({ email }); - - console.log(req.body, email); + console.log("login", email); const config = Config.get(); @@ -41,27 +38,24 @@ router.post( // TODO: check captcha } - const user = await UserModel.findOne( - { $or: query }, - { "user_data.hash": true, id: true, disabled: true, deleted: true, "user_settings.locale": true, "user_settings.theme": true } - ) - .exec() - .catch((e) => { - console.log(e, query); - throw FieldErrors({ login: { message: req.t("auth:login.INVALID_LOGIN"), code: "INVALID_LOGIN" } }); - }); + const user = await User.findOneOrFail({ + where: [{ phone: login }, { email: login }], + select: ["data", "id", "disabled", "deleted", "settings"] + }).catch((e) => { + throw FieldErrors({ login: { message: req.t("auth:login.INVALID_LOGIN"), code: "INVALID_LOGIN" } }); + }); if (undelete) { // undelete refers to un'disable' here - if (user.disabled) await UserModel.updateOne({ id: user.id }, { disabled: false }).exec(); - if (user.deleted) await UserModel.updateOne({ id: user.id }, { deleted: false }).exec(); + if (user.disabled) await User.update({ id: user.id }, { disabled: false }); + if (user.deleted) await User.update({ id: user.id }, { deleted: false }); } else { if (user.deleted) return res.status(400).json({ message: "This account is scheduled for deletion.", code: 20011 }); if (user.disabled) return res.status(400).json({ message: req.t("auth:login.ACCOUNT_DISABLED"), code: 20013 }); } // the salt is saved in the password refer to bcrypt docs - const same_password = await bcrypt.compare(password, user.user_data.hash || ""); + const same_password = await bcrypt.compare(password, user.data.hash || ""); if (!same_password) { throw FieldErrors({ password: { message: req.t("auth:login.INVALID_PASSWORD"), code: "INVALID_PASSWORD" } }); } @@ -72,7 +66,7 @@ router.post( // Discord header is just the user id as string, which is not possible with npm-jsonwebtoken package // https://user-images.githubusercontent.com/6506416/81051916-dd8c9900-8ec2-11ea-8794-daf12d6f31f0.png - res.json({ token, user_settings: user.user_settings }); + res.json({ token, settings: user.settings }); } ); @@ -106,6 +100,6 @@ export async function generateToken(id: string) { * @returns {"captcha_key": ["captcha-required"], "captcha_sitekey": null, "captcha_service": "recaptcha"} * Sucess: - * @returns {"token": "USERTOKEN", "user_settings": {"locale": "en", "theme": "dark"}} + * @returns {"token": "USERTOKEN", "settings": {"locale": "en", "theme": "dark"}} */ diff --git a/api/src/routes/auth/register.ts b/api/src/routes/auth/register.ts index fecde874..8bcecda1 100644 --- a/api/src/routes/auth/register.ts +++ b/api/src/routes/auth/register.ts @@ -1,12 +1,12 @@ import { Request, Response, Router } from "express"; -import { trimSpecial, User, Snowflake, UserModel, Config } from "@fosscord/util"; +import { trimSpecial, User, Snowflake, Config, defaultSettings } from "@fosscord/util"; import bcrypt from "bcrypt"; import { check, Email, EMAIL_REGEX, FieldErrors, Length } from "../../util/instanceOf"; import "missing-native-js-functions"; import { generateToken } from "./login"; import { getIpAdress, IPAnalysis, isProxy } from "../../util/ipAddress"; import { HTTPError } from "lambert-server"; -import RateLimit from "../../middlewares/RateLimit"; +import { In } from "typeorm"; const router: Router = Router(); @@ -55,13 +55,13 @@ router.post( // TODO: check password strength // adjusted_email will be slightly modified version of the user supplied email -> e.g. protection against GMail Trick - let adjusted_email: string | null = adjustEmail(email); + let adjusted_email = adjustEmail(email); // adjusted_password will be the hash of the password - let adjusted_password: string = ""; + let adjusted_password = ""; // trim special uf8 control characters -> Backspace, Newline, ... - let adjusted_username: string = trimSpecial(username); + let adjusted_username = trimSpecial(username); // discriminator will be randomly generated let discriminator = ""; @@ -92,9 +92,7 @@ router.post( if (!adjusted_email) throw FieldErrors({ email: { code: "INVALID_EMAIL", message: req.t("auth:register.INVALID_EMAIL") } }); // check if there is already an account with this email - const exists = await UserModel.findOne({ email: adjusted_email }) - .exec() - .catch((e) => {}); + const exists = await User.findOneOrFail({ email: adjusted_email }).catch((e) => {}); if (exists) { throw FieldErrors({ @@ -131,9 +129,7 @@ router.post( if (!register.allowMultipleAccounts) { // TODO: check if fingerprint was eligible generated - const exists = await UserModel.findOne({ fingerprints: fingerprint }) - .exec() - .catch((e) => {}); + const exists = await User.findOne({ where: { fingerprints: In(fingerprint) } }); if (exists) { throw FieldErrors({ @@ -168,12 +164,8 @@ router.post( // TODO: is there any better way to generate a random discriminator only once, without checking if it already exists in the mongodb database? for (let tries = 0; tries < 5; tries++) { discriminator = Math.randomIntBetween(1, 9999).toString().padStart(4, "0"); - try { - exists = await UserModel.findOne({ discriminator, username: adjusted_username }, "id").exec(); - } catch (error) { - // doesn't exist -> break - break; - } + exists = await User.findOne({ where: { discriminator, username: adjusted_username }, select: ["id"] }); + if (!exists) break; } if (exists) { @@ -189,96 +181,40 @@ router.post( // appearently discord doesn't save the date of birth and just calculate if nsfw is allowed // if nsfw_allowed is null/undefined it'll require date_of_birth to set it to true/false - const user: User = { - id: Snowflake.generate(), + const user = { created_at: new Date(), username: adjusted_username, discriminator, - avatar: null, - accent_color: null, - banner: null, + id: Snowflake.generate(), bot: false, system: false, desktop: false, mobile: false, premium: true, premium_type: 2, - phone: null, bio: "", mfa_enabled: false, verified: false, disabled: false, deleted: false, - presence: { - activities: [], - client_status: { - desktop: undefined, - mobile: undefined, - web: undefined - }, - status: "offline" - }, email: adjusted_email, nsfw_allowed: true, // TODO: depending on age - public_flags: 0n, - flags: 0n, // TODO: generate default flags - guilds: [], - user_data: { + public_flags: "0", + flags: "0", // TODO: generate + data: { hash: adjusted_password, - valid_tokens_since: new Date(), - relationships: [], - connected_accounts: [], - fingerprints: [] + valid_tokens_since: new Date() }, - user_settings: { - afk_timeout: 300, - allow_accessibility_detection: true, - animate_emoji: true, - animate_stickers: 0, - contact_sync_enabled: false, - convert_emoticons: false, - custom_status: { - emoji_id: null, - emoji_name: null, - expires_at: null, - text: null - }, - default_guilds_restricted: false, - detect_platform_accounts: true, - developer_mode: false, - disable_games_tab: false, - enable_tts_command: true, - explicit_content_filter: 0, - friend_source_flags: { all: true }, - gateway_connected: false, - gif_auto_play: true, - guild_folders: [], - guild_positions: [], - inline_attachment_media: true, - inline_embed_media: true, - locale: req.language, - message_display_compact: false, - native_phone_integration_enabled: true, - render_embeds: true, - render_reactions: true, - restricted_guilds: [], - show_current_game: true, - status: "offline", - stream_notifications_enabled: true, - theme: "dark", - timezone_offset: 0 - // timezone_offset: // TODO: timezone from request - } + settings: defaultSettings, + fingerprints: [] }; - - // insert user into database - await new UserModel(user).save(); + await User.insert(user); return res.json({ token: await generateToken(user.id) }); } ); -export function adjustEmail(email: string): string | null { +export function adjustEmail(email: string): string | undefined { // body parser already checked if it is a valid email const parts = email.match(EMAIL_REGEX); // @ts-ignore @@ -304,6 +240,6 @@ export default router; * Field Error * @returns { "code": 50035, "errors": { "consent": { "_errors": [{ "code": "CONSENT_REQUIRED", "message": "You must agree to Discord's Terms of Service and Privacy Policy." }]}}, "message": "Invalid Form Body"} * - * Success 201: + * Success 200: * @returns {token: "OMITTED"} */ diff --git a/api/src/routes/channels/#channel_id/index.ts b/api/src/routes/channels/#channel_id/index.ts index fb6bcb1a..4aa5a5b9 100644 --- a/api/src/routes/channels/#channel_id/index.ts +++ b/api/src/routes/channels/#channel_id/index.ts @@ -1,4 +1,4 @@ -import { ChannelDeleteEvent, ChannelModel, ChannelUpdateEvent, emitEvent, getPermission, GuildUpdateEvent, toObject } from "@fosscord/util"; +import { ChannelDeleteEvent, Channel, ChannelUpdateEvent, emitEvent, getPermission } from "@fosscord/util"; import { Router, Response, Request } from "express"; import { HTTPError } from "lambert-server"; import { ChannelModifySchema } from "../../../schema/Channel"; @@ -10,28 +10,28 @@ const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { const { channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); const permission = await getPermission(req.user_id, channel.guild_id, channel_id); permission.hasThrow("VIEW_CHANNEL"); - return res.send(toObject(channel)); + return res.send(channel); }); router.delete("/", async (req: Request, res: Response) => { const { channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); - const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel }); + const permission = await getPermission(req.user_id, channel?.guild_id, channel_id); permission.hasThrow("MANAGE_CHANNELS"); // TODO: Dm channel "close" not delete - const data = toObject(channel); + const data = channel; await emitEvent({ event: "CHANNEL_DELETE", data, channel_id } as ChannelDeleteEvent); - await ChannelModel.deleteOne({ id: channel_id }); + await Channel.delete({ id: channel_id }); res.send(data); }); @@ -43,17 +43,19 @@ router.patch("/", check(ChannelModifySchema), async (req: Request, res: Response const permission = await getPermission(req.user_id, undefined, channel_id); permission.hasThrow("MANAGE_CHANNELS"); - const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, payload, { new: true }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); + channel.assign(payload); - const data = toObject(channel); + await Promise.all([ + channel.save(), + emitEvent({ + event: "CHANNEL_UPDATE", + data: channel, + channel_id + } as ChannelUpdateEvent) + ]); - await emitEvent({ - event: "CHANNEL_UPDATE", - data, - channel_id - } as ChannelUpdateEvent); - - res.send(data); + res.send(channel); }); export default router; diff --git a/api/src/routes/channels/#channel_id/invites.ts b/api/src/routes/channels/#channel_id/invites.ts index 438f8c51..35006ac4 100644 --- a/api/src/routes/channels/#channel_id/invites.ts +++ b/api/src/routes/channels/#channel_id/invites.ts @@ -6,14 +6,14 @@ import { random } from "../../../util/RandomInviteID"; import { InviteCreateSchema } from "../../../schema/Invite"; -import { getPermission, ChannelModel, InviteModel, InviteCreateEvent, toObject, emitEvent } from "@fosscord/util"; +import { getPermission, Channel, Invite, InviteCreateEvent, emitEvent } from "@fosscord/util"; const router: Router = Router(); router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) => { const { user_id } = req; const { channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); if (!channel.guild_id) { throw new HTTPError("This channel doesn't exist", 404); @@ -38,7 +38,7 @@ router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) inviter_id: user_id }; - await new InviteModel(invite).save(); + await new Invite(invite).save(); await emitEvent({ event: "INVITE_CREATE", data: invite, guild_id } as InviteCreateEvent); res.status(201).send(invite); @@ -47,7 +47,7 @@ router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) router.get("/", async (req: Request, res: Response) => { const { user_id } = req; const { channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); if (!channel.guild_id) { throw new HTTPError("This channel doesn't exist", 404); @@ -56,9 +56,9 @@ router.get("/", async (req: Request, res: Response) => { const permission = await getPermission(user_id, guild_id); permission.hasThrow("MANAGE_CHANNELS"); - const invites = await InviteModel.find({ guild_id }).exec(); + const invites = await Invite.find({ guild_id }); - res.status(200).send(toObject(invites)); + res.status(200).send(invites); }); export default router; diff --git a/api/src/routes/channels/#channel_id/messages/#message_id/ack.ts b/api/src/routes/channels/#channel_id/messages/#message_id/ack.ts index bbc779dd..0fd5f2be 100644 --- a/api/src/routes/channels/#channel_id/messages/#message_id/ack.ts +++ b/api/src/routes/channels/#channel_id/messages/#message_id/ack.ts @@ -1,4 +1,4 @@ -import { emitEvent, getPermission, MessageAckEvent, ReadStateModel } from "@fosscord/util"; +import { emitEvent, getPermission, MessageAckEvent, ReadState } from "@fosscord/util"; import { Request, Response, Router } from "express"; import { check } from "../../../../../util/instanceOf"; @@ -14,10 +14,7 @@ router.post("/", check({ $manual: Boolean, $mention_count: Number }), async (req const permission = await getPermission(req.user_id, undefined, channel_id); permission.hasThrow("VIEW_CHANNEL"); - await ReadStateModel.updateOne( - { user_id: req.user_id, channel_id, message_id }, - { user_id: req.user_id, channel_id, message_id } - ).exec(); + await ReadState.update({ user_id: req.user_id, channel_id }, { user_id: req.user_id, channel_id, last_message_id: message_id }); await emitEvent({ event: "MESSAGE_ACK", diff --git a/api/src/routes/channels/#channel_id/messages/#message_id/index.ts b/api/src/routes/channels/#channel_id/messages/#message_id/index.ts index 35952d26..b9d46c4f 100644 --- a/api/src/routes/channels/#channel_id/messages/#message_id/index.ts +++ b/api/src/routes/channels/#channel_id/messages/#message_id/index.ts @@ -1,6 +1,5 @@ -import { ChannelModel, emitEvent, getPermission, MessageDeleteEvent, MessageModel, MessageUpdateEvent, toObject } from "@fosscord/util"; +import { Channel, emitEvent, getPermission, MessageDeleteEvent, Message, MessageUpdateEvent } from "@fosscord/util"; import { Router, Response, Request } from "express"; -import { HTTPError } from "lambert-server"; import { MessageCreateSchema } from "../../../../../schema/Message"; import { check } from "../../../../../util/instanceOf"; @@ -12,7 +11,7 @@ router.patch("/", check(MessageCreateSchema), async (req: Request, res: Response const { message_id, channel_id } = req.params; var body = req.body as MessageCreateSchema; - var message = await MessageModel.findOne({ id: message_id, channel_id }, { author_id: true, message_reference: true }).lean().exec(); + const message = await Message.findOneOrFail({ id: message_id, channel_id }); const permissions = await getPermission(req.user_id, undefined, channel_id); @@ -21,7 +20,9 @@ router.patch("/", check(MessageCreateSchema), async (req: Request, res: Response body = { flags: body.flags }; // admins can only suppress embeds of other messages } - const opts = await handleMessage({ + const new_message = await handleMessage({ + // TODO: should be message_reference overridable? + // @ts-ignore message_reference: message.message_reference, ...body, author_id: message.author_id, @@ -30,18 +31,18 @@ router.patch("/", check(MessageCreateSchema), async (req: Request, res: Response edited_timestamp: new Date() }); - // @ts-ignore - message = await MessageModel.findOneAndUpdate({ id: message_id }, opts, { new: true }).populate("author").exec(); - - await emitEvent({ - event: "MESSAGE_UPDATE", - channel_id, - data: { ...toObject(message), nonce: undefined } - } as MessageUpdateEvent); + await Promise.all([ + new_message.save(), + await emitEvent({ + event: "MESSAGE_UPDATE", + channel_id, + data: { ...message, nonce: undefined } + } as MessageUpdateEvent) + ]); postHandleMessage(message); - return res.json(toObject(message)); + return res.json(message); }); // TODO: delete attachments in message @@ -49,13 +50,13 @@ router.patch("/", check(MessageCreateSchema), async (req: Request, res: Response router.delete("/", async (req: Request, res: Response) => { const { message_id, channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }); - const message = await MessageModel.findOne({ id: message_id }, { author_id: true }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); + const message = await Message.findOneOrFail({ id: message_id }); const permission = await getPermission(req.user_id, channel.guild_id, channel_id); if (message.author_id !== req.user_id) permission.hasThrow("MANAGE_MESSAGES"); - await MessageModel.deleteOne({ id: message_id }).exec(); + await Message.delete({ id: message_id }); await emitEvent({ event: "MESSAGE_DELETE", diff --git a/api/src/routes/channels/#channel_id/messages/#message_id/reactions.ts b/api/src/routes/channels/#channel_id/messages/#message_id/reactions.ts index 7da63644..f60484b5 100644 --- a/api/src/routes/channels/#channel_id/messages/#message_id/reactions.ts +++ b/api/src/routes/channels/#channel_id/messages/#message_id/reactions.ts @@ -1,21 +1,21 @@ import { - ChannelModel, + Channel, emitEvent, - EmojiModel, + Emoji, getPermission, - MemberModel, - MessageModel, + Member, + Message, MessageReactionAddEvent, MessageReactionRemoveAllEvent, MessageReactionRemoveEmojiEvent, MessageReactionRemoveEvent, PartialEmoji, PublicUserProjection, - toObject, - UserModel + User } from "@fosscord/util"; import { Router, Response, Request } from "express"; import { HTTPError } from "lambert-server"; +import { In } from "typeorm"; const router = Router(); // TODO: check if emoji is really an unicode emoji or a prperly encoded external emoji @@ -38,12 +38,12 @@ function getEmoji(emoji: string): PartialEmoji { router.delete("/", async (req: Request, res: Response) => { const { message_id, channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); const permissions = await getPermission(req.user_id, undefined, channel_id); permissions.hasThrow("MANAGE_MESSAGES"); - await MessageModel.findOneAndUpdate({ id: message_id, channel_id }, { reactions: [] }, { new: true }).exec(); + await Message.update({ id: message_id, channel_id }, { reactions: [] }); await emitEvent({ event: "MESSAGE_REACTION_REMOVE_ALL", @@ -62,29 +62,28 @@ router.delete("/:emoji", async (req: Request, res: Response) => { const { message_id, channel_id } = req.params; const emoji = getEmoji(req.params.emoji); - const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); - const permissions = await getPermission(req.user_id, undefined, channel_id); permissions.hasThrow("MANAGE_MESSAGES"); - const message = await MessageModel.findOne({ id: message_id, channel_id }).exec(); + const message = await Message.findOneOrFail({ id: message_id, channel_id }); const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name); if (!already_added) throw new HTTPError("Reaction not found", 404); message.reactions.remove(already_added); - await MessageModel.updateOne({ id: message_id, channel_id }, message).exec(); - - await emitEvent({ - event: "MESSAGE_REACTION_REMOVE_EMOJI", - channel_id, - data: { + await Promise.all([ + message.save(), + emitEvent({ + event: "MESSAGE_REACTION_REMOVE_EMOJI", channel_id, - message_id, - guild_id: channel.guild_id, - emoji - } - } as MessageReactionRemoveEmojiEvent); + data: { + channel_id, + message_id, + guild_id: message.guild_id, + emoji + } + } as MessageReactionRemoveEmojiEvent) + ]); res.sendStatus(204); }); @@ -93,17 +92,21 @@ router.get("/:emoji", async (req: Request, res: Response) => { const { message_id, channel_id } = req.params; const emoji = getEmoji(req.params.emoji); - const message = await MessageModel.findOne({ id: message_id, channel_id }).exec(); - if (!message) throw new HTTPError("Message not found", 404); + const message = await Message.findOneOrFail({ id: message_id, channel_id }); const reaction = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name); if (!reaction) throw new HTTPError("Reaction not found", 404); const permissions = await getPermission(req.user_id, undefined, channel_id); permissions.hasThrow("VIEW_CHANNEL"); - const users = await UserModel.find({ id: { $in: reaction.user_ids } }, PublicUserProjection).exec(); + const users = await User.find({ + where: { + id: In(reaction.user_ids) + }, + select: PublicUserProjection + }); - res.json(toObject(users)); + res.json(users); }); router.put("/:emoji/:user_id", async (req: Request, res: Response) => { @@ -111,8 +114,8 @@ router.put("/:emoji/:user_id", async (req: Request, res: Response) => { if (user_id !== "@me") throw new HTTPError("Invalid user"); const emoji = getEmoji(req.params.emoji); - const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); - const message = await MessageModel.findOne({ id: message_id, channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); + const message = await Message.findOneOrFail({ id: message_id, channel_id }); const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name); const permissions = await getPermission(req.user_id, undefined, channel_id); @@ -120,7 +123,7 @@ router.put("/:emoji/:user_id", async (req: Request, res: Response) => { if (!already_added) permissions.hasThrow("ADD_REACTIONS"); if (emoji.id) { - const external_emoji = await EmojiModel.findOne({ id: emoji.id }).exec(); + const external_emoji = await Emoji.findOneOrFail({ id: emoji.id }); if (!already_added) permissions.hasThrow("USE_EXTERNAL_EMOJIS"); emoji.animated = external_emoji.animated; emoji.name = external_emoji.name; @@ -131,9 +134,9 @@ router.put("/:emoji/:user_id", async (req: Request, res: Response) => { already_added.count++; } else message.reactions.push({ count: 1, emoji, user_ids: [req.user_id] }); - await MessageModel.updateOne({ id: message_id, channel_id }, message).exec(); + await Message.update({ id: message_id, channel_id }, message); - const member = channel.guild_id && (await MemberModel.findOne({ id: req.user_id }).exec()); + const member = channel.guild_id && (await Member.findOneOrFail({ id: req.user_id })); await emitEvent({ event: "MESSAGE_REACTION_ADD", @@ -156,8 +159,8 @@ router.delete("/:emoji/:user_id", async (req: Request, res: Response) => { const emoji = getEmoji(req.params.emoji); - const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); - const message = await MessageModel.findOne({ id: message_id, channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); + const message = await Message.findOneOrFail({ id: message_id, channel_id }); const permissions = await getPermission(req.user_id, undefined, channel_id); @@ -171,7 +174,7 @@ router.delete("/:emoji/:user_id", async (req: Request, res: Response) => { if (already_added.count <= 0) message.reactions.remove(already_added); - await MessageModel.updateOne({ id: message_id, channel_id }, message).exec(); + await Message.update({ id: message_id, channel_id }, message); await emitEvent({ event: "MESSAGE_REACTION_REMOVE", diff --git a/api/src/routes/channels/#channel_id/messages/bulk-delete.ts b/api/src/routes/channels/#channel_id/messages/bulk-delete.ts index 8132462f..5c486676 100644 --- a/api/src/routes/channels/#channel_id/messages/bulk-delete.ts +++ b/api/src/routes/channels/#channel_id/messages/bulk-delete.ts @@ -1,8 +1,9 @@ import { Router, Response, Request } from "express"; -import { ChannelModel, Config, emitEvent, getPermission, MessageDeleteBulkEvent, MessageModel } from "@fosscord/util"; +import { Channel, Config, emitEvent, getPermission, MessageDeleteBulkEvent, Message } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { check } from "../../../../util/instanceOf"; +import { In } from "typeorm"; const router: Router = Router(); @@ -13,10 +14,10 @@ export default router; // https://discord.com/developers/docs/resources/channel#bulk-delete-messages router.post("/", check({ messages: [String] }), async (req: Request, res: Response) => { const { channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }, { permission_overwrites: true, guild_id: true }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); if (!channel.guild_id) throw new HTTPError("Can't bulk delete dm channel messages", 400); - const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel }); + const permission = await getPermission(req.user_id, channel?.guild_id, channel_id); permission.hasThrow("MANAGE_MESSAGES"); const { maxBulkDelete } = Config.get().limits.message; @@ -25,7 +26,7 @@ router.post("/", check({ messages: [String] }), async (req: Request, res: Respon if (messages.length < 2) throw new HTTPError("You must at least specify 2 messages to bulk delete"); if (messages.length > maxBulkDelete) throw new HTTPError(`You cannot delete more than ${maxBulkDelete} messages`); - await MessageModel.deleteMany({ id: { $in: messages } }).exec(); + await Message.delete({ id: In(messages) }); await emitEvent({ event: "MESSAGE_DELETE_BULK", diff --git a/api/src/routes/channels/#channel_id/messages/index.ts b/api/src/routes/channels/#channel_id/messages/index.ts index 6ae6491f..86de6de8 100644 --- a/api/src/routes/channels/#channel_id/messages/index.ts +++ b/api/src/routes/channels/#channel_id/messages/index.ts @@ -1,5 +1,5 @@ import { Router, Response, Request } from "express"; -import { Attachment, ChannelModel, ChannelType, getPermission, MessageDocument, MessageModel, toObject } from "@fosscord/util"; +import { Attachment, Channel, ChannelType, getPermission, Message } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { MessageCreateSchema } from "../../../../schema/Message"; import { check, instanceOf, Length } from "../../../../util/instanceOf"; @@ -7,6 +7,7 @@ import multer from "multer"; import { Query } from "mongoose"; import { sendMessage } from "../../../../util/Message"; import { uploadFile } from "../../../../util/cdn"; +import { FindManyOptions, LessThan, MoreThan } from "typeorm"; const router: Router = Router(); @@ -30,12 +31,7 @@ export function isTextChannel(type: ChannelType): boolean { // get messages router.get("/", async (req: Request, res: Response) => { const channel_id = req.params.channel_id; - const channel = await ChannelModel.findOne( - { id: channel_id }, - { guild_id: true, type: true, permission_overwrites: true, recipient_ids: true, owner_id: true } - ) - .lean() // lean is needed, because we don't want to populate .recipients that also auto deletes .recipient_ids - .exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); if (!channel) throw new HTTPError("Channel not found", 404); isTextChannel(channel.type); @@ -57,32 +53,34 @@ router.get("/", async (req: Request, res: Response) => { permissions.hasThrow("VIEW_CHANNEL"); if (!permissions.has("READ_MESSAGE_HISTORY")) return res.json([]); - var query: Query; - if (after) query = MessageModel.find({ channel_id, id: { $gt: after } }); - else if (before) query = MessageModel.find({ channel_id, id: { $lt: before } }); - else if (around) - query = MessageModel.find({ - channel_id, - id: { $gt: (BigInt(around) - BigInt(halfLimit)).toString(), $lt: (BigInt(around) + BigInt(halfLimit)).toString() } - }); - else { - query = MessageModel.find({ channel_id }); + var query: FindManyOptions & { where: { id?: any } } = { + order: { id: "DESC" }, + take: limit, + where: { channel_id }, + relations: ["author", "webhook", "application", "mentions", "mention_roles", "mention_channels", "sticker_items", "attachments"] + }; + + if (after) query.where.id = MoreThan(after); + else if (before) query.where.id = LessThan(before); + else if (around) { + query.where.id = [ + MoreThan((BigInt(around) - BigInt(halfLimit)).toString()), + LessThan((BigInt(around) + BigInt(halfLimit)).toString()) + ]; } - query = query.sort({ id: -1 }); - - const messages = await query.limit(limit).exec(); + const messages = await Message.find(query); return res.json( - toObject(messages).map((x) => { - (x.reactions || []).forEach((x) => { + messages.map((x) => { + (x.reactions || []).forEach((x: any) => { // @ts-ignore if ((x.user_ids || []).includes(req.user_id)) x.me = true; // @ts-ignore delete x.user_ids; }); // @ts-ignore - if (!x.author) x.author = { discriminator: "0000", username: "Deleted User", public_flags: 0n, avatar: null }; + if (!x.author) x.author = { discriminator: "0000", username: "Deleted User", public_flags: "0", avatar: null }; return x; }) @@ -139,8 +137,8 @@ router.post("/", messageUpload.single("file"), async (req: Request, res: Respons embeds, channel_id, attachments, - edited_timestamp: null + edited_timestamp: undefined }); - return res.send(data); + return res.json(data); }); diff --git a/api/src/routes/channels/#channel_id/permissions.ts b/api/src/routes/channels/#channel_id/permissions.ts index f93075b1..9c49542b 100644 --- a/api/src/routes/channels/#channel_id/permissions.ts +++ b/api/src/routes/channels/#channel_id/permissions.ts @@ -1,13 +1,4 @@ -import { - ChannelModel, - ChannelPermissionOverwrite, - ChannelUpdateEvent, - emitEvent, - getPermission, - MemberModel, - RoleModel, - toObject -} from "@fosscord/util"; +import { Channel, ChannelPermissionOverwrite, ChannelUpdateEvent, emitEvent, getPermission, Member, Role } from "@fosscord/util"; import { Router, Response, Request } from "express"; import { HTTPError } from "lambert-server"; @@ -20,16 +11,16 @@ router.put("/:overwrite_id", check({ allow: String, deny: String, type: Number, const { channel_id, overwrite_id } = req.params; const body = req.body as { allow: bigint; deny: bigint; type: number; id: string }; - var channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, permission_overwrites: true }).exec(); + var channel = await Channel.findOneOrFail({ id: channel_id }); if (!channel.guild_id) throw new HTTPError("Channel not found", 404); const permissions = await getPermission(req.user_id, channel.guild_id, channel_id); permissions.hasThrow("MANAGE_ROLES"); if (body.type === 0) { - if (!(await RoleModel.exists({ id: overwrite_id }))) throw new HTTPError("role not found", 404); + if (!(await Role.count({ id: overwrite_id }))) throw new HTTPError("role not found", 404); } else if (body.type === 1) { - if (!(await MemberModel.exists({ id: overwrite_id }))) throw new HTTPError("user not found", 404); + if (!(await Member.count({ id: overwrite_id }))) throw new HTTPError("user not found", 404); } else throw new HTTPError("type not supported", 501); // @ts-ignore @@ -48,12 +39,12 @@ router.put("/:overwrite_id", check({ allow: String, deny: String, type: Number, overwrite.deny = body.deny; // @ts-ignore - channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, channel, { new: true }).exec(); + channel = await Channel.findOneOrFailAndUpdate({ id: channel_id }, channel, { new: true }); await emitEvent({ event: "CHANNEL_UPDATE", channel_id, - data: toObject(channel) + data: channel } as ChannelUpdateEvent); return res.sendStatus(204); @@ -66,18 +57,19 @@ router.delete("/:overwrite_id", async (req: Request, res: Response) => { const permissions = await getPermission(req.user_id, undefined, channel_id); permissions.hasThrow("MANAGE_ROLES"); - const channel = await ChannelModel.findOneAndUpdate( - { id: channel_id }, - { $pull: { permission_overwrites: { id: overwrite_id } } }, - { new: true } - ); + const channel = await Channel.findOneOrFail({ id: channel_id }); if (!channel.guild_id) throw new HTTPError("Channel not found", 404); - await emitEvent({ - event: "CHANNEL_UPDATE", - channel_id, - data: toObject(channel) - } as ChannelUpdateEvent); + channel.permission_overwrites = channel.permission_overwrites.filter((x) => x.id === overwrite_id); + + await Promise.all([ + channel.save(), + emitEvent({ + event: "CHANNEL_UPDATE", + channel_id, + data: channel + } as ChannelUpdateEvent) + ]); return res.sendStatus(204); }); diff --git a/api/src/routes/channels/#channel_id/pins.ts b/api/src/routes/channels/#channel_id/pins.ts index 0dd81bd3..96a3fdbf 100644 --- a/api/src/routes/channels/#channel_id/pins.ts +++ b/api/src/routes/channels/#channel_id/pins.ts @@ -1,13 +1,4 @@ -import { - ChannelModel, - ChannelPinsUpdateEvent, - Config, - emitEvent, - getPermission, - MessageModel, - MessageUpdateEvent, - toObject -} from "@fosscord/util"; +import { Channel, ChannelPinsUpdateEvent, Config, emitEvent, getPermission, Message, MessageUpdateEvent } from "@fosscord/util"; import { Router, Request, Response } from "express"; import { HTTPError } from "lambert-server"; @@ -15,35 +6,36 @@ const router: Router = Router(); router.put("/:message_id", async (req: Request, res: Response) => { const { channel_id, message_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }).exec(); - const permission = await getPermission(req.user_id, channel.guild_id, channel_id); + + const message = await Message.findOneOrFail({ id: message_id }); + const permission = await getPermission(req.user_id, message.guild_id, channel_id); permission.hasThrow("VIEW_CHANNEL"); // * in dm channels anyone can pin messages -> only check for guilds - if (channel.guild_id) permission.hasThrow("MANAGE_MESSAGES"); + if (message.guild_id) permission.hasThrow("MANAGE_MESSAGES"); - const pinned_count = await MessageModel.count({ channel_id, pinned: true }).exec(); + const pinned_count = await Message.count({ channel: { id: channel_id }, pinned: true }); const { maxPins } = Config.get().limits.channel; if (pinned_count >= maxPins) throw new HTTPError("Max pin count reached: " + maxPins); - await MessageModel.updateOne({ id: message_id }, { pinned: true }).exec(); - const message = toObject(await MessageModel.findOne({ id: message_id }).exec()); - - await emitEvent({ - event: "MESSAGE_UPDATE", - channel_id, - data: message - } as MessageUpdateEvent); - - await emitEvent({ - event: "CHANNEL_PINS_UPDATE", - channel_id, - data: { + await Promise.all([ + Message.update({ id: message_id }, { pinned: true }), + emitEvent({ + event: "MESSAGE_UPDATE", channel_id, - guild_id: channel.guild_id, - last_pin_timestamp: undefined - } - } as ChannelPinsUpdateEvent); + data: message + } as MessageUpdateEvent), + + emitEvent({ + event: "CHANNEL_PINS_UPDATE", + channel_id, + data: { + channel_id, + guild_id: message.guild_id, + last_pin_timestamp: undefined + } + } as ChannelPinsUpdateEvent) + ]); res.sendStatus(204); }); @@ -51,29 +43,34 @@ router.put("/:message_id", async (req: Request, res: Response) => { router.delete("/:message_id", async (req: Request, res: Response) => { const { channel_id, message_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); const permission = await getPermission(req.user_id, channel.guild_id, channel_id); permission.hasThrow("VIEW_CHANNEL"); if (channel.guild_id) permission.hasThrow("MANAGE_MESSAGES"); - const message = toObject(await MessageModel.findOneAndUpdate({ id: message_id }, { pinned: false }, { new: true }).exec()); + const message = await Message.findOneOrFail({ id: message_id }); + message.pinned = false; - await emitEvent({ - event: "MESSAGE_UPDATE", - channel_id, - data: message - } as MessageUpdateEvent); + await Promise.all([ + message.save(), - await emitEvent({ - event: "CHANNEL_PINS_UPDATE", - channel_id, - data: { + emitEvent({ + event: "MESSAGE_UPDATE", channel_id, - guild_id: channel.guild_id, - last_pin_timestamp: undefined - } - } as ChannelPinsUpdateEvent); + data: message + } as MessageUpdateEvent), + + emitEvent({ + event: "CHANNEL_PINS_UPDATE", + channel_id, + data: { + channel_id, + guild_id: channel.guild_id, + last_pin_timestamp: undefined + } + } as ChannelPinsUpdateEvent) + ]); res.sendStatus(204); }); @@ -81,13 +78,13 @@ router.delete("/:message_id", async (req: Request, res: Response) => { router.get("/", async (req: Request, res: Response) => { const { channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); const permission = await getPermission(req.user_id, channel.guild_id, channel_id); permission.hasThrow("VIEW_CHANNEL"); - let pins = await MessageModel.find({ channel_id: channel_id, pinned: true }).exec(); + let pins = await Message.find({ channel_id: channel_id, pinned: true }); - res.send(toObject(pins)); + res.send(pins); }); export default router; diff --git a/api/src/routes/channels/#channel_id/typing.ts b/api/src/routes/channels/#channel_id/typing.ts index 21d453d8..f1fb3c86 100644 --- a/api/src/routes/channels/#channel_id/typing.ts +++ b/api/src/routes/channels/#channel_id/typing.ts @@ -1,4 +1,4 @@ -import { ChannelModel, emitEvent, MemberModel, toObject, TypingStartEvent } from "@fosscord/util"; +import { Channel, emitEvent, Member, TypingStartEvent } from "@fosscord/util"; import { Router, Request, Response } from "express"; import { HTTPError } from "lambert-server"; @@ -9,15 +9,15 @@ router.post("/", async (req: Request, res: Response) => { const { channel_id } = req.params; const user_id = req.user_id; const timestamp = Date.now(); - const channel = await ChannelModel.findOne({ id: channel_id }); - const member = await MemberModel.findOne({ id: user_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); + const member = await Member.findOneOrFail({ id: user_id }); await emitEvent({ event: "TYPING_START", channel_id: channel_id, data: { // this is the paylod - member: toObject(member), + member: { ...member, roles: member.roles.map((x) => x.id) }, channel_id, timestamp, user_id, diff --git a/api/src/routes/channels/#channel_id/webhooks.ts b/api/src/routes/channels/#channel_id/webhooks.ts index 7852f8f3..775053ba 100644 --- a/api/src/routes/channels/#channel_id/webhooks.ts +++ b/api/src/routes/channels/#channel_id/webhooks.ts @@ -1,6 +1,6 @@ import { Router, Response, Request } from "express"; import { check, Length } from "../../../util/instanceOf"; -import { ChannelModel, getPermission, trimSpecial } from "@fosscord/util"; +import { Channel, getPermission, trimSpecial } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { isTextChannel } from "./messages/index"; @@ -10,7 +10,7 @@ const router: Router = Router(); // TODO: use Image Data Type for avatar instead of String router.post("/", check({ name: new Length(String, 1, 80), $avatar: String }), async (req: Request, res: Response) => { const channel_id = req.params.channel_id; - const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); isTextChannel(channel.type); if (!channel.guild_id) throw new HTTPError("Not a guild channel", 400); diff --git a/api/src/routes/guilds/#guild_id/bans.ts b/api/src/routes/guilds/#guild_id/bans.ts index bb3eac03..b84a68a7 100644 --- a/api/src/routes/guilds/#guild_id/bans.ts +++ b/api/src/routes/guilds/#guild_id/bans.ts @@ -1,30 +1,25 @@ import { Request, Response, Router } from "express"; -import { BanModel, emitEvent, getPermission, GuildBanAddEvent, GuildBanRemoveEvent, GuildModel, toObject } from "@fosscord/util"; +import { emitEvent, getPermission, GuildBanAddEvent, GuildBanRemoveEvent, Guild, Ban, User, Member } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { getIpAdress } from "../../../util/ipAddress"; import { BanCreateSchema } from "../../../schema/Ban"; import { check } from "../../../util/instanceOf"; -import { removeMember } from "../../../util/Member"; -import { getPublicUser } from "../../../util/User"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; - const guild = await GuildModel.exists({ id: guild_id }); - if (!guild) throw new HTTPError("Guild not found", 404); - - var bans = await BanModel.find({ guild_id: guild_id }, { user_id: true, reason: true }).exec(); - return res.json(toObject(bans)); + var bans = await Ban.find({ guild_id: guild_id }); + return res.json(bans); }); router.get("/:user", async (req: Request, res: Response) => { const { guild_id } = req.params; const user_id = req.params.ban; - var ban = await BanModel.findOne({ guild_id: guild_id, user_id: user_id }).exec(); + var ban = await Ban.findOneOrFail({ guild_id: guild_id, user_id: user_id }); return res.json(ban); }); @@ -32,57 +27,59 @@ router.put("/:user_id", check(BanCreateSchema), async (req: Request, res: Respon const { guild_id } = req.params; const banned_user_id = req.params.user_id; - const banned_user = await getPublicUser(banned_user_id); + const banned_user = await User.getPublicUser(banned_user_id); const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("BAN_MEMBERS"); if (req.user_id === banned_user_id) throw new HTTPError("You can't ban yourself", 400); + if (perms.cache.guild?.owner_id === banned_user_id) throw new HTTPError("You can't ban the owner", 400); - await removeMember(banned_user_id, guild_id); - - const ban = await new BanModel({ + const ban = new Ban({ user_id: banned_user_id, guild_id: guild_id, ip: getIpAdress(req), executor_id: req.user_id, reason: req.body.reason // || otherwise empty - }).save(); + }); - await emitEvent({ - event: "GUILD_BAN_ADD", - data: { - guild_id: guild_id, - user: banned_user - }, - guild_id: guild_id - } as GuildBanAddEvent); + await Promise.all([ + Member.removeFromGuild(banned_user_id, guild_id), + ban.save(), + emitEvent({ + event: "GUILD_BAN_ADD", + data: { + guild_id: guild_id, + user: banned_user + }, + guild_id: guild_id + } as GuildBanAddEvent) + ]); - return res.json(toObject(ban)); + return res.json(ban); }); router.delete("/:user_id", async (req: Request, res: Response) => { var { guild_id } = req.params; var banned_user_id = req.params.user_id; - const banned_user = await getPublicUser(banned_user_id); - const guild = await GuildModel.exists({ id: guild_id }); - if (!guild) throw new HTTPError("Guild not found", 404); - + const banned_user = await User.getPublicUser(banned_user_id); const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("BAN_MEMBERS"); - await BanModel.deleteOne({ - user_id: banned_user_id, - guild_id - }).exec(); + await Promise.all([ + Ban.delete({ + user_id: banned_user_id, + guild_id + }), - await emitEvent({ - event: "GUILD_BAN_REMOVE", - data: { - guild_id, - user: banned_user - }, - guild_id - } as GuildBanRemoveEvent); + emitEvent({ + event: "GUILD_BAN_REMOVE", + data: { + guild_id, + user: banned_user + }, + guild_id + } as GuildBanRemoveEvent) + ]); return res.status(204).send(); }); diff --git a/api/src/routes/guilds/#guild_id/channels.ts b/api/src/routes/guilds/#guild_id/channels.ts index 1c55ef24..5aa1d33d 100644 --- a/api/src/routes/guilds/#guild_id/channels.ts +++ b/api/src/routes/guilds/#guild_id/channels.ts @@ -1,17 +1,16 @@ import { Router, Response, Request } from "express"; -import { ChannelModel, toObject, ChannelUpdateEvent, getPermission, emitEvent } from "@fosscord/util"; +import { Channel, ChannelUpdateEvent, getPermission, emitEvent } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { ChannelModifySchema } from "../../../schema/Channel"; import { check } from "../../../util/instanceOf"; -import { createChannel } from "../../../util/Channel"; const router = Router(); router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; - const channels = await ChannelModel.find({ guild_id }).exec(); + const channels = await Channel.find({ guild_id }); - res.json(toObject(channels)); + res.json(channels); }); // TODO: check if channel type is permitted @@ -22,9 +21,9 @@ router.post("/", check(ChannelModifySchema), async (req: Request, res: Response) const { guild_id } = req.params; const body = req.body as ChannelModifySchema; - const channel = await createChannel({ ...body, guild_id }, req.user_id); + const channel = await Channel.createChannel({ ...body, guild_id }, req.user_id); - res.status(201).json(toObject(channel)); + res.status(201).json(channel); }); // TODO: check if parent_id exists @@ -48,18 +47,19 @@ router.patch( if (x.parent_id) { opts.parent_id = x.parent_id; - const parent_channel = await ChannelModel.findOne( - { id: x.parent_id, guild_id }, - { permission_overwrites: true } - ).exec(); + const parent_channel = await Channel.findOneOrFail({ + where: { id: x.parent_id, guild_id }, + select: ["permission_overwrites"] + }); if (x.lock_permissions) { opts.permission_overwrites = parent_channel.permission_overwrites; } } - const channel = await ChannelModel.findOneAndUpdate({ id: x.id, guild_id }, opts, { new: true }).exec(); + await Channel.update({ guild_id, id: x.id }, opts); + const channel = await Channel.findOneOrFail({ guild_id, id: x.id }); - await emitEvent({ event: "CHANNEL_UPDATE", data: toObject(channel), channel_id: x.id, guild_id } as ChannelUpdateEvent); + await emitEvent({ event: "CHANNEL_UPDATE", data: channel, channel_id: x.id, guild_id } as ChannelUpdateEvent); }) ]); diff --git a/api/src/routes/guilds/#guild_id/delete.ts b/api/src/routes/guilds/#guild_id/delete.ts index ba1c2fde..043260e9 100644 --- a/api/src/routes/guilds/#guild_id/delete.ts +++ b/api/src/routes/guilds/#guild_id/delete.ts @@ -1,15 +1,4 @@ -import { - ChannelModel, - emitEvent, - EmojiModel, - GuildDeleteEvent, - GuildModel, - InviteModel, - MemberModel, - MessageModel, - RoleModel, - UserModel -} from "@fosscord/util"; +import { Channel, emitEvent, GuildDeleteEvent, Guild, Member, Message, Role, Invite, Emoji } from "@fosscord/util"; import { Router, Request, Response } from "express"; import { HTTPError } from "lambert-server"; @@ -20,7 +9,7 @@ const router = Router(); router.post("/", async (req: Request, res: Response) => { var { guild_id } = req.params; - const guild = await GuildModel.findOne({ id: guild_id }, "owner_id").exec(); + const guild = await Guild.findOneOrFail({ where: { id: guild_id }, select: ["owner_id"] }); if (guild.owner_id !== req.user_id) throw new HTTPError("You are not the owner of this guild", 401); await emitEvent({ @@ -32,14 +21,13 @@ router.post("/", async (req: Request, res: Response) => { } as GuildDeleteEvent); await Promise.all([ - GuildModel.deleteOne({ id: guild_id }).exec(), - UserModel.updateMany({ guilds: guild_id }, { $pull: { guilds: guild_id } }).exec(), - RoleModel.deleteMany({ guild_id }).exec(), - ChannelModel.deleteMany({ guild_id }).exec(), - EmojiModel.deleteMany({ guild_id }).exec(), - InviteModel.deleteMany({ guild_id }).exec(), - MessageModel.deleteMany({ guild_id }).exec(), - MemberModel.deleteMany({ guild_id }).exec() + Guild.delete({ id: guild_id }), + Role.delete({ guild_id }), + Channel.delete({ guild_id }), + Emoji.delete({ guild_id }), + Invite.delete({ guild_id }), + Message.delete({ guild_id }), + Member.delete({ guild_id }) ]); return res.sendStatus(204); diff --git a/api/src/routes/guilds/#guild_id/index.ts b/api/src/routes/guilds/#guild_id/index.ts index 87103caa..6f55be3b 100644 --- a/api/src/routes/guilds/#guild_id/index.ts +++ b/api/src/routes/guilds/#guild_id/index.ts @@ -1,19 +1,5 @@ import { Request, Response, Router } from "express"; -import { - ChannelModel, - emitEvent, - EmojiModel, - getPermission, - GuildDeleteEvent, - GuildModel, - GuildUpdateEvent, - InviteModel, - MemberModel, - MessageModel, - RoleModel, - toObject, - UserModel -} from "@fosscord/util"; +import { emitEvent, getPermission, Guild, GuildUpdateEvent, Member } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { GuildUpdateSchema } from "../../../schema/Guild"; @@ -26,12 +12,15 @@ const router = Router(); router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; - const guild = await GuildModel.findOne({ id: guild_id }) - .populate({ path: "joined_at", match: { id: req.user_id } }) - .exec(); + const [guild, member_count, member] = await Promise.all([ + Guild.findOneOrFail({ id: guild_id }), + Member.count({ guild: { id: guild_id }, id: req.user_id }), + Member.findOneOrFail({ id: req.user_id }) + ]); + if (!member_count) throw new HTTPError("You are not a member of the guild you are trying to access", 401); - const member = await MemberModel.exists({ guild_id: guild_id, id: req.user_id }); - if (!member) throw new HTTPError("You are not a member of the guild you are trying to access", 401); + // @ts-ignore + guild.joined_at = member?.joined_at; return res.json(guild); }); @@ -48,15 +37,12 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response) if (body.banner) body.banner = await handleFile(`/banners/${guild_id}`, body.banner); if (body.splash) body.splash = await handleFile(`/splashes/${guild_id}`, body.splash); - const guild = await GuildModel.findOneAndUpdate({ id: guild_id }, body, { new: true }) - .populate({ path: "joined_at", match: { id: req.user_id } }) - .exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }); + guild.assign(body); - const data = toObject(guild); + await Promise.all([guild.save(), emitEvent({ event: "GUILD_UPDATE", data: guild, guild_id } as GuildUpdateEvent)]); - emitEvent({ event: "GUILD_UPDATE", data: data, guild_id } as GuildUpdateEvent); - - return res.json(data); + return res.json(guild); }); export default router; diff --git a/api/src/routes/guilds/#guild_id/invites.ts b/api/src/routes/guilds/#guild_id/invites.ts index 08048d61..1843b689 100644 --- a/api/src/routes/guilds/#guild_id/invites.ts +++ b/api/src/routes/guilds/#guild_id/invites.ts @@ -1,4 +1,4 @@ -import { getPermission, InviteModel, toObject } from "@fosscord/util"; +import { getPermission, Invite } from "@fosscord/util"; import { Request, Response, Router } from "express"; const router = Router(); @@ -9,9 +9,9 @@ router.get("/", async (req: Request, res: Response) => { const permissions = await getPermission(req.user_id, guild_id); permissions.hasThrow("MANAGE_GUILD"); - const invites = await InviteModel.find({ guild_id }).exec(); + const invites = await Invite.find({ guild_id }); - return res.json(toObject(invites)); + return res.json(invites); }); export default router; diff --git a/api/src/routes/guilds/#guild_id/members/#member_id/index.ts b/api/src/routes/guilds/#guild_id/members/#member_id/index.ts index 515434d6..d9ce91c0 100644 --- a/api/src/routes/guilds/#guild_id/members/#member_id/index.ts +++ b/api/src/routes/guilds/#guild_id/members/#member_id/index.ts @@ -1,50 +1,53 @@ import { Request, Response, Router } from "express"; import { - GuildModel, - MemberModel, - UserModel, - toObject, + Guild, + Member, + User, GuildMemberAddEvent, getPermission, PermissionResolvable, - RoleModel, + Role, GuildMemberUpdateEvent, emitEvent } from "@fosscord/util"; import { HTTPError } from "lambert-server"; -import { addMember, isMember, removeMember } from "../../../../../util/Member"; import { check } from "../../../../../util/instanceOf"; import { MemberChangeSchema } from "../../../../../schema/Member"; +import { In } from "typeorm"; const router = Router(); router.get("/", async (req: Request, res: Response) => { const { guild_id, member_id } = req.params; - await isMember(req.user_id, guild_id); + await Member.IsInGuildOrFail(req.user_id, guild_id); - const member = await MemberModel.findOne({ id: member_id, guild_id }).exec(); + const member = await Member.findOneOrFail({ id: member_id, guild_id }); - return res.json(toObject(member)); + return res.json(member); }); router.patch("/", check(MemberChangeSchema), async (req: Request, res: Response) => { const { guild_id, member_id } = req.params; const body = req.body as MemberChangeSchema; if (body.roles) { - const roles = await RoleModel.find({ id: { $in: body.roles } }).exec(); + const roles = await Role.find({ id: In(body.roles) }); if (body.roles.length !== roles.length) throw new HTTPError("Roles not found", 404); // TODO: check if user has permission to add role } - const member = await MemberModel.findOneAndUpdate({ id: member_id, guild_id }, body, { new: true }).exec(); + const member = await Member.findOneOrFail({ id: member_id, guild_id }); + member.assign(req.body); - await emitEvent({ - event: "GUILD_MEMBER_UPDATE", - guild_id, - data: toObject(member) - } as GuildMemberUpdateEvent); + Promise.all([ + member.save(), + emitEvent({ + event: "GUILD_MEMBER_UPDATE", + guild_id, + data: { ...member, roles: member.roles.map((x) => x.id) } + } as GuildMemberUpdateEvent) + ]); - res.json(toObject(member)); + res.json(member); }); router.put("/", async (req: Request, res: Response) => { @@ -52,7 +55,7 @@ router.put("/", async (req: Request, res: Response) => { throw new HTTPError("Maintenance: Currently you can't add a member", 403); // TODO: only for oauth2 applications - await addMember(member_id, guild_id); + await Member.addToGuild(member_id, guild_id); res.sendStatus(204); }); @@ -62,7 +65,7 @@ router.delete("/", async (req: Request, res: Response) => { const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("KICK_MEMBERS"); - await removeMember(member_id, guild_id); + await Member.removeFromGuild(member_id, guild_id); res.sendStatus(204); }); diff --git a/api/src/routes/guilds/#guild_id/members/#member_id/nick.ts b/api/src/routes/guilds/#guild_id/members/#member_id/nick.ts index e4308364..3f2975e6 100644 --- a/api/src/routes/guilds/#guild_id/members/#member_id/nick.ts +++ b/api/src/routes/guilds/#guild_id/members/#member_id/nick.ts @@ -1,8 +1,7 @@ -import { getPermission, PermissionResolvable } from "@fosscord/util"; +import { getPermission, Member, PermissionResolvable } from "@fosscord/util"; import { Request, Response, Router } from "express"; import { check } from "lambert-server"; import { MemberNickChangeSchema } from "../../../../../schema/Member"; -import { changeNickname } from "../../../../../util/Member"; const router = Router(); @@ -17,7 +16,7 @@ router.patch("/", check(MemberNickChangeSchema), async (req: Request, res: Respo const perms = await getPermission(req.user_id, guild_id); perms.hasThrow(permissionString); - await changeNickname(member_id, guild_id, req.body.nick); + await Member.changeNickname(member_id, guild_id, req.body.nick); res.status(200).send(); }); diff --git a/api/src/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts b/api/src/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts index fad0695e..cb9bad9a 100644 --- a/api/src/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts +++ b/api/src/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts @@ -1,6 +1,5 @@ -import { getPermission } from "@fosscord/util"; +import { getPermission, Member } from "@fosscord/util"; import { Request, Response, Router } from "express"; -import { addRole, removeRole } from "../../../../../../../util/Member"; const router = Router(); @@ -10,7 +9,7 @@ router.delete("/:member_id/roles/:role_id", async (req: Request, res: Response) const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_ROLES"); - await removeRole(member_id, guild_id, role_id); + await Member.removeRole(member_id, guild_id, role_id); res.sendStatus(204); }); @@ -20,7 +19,7 @@ router.put("/:member_id/roles/:role_id", async (req: Request, res: Response) => const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_ROLES"); - await addRole(member_id, guild_id, role_id); + await Member.addRole(member_id, guild_id, role_id); res.sendStatus(204); }); diff --git a/api/src/routes/guilds/#guild_id/members/index.ts b/api/src/routes/guilds/#guild_id/members/index.ts index 70303436..0bfd71cb 100644 --- a/api/src/routes/guilds/#guild_id/members/index.ts +++ b/api/src/routes/guilds/#guild_id/members/index.ts @@ -1,8 +1,7 @@ import { Request, Response, Router } from "express"; -import { GuildModel, MemberModel, toObject } from "@fosscord/util"; -import { HTTPError } from "lambert-server"; +import { Guild, Member, PublicMemberProjection } from "@fosscord/util"; import { instanceOf, Length } from "../../../../util/instanceOf"; -import { PublicMemberProjection, isMember } from "../../../../util/Member"; +import { MoreThan } from "typeorm"; const router = Router(); @@ -10,8 +9,8 @@ const router = Router(); // TODO: send over websocket router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; - const guild = await GuildModel.findOne({ id: guild_id }).exec(); - await isMember(req.user_id, guild_id); + const guild = await Guild.findOneOrFail({ id: guild_id }); + await Member.IsInGuildOrFail(req.user_id, guild_id); try { instanceOf({ $limit: new Length(Number, 1, 1000), $after: String }, req.query, { @@ -23,16 +22,17 @@ router.get("/", async (req: Request, res: Response) => { return res.status(400).json({ code: 50035, message: "Invalid Query", success: false, errors: error }); } - // @ts-ignore - if (!req.query.limit) req.query.limit = 1; - const { limit, after } = (req.query) as { limit: number; after: string }; - const query = after ? { id: { $gt: after } } : {}; + const { limit, after } = (req.query) as { limit?: number; after?: string }; + const query = after ? { id: MoreThan(after) } : {}; - var members = await MemberModel.find({ guild_id, ...query }, PublicMemberProjection) - .limit(limit) - .exec(); + const members = await Member.find({ + where: { guild_id, ...query }, + select: PublicMemberProjection, + take: limit || 1, + order: { id: "ASC" } + }); - return res.json(toObject(members)); + return res.json(members); }); export default router; diff --git a/api/src/routes/guilds/#guild_id/regions.ts b/api/src/routes/guilds/#guild_id/regions.ts index 5ec649ee..212c9bcd 100644 --- a/api/src/routes/guilds/#guild_id/regions.ts +++ b/api/src/routes/guilds/#guild_id/regions.ts @@ -1,10 +1,15 @@ -import { Config } from "@fosscord/util"; +import {Config, Guild, Member} from "@fosscord/util"; import { Request, Response, Router } from "express"; +import {getVoiceRegions} from "../../../util/Voice"; +import {getIpAdress} from "../../../util/ipAddress"; const router = Router(); router.get("/", async (req: Request, res: Response) => { - return res.json(Config.get().regions.available); + const { guild_id } = req.params; + const guild = await Guild.findOneOrFail({ id: guild_id }); + //TODO we should use an enum for guild's features and not hardcoded strings + return res.json(await getVoiceRegions(getIpAdress(req), guild.features.includes("VIP_REGIONS"))); }); export default router; diff --git a/api/src/routes/guilds/#guild_id/roles.ts b/api/src/routes/guilds/#guild_id/roles.ts index f095c885..f6ac8caa 100644 --- a/api/src/routes/guilds/#guild_id/roles.ts +++ b/api/src/routes/guilds/#guild_id/roles.ts @@ -1,12 +1,9 @@ import { Request, Response, Router } from "express"; import { - RoleModel, - GuildModel, + Role, getPermission, - toObject, - UserModel, Snowflake, - MemberModel, + Member, GuildRoleCreateEvent, GuildRoleUpdateEvent, GuildRoleDeleteEvent, @@ -16,40 +13,34 @@ import { HTTPError } from "lambert-server"; import { check } from "../../../util/instanceOf"; import { RoleModifySchema } from "../../../schema/Roles"; -import { getPublicUser } from "../../../util/User"; -import { isMember } from "../../../util/Member"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { const guild_id = req.params.guild_id; - await isMember(req.user_id, guild_id); + await Member.IsInGuildOrFail(req.user_id, guild_id); - const roles = await RoleModel.find({ guild_id: guild_id }).exec(); + const roles = await Role.find({ guild_id: guild_id }); - return res.json(toObject(roles)); + return res.json(roles); }); router.post("/", check(RoleModifySchema), async (req: Request, res: Response) => { const guild_id = req.params.guild_id; const body = req.body as RoleModifySchema; - const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); - const user = await UserModel.findOne({ id: req.user_id }).exec(); - const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_ROLES"); - if (!body.name) throw new HTTPError("You need to specify a name"); - const role = await new RoleModel({ + const role = await new Role({ ...body, id: Snowflake.generate(), guild_id: guild_id, managed: false, position: 0, tags: null, - permissions: body.permissions || 0n + permissions: String(perms.bitfield & (body.permissions || 0n)) }).save(); await emitEvent({ @@ -57,11 +48,11 @@ router.post("/", check(RoleModifySchema), async (req: Request, res: Response) => guild_id, data: { guild_id, - role: toObject(role) + role: role } } as GuildRoleCreateEvent); - res.json(toObject(role)); + res.json(role); }); router.delete("/:role_id", async (req: Request, res: Response) => { @@ -72,19 +63,20 @@ router.delete("/:role_id", async (req: Request, res: Response) => { const permissions = await getPermission(req.user_id, guild_id); permissions.hasThrow("MANAGE_ROLES"); - await RoleModel.deleteOne({ - id: role_id, - guild_id: guild_id - }).exec(); - - await emitEvent({ - event: "GUILD_ROLE_DELETE", - guild_id, - data: { + await Promise.all([ + Role.delete({ + id: role_id, + guild_id: guild_id + }), + emitEvent({ + event: "GUILD_ROLE_DELETE", guild_id, - role_id - } - } as GuildRoleDeleteEvent); + data: { + guild_id, + role_id + } + } as GuildRoleDeleteEvent) + ]); res.sendStatus(204); }); @@ -96,32 +88,24 @@ router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Res const { role_id } = req.params; const body = req.body as RoleModifySchema; - const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); - const user = await UserModel.findOne({ id: req.user_id }).exec(); - const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_ROLES"); - const role = await RoleModel.findOneAndUpdate( - { - id: role_id, - guild_id: guild_id - }, - // @ts-ignore - body, - { new: true } - ).exec(); + const role = new Role({ ...body, id: role_id, guild_id, permissions: perms.bitfield & (body.permissions || 0n) }); - await emitEvent({ - event: "GUILD_ROLE_UPDATE", - guild_id, - data: { + await Promise.all([ + role.save(), + emitEvent({ + event: "GUILD_ROLE_UPDATE", guild_id, - role - } - } as GuildRoleUpdateEvent); + data: { + guild_id, + role + } + } as GuildRoleUpdateEvent) + ]); - res.json(toObject(role)); + res.json(role); }); export default router; diff --git a/api/src/routes/guilds/#guild_id/templates.ts b/api/src/routes/guilds/#guild_id/templates.ts index e441ee12..a7613abf 100644 --- a/api/src/routes/guilds/#guild_id/templates.ts +++ b/api/src/routes/guilds/#guild_id/templates.ts @@ -1,5 +1,5 @@ import { Request, Response, Router } from "express"; -import { TemplateModel, GuildModel, getPermission, toObject, UserModel, Snowflake } from "@fosscord/util"; +import { Guild, getPermission, Template } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { TemplateCreateSchema, TemplateModifySchema } from "../../../schema/Template"; import { check } from "../../../util/instanceOf"; @@ -7,43 +7,41 @@ import { generateCode } from "../../../util/String"; const router: Router = Router(); -const TemplateGuildProjection = { - name: true, - description: true, - region: true, - verification_level: true, - default_message_notifications: true, - explicit_content_filter: true, - preferred_locale: true, - afk_timeout: true, - roles: true, - channels: true, - afk_channel_id: true, - system_channel_id: true, - system_channel_flags: true, - icon_hash: true -}; +const TemplateGuildProjection: (keyof Guild)[] = [ + "name", + "description", + "region", + "verification_level", + "default_message_notifications", + "explicit_content_filter", + "preferred_locale", + "afk_timeout", + "roles", + // "channels", + "afk_channel_id", + "system_channel_id", + "system_channel_flags", + "icon" +]; router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; - var templates = await TemplateModel.find({ source_guild_id: guild_id }).exec(); + var templates = await Template.find({ source_guild_id: guild_id }); - return res.json(toObject(templates)); + return res.json(templates); }); router.post("/", check(TemplateCreateSchema), async (req: Request, res: Response) => { const { guild_id } = req.params; - const guild = await GuildModel.findOne({ id: guild_id }, TemplateGuildProjection).exec(); + const guild = await Guild.findOneOrFail({ where: { id: guild_id }, select: TemplateGuildProjection }); const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); - const exists = await TemplateModel.findOne({ id: guild_id }) - .exec() - .catch((e) => {}); + const exists = await Template.findOneOrFail({ id: guild_id }).catch((e) => {}); if (exists) throw new HTTPError("Template already exists", 400); - const template = await new TemplateModel({ + const template = await new Template({ ...req.body, code: generateCode(), creator_id: req.user_id, @@ -53,7 +51,7 @@ router.post("/", check(TemplateCreateSchema), async (req: Request, res: Response serialized_source_guild: guild }).save(); - res.json(toObject(template)).send(); + res.json(template); }); router.delete("/:code", async (req: Request, res: Response) => { @@ -63,41 +61,39 @@ router.delete("/:code", async (req: Request, res: Response) => { const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); - const template = await TemplateModel.findOneAndDelete({ + const template = await Template.delete({ code - }).exec(); + }); - res.send(toObject(template)); + res.json(template); }); router.put("/:code", async (req: Request, res: Response) => { - const guild_id = req.params.guild_id; - const { code } = req.params; + // synchronizes the template + const { code, guild_id } = req.params; - const guild = await GuildModel.findOne({ id: guild_id }, TemplateGuildProjection).exec(); + const guild = await Guild.findOneOrFail({ where: { id: guild_id }, select: TemplateGuildProjection }); const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); - const template = await TemplateModel.findOneAndUpdate({ code }, { serialized_source_guild: guild }, { new: true }).exec(); + const template = await new Template({ code, serialized_source_guild: guild }).save(); - res.json(toObject(template)).send(); + res.json(template); }); router.patch("/:code", check(TemplateModifySchema), async (req: Request, res: Response) => { + // updates the template description const { guild_id } = req.params; const { code } = req.params; + const { name, description } = req.body; const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); - const template = await TemplateModel.findOneAndUpdate( - { code }, - { name: req.body.name, description: req.body.description }, - { new: true } - ).exec(); + const template = await new Template({ code, name: name, description: description }).save(); - res.json(toObject(template)).send(); + res.json(template); }); export default router; diff --git a/api/src/routes/guilds/#guild_id/vanity-url.ts b/api/src/routes/guilds/#guild_id/vanity-url.ts index 1e659d8d..58940b42 100644 --- a/api/src/routes/guilds/#guild_id/vanity-url.ts +++ b/api/src/routes/guilds/#guild_id/vanity-url.ts @@ -1,8 +1,7 @@ -import { ChannelModel, ChannelType, getPermission, GuildModel, InviteModel, trimSpecial } from "@fosscord/util"; +import { Channel, ChannelType, getPermission, Guild, Invite, trimSpecial } from "@fosscord/util"; import { Router, Request, Response } from "express"; import { HTTPError } from "lambert-server"; import { check, Length } from "../../../util/instanceOf"; -import { isMember } from "../../../util/Member"; const router = Router(); @@ -14,46 +13,37 @@ router.get("/", async (req: Request, res: Response) => { const permission = await getPermission(req.user_id, guild_id); permission.hasThrow("MANAGE_GUILD"); - const guild = await GuildModel.findOne({ id: guild_id }).exec(); - if (!guild.vanity_url_code) return res.json({ code: null }); - const { uses } = await InviteModel.findOne({ code: guild.vanity_url_code }).exec(); + const guild = await Guild.findOneOrFail({ where: { id: guild_id }, relations: ["vanity_url"] }); + if (!guild.vanity_url) return res.json({ code: null }); - return res.json({ code: guild.vanity_url_code, uses }); + return res.json({ code: guild.vanity_url_code, uses: guild.vanity_url.uses }); }); // TODO: check if guild is elgible for vanity url router.patch("/", check({ code: new Length(String, 0, 20) }), async (req: Request, res: Response) => { const { guild_id } = req.params; - var code = req.body.code.replace(InviteRegex); - if (!code) code = null; + const code = req.body.code.replace(InviteRegex); - const guild = await GuildModel.findOne({ id: guild_id }).exec(); - const permission = await getPermission(req.user_id, guild_id, undefined, { guild }); + await Invite.findOneOrFail({ code }); + + const guild = await Guild.findOneOrFail({ id: guild_id }); + const permission = await getPermission(req.user_id, guild_id); permission.hasThrow("MANAGE_GUILD"); - const alreadyExists = await Promise.all([ - GuildModel.findOne({ vanity_url_code: code }) - .exec() - .catch(() => null), - InviteModel.findOne({ code: code }) - .exec() - .catch(() => null) - ]); - if (alreadyExists.some((x) => x)) throw new HTTPError("Vanity url already exists", 400); + const { id } = await Channel.findOneOrFail({ guild_id, type: ChannelType.GUILD_TEXT }); + guild.vanity_url_code = code; - await GuildModel.updateOne({ id: guild_id }, { vanity_url_code: code }).exec(); - const { id } = await ChannelModel.findOne({ guild_id, type: ChannelType.GUILD_TEXT }).exec(); - await InviteModel.updateOne( - { code: guild.vanity_url_code }, - { + Promise.all([ + guild.save(), + Invite.delete({ code: guild.vanity_url_code }), + new Invite({ code: code, uses: 0, created_at: new Date(), guild_id, channel_id: id - }, - { upsert: true } - ).exec(); + }).save() + ]); return res.json({ code: code }); }); diff --git a/api/src/routes/guilds/#guild_id/welcome_screen.ts b/api/src/routes/guilds/#guild_id/welcome_screen.ts index c717042e..defbcd40 100644 --- a/api/src/routes/guilds/#guild_id/welcome_screen.ts +++ b/api/src/routes/guilds/#guild_id/welcome_screen.ts @@ -1,47 +1,35 @@ import { Request, Response, Router } from "express"; -import { GuildModel, getPermission, toObject, Snowflake } from "@fosscord/util"; +import { Guild, getPermission, Snowflake, Member } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { check } from "../../../util/instanceOf"; -import { isMember } from "../../../util/Member"; -import { GuildAddChannelToWelcomeScreenSchema } from "../../../schema/Guild"; -import { getPublicUser } from "../../../util/User"; +import { GuildUpdateWelcomeScreenSchema } from "../../../schema/Guild"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { const guild_id = req.params.guild_id; - const guild = await GuildModel.findOne({ id: guild_id }); + const guild = await Guild.findOneOrFail({ id: guild_id }); - await isMember(req.user_id, guild_id); + await Member.IsInGuildOrFail(req.user_id, guild_id); - res.json(toObject(guild.welcome_screen)); + res.json(guild.welcome_screen); }); -router.post("/", check(GuildAddChannelToWelcomeScreenSchema), async (req: Request, res: Response) => { +router.patch("/", check(GuildUpdateWelcomeScreenSchema), async (req: Request, res: Response) => { const guild_id = req.params.guild_id; - const body = req.body as GuildAddChannelToWelcomeScreenSchema; + const body = req.body as GuildUpdateWelcomeScreenSchema; - const guild = await GuildModel.findOne({ id: guild_id }).exec(); - - var channelObject = { - ...body - }; + const guild = await Guild.findOneOrFail({ id: guild_id }); const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); if (!guild.welcome_screen.enabled) throw new HTTPError("Welcome screen disabled", 400); - if (guild.welcome_screen.welcome_channels.some((channel) => channel.channel_id === body.channel_id)) - throw new Error("Welcome Channel exists"); - - await GuildModel.findOneAndUpdate( - { - id: guild_id - }, - { $push: { "welcome_screen.welcome_channels": channelObject } } - ).exec(); + if (body.welcome_channels) guild.welcome_screen.welcome_channels = body.welcome_channels; // TODO: check if they exist and are valid + if (body.description) guild.welcome_screen.description = body.description; + if (body.enabled != null) guild.welcome_screen.enabled = body.enabled; res.sendStatus(204); }); diff --git a/api/src/routes/guilds/#guild_id/widget.json.ts b/api/src/routes/guilds/#guild_id/widget.json.ts index 8719bd85..ae1f0599 100644 --- a/api/src/routes/guilds/#guild_id/widget.json.ts +++ b/api/src/routes/guilds/#guild_id/widget.json.ts @@ -1,5 +1,5 @@ import { Request, Response, Router } from "express"; -import { Config, Permissions, GuildModel, InviteModel, ChannelModel, MemberModel } from "@fosscord/util"; +import { Config, Permissions, Guild, Invite, Channel, Member } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { random } from "../../../util/RandomInviteID"; @@ -17,11 +17,12 @@ const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; - const guild = await GuildModel.findOne({ id: guild_id }).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }); if (!guild.widget_enabled) throw new HTTPError("Widget Disabled", 404); // Fetch existing widget invite for widget channel - var invite = await InviteModel.findOne({ channel_id: guild.widget_channel_id, inviter_id: { $type: 10 } }).exec(); + var invite = await Invite.findOne({ channel_id: guild.widget_channel_id }); + if (guild.widget_channel_id && !invite) { // Create invite for channel if none exists // TODO: Refactor invite create code to a shared function @@ -40,87 +41,29 @@ router.get("/", async (req: Request, res: Response) => { inviter_id: null }; - invite = await new InviteModel(body).save(); + invite = await new Invite(body).save(); } // Fetch voice channels, and the @everyone permissions object - let channels: any[] = []; - await ChannelModel.find({ guild_id: guild_id, type: 2 }, { permission_overwrites: { $elemMatch: { id: guild_id } } }) - .lean() - .select("id name position permission_overwrites") - .sort({ position: 1 }) - .cursor() - .eachAsync((doc) => { - // Only return channels where @everyone has the CONNECT permission - if ( - doc.permission_overwrites === undefined || - Permissions.channelPermission(doc.permission_overwrites, Permissions.FLAGS.CONNECT) === Permissions.FLAGS.CONNECT - ) { - channels.push({ - id: doc.id, - name: doc.name, - position: doc.position - }); - } - }); + const channels = [] as any[]; + + (await Channel.find({ where: { guild_id: guild_id, type: 2 }, order: { position: "ASC" } })).filter((doc) => { + // Only return channels where @everyone has the CONNECT permission + if ( + doc.permission_overwrites === undefined || + Permissions.channelPermission(doc.permission_overwrites, Permissions.FLAGS.CONNECT) === Permissions.FLAGS.CONNECT + ) { + channels.push({ + id: doc.id, + name: doc.name, + position: doc.position + }); + } + }); // Fetch members // TODO: Understand how Discord's max 100 random member sample works, and apply to here (see top of this file) - let members: any[] = []; - await MemberModel.find({ guild_id: guild_id }) - .lean() - .populate({ path: "user", select: { _id: 0, username: 1, avatar: 1, presence: 1 } }) - .select("id user nick deaf mute") - .cursor() - .eachAsync((doc) => { - const status = doc.user?.presence?.status || "offline"; - if (status == "offline") return; - - let item = {}; - - item = { - ...item, - id: null, // this is updated during the sort outside of the query - username: doc.nick || doc.user?.username, - discriminator: "0000", // intended (https://github.com/discord/discord-api-docs/issues/1287) - avatar: null, // intended, avatar_url below will return a unique guild + user url to the avatar - status: status - }; - - const activity = doc.user?.presence?.activities?.[0]; - if (activity) { - item = { - ...item, - game: { name: activity.name } - }; - } - - // TODO: If the member is in a voice channel, return extra widget details - // Extra fields returned include deaf, mute, self_deaf, self_mute, supress, and channel_id (voice channel connected to) - // Get this from VoiceState - - // TODO: Implement a widget-avatar endpoint on the CDN, and implement logic here to request it - // Get unique avatar url for guild user, cdn to serve the actual avatar image on this url - /* - const avatar = doc.user?.avatar; - if (avatar) { - const CDN_HOST = Config.get().cdn.endpoint || "http://localhost:3003"; - const avatar_url = "/widget-avatars/" + ; - item = { - ...item, - avatar_url: avatar_url - } - } - */ - - members.push(item); - }); - - // Sort members, and update ids (Unable to do under the mongoose query due to https://mongoosejs.com/docs/faq.html#populate_sort_order) - members = members.sort((first, second) => 0 - (first.username > second.username ? -1 : 1)); - members.forEach((x, i) => { - x.id = i; - }); + let members = await Member.find({ where: { guild_id: guild_id } }); // Construct object to respond with const data = { diff --git a/api/src/routes/guilds/#guild_id/widget.png.ts b/api/src/routes/guilds/#guild_id/widget.png.ts index 80dc9f2b..89b31153 100644 --- a/api/src/routes/guilds/#guild_id/widget.png.ts +++ b/api/src/routes/guilds/#guild_id/widget.png.ts @@ -1,5 +1,5 @@ import { Request, Response, Router } from "express"; -import { GuildModel } from "@fosscord/util"; +import { Guild } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import fs from "fs"; import path from "path"; @@ -13,7 +13,7 @@ const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; - const guild = await GuildModel.findOne({ id: guild_id }).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }); if (!guild.widget_enabled) throw new HTTPError("Unknown Guild", 404); // Fetch guild information diff --git a/api/src/routes/guilds/#guild_id/widget.ts b/api/src/routes/guilds/#guild_id/widget.ts index 85eed5e9..fcf71402 100644 --- a/api/src/routes/guilds/#guild_id/widget.ts +++ b/api/src/routes/guilds/#guild_id/widget.ts @@ -1,5 +1,5 @@ import { Request, Response, Router } from "express"; -import { getPermission, GuildModel } from "@fosscord/util"; +import { getPermission, Guild } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { check } from "../../../util/instanceOf"; import { WidgetModifySchema } from "../../../schema/Widget"; @@ -13,7 +13,7 @@ router.get("/", async (req: Request, res: Response) => { const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); - const guild = await GuildModel.findOne({ id: guild_id }).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }); return res.json({ enabled: guild.widget_enabled || false, channel_id: guild.widget_channel_id || null }); }); @@ -26,7 +26,7 @@ router.patch("/", check(WidgetModifySchema), async (req: Request, res: Response) const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); - await GuildModel.updateOne({ id: guild_id }, { widget_enabled: body.enabled, widget_channel_id: body.channel_id }).exec(); + await Guild.update({ id: guild_id }, { widget_enabled: body.enabled, widget_channel_id: body.channel_id }); // Widget invite for the widget_channel_id gets created as part of the /guilds/{guild.id}/widget.json request return res.json(body); diff --git a/api/src/routes/guilds/index.ts b/api/src/routes/guilds/index.ts index 92feed4e..e4157384 100644 --- a/api/src/routes/guilds/index.ts +++ b/api/src/routes/guilds/index.ts @@ -1,11 +1,8 @@ import { Router, Request, Response } from "express"; -import { RoleModel, GuildModel, Snowflake, Guild, RoleDocument, Config } from "@fosscord/util"; +import { Role, Guild, Snowflake, Config, User, Member, Channel } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { check } from "./../../util/instanceOf"; import { GuildCreateSchema } from "../../schema/Guild"; -import { getPublicUser } from "../../util/User"; -import { addMember } from "../../util/Member"; -import { createChannel } from "../../util/Channel"; const router: Router = Router(); @@ -15,58 +12,43 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) = const body = req.body as GuildCreateSchema; const { maxGuilds } = Config.get().limits.user; - const user = await getPublicUser(req.user_id, { guilds: true }); - - if (user.guilds.length >= maxGuilds) { + const guild_count = await Member.count({ id: req.user_id }); + if (guild_count >= maxGuilds) { throw new HTTPError(`Maximum number of guilds reached ${maxGuilds}`, 403); } const guild_id = Snowflake.generate(); - const guild: Guild = { - name: body.name, - region: Config.get().regions.default, - owner_id: req.user_id, - icon: undefined, - afk_channel_id: undefined, - afk_timeout: 300, - application_id: undefined, - banner: undefined, - default_message_notifications: 0, - description: undefined, - splash: undefined, - discovery_splash: undefined, - explicit_content_filter: 0, - features: [], - id: guild_id, - large: undefined, - max_members: 250000, - max_presences: 250000, - max_video_channel_users: 25, - presence_count: 0, - member_count: 0, // will automatically be increased by addMember() - mfa_level: 0, - preferred_locale: "en-US", - premium_subscription_count: 0, - premium_tier: 0, - public_updates_channel_id: undefined, - rules_channel_id: undefined, - system_channel_flags: 0, - system_channel_id: undefined, - unavailable: false, - vanity_url_code: undefined, - verification_level: 0, - welcome_screen: { - enabled: false, - description: "No description", - welcome_channels: [] - }, - widget_channel_id: undefined, - widget_enabled: false - }; - const [guild_doc, role] = await Promise.all([ - new GuildModel(guild).save(), - new RoleModel({ + const [guild, role] = await Promise.all([ + Guild.insert({ + name: body.name, + region: Config.get().regions.default, + owner_id: req.user_id, + afk_timeout: 300, + default_message_notifications: 0, + explicit_content_filter: 0, + features: [], + id: guild_id, + max_members: 250000, + max_presences: 250000, + max_video_channel_users: 25, + presence_count: 0, + member_count: 0, // will automatically be increased by addMember() + mfa_level: 0, + preferred_locale: "en-US", + premium_subscription_count: 0, + premium_tier: 0, + system_channel_flags: 0, + unavailable: false, + verification_level: 0, + welcome_screen: { + enabled: false, + description: "No description", + welcome_channels: [] + }, + widget_enabled: false + }), + Role.insert({ id: guild_id, guild_id: guild_id, color: 0, @@ -74,10 +56,9 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) = managed: false, mentionable: false, name: "@everyone", - permissions: 2251804225n, - position: 0, - tags: null - }).save() + permissions: String("2251804225"), + position: 0 + }) ]); if (!body.channels || !body.channels.length) body.channels = [{ id: "01", type: 0, name: "general" }]; @@ -94,16 +75,21 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) = body.channels?.map((x) => { var id = ids.get(x.id) || Snowflake.generate(); - // TODO: should we abort if parent_id is a category? (or not to allow sub category channels) + // TODO: should we abort if parent_id is a category? (to disallow sub category channels) var parent_id = ids.get(x.parent_id); - return createChannel({ ...x, guild_id, id, parent_id }, req.user_id, { keepId: true, skipExistsCheck: true }); + return Channel.createChannel({ ...x, guild_id, id, parent_id }, req.user_id, { + keepId: true, + skipExistsCheck: true, + skipPermissionCheck: true, + skipEventEmit: true + }); }) ); - await addMember(req.user_id, guild_id); + await Member.addToGuild(req.user_id, guild_id); - res.status(201).json({ id: guild.id }); + res.status(201).json({ id: guild_id }); }); export default router; diff --git a/api/src/routes/guilds/templates/index.ts b/api/src/routes/guilds/templates/index.ts index 7fed3c5d..7a8ac886 100644 --- a/api/src/routes/guilds/templates/index.ts +++ b/api/src/routes/guilds/templates/index.ts @@ -1,18 +1,16 @@ import { Request, Response, Router } from "express"; const router: Router = Router(); -import { TemplateModel, GuildModel, toObject, UserModel, RoleModel, Snowflake, Guild, Config } from "@fosscord/util"; +import { Template, Guild, Role, Snowflake, Config, User, Member } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { GuildTemplateCreateSchema } from "../../../schema/Guild"; -import { getPublicUser } from "../../../util/User"; import { check } from "../../../util/instanceOf"; -import { addMember } from "../../../util/Member"; router.get("/:code", async (req: Request, res: Response) => { const { code } = req.params; - const template = await TemplateModel.findOne({ code: code }).exec(); + const template = await Template.findOneOrFail({ code: code }); - res.json(toObject(template)).send(); + res.json(template); }); router.post("/:code", check(GuildTemplateCreateSchema), async (req: Request, res: Response) => { @@ -20,26 +18,24 @@ router.post("/:code", check(GuildTemplateCreateSchema), async (req: Request, res const body = req.body as GuildTemplateCreateSchema; const { maxGuilds } = Config.get().limits.user; - const user = await getPublicUser(req.user_id, { guilds: true }); - if (user.guilds.length >= maxGuilds) { + const guild_count = await Member.count({ id: req.user_id }); + if (guild_count >= maxGuilds) { throw new HTTPError(`Maximum number of guilds reached ${maxGuilds}`, 403); } - const template = await TemplateModel.findOne({ code: code }).exec(); + const template = await Template.findOneOrFail({ code: code }); const guild_id = Snowflake.generate(); - const guild: Guild = { - ...body, - ...template.serialized_source_guild, - id: guild_id, - owner_id: req.user_id - }; - - const [guild_doc, role] = await Promise.all([ - new GuildModel(guild).save(), - new RoleModel({ + const [guild, role] = await Promise.all([ + new Guild({ + ...body, + ...template.serialized_source_guild, + id: guild_id, + owner_id: req.user_id + }).save(), + new Role({ id: guild_id, guild_id: guild_id, color: 0, @@ -53,7 +49,7 @@ router.post("/:code", check(GuildTemplateCreateSchema), async (req: Request, res }).save() ]); - await addMember(req.user_id, guild_id, { guild: guild_doc }); + await Member.addToGuild(req.user_id, guild_id); res.status(201).json({ id: guild.id }); }); diff --git a/api/src/routes/invites/index.ts b/api/src/routes/invites/index.ts index e7543dbb..23fa3aec 100644 --- a/api/src/routes/invites/index.ts +++ b/api/src/routes/invites/index.ts @@ -1,48 +1,41 @@ import { Router, Request, Response } from "express"; -import { getPermission, GuildModel, InviteModel, toObject } from "@fosscord/util"; +import { getPermission, Guild, Invite, Member } from "@fosscord/util"; import { HTTPError } from "lambert-server"; -import { addMember } from "../../util/Member"; const router: Router = Router(); router.get("/:code", async (req: Request, res: Response) => { const { code } = req.params; - const invite = await InviteModel.findOne({ code }).exec(); - if (!invite) throw new HTTPError("Unknown Invite", 404); + const invite = await Invite.findOneOrFail({ code }); - res.status(200).send(toObject(invite)); + res.status(200).send(invite); }); router.post("/:code", async (req: Request, res: Response) => { const { code } = req.params; - const invite = await InviteModel.findOneAndUpdate({ code }, { $inc: { uses: 1 } }, { new: true }).exec(); - if (!invite) throw new HTTPError("Unknown Invite", 404); - if (invite.uses >= invite.max_uses) await InviteModel.deleteOne({ code }); + const invite = await Invite.findOneOrFail({ code }); + if (invite.uses++ >= invite.max_uses) await Invite.delete({ code }); + else await invite.save(); - await addMember(req.user_id, invite.guild_id); + await Member.addToGuild(req.user_id, invite.guild_id); - res.status(200).send(toObject(invite)); + res.status(200).send(invite); }); router.delete("/:code", async (req: Request, res: Response) => { const { code } = req.params; - const invite = await InviteModel.findOne({ code }).exec(); + const invite = await Invite.findOneOrFail({ code }); const { guild_id, channel_id } = invite; - const guild = await GuildModel.findOne({ id: guild_id }).exec(); - const permission = await getPermission(req.user_id, guild_id, channel_id, { guild }); + const permission = await getPermission(req.user_id, guild_id, channel_id); if (!permission.has("MANAGE_GUILD") && !permission.has("MANAGE_CHANNELS")) throw new HTTPError("You missing the MANAGE_GUILD or MANAGE_CHANNELS permission", 401); - await InviteModel.deleteOne({ code }).exec(); + await Promise.all([Invite.delete({ code }), Guild.update({ vanity_url_code: code }, { vanity_url_code: undefined })]); - await GuildModel.updateOne({ vanity_url_code: code }, { $unset: { vanity_url_code: 1 } }) - .exec() - .catch((e) => {}); - - res.status(200).send({ invite: toObject(invite) }); + res.json({ invite: invite }); }); export default router; diff --git a/api/src/routes/users/#id/index.ts b/api/src/routes/users/#id/index.ts index a2ad3ae6..3841756b 100644 --- a/api/src/routes/users/#id/index.ts +++ b/api/src/routes/users/#id/index.ts @@ -1,13 +1,12 @@ import { Router, Request, Response } from "express"; -import { getPublicUser } from "../../../util/User"; -import { HTTPError } from "lambert-server"; +import { User } from "../../../../../util/dist"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { const { id } = req.params; - res.json(await getPublicUser(id)); + res.json(await User.getPublicUser(id)); }); export default router; diff --git a/api/src/routes/users/#id/profile.ts b/api/src/routes/users/#id/profile.ts index 4b4b9439..afccfed5 100644 --- a/api/src/routes/users/#id/profile.ts +++ b/api/src/routes/users/#id/profile.ts @@ -1,27 +1,27 @@ import { Router, Request, Response } from "express"; -import { getPublicUser } from "../../../util/User"; +import { User } from "../../../../../util/dist"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { - const user = await getPublicUser(req.params.id, { user_data: true }) + const user = await User.getPublicUser(req.params.id, { relations: ["connected_accounts"] }); - res.json({ - connected_accounts: user.user_data.connected_accounts, - premium_guild_since: null, // TODO - premium_since: null, // TODO - user: { - username: user.username, - discriminator: user.discriminator, - id: user.id, - public_flags: user.public_flags, - avatar: user.avatar, - accent_color: user.accent_color, - banner: user.banner, - bio: req.user_bot ? null : user.bio, - bot: user.bot, - } - }); + res.json({ + connected_accounts: user.connected_accounts, + premium_guild_since: null, // TODO + premium_since: null, // TODO + user: { + username: user.username, + discriminator: user.discriminator, + id: user.id, + public_flags: user.public_flags, + avatar: user.avatar, + accent_color: user.accent_color, + banner: user.banner, + bio: req.user_bot ? null : user.bio, + bot: user.bot + } + }); }); export default router; diff --git a/api/src/routes/users/@me/channels.ts b/api/src/routes/users/@me/channels.ts index db9f8832..880e09c1 100644 --- a/api/src/routes/users/@me/channels.ts +++ b/api/src/routes/users/@me/channels.ts @@ -1,27 +1,18 @@ import { Router, Request, Response } from "express"; -import { - ChannelModel, - ChannelCreateEvent, - toObject, - ChannelType, - Snowflake, - trimSpecial, - Channel, - DMChannel, - UserModel, - emitEvent -} from "@fosscord/util"; +import { Channel, ChannelCreateEvent, ChannelType, Snowflake, trimSpecial, User, emitEvent } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { DmChannelCreateSchema } from "../../../schema/Channel"; import { check } from "../../../util/instanceOf"; +import { In } from "typeorm"; +import { Recipient } from "../../../../../util/dist/entities/Recipient"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { - var channels = await ChannelModel.find({ recipient_ids: req.user_id }).exec(); + const recipients = await Recipient.find({ where: { id: req.user_id }, relations: ["channel"] }); - res.json(toObject(channels)); + res.json(recipients.map((x) => x.channel)); }); router.post("/", check(DmChannelCreateSchema), async (req: Request, res: Response) => { @@ -29,26 +20,27 @@ router.post("/", check(DmChannelCreateSchema), async (req: Request, res: Respons body.recipients = body.recipients.filter((x) => x !== req.user_id).unique(); - if (!(await Promise.all(body.recipients.map((x) => UserModel.exists({ id: x })))).every((x) => x)) { - throw new HTTPError("Recipient not found"); + const recipients = await User.find({ id: In(body.recipients) }); + + if (recipients.length !== body.recipients.length) { + throw new HTTPError("Recipient/s not found"); } const type = body.recipients.length === 1 ? ChannelType.DM : ChannelType.GROUP_DM; const name = trimSpecial(body.name); - const channel = await new ChannelModel({ + const channel = await new Channel({ name, type, owner_id: req.user_id, - id: Snowflake.generate(), created_at: new Date(), last_message_id: null, - recipient_ids: [...body.recipients, req.user_id] + recipients: [...body.recipients.map((x) => new Recipient({ id: x })), new Recipient({ id: req.user_id })] }).save(); - await emitEvent({ event: "CHANNEL_CREATE", data: toObject(channel), user_id: req.user_id } as ChannelCreateEvent); + await emitEvent({ event: "CHANNEL_CREATE", data: channel, user_id: req.user_id } as ChannelCreateEvent); - res.json(toObject(channel)); + res.json(channel); }); export default router; diff --git a/api/src/routes/users/@me/delete.ts b/api/src/routes/users/@me/delete.ts index f863237d..6bfe0b93 100644 --- a/api/src/routes/users/@me/delete.ts +++ b/api/src/routes/users/@me/delete.ts @@ -1,16 +1,23 @@ import { Router, Request, Response } from "express"; -import { GuildModel, MemberModel, UserModel } from "@fosscord/util"; +import { Guild, Member, User } from "@fosscord/util"; import bcrypt from "bcrypt"; const router = Router(); router.post("/", async (req: Request, res: Response) => { - const user = await UserModel.findOne({ id: req.user_id }).exec(); //User object + const user = await User.findOneOrFail({ id: req.user_id }); //User object + let correctpass = true; + + if (user.data.hash) { + // guest accounts can delete accounts without password + correctpass = await bcrypt.compare(req.body.password, user.data.hash); //Not sure if user typed right password :/ + } + + // TODO: decrement guild member count - let correctpass = await bcrypt.compare(req.body.password, user!.user_data.hash); //Not sure if user typed right password :/ if (correctpass) { await Promise.all([ - UserModel.deleteOne({ id: req.user_id }).exec(), //Yeetus user deletus - MemberModel.deleteMany({ id: req.user_id }).exec() + User.delete({ id: req.user_id }), //Yeetus user deletus + Member.delete({ id: req.user_id }) ]); res.sendStatus(204); diff --git a/api/src/routes/users/@me/disable.ts b/api/src/routes/users/@me/disable.ts index 2d3a9850..7b8a130c 100644 --- a/api/src/routes/users/@me/disable.ts +++ b/api/src/routes/users/@me/disable.ts @@ -1,15 +1,20 @@ -import { UserModel } from "@fosscord/util"; +import { User } from "@fosscord/util"; import { Router, Response, Request } from "express"; import bcrypt from "bcrypt"; const router = Router(); router.post("/", async (req: Request, res: Response) => { - const user = await UserModel.findOne({ id: req.user_id }).exec(); //User object + const user = await User.findOneOrFail({ id: req.user_id }); //User object + let correctpass = true; + + if (user.data.hash) { + // guest accounts can delete accounts without password + correctpass = await bcrypt.compare(req.body.password, user.data.hash); //Not sure if user typed right password :/ + } - let correctpass = await bcrypt.compare(req.body.password, user!.user_data.hash); //Not sure if user typed right password :/ if (correctpass) { - await UserModel.updateOne({ id: req.user_id }, { disabled: true }).exec(); + await User.update({ id: req.user_id }, { disabled: true }); res.sendStatus(204); } else { diff --git a/api/src/routes/users/@me/guilds.ts b/api/src/routes/users/@me/guilds.ts index a9b53b75..fb88281b 100644 --- a/api/src/routes/users/@me/guilds.ts +++ b/api/src/routes/users/@me/guilds.ts @@ -1,34 +1,26 @@ import { Router, Request, Response } from "express"; -import { GuildModel, MemberModel, UserModel, GuildDeleteEvent, GuildMemberRemoveEvent, toObject, emitEvent } from "@fosscord/util"; +import { Guild, Member, User, GuildDeleteEvent, GuildMemberRemoveEvent, emitEvent } from "@fosscord/util"; import { HTTPError } from "lambert-server"; - -import { getPublicUser } from "../../../util/User"; +import { In } from "typeorm"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { - const user = await UserModel.findOne({ id: req.user_id }, { guilds: true }).exec(); - if (!user) throw new HTTPError("User not found", 404); + const members = await Member.find({ relations: ["guild"], where: { id: req.user_id } }); - var guildIDs = user.guilds || []; - var guild = await GuildModel.find({ id: { $in: guildIDs } }) - .populate({ path: "joined_at", match: { id: req.user_id } }) - .exec(); - - res.json(toObject(guild)); + res.json(members.map((x) => x.guild)); }); // user send to leave a certain guild router.delete("/:id", async (req: Request, res: Response) => { const guild_id = req.params.id; - const guild = await GuildModel.findOne({ id: guild_id }, { guild_id: true }).exec(); + const guild = await Guild.findOneOrFail({ where: { id: guild_id }, select: ["owner_id"] }); if (!guild) throw new HTTPError("Guild doesn't exist", 404); if (guild.owner_id === req.user_id) throw new HTTPError("You can't leave your own guild", 400); await Promise.all([ - MemberModel.deleteOne({ id: req.user_id, guild_id: guild_id }).exec(), - UserModel.updateOne({ id: req.user_id }, { $pull: { guilds: guild_id } }).exec(), + Member.delete({ id: req.user_id, guild_id: guild_id }), emitEvent({ event: "GUILD_DELETE", data: { @@ -38,7 +30,7 @@ router.delete("/:id", async (req: Request, res: Response) => { } as GuildDeleteEvent) ]); - const user = await getPublicUser(req.user_id); + const user = await User.getPublicUser(req.user_id); await emitEvent({ event: "GUILD_MEMBER_REMOVE", diff --git a/api/src/routes/users/@me/index.ts b/api/src/routes/users/@me/index.ts index 6ebc6634..d5a5723c 100644 --- a/api/src/routes/users/@me/index.ts +++ b/api/src/routes/users/@me/index.ts @@ -1,6 +1,5 @@ import { Router, Request, Response } from "express"; -import { UserModel, toObject, PublicUserProjection } from "@fosscord/util"; -import { getPublicUser } from "../../../util/User"; +import { User } from "@fosscord/util"; import { UserModifySchema } from "../../../schema/User"; import { check } from "../../../util/instanceOf"; import { handleFile } from "../../../util/cdn"; @@ -8,29 +7,29 @@ import { handleFile } from "../../../util/cdn"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { - res.json(await getPublicUser(req.user_id)); + res.json(await User.getPublicUser(req.user_id)); }); -const UserUpdateProjection = { - accent_color: true, - avatar: true, - banner: true, - bio: true, - bot: true, - discriminator: true, - email: true, - flags: true, - id: true, - locale: true, - mfa_enabled: true, - nsfw_alllowed: true, - phone: true, - public_flags: true, - purchased_flags: true, - // token: true, // this isn't saved in the db and needs to be set manually - username: true, - verified: true -}; +const UserUpdateProjection = [ + "accent_color", + "avatar", + "banner", + "bio", + "bot", + "discriminator", + "email", + "flags", + "id", + "locale", + "mfa_enabled", + "nsfw_alllowed", + "phone", + "public_flags", + "purchased_flags", + // "token", // this isn't saved in the db and needs to be set manually + "username", + "verified" +]; router.patch("/", check(UserModifySchema), async (req: Request, res: Response) => { const body = req.body as UserModifySchema; @@ -38,10 +37,10 @@ router.patch("/", check(UserModifySchema), async (req: Request, res: Response) = if (body.avatar) body.avatar = await handleFile(`/avatars/${req.user_id}`, body.avatar as string); if (body.banner) body.banner = await handleFile(`/banners/${req.user_id}`, body.banner as string); - const user = await UserModel.findOneAndUpdate({ id: req.user_id }, body, { projection: UserUpdateProjection, new: true }).exec(); + const user = await new User({ ...body, id: req.user_id }).save(); // TODO: dispatch user update event - res.json(toObject(user)); + res.json(user); }); export default router; diff --git a/api/src/routes/users/@me/profile.ts b/api/src/routes/users/@me/profile.ts index b67d1964..5ba03c68 100644 --- a/api/src/routes/users/@me/profile.ts +++ b/api/src/routes/users/@me/profile.ts @@ -1,27 +1,27 @@ import { Router, Request, Response } from "express"; -import { getPublicUser } from "../../../util/User"; +import { User } from "../../../../../util/dist"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { - const user = await getPublicUser(req.user_id, { user_data: true }) + const user = await User.getPublicUser(req.user_id, { relations: ["connected_accounts"] }); - res.json({ - connected_accounts: user.user_data.connected_accounts, - premium_guild_since: null, // TODO - premium_since: null, // TODO - user: { - username: user.username, - discriminator: user.discriminator, - id: user.id, - public_flags: user.public_flags, - avatar: user.avatar, - accent_color: user.accent_color, - banner: user.banner, - bio: user.bio, - bot: user.bot, - } - }); + res.json({ + connected_accounts: user.connected_accounts, + premium_guild_since: null, // TODO + premium_since: null, // TODO + user: { + username: user.username, + discriminator: user.discriminator, + id: user.id, + public_flags: user.public_flags, + avatar: user.avatar, + accent_color: user.accent_color, + banner: user.banner, + bio: user.bio, + bot: user.bot + } + }); }); export default router; diff --git a/api/src/routes/users/@me/relationships.ts b/api/src/routes/users/@me/relationships.ts index 642ee5f9..0b864d88 100644 --- a/api/src/routes/users/@me/relationships.ts +++ b/api/src/routes/users/@me/relationships.ts @@ -1,12 +1,11 @@ import { RelationshipAddEvent, - UserModel, + User, PublicUserProjection, - toObject, RelationshipType, RelationshipRemoveEvent, - UserDocument, - emitEvent + emitEvent, + Relationship } from "@fosscord/util"; import { Router, Response, Request } from "express"; import { HTTPError } from "lambert-server"; @@ -15,40 +14,36 @@ import { check, Length } from "../../../util/instanceOf"; const router = Router(); -const userProjection = { "user_data.relationships": true, ...PublicUserProjection }; +const userProjection: (keyof User)[] = ["relationships", ...PublicUserProjection]; router.get("/", async (req: Request, res: Response) => { - const user = await UserModel.findOne({ id: req.user_id }, { user_data: { relationships: true } }) - .populate({ path: "user_data.relationships.id", model: UserModel }) - .exec(); + const user = await User.findOneOrFail({ where: { id: req.user_id }, select: ["relationships"] }); - return res.json(toObject(user.user_data.relationships)); + return res.json(user.relationships); }); -async function addRelationship(req: Request, res: Response, friend: UserDocument, type: RelationshipType) { +async function updateRelationship(req: Request, res: Response, friend: User, type: RelationshipType) { const id = friend.id; if (id === req.user_id) throw new HTTPError("You can't add yourself as a friend"); - const user = await UserModel.findOne({ id: req.user_id }, userProjection).exec(); - const newUserRelationships = [...user.user_data.relationships]; - const newFriendRelationships = [...friend.user_data.relationships]; + const user = await User.findOneOrFail({ id: req.user_id }, { relations: ["relationships"], select: userProjection }); - var relationship = newUserRelationships.find((x) => x.id === id); - const friendRequest = newFriendRelationships.find((x) => x.id === req.user_id); + var relationship = user.relationships.find((x) => x.id === id); + const friendRequest = friend.relationships.find((x) => x.id === req.user_id); if (type === RelationshipType.blocked) { if (relationship) { if (relationship.type === RelationshipType.blocked) throw new HTTPError("You already blocked the user"); relationship.type = RelationshipType.blocked; } else { - relationship = { id, type: RelationshipType.blocked }; - newUserRelationships.push(relationship); + relationship = new Relationship({ id, type: RelationshipType.blocked }); + user.relationships.push(relationship); } if (friendRequest && friendRequest.type !== RelationshipType.blocked) { - newFriendRelationships.remove(friendRequest); + friend.relationships.remove(friendRequest); await Promise.all([ - UserModel.updateOne({ id: friend.id }, { "user_data.relationships": newFriendRelationships }).exec(), + user.save(), emitEvent({ event: "RELATIONSHIP_REMOVE", data: friendRequest, @@ -58,12 +53,12 @@ async function addRelationship(req: Request, res: Response, friend: UserDocument } await Promise.all([ - UserModel.updateOne({ id: req.user_id }, { "user_data.relationships": newUserRelationships }).exec(), + user.save(), emitEvent({ event: "RELATIONSHIP_ADD", data: { - ...toObject(relationship), - user: { ...toObject(friend), user_data: undefined } + ...relationship, + user: { ...friend } }, user_id: req.user_id } as RelationshipAddEvent) @@ -72,41 +67,40 @@ async function addRelationship(req: Request, res: Response, friend: UserDocument return res.sendStatus(204); } - var incoming_relationship = { id: req.user_id, nickname: undefined, type: RelationshipType.incoming }; - var outgoing_relationship = { id, nickname: undefined, type: RelationshipType.outgoing }; + var incoming_relationship = new Relationship({ nickname: undefined, type: RelationshipType.incoming, id: req.user_id }); + var outgoing_relationship = new Relationship({ nickname: undefined, type: RelationshipType.outgoing, id }); if (friendRequest) { if (friendRequest.type === RelationshipType.blocked) throw new HTTPError("The user blocked you"); // accept friend request - // @ts-ignore incoming_relationship = friendRequest; incoming_relationship.type = RelationshipType.friends; outgoing_relationship.type = RelationshipType.friends; - } else newFriendRelationships.push(incoming_relationship); + } else friend.relationships.push(incoming_relationship); if (relationship) { if (relationship.type === RelationshipType.outgoing) throw new HTTPError("You already sent a friend request"); if (relationship.type === RelationshipType.blocked) throw new HTTPError("Unblock the user before sending a friend request"); if (relationship.type === RelationshipType.friends) throw new HTTPError("You are already friends with the user"); - } else newUserRelationships.push(outgoing_relationship); + } else user.relationships.push(outgoing_relationship); await Promise.all([ - UserModel.updateOne({ id: req.user_id }, { "user_data.relationships": newUserRelationships }).exec(), - UserModel.updateOne({ id: friend.id }, { "user_data.relationships": newFriendRelationships }).exec(), + user.save(), + friend.save(), emitEvent({ event: "RELATIONSHIP_ADD", data: { ...outgoing_relationship, - user: { ...toObject(friend), user_data: undefined } + user: { ...friend } }, user_id: req.user_id } as RelationshipAddEvent), emitEvent({ event: "RELATIONSHIP_ADD", data: { - ...toObject(incoming_relationship), + ...incoming_relationship, should_notify: true, - user: { ...toObject(user), user_data: undefined } + user: { ...user } }, user_id: id } as RelationshipAddEvent) @@ -116,14 +110,23 @@ async function addRelationship(req: Request, res: Response, friend: UserDocument } router.put("/:id", check({ $type: new Length(Number, 1, 4) }), async (req: Request, res: Response) => { - return await addRelationship(req, res, await UserModel.findOne({ id: req.params.id }), req.body.type); + return await updateRelationship( + req, + res, + await User.findOneOrFail({ id: req.params.id }, { relations: ["relationships"], select: userProjection }), + req.body.type + ); }); router.post("/", check({ discriminator: String, username: String }), async (req: Request, res: Response) => { - return await addRelationship( + return await updateRelationship( req, res, - await UserModel.findOne(req.body as { discriminator: string; username: string }).exec(), + await User.findOneOrFail({ + relations: ["relationships"], + select: userProjection, + where: req.body as { discriminator: string; username: string } + }), req.body.type ); }); @@ -132,17 +135,15 @@ router.delete("/:id", async (req: Request, res: Response) => { const { id } = req.params; if (id === req.user_id) throw new HTTPError("You can't remove yourself as a friend"); - const user = await UserModel.findOne({ id: req.user_id }).exec(); - if (!user) throw new HTTPError("Invalid token", 400); + const user = await User.findOneOrFail({ id: req.user_id }, { select: userProjection, relations: ["relationships"] }); + const friend = await User.findOneOrFail({ id: id }, { select: userProjection, relations: ["relationships"] }); - const friend = await UserModel.findOne({ id }, userProjection).exec(); - if (!friend) throw new HTTPError("User not found", 404); + const relationship = user.relationships.find((x) => x.id === id); + const friendRequest = friend.relationships.find((x) => x.id === req.user_id); - const relationship = user.user_data.relationships.find((x) => x.id === id); - const friendRequest = friend.user_data.relationships.find((x) => x.id === req.user_id); if (relationship?.type === RelationshipType.blocked) { // unblock user - user.user_data.relationships.remove(relationship); + user.relationships.remove(relationship); await Promise.all([ user.save(), @@ -153,8 +154,8 @@ router.delete("/:id", async (req: Request, res: Response) => { if (!relationship || !friendRequest) throw new HTTPError("You are not friends with the user", 404); if (friendRequest.type === RelationshipType.blocked) throw new HTTPError("The user blocked you"); - user.user_data.relationships.remove(relationship); - friend.user_data.relationships.remove(friendRequest); + user.relationships.remove(relationship); + friend.relationships.remove(friendRequest); await Promise.all([ user.save(), diff --git a/api/src/routes/users/@me/settings.ts b/api/src/routes/users/@me/settings.ts index 5664fc2f..90ee6372 100644 --- a/api/src/routes/users/@me/settings.ts +++ b/api/src/routes/users/@me/settings.ts @@ -1,5 +1,5 @@ import { Router, Response, Request } from "express"; -import { UserModel, UserSettings } from "@fosscord/util"; +import { User, UserSettings } from "@fosscord/util"; import { check } from "../../../util/instanceOf"; import { UserSettingsSchema } from "../../../schema/User"; @@ -8,7 +8,8 @@ const router = Router(); router.patch("/", check(UserSettingsSchema), async (req: Request, res: Response) => { const body = req.body as UserSettings; - await UserModel.updateOne({ id: req.user_id }, body).exec(); + // only users can update user settings + await User.update({ id: req.user_id, bot: false }, { settings: body }); res.sendStatus(204); }); diff --git a/api/src/routes/voice/regions.ts b/api/src/routes/voice/regions.ts new file mode 100644 index 00000000..812aa8f6 --- /dev/null +++ b/api/src/routes/voice/regions.ts @@ -0,0 +1,11 @@ +import { Router, Request, Response } from "express"; +import {getIpAdress} from "../../util/ipAddress"; +import {getVoiceRegions} from "../../util/Voice"; + +const router: Router = Router(); + +router.get("/", async (req: Request, res: Response) => { + res.json(await getVoiceRegions(getIpAdress(req), true))//vip true? +}); + +export default router; diff --git a/api/src/schema/Guild.ts b/api/src/schema/Guild.ts index e5971baf..3e98fe76 100644 --- a/api/src/schema/Guild.ts +++ b/api/src/schema/Guild.ts @@ -1,4 +1,4 @@ -import { ChannelSchema, GuildChannel } from "@fosscord/util"; +import { Channel } from "@fosscord/util"; import { Length } from "../util/instanceOf"; import { ChannelModifySchema } from "./Channel"; @@ -33,7 +33,7 @@ export const GuildUpdateSchema = { $icon: String, $verification_level: Number, $default_message_notifications: Number, - $system_channel_flags: Number, + $system_channel_flags: String, $system_channel_id: String, $explicit_content_filter: Number, $public_updates_channel_id: String, @@ -59,54 +59,6 @@ export interface GuildUpdateSchema extends Omit { preferred_locale?: string; } -export const GuildGetSchema = { - id: true, - name: true, - icon: true, - splash: true, - discovery_splash: true, - owner: true, - owner_id: true, - permissions: true, - region: true, - afk_channel_id: true, - afk_timeout: true, - widget_enabled: true, - widget_channel_id: true, - verification_level: true, - default_message_notifications: true, - explicit_content_filter: true, - roles: true, - emojis: true, - features: true, - mfa_level: true, - application_id: true, - system_channel_id: true, - system_channel_flags: true, - rules_channel_id: true, - joined_at: true, - // large: true, - // unavailable: true, - member_count: true, - // voice_states: true, - // members: true, - // channels: true, - // presences: true, - max_presences: true, - max_members: true, - vanity_url_code: true, - description: true, - banner: true, - premium_tier: true, - premium_subscription_count: true, - preferred_locale: true, - public_updates_channel_id: true, - max_video_channel_users: true, - approximate_member_count: true, - approximate_presence_count: true - // welcome_screen: true, -}; - export const GuildTemplateCreateSchema = { name: String, $avatar: String @@ -117,16 +69,26 @@ export interface GuildTemplateCreateSchema { avatar?: string; } -export const GuildAddChannelToWelcomeScreenSchema = { - channel_id: String, - description: String, - $emoji_id: String, - emoji_name: String +export const GuildUpdateWelcomeScreenSchema = { + $welcome_channels: [ + { + channel_id: String, + description: String, + $emoji_id: String, + emoji_name: String + } + ], + $enabled: Boolean, + $description: new Length(String, 0, 140) }; -export interface GuildAddChannelToWelcomeScreenSchema { - channel_id: string; - description: string; - emoji_id?: string; - emoji_name: string; +export interface GuildUpdateWelcomeScreenSchema { + welcome_channels?: { + channel_id: string; + description: string; + emoji_id?: string; + emoji_name: string; + }[]; + enabled?: boolean; + description?: string; } diff --git a/api/src/schema/Message.ts b/api/src/schema/Message.ts index 2dd54f0c..742542df 100644 --- a/api/src/schema/Message.ts +++ b/api/src/schema/Message.ts @@ -1,11 +1,17 @@ -import { Embed, EmbedImage } from "@fosscord/util"; +import { Embed } from "@fosscord/util"; import { Length } from "../util/instanceOf"; +export const EmbedImage = { + $url: String, + $width: Number, + $height: Number +}; + export const MessageCreateSchema = { $content: new Length(String, 0, 2000), $nonce: String, $tts: Boolean, - $flags: BigInt, + $flags: String, $embed: { $title: new Length(String, 0, 256), //title of embed $type: String, // type of embed (always "rich" for webhook embeds) @@ -63,7 +69,7 @@ export interface MessageCreateSchema { content?: string; nonce?: string; tts?: boolean; - flags?: bigint; + flags?: string; embed?: Embed & { timestamp?: string }; allowed_mentions?: { parse?: string[]; @@ -75,7 +81,7 @@ export interface MessageCreateSchema { message_id: string; channel_id: string; guild_id?: string; - fail_if_not_exists: boolean; + fail_if_not_exists?: boolean; }; payload_json?: string; file?: any; diff --git a/api/src/schema/Roles.ts b/api/src/schema/Roles.ts index f662e61b..1e5f560f 100644 --- a/api/src/schema/Roles.ts +++ b/api/src/schema/Roles.ts @@ -9,7 +9,7 @@ export const RoleModifySchema = { export interface RoleModifySchema { name?: string; - permissions?: BigInt; + permissions?: bigint; color?: number; hoist?: boolean; // whether the role should be displayed separately in the sidebar mentionable?: boolean; // whether the role should be mentionable diff --git a/api/src/test/mongo_test.ts b/api/src/test/mongo_test.ts deleted file mode 100644 index 44b04c5b..00000000 --- a/api/src/test/mongo_test.ts +++ /dev/null @@ -1,40 +0,0 @@ -import mongoose, { Schema, Types } from "mongoose"; -require("mongoose-long")(mongoose); - -const userSchema = new Schema({ - id: String, -}); - -const messageSchema = new Schema({ - id: String, - content: String, -}); -const message = mongoose.model("message", messageSchema, "messages"); -const user = mongoose.model("user", userSchema, "users"); - -messageSchema.virtual("u", { - ref: user, - localField: "id", - foreignField: "id", - justOne: true, -}); - -messageSchema.set("toObject", { virtuals: true }); -messageSchema.set("toJSON", { virtuals: true }); - -async function main() { - const conn = await mongoose.connect("mongodb://localhost:27017/lambert?readPreference=secondaryPreferred", { - useNewUrlParser: true, - useUnifiedTopology: false, - }); - console.log("connected"); - - // const u = await new user({ name: "test" }).save(); - // await new message({ user: u._id, content: "test" }).save(); - - const test = await message.findOne({}).populate("u").exec(); - // @ts-ignore - console.log(test?.toJSON()); -} - -main(); diff --git a/api/src/test/server_benchmark.ts b/api/src/test/server_benchmark.ts deleted file mode 100644 index c582ee89..00000000 --- a/api/src/test/server_benchmark.ts +++ /dev/null @@ -1,39 +0,0 @@ -// @ts-nocheck -import "missing-native-js-functions"; -import { config } from "dotenv"; -config(); -import { DiscordServer } from "../Server"; -import fetch from "node-fetch"; -import { promises } from "fs"; -const count = 100; - -async function main() { - const server = new DiscordServer({ port: 3000 }); - await server.start(); - - const tasks = []; - for (let i = 0; i < count; i++) { - tasks.push(test()); - } - - await Promise.all(tasks); - - console.log("logging in 5secs"); - setTimeout(async () => { - await test(); - - process.exit(); - }, 5000); -} -main(); - -async function test() { - const res = await fetch("http://localhost:3000/api/v8/guilds/813524615463698433/members/813524464300982272", { - headers: { - authorization: - "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjgxMzUyNDQ2NDMwMDk4MjI3MiIsImlhdCI6MTYxNDAyOTc0Nn0.6WQiU4D5HHRi3sliHOQe1hsW-hZTEttvdtZuNIdviNI", - }, - }); - - return await res.text(); -} diff --git a/api/src/test/test.ts b/api/src/test/test.ts deleted file mode 100644 index c37c762b..00000000 --- a/api/src/test/test.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { Snowflake } from "@fosscord/util"; - -console.log(Snowflake.deconstruct("0")); diff --git a/api/src/util/ApiError.ts b/api/src/util/ApiError.ts new file mode 100644 index 00000000..2316cd71 --- /dev/null +++ b/api/src/util/ApiError.ts @@ -0,0 +1,23 @@ +export class ApiError extends Error { + constructor(readonly message: string, public readonly code: number, public readonly httpStatus: number = 400, public readonly defaultParams?: string[]) { + super(message); + } + + withDefaultParams(): ApiError { + if(this.defaultParams) + return new ApiError(applyParamsToString(this.message, this.defaultParams), this.code, this.httpStatus) + return this + } + + withParams(...params: string[]): ApiError { + return new ApiError(applyParamsToString(this.message, params), this.code, this.httpStatus) + } +} + +export function applyParamsToString(s: string, params: string[]): string { + let newString = s + params.forEach(a => { + newString = newString.replace("{}", a) + }) + return newString +} diff --git a/api/src/util/Channel.ts b/api/src/util/Channel.ts deleted file mode 100644 index fb6f9c8c..00000000 --- a/api/src/util/Channel.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { - ChannelCreateEvent, - ChannelModel, - ChannelType, - emitEvent, - getPermission, - GuildModel, - Snowflake, - TextChannel, - toObject, - VoiceChannel -} from "@fosscord/util"; -import { HTTPError } from "lambert-server"; - -// TODO: DM channel -export async function createChannel( - channel: Partial, - user_id: string = "0", - opts?: { - keepId?: boolean; - skipExistsCheck?: boolean; - } -) { - // Always check if user has permission first - const permissions = await getPermission(user_id, channel.guild_id); - permissions.hasThrow("MANAGE_CHANNELS"); - - switch (channel.type) { - case ChannelType.GUILD_TEXT: - case ChannelType.GUILD_VOICE: - if (channel.parent_id && !opts?.skipExistsCheck) { - const exists = await ChannelModel.findOne({ id: channel.parent_id }, { guild_id: true }).exec(); - if (!exists) throw new HTTPError("Parent id channel doesn't exist", 400); - if (exists.guild_id !== channel.guild_id) throw new HTTPError("The category channel needs to be in the guild"); - } - break; - case ChannelType.GUILD_CATEGORY: - break; - case ChannelType.DM: - case ChannelType.GROUP_DM: - throw new HTTPError("You can't create a dm channel in a guild"); - // TODO: check if guild is community server - case ChannelType.GUILD_STORE: - case ChannelType.GUILD_NEWS: - default: - throw new HTTPError("Not yet supported"); - } - - if (!channel.permission_overwrites) channel.permission_overwrites = []; - // TODO: auto generate position - - channel = await new ChannelModel({ - ...channel, - ...(!opts?.keepId && { id: Snowflake.generate() }), - created_at: new Date(), - // @ts-ignore - recipient_ids: null - }).save(); - - await emitEvent({ event: "CHANNEL_CREATE", data: toObject(channel), guild_id: channel.guild_id } as ChannelCreateEvent); - - return channel; -} diff --git a/api/src/util/Constants.ts b/api/src/util/Constants.ts index f3a8dd67..15fdc519 100644 --- a/api/src/util/Constants.ts +++ b/api/src/util/Constants.ts @@ -1,3 +1,5 @@ +import {ApiError} from "./ApiError"; + export const WSCodes = { 1000: "WS_CLOSE_REQUESTED", 4004: "TOKEN_INVALID", @@ -421,6 +423,7 @@ export const VerificationLevels = ["NONE", "LOW", "MEDIUM", "HIGH", "VERY_HIGH"] /** * An error encountered while performing an API request. Here are the potential errors: + * * GENERAL_ERROR * * UNKNOWN_ACCOUNT * * UNKNOWN_APPLICATION * * UNKNOWN_CHANNEL @@ -436,27 +439,70 @@ export const VerificationLevels = ["NONE", "LOW", "MEDIUM", "HIGH", "VERY_HIGH"] * * UNKNOWN_USER * * UNKNOWN_EMOJI * * UNKNOWN_WEBHOOK + * * UNKNOWN_WEBHOOK_SERVICE + * * UNKNOWN_SESSION * * UNKNOWN_BAN + * * UNKNOWN_SKU + * * UNKNOWN_STORE_LISTING + * * UNKNOWN_ENTITLEMENT + * * UNKNOWN_BUILD + * * UNKNOWN_LOBBY + * * UNKNOWN_BRANCH + * * UNKNOWN_STORE_DIRECTORY_LAYOUT + * * UNKNOWN_REDISTRIBUTABLE + * * UNKNOWN_GIFT_CODE + * * UNKNOWN_STREAM + * * UNKNOWN_PREMIUM_SERVER_SUBSCRIBE_COOLDOWN * * UNKNOWN_GUILD_TEMPLATE + * * UNKNOWN_DISCOVERABLE_SERVER_CATEGORY + * * UNKNOWN_STICKER + * * UNKNOWN_INTERACTION + * * UNKNOWN_APPLICATION_COMMAND + * * UNKNOWN_APPLICATION_COMMAND_PERMISSIONS + * * UNKNOWN_STAGE_INSTANCE + * * UNKNOWN_GUILD_MEMBER_VERIFICATION_FORM + * * UNKNOWN_GUILD_WELCOME_SCREEN + * * UNKNOWN_GUILD_SCHEDULED_EVENT + * * UNKNOWN_GUILD_SCHEDULED_EVENT_USER * * BOT_PROHIBITED_ENDPOINT * * BOT_ONLY_ENDPOINT - * * CHANNEL_HIT_WRITE_RATELIMIT + * * EXPLICIT_CONTENT_CANNOT_BE_SENT_TO_RECIPIENT + * * ACTION_NOT_AUTHORIZED_ON_APPLICATION + * * SLOWMODE_RATE_LIMIT + * * ONLY_OWNER + * * ANNOUNCEMENT_RATE_LIMITS + * * CHANNEL_WRITE_RATELIMIT + * * WORDS_NOT_ALLOWED + * * GUILD_PREMIUM_LEVEL_TOO_LOW * * MAXIMUM_GUILDS * * MAXIMUM_FRIENDS * * MAXIMUM_PINS + * * MAXIMUM_NUMBER_OF_RECIPIENTS_REACHED * * MAXIMUM_ROLES * * MAXIMUM_WEBHOOKS + * * MAXIMUM_NUMBER_OF_EMOJIS_REACHED * * MAXIMUM_REACTIONS * * MAXIMUM_CHANNELS * * MAXIMUM_ATTACHMENTS * * MAXIMUM_INVITES + * * MAXIMUM_ANIMATED_EMOJIS + * * MAXIMUM_SERVER_MEMBERS + * * MAXIMUM_SERVER_CATEGORIES * * GUILD_ALREADY_HAS_TEMPLATE + * * MAXIMUM_THREAD_PARTICIPANTS + * * MAXIMUM_BANS_FOR_NON_GUILD_MEMBERS + * * MAXIMUM_BANS_FETCHES + * * MAXIMUM_STICKERS + * * MAXIMUM_PRUNE_REQUESTS * * UNAUTHORIZED * * ACCOUNT_VERIFICATION_REQUIRED + * * OPENING_DIRECT_MESSAGES_TOO_FAST * * REQUEST_ENTITY_TOO_LARGE * * FEATURE_TEMPORARILY_DISABLED * * USER_BANNED + * * TARGET_USER_IS_NOT_CONNECTED_TO_VOICE * * ALREADY_CROSSPOSTED + * * APPLICATION_COMMAND_ALREADY_EXISTS * * MISSING_ACCESS * * INVALID_ACCOUNT_TYPE * * CANNOT_EXECUTE_ON_DM @@ -476,81 +522,196 @@ export const VerificationLevels = ["NONE", "LOW", "MEDIUM", "HIGH", "VERY_HIGH"] * * CANNOT_PIN_MESSAGE_IN_OTHER_CHANNEL * * INVALID_OR_TAKEN_INVITE_CODE * * CANNOT_EXECUTE_ON_SYSTEM_MESSAGE + * * CANNOT_EXECUTE_ON_THIS_CHANNEL_TYPE * * INVALID_OAUTH_TOKEN + * * MISSING_REQUIRED_OAUTH2_SCOPE + * * INVALID_WEBHOOK_TOKEN_PROVIDED + * * INVALID_ROLE + * * INVALID_RECIPIENT * * BULK_DELETE_MESSAGE_TOO_OLD * * INVALID_FORM_BODY * * INVITE_ACCEPTED_TO_GUILD_NOT_CONTAINING_BOT * * INVALID_API_VERSION + * * FILE_EXCEEDS_MAXIMUM_SIZE + * * INVALID_FILE_UPLOADED + * * CANNOT_SELF_REDEEM_GIFT + * * PAYMENT_SOURCE_REQUIRED * * CANNOT_DELETE_COMMUNITY_REQUIRED_CHANNEL + * * INVALID_STICKER_SENT + * * CANNOT_EDIT_ARCHIVED_THREAD + * * INVALID_THREAD_NOTIFICATION_SETTINGS + * * BEFORE_EARLIER_THAN_THREAD_CREATION_DATE + * * SERVER_NOT_AVAILABLE_IN_YOUR_LOCATION + * * SERVER_NEEDS_MONETIZATION_ENABLED + * * TWO_FACTOR_REQUIRED + * * NO_USERS_WITH_DISCORDTAG_EXIST * * REACTION_BLOCKED * * RESOURCE_OVERLOADED + * * STAGE_ALREADY_OPEN + * * THREAD_ALREADY_CREATED_FOR_THIS_MESSAGE + * * THREAD_IS_LOCKED + * * MAXIMUM_NUMBER_OF_ACTIVE_THREADS + * * MAXIMUM_NUMBER_OF_ACTIVE_ANNOUNCEMENT_THREADS + * * INVALID_JSON_FOR_UPLOADED_LOTTIE_FILE + * * LOTTIES_CANNOT_CONTAIN_RASTERIZED_IMAGES + * * STICKER_MAXIMUM_FRAMERATE + * * STICKER_MAXIMUM_FRAME_COUNT + * * LOTTIE_ANIMATION_MAXIMUM_DIMENSIONS + * * STICKER_FRAME_RATE_TOO_SMALL_OR_TOO_LARGE + * * STICKER_ANIMATION_DURATION_MAXIMUM + * * UNKNOWN_VOICE_STATE * @typedef {string} APIError */ -export const APIErrors = { - UNKNOWN_ACCOUNT: 10001, - UNKNOWN_APPLICATION: 10002, - UNKNOWN_CHANNEL: 10003, - UNKNOWN_GUILD: 10004, - UNKNOWN_INTEGRATION: 10005, - UNKNOWN_INVITE: 10006, - UNKNOWN_MEMBER: 10007, - UNKNOWN_MESSAGE: 10008, - UNKNOWN_OVERWRITE: 10009, - UNKNOWN_PROVIDER: 10010, - UNKNOWN_ROLE: 10011, - UNKNOWN_TOKEN: 10012, - UNKNOWN_USER: 10013, - UNKNOWN_EMOJI: 10014, - UNKNOWN_WEBHOOK: 10015, - UNKNOWN_BAN: 10026, - UNKNOWN_GUILD_TEMPLATE: 10057, - BOT_PROHIBITED_ENDPOINT: 20001, - BOT_ONLY_ENDPOINT: 20002, - CHANNEL_HIT_WRITE_RATELIMIT: 20028, - MAXIMUM_GUILDS: 30001, - MAXIMUM_FRIENDS: 30002, - MAXIMUM_PINS: 30003, - MAXIMUM_ROLES: 30005, - MAXIMUM_WEBHOOKS: 30007, - MAXIMUM_REACTIONS: 30010, - MAXIMUM_CHANNELS: 30013, - MAXIMUM_ATTACHMENTS: 30015, - MAXIMUM_INVITES: 30016, - GUILD_ALREADY_HAS_TEMPLATE: 30031, - UNAUTHORIZED: 40001, - ACCOUNT_VERIFICATION_REQUIRED: 40002, - REQUEST_ENTITY_TOO_LARGE: 40005, - FEATURE_TEMPORARILY_DISABLED: 40006, - USER_BANNED: 40007, - ALREADY_CROSSPOSTED: 40033, - MISSING_ACCESS: 50001, - INVALID_ACCOUNT_TYPE: 50002, - CANNOT_EXECUTE_ON_DM: 50003, - EMBED_DISABLED: 50004, - CANNOT_EDIT_MESSAGE_BY_OTHER: 50005, - CANNOT_SEND_EMPTY_MESSAGE: 50006, - CANNOT_MESSAGE_USER: 50007, - CANNOT_SEND_MESSAGES_IN_VOICE_CHANNEL: 50008, - CHANNEL_VERIFICATION_LEVEL_TOO_HIGH: 50009, - OAUTH2_APPLICATION_BOT_ABSENT: 50010, - MAXIMUM_OAUTH2_APPLICATIONS: 50011, - INVALID_OAUTH_STATE: 50012, - MISSING_PERMISSIONS: 50013, - INVALID_AUTHENTICATION_TOKEN: 50014, - NOTE_TOO_LONG: 50015, - INVALID_BULK_DELETE_QUANTITY: 50016, - CANNOT_PIN_MESSAGE_IN_OTHER_CHANNEL: 50019, - INVALID_OR_TAKEN_INVITE_CODE: 50020, - CANNOT_EXECUTE_ON_SYSTEM_MESSAGE: 50021, - INVALID_OAUTH_TOKEN: 50025, - BULK_DELETE_MESSAGE_TOO_OLD: 50034, - INVALID_FORM_BODY: 50035, - INVITE_ACCEPTED_TO_GUILD_NOT_CONTAINING_BOT: 50036, - INVALID_API_VERSION: 50041, - CANNOT_DELETE_COMMUNITY_REQUIRED_CHANNEL: 50074, - REACTION_BLOCKED: 90001, - RESOURCE_OVERLOADED: 130000, -}; +export const DiscordApiErrors = { + //https://discord.com/developers/docs/topics/opcodes-and-status-codes#json-json-error-codes + GENERAL_ERROR: new ApiError("General error (such as a malformed request body, amongst other things)", 0), + UNKNOWN_ACCOUNT: new ApiError("Unknown account", 10001), + UNKNOWN_APPLICATION: new ApiError("Unknown application", 10002), + UNKNOWN_CHANNEL: new ApiError("Unknown channel", 10003), + UNKNOWN_GUILD: new ApiError("Unknown guild", 10004), + UNKNOWN_INTEGRATION: new ApiError("Unknown integration", 10005), + UNKNOWN_INVITE: new ApiError("Unknown invite", 10006), + UNKNOWN_MEMBER: new ApiError("Unknown member", 10007), + UNKNOWN_MESSAGE: new ApiError("Unknown message", 10008), + UNKNOWN_OVERWRITE: new ApiError("Unknown permission overwrite", 10009), + UNKNOWN_PROVIDER: new ApiError("Unknown provider", 10010), + UNKNOWN_ROLE: new ApiError("Unknown role", 10011), + UNKNOWN_TOKEN: new ApiError("Unknown token", 10012), + UNKNOWN_USER: new ApiError("Unknown user", 10013), + UNKNOWN_EMOJI: new ApiError("Unknown emoji", 10014), + UNKNOWN_WEBHOOK: new ApiError("Unknown webhook", 10015), + UNKNOWN_WEBHOOK_SERVICE: new ApiError("Unknown webhook service", 10016), + UNKNOWN_SESSION: new ApiError("Unknown session", 10020), + UNKNOWN_BAN: new ApiError("Unknown ban", 10026), + UNKNOWN_SKU: new ApiError("Unknown SKU", 10027), + UNKNOWN_STORE_LISTING: new ApiError("Unknown Store Listing", 10028), + UNKNOWN_ENTITLEMENT: new ApiError("Unknown entitlement", 10029), + UNKNOWN_BUILD: new ApiError("Unknown build", 10030), + UNKNOWN_LOBBY: new ApiError("Unknown lobby", 10031), + UNKNOWN_BRANCH: new ApiError("Unknown branch", 10032), + UNKNOWN_STORE_DIRECTORY_LAYOUT: new ApiError("Unknown store directory layout", 10033), + UNKNOWN_REDISTRIBUTABLE: new ApiError("Unknown redistributable", 10036), + UNKNOWN_GIFT_CODE: new ApiError("Unknown gift code", 10038), + UNKNOWN_STREAM: new ApiError("Unknown stream", 10049), + UNKNOWN_PREMIUM_SERVER_SUBSCRIBE_COOLDOWN: new ApiError("Unknown premium server subscribe cooldown", 10050), + UNKNOWN_GUILD_TEMPLATE: new ApiError("Unknown guild template", 10057), + UNKNOWN_DISCOVERABLE_SERVER_CATEGORY: new ApiError("Unknown discoverable server category", 10059), + UNKNOWN_STICKER: new ApiError("Unknown sticker", 10060), + UNKNOWN_INTERACTION: new ApiError("Unknown interaction", 10062), + UNKNOWN_APPLICATION_COMMAND: new ApiError("Unknown application command", 10063), + UNKNOWN_APPLICATION_COMMAND_PERMISSIONS: new ApiError("Unknown application command permissions", 10066), + UNKNOWN_STAGE_INSTANCE: new ApiError("Unknown Stage Instance", 10067), + UNKNOWN_GUILD_MEMBER_VERIFICATION_FORM: new ApiError("Unknown Guild Member Verification Form", 10068), + UNKNOWN_GUILD_WELCOME_SCREEN: new ApiError("Unknown Guild Welcome Screen", 10069), + UNKNOWN_GUILD_SCHEDULED_EVENT: new ApiError("Unknown Guild Scheduled Event", 10070), + UNKNOWN_GUILD_SCHEDULED_EVENT_USER: new ApiError("Unknown Guild Scheduled Event User", 10071), + BOT_PROHIBITED_ENDPOINT: new ApiError("Bots cannot use this endpoint", 20001), + BOT_ONLY_ENDPOINT: new ApiError("Only bots can use this endpoint", 20002), + EXPLICIT_CONTENT_CANNOT_BE_SENT_TO_RECIPIENT: new ApiError("Explicit content cannot be sent to the desired recipient(s)", 20009), + ACTION_NOT_AUTHORIZED_ON_APPLICATION: new ApiError("You are not authorized to perform this action on this application", 20012), + SLOWMODE_RATE_LIMIT: new ApiError("This action cannot be performed due to slowmode rate limit", 20016), + ONLY_OWNER: new ApiError("Only the owner of this account can perform this action", 20018), + ANNOUNCEMENT_RATE_LIMITS: new ApiError("This message cannot be edited due to announcement rate limits", 20022), + CHANNEL_WRITE_RATELIMIT: new ApiError("The channel you are writing has hit the write rate limit", 20028), + WORDS_NOT_ALLOWED: new ApiError("Your Stage topic, server name, server description, or channel names contain words that are not allowed", 20031), + GUILD_PREMIUM_LEVEL_TOO_LOW: new ApiError("Guild premium subscription level too low", 20035), + MAXIMUM_GUILDS: new ApiError("Maximum number of guilds reached ({})", 30001, undefined, ["100"]), + MAXIMUM_FRIENDS: new ApiError("Maximum number of friends reached ({})", 30002, undefined, ["1000"]), + MAXIMUM_PINS: new ApiError("Maximum number of pins reached for the channel ({})", 30003, undefined, ["50"]), + MAXIMUM_NUMBER_OF_RECIPIENTS_REACHED: new ApiError("Maximum number of recipients reached ({})", 30004, undefined, ["10"]), + MAXIMUM_ROLES: new ApiError("Maximum number of guild roles reached ({})", 30005, undefined, ["250"]), + MAXIMUM_WEBHOOKS: new ApiError("Maximum number of webhooks reached ({})", 30007, undefined, ["10"]), + MAXIMUM_NUMBER_OF_EMOJIS_REACHED: new ApiError("Maximum number of emojis reached", 30008), + MAXIMUM_REACTIONS: new ApiError("Maximum number of reactions reached ({})", 30010, undefined, ["20"]), + MAXIMUM_CHANNELS: new ApiError("Maximum number of guild channels reached ({})", 30013, undefined, ["500"]), + MAXIMUM_ATTACHMENTS: new ApiError("Maximum number of attachments in a message reached ({})", 30015, undefined, ["10"]), + MAXIMUM_INVITES: new ApiError("Maximum number of invites reached ({})", 30016, undefined, ["1000"]), + MAXIMUM_ANIMATED_EMOJIS: new ApiError("Maximum number of animated emojis reached", 30018), + MAXIMUM_SERVER_MEMBERS: new ApiError("Maximum number of server members reached", 30019), + MAXIMUM_SERVER_CATEGORIES: new ApiError("Maximum number of server categories has been reached ({})", 30030, undefined, ["5"]), + GUILD_ALREADY_HAS_TEMPLATE: new ApiError("Guild already has a template", 30031), + MAXIMUM_THREAD_PARTICIPANTS: new ApiError("Max number of thread participants has been reached", 30033), + MAXIMUM_BANS_FOR_NON_GUILD_MEMBERS: new ApiError("Maximum number of bans for non-guild members have been exceeded", 30035), + MAXIMUM_BANS_FETCHES: new ApiError("Maximum number of bans fetches has been reached", 30037), + MAXIMUM_STICKERS: new ApiError("Maximum number of stickers reached", 30039), + MAXIMUM_PRUNE_REQUESTS: new ApiError("Maximum number of prune requests has been reached. Try again later", 30040), + UNAUTHORIZED: new ApiError("Unauthorized. Provide a valid token and try again", 40001), + ACCOUNT_VERIFICATION_REQUIRED: new ApiError("You need to verify your account in order to perform this action", 40002), + OPENING_DIRECT_MESSAGES_TOO_FAST: new ApiError("You are opening direct messages too fast", 40003), + REQUEST_ENTITY_TOO_LARGE: new ApiError("Request entity too large. Try sending something smaller in size", 40005), + FEATURE_TEMPORARILY_DISABLED: new ApiError("This feature has been temporarily disabled server-side", 40006), + USER_BANNED: new ApiError("The user is banned from this guild", 40007), + TARGET_USER_IS_NOT_CONNECTED_TO_VOICE: new ApiError("Target user is not connected to voice", 40032), + ALREADY_CROSSPOSTED: new ApiError("This message has already been crossposted", 40033), + APPLICATION_COMMAND_ALREADY_EXISTS: new ApiError("An application command with that name already exists", 40041), + MISSING_ACCESS: new ApiError("Missing access", 50001), + INVALID_ACCOUNT_TYPE: new ApiError("Invalid account type", 50002), + CANNOT_EXECUTE_ON_DM: new ApiError("Cannot execute action on a DM channel", 50003), + EMBED_DISABLED: new ApiError("Guild widget disabled", 50004), + CANNOT_EDIT_MESSAGE_BY_OTHER: new ApiError("Cannot edit a message authored by another user", 50005), + CANNOT_SEND_EMPTY_MESSAGE: new ApiError("Cannot send an empty message", 50006), + CANNOT_MESSAGE_USER: new ApiError("Cannot send messages to this user", 50007), + CANNOT_SEND_MESSAGES_IN_VOICE_CHANNEL: new ApiError("Cannot send messages in a voice channel", 50008), + CHANNEL_VERIFICATION_LEVEL_TOO_HIGH: new ApiError("Channel verification level is too high for you to gain access", 50009), + OAUTH2_APPLICATION_BOT_ABSENT: new ApiError("OAuth2 application does not have a bot", 50010), + MAXIMUM_OAUTH2_APPLICATIONS: new ApiError("OAuth2 application limit reached", 50011), + INVALID_OAUTH_STATE: new ApiError("Invalid OAuth2 state", 50012), + MISSING_PERMISSIONS: new ApiError("You lack permissions to perform that action", 50013), + INVALID_AUTHENTICATION_TOKEN: new ApiError("Invalid authentication token provided", 50014), + NOTE_TOO_LONG: new ApiError("Note was too long", 50015), + INVALID_BULK_DELETE_QUANTITY: new ApiError("Provided too few or too many messages to delete. Must provide at least {} and fewer than {} messages to delete", 50016, undefined, ["2","100"]), + CANNOT_PIN_MESSAGE_IN_OTHER_CHANNEL: new ApiError("A message can only be pinned to the channel it was sent in", 50019), + INVALID_OR_TAKEN_INVITE_CODE: new ApiError("Invite code was either invalid or taken", 50020), + CANNOT_EXECUTE_ON_SYSTEM_MESSAGE: new ApiError("Cannot execute action on a system message", 50021), + CANNOT_EXECUTE_ON_THIS_CHANNEL_TYPE: new ApiError("Cannot execute action on this channel type", 50024), + INVALID_OAUTH_TOKEN: new ApiError("Invalid OAuth2 access token provided", 50025), + MISSING_REQUIRED_OAUTH2_SCOPE: new ApiError("Missing required OAuth2 scope", 50026), + INVALID_WEBHOOK_TOKEN_PROVIDED: new ApiError("Invalid webhook token provided", 50027), + INVALID_ROLE: new ApiError("Invalid role", 50028), + INVALID_RECIPIENT: new ApiError("Invalid Recipient(s)", 50033), + BULK_DELETE_MESSAGE_TOO_OLD: new ApiError("A message provided was too old to bulk delete", 50034), + INVALID_FORM_BODY: new ApiError("Invalid form body (returned for both application/json and multipart/form-data bodies), or invalid Content-Type provided", 50035), + INVITE_ACCEPTED_TO_GUILD_NOT_CONTAINING_BOT: new ApiError("An invite was accepted to a guild the application's bot is not in", 50036), + INVALID_API_VERSION: new ApiError("Invalid API version provided", 50041), + FILE_EXCEEDS_MAXIMUM_SIZE: new ApiError("File uploaded exceeds the maximum size", 50045), + INVALID_FILE_UPLOADED: new ApiError("Invalid file uploaded", 50046), + CANNOT_SELF_REDEEM_GIFT: new ApiError("Cannot self-redeem this gift", 50054), + PAYMENT_SOURCE_REQUIRED: new ApiError("Payment source required to redeem gift", 50070), + CANNOT_DELETE_COMMUNITY_REQUIRED_CHANNEL: new ApiError("Cannot delete a channel required for Community guilds", 50074), + INVALID_STICKER_SENT: new ApiError("Invalid sticker sent", 50081), + CANNOT_EDIT_ARCHIVED_THREAD: new ApiError("Tried to perform an operation on an archived thread, such as editing a message or adding a user to the thread", 50083), + INVALID_THREAD_NOTIFICATION_SETTINGS: new ApiError("Invalid thread notification settings", 50084), + BEFORE_EARLIER_THAN_THREAD_CREATION_DATE: new ApiError("before value is earlier than the thread creation date", 50085), + SERVER_NOT_AVAILABLE_IN_YOUR_LOCATION: new ApiError("This server is not available in your location", 50095), + SERVER_NEEDS_MONETIZATION_ENABLED: new ApiError("This server needs monetization enabled in order to perform this action", 50097), + TWO_FACTOR_REQUIRED: new ApiError("Two factor is required for this operation", 60003), + NO_USERS_WITH_DISCORDTAG_EXIST: new ApiError("No users with DiscordTag exist", 80004), + REACTION_BLOCKED: new ApiError("Reaction was blocked", 90001), + RESOURCE_OVERLOADED: new ApiError("API resource is currently overloaded. Try again a little later", 130000), + STAGE_ALREADY_OPEN: new ApiError("The Stage is already open", 150006), + THREAD_ALREADY_CREATED_FOR_THIS_MESSAGE: new ApiError("A thread has already been created for this message", 160004), + THREAD_IS_LOCKED: new ApiError("Thread is locked", 160005), + MAXIMUM_NUMBER_OF_ACTIVE_THREADS: new ApiError("Maximum number of active threads reached", 160006), + MAXIMUM_NUMBER_OF_ACTIVE_ANNOUNCEMENT_THREADS: new ApiError("Maximum number of active announcement threads reached", 160007), + INVALID_JSON_FOR_UPLOADED_LOTTIE_FILE: new ApiError("Invalid JSON for uploaded Lottie file", 170001), + LOTTIES_CANNOT_CONTAIN_RASTERIZED_IMAGES: new ApiError("Uploaded Lotties cannot contain rasterized images such as PNG or JPEG", 170002), + STICKER_MAXIMUM_FRAMERATE: new ApiError("Sticker maximum framerate exceeded", 170003), + STICKER_MAXIMUM_FRAME_COUNT: new ApiError("Sticker frame count exceeds maximum of {} frames", 170004, undefined, ["1000"]), + LOTTIE_ANIMATION_MAXIMUM_DIMENSIONS: new ApiError("Lottie animation maximum dimensions exceeded", 170005), + STICKER_FRAME_RATE_TOO_SMALL_OR_TOO_LARGE: new ApiError("Sticker frame rate is either too small or too large", 170006), + STICKER_ANIMATION_DURATION_MAXIMUM: new ApiError("Sticker animation duration exceeds maximum of {} seconds", 170007, undefined, ["5"]), + + + //Other errors + UNKNOWN_VOICE_STATE: new ApiError("Unknown Voice State", 10065, 404), +} + +/** + * An error encountered while performing an API request (Fosscord only). Here are the potential errors: + */ +export const FosscordApiErrors = { + +} /** * The value set for a guild's default message notifications, e.g. `ALL`. Here are the available types: diff --git a/api/src/util/Member.ts b/api/src/util/Member.ts deleted file mode 100644 index da02735c..00000000 --- a/api/src/util/Member.ts +++ /dev/null @@ -1,222 +0,0 @@ -import { - Guild, - GuildCreateEvent, - GuildDeleteEvent, - GuildMemberAddEvent, - GuildMemberRemoveEvent, - GuildMemberUpdateEvent, - GuildModel, - MemberModel, - RoleModel, - toObject, - UserModel, - GuildDocument, - Config, - emitEvent -} from "@fosscord/util"; - -import { HTTPError } from "lambert-server"; - -import { getPublicUser } from "./User"; - -export const PublicMemberProjection = { - id: true, - guild_id: true, - nick: true, - roles: true, - joined_at: true, - pending: true, - deaf: true, - mute: true, - premium_since: true -}; - -export async function isMember(user_id: string, guild_id: string) { - const exists = await MemberModel.exists({ id: user_id, guild_id }); - if (!exists) throw new HTTPError("You are not a member of this guild", 403); - return exists; -} - -export async function addMember(user_id: string, guild_id: string, cache?: { guild?: GuildDocument }) { - const user = await getPublicUser(user_id, { guilds: true }); - - const { maxGuilds } = Config.get().limits.user; - if (user.guilds.length >= maxGuilds) { - throw new HTTPError(`You are at the ${maxGuilds} server limit.`, 403); - } - - const guild = cache?.guild || (await GuildModel.findOne({ id: guild_id }).exec()); - - if (!guild) throw new HTTPError("Guild not found", 404); - - if (await MemberModel.exists({ id: user.id, guild_id })) throw new HTTPError("You are already a member of this guild", 400); - - const member = { - id: user_id, - guild_id: guild_id, - nick: undefined, - roles: [guild_id], // @everyone role - joined_at: new Date(), - premium_since: undefined, - deaf: false, - mute: false, - pending: false - }; - - await Promise.all([ - new MemberModel({ - ...member, - read_state: {}, - settings: { - channel_overrides: [], - message_notifications: 0, - mobile_push: true, - mute_config: null, - muted: false, - suppress_everyone: false, - suppress_roles: false, - version: 0 - } - }).save(), - - UserModel.updateOne({ id: user_id }, { $push: { guilds: guild_id } }).exec(), - GuildModel.updateOne({ id: guild_id }, { $inc: { member_count: 1 } }).exec(), - - emitEvent({ - event: "GUILD_MEMBER_ADD", - data: { - ...member, - user, - guild_id: guild_id - }, - guild_id: guild_id - } as GuildMemberAddEvent) - ]); - - await emitEvent({ - event: "GUILD_CREATE", - data: toObject( - await guild - .populate({ path: "members", match: { guild_id } }) - .populate({ path: "joined_at", match: { id: user.id } }) - .execPopulate() - ), - user_id - } as GuildCreateEvent); -} - -export async function removeMember(user_id: string, guild_id: string) { - const user = await getPublicUser(user_id); - - const guild = await GuildModel.findOne({ id: guild_id }, { owner_id: true }).exec(); - if (!guild) throw new HTTPError("Guild not found", 404); - if (guild.owner_id === user_id) throw new Error("The owner cannot be removed of the guild"); - if (!(await MemberModel.exists({ id: user.id, guild_id }))) throw new HTTPError("Is not member of this guild", 404); - - // use promise all to execute all promises at the same time -> save time - return Promise.all([ - MemberModel.deleteOne({ - id: user_id, - guild_id: guild_id - }).exec(), - UserModel.updateOne({ id: user.id }, { $pull: { guilds: guild_id } }).exec(), - GuildModel.updateOne({ id: guild_id }, { $inc: { member_count: -1 } }).exec(), - - emitEvent({ - event: "GUILD_DELETE", - data: { - id: guild_id - }, - user_id: user_id - } as GuildDeleteEvent), - emitEvent({ - event: "GUILD_MEMBER_REMOVE", - data: { - guild_id: guild_id, - user: user - }, - guild_id: guild_id - } as GuildMemberRemoveEvent) - ]); -} - -export async function addRole(user_id: string, guild_id: string, role_id: string) { - const user = await getPublicUser(user_id); - - const role = await RoleModel.findOne({ id: role_id, guild_id: guild_id }).exec(); - if (!role) throw new HTTPError("role not found", 404); - - var memberObj = await MemberModel.findOneAndUpdate( - { - id: user_id, - guild_id: guild_id - }, - { $push: { roles: role_id } }, - { new: true } - ).exec(); - - if (!memberObj) throw new HTTPError("Member not found", 404); - - await emitEvent({ - event: "GUILD_MEMBER_UPDATE", - data: { - guild_id: guild_id, - user: user, - roles: memberObj.roles - }, - guild_id: guild_id - } as GuildMemberUpdateEvent); -} - -export async function removeRole(user_id: string, guild_id: string, role_id: string) { - const user = await getPublicUser(user_id); - - const role = await RoleModel.findOne({ id: role_id, guild_id: guild_id }).exec(); - if (!role) throw new HTTPError("role not found", 404); - - var memberObj = await MemberModel.findOneAndUpdate( - { - id: user_id, - guild_id: guild_id - }, - { $pull: { roles: role_id } }, - { new: true } - ).exec(); - - if (!memberObj) throw new HTTPError("Member not found", 404); - - await emitEvent({ - event: "GUILD_MEMBER_UPDATE", - data: { - guild_id: guild_id, - user: user, - roles: memberObj.roles - }, - guild_id: guild_id - } as GuildMemberUpdateEvent); -} - -export async function changeNickname(user_id: string, guild_id: string, nickname: string) { - const user = await getPublicUser(user_id); - - var memberObj = await MemberModel.findOneAndUpdate( - { - id: user_id, - guild_id: guild_id - }, - { nick: nickname }, - { new: true } - ).exec(); - - if (!memberObj) throw new HTTPError("Member not found", 404); - - await emitEvent({ - event: "GUILD_MEMBER_UPDATE", - data: { - guild_id: guild_id, - user: user, - nick: nickname - }, - guild_id: guild_id - } as GuildMemberUpdateEvent); -} diff --git a/api/src/util/Message.ts b/api/src/util/Message.ts index 8a1e959e..fea553bc 100644 --- a/api/src/util/Message.ts +++ b/api/src/util/Message.ts @@ -1,5 +1,5 @@ import { - ChannelModel, + Channel, Embed, emitEvent, Message, @@ -7,21 +7,23 @@ import { MessageUpdateEvent, getPermission, CHANNEL_MENTION, - toObject, - MessageModel, Snowflake, - PublicMemberProjection, USER_MENTION, ROLE_MENTION, - RoleModel, + Role, EVERYONE_MENTION, - HERE_MENTION + HERE_MENTION, + MessageType, + User, + Application, + Webhook, + Attachment } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import fetch from "node-fetch"; import cheerio from "cheerio"; +import { MessageCreateSchema } from "../schema/Message"; -import { MessageType } from "@fosscord/util/dist/util/Constants"; // TODO: check webhook, application, system author const LINK_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g; @@ -37,19 +39,37 @@ const DEFAULT_FETCH_OPTIONS: any = { method: "GET" }; -export async function handleMessage(opts: Partial) { - const channel = await ChannelModel.findOne( - { id: opts.channel_id }, - { guild_id: true, type: true, permission_overwrites: true, recipient_ids: true, owner_id: true } - ) - .lean() // lean is needed, because we don't want to populate .recipients that also auto deletes .recipient_ids - .exec(); +export async function handleMessage(opts: MessageOptions): Promise { + const channel = await Channel.findOneOrFail({ where: { id: opts.channel_id }, relations: ["recipients"] }); if (!channel || !opts.channel_id) throw new HTTPError("Channel not found", 404); - // TODO: are tts messages allowed in dm channels? should permission be checked? - // @ts-ignore - const permission = await getPermission(opts.author_id, channel.guild_id, opts.channel_id, { channel }); + const message = new Message({ + ...opts, + guild_id: channel.guild_id, + channel_id: opts.channel_id, + attachments: opts.attachments || [], + embeds: opts.embeds || [], + reactions: /*opts.reactions ||*/ [], + type: opts.type ?? 0 + }); + + // TODO: are tts messages allowed in dm channels? should permission be checked? + if (opts.author_id) { + message.author = await User.getPublicUser(opts.author_id); + } + if (opts.application_id) { + message.application = await Application.findOneOrFail({ id: opts.application_id }); + } + if (opts.webhook_id) { + message.webhook = await Webhook.findOneOrFail({ id: opts.webhook_id }); + } + + const permission = await getPermission(opts.author_id, channel.guild_id, opts.channel_id); permission.hasThrow("SEND_MESSAGES"); + if (permission.cache.member) { + message.member = permission.cache.member; + } + if (opts.tts) permission.hasThrow("SEND_TTS_MESSAGES"); if (opts.message_reference) { permission.hasThrow("READ_MESSAGE_HISTORY"); @@ -57,24 +77,24 @@ export async function handleMessage(opts: Partial) { if (opts.message_reference.channel_id !== opts.channel_id) throw new HTTPError("You can only reference messages from this channel"); // TODO: should be checked if the referenced message exists? // @ts-ignore - opts.type = MessageType.REPLY; + message.type = MessageType.REPLY; } - if (!opts.content && !opts.embeds?.length && !opts.attachments?.length && !opts.stickers?.length && !opts.activity) { + // TODO: stickers/activity + if (!opts.content && !opts.embeds?.length && !opts.attachments?.length) { throw new HTTPError("Empty messages are not allowed", 50006); } var content = opts.content; - var mention_channels_ids = [] as string[]; + var mention_channel_ids = [] as string[]; var mention_role_ids = [] as string[]; var mention_user_ids = [] as string[]; var mention_everyone = false; - var mention_everyone = false; if (content) { - content = content.trim(); + message.content = content.trim(); for (const [_, mention] of content.matchAll(CHANNEL_MENTION)) { - if (!mention_channels_ids.includes(mention)) mention_channels_ids.push(mention); + if (!mention_channel_ids.includes(mention)) mention_channel_ids.push(mention); } for (const [_, mention] of content.matchAll(USER_MENTION)) { @@ -83,7 +103,7 @@ export async function handleMessage(opts: Partial) { await Promise.all( Array.from(content.matchAll(ROLE_MENTION)).map(async ([_, mention]) => { - const role = await RoleModel.findOne({ id: mention, guild_id: channel.guild_id }).exec(); + const role = await Role.findOneOrFail({ id: mention, guild_id: channel.guild_id }); if (role.mentionable || permission.has("MANAGE_ROLES")) { mention_role_ids.push(mention); } @@ -95,20 +115,14 @@ export async function handleMessage(opts: Partial) { } } + message.mention_channels = mention_channel_ids.map((x) => new Channel({ id: x })); + message.mention_roles = mention_role_ids.map((x) => new Role({ id: x })); + message.mentions = mention_user_ids.map((x) => new User({ id: x })); + message.mention_everyone = mention_everyone; + // TODO: check and put it all in the body - return { - ...opts, - guild_id: channel.guild_id, - channel_id: opts.channel_id, - mention_channels_ids, - mention_role_ids, - mention_user_ids, - mention_everyone, - attachments: opts.attachments || [], - embeds: opts.embeds || [], - reactions: opts.reactions || [], - type: opts.type ?? 0 - }; + + return message; } // TODO: cache link result in db @@ -160,20 +174,33 @@ export async function postHandleMessage(message: Message) { channel_id: message.channel_id, data } as MessageUpdateEvent), - MessageModel.updateOne({ id: message.id, channel_id: message.channel_id }, data).exec() + Message.update({ id: message.id, channel_id: message.channel_id }, data) ]); } -export async function sendMessage(opts: Partial) { - const message = await handleMessage({ ...opts, id: Snowflake.generate(), timestamp: new Date() }); +export async function sendMessage(opts: MessageOptions) { + const message = await handleMessage({ ...opts, timestamp: new Date() }); - const data = toObject( - await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).populate("referenced_message").save() - ); + await Promise.all([ + message.save(), + emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data: message.toJSON() } as MessageCreateEvent) + ]); - await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data } as MessageCreateEvent); + postHandleMessage(message).catch((e) => {}); // no await as it shouldnt block the message send function and silently catch error - postHandleMessage(data).catch((e) => {}); // no await as it shouldnt block the message send function and silently catch error - - return data; + return message; +} + +interface MessageOptions extends MessageCreateSchema { + id?: string; + type?: MessageType; + pinned?: boolean; + author_id?: string; + webhook_id?: string; + application_id?: string; + embeds?: Embed[]; + channel_id?: string; + attachments?: Attachment[]; + edited_timestamp?: Date; + timestamp?: Date; } diff --git a/api/src/util/User.ts b/api/src/util/User.ts deleted file mode 100644 index 392c7101..00000000 --- a/api/src/util/User.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { toObject, UserModel, PublicUserProjection } from "@fosscord/util"; -import { HTTPError } from "lambert-server"; - -export { PublicUserProjection }; - -export async function getPublicUser(user_id: string, additional_fields?: any) { - const user = await UserModel.findOne( - { id: user_id }, - { - ...PublicUserProjection, - ...additional_fields - } - ).exec(); - if (!user) throw new HTTPError("User not found", 404); - return toObject(user); -} diff --git a/api/src/util/Voice.ts b/api/src/util/Voice.ts new file mode 100644 index 00000000..087bdfa8 --- /dev/null +++ b/api/src/util/Voice.ts @@ -0,0 +1,32 @@ +import {Config} from "@fosscord/util"; +import {distanceBetweenLocations, IPAnalysis} from "./ipAddress"; + +export async function getVoiceRegions(ipAddress: string, vip: boolean) { + const regions = Config.get().regions; + const availableRegions = regions.available.filter(ar => vip ? true : !ar.vip); + let optimalId = regions.default + + if(!regions.useDefaultAsOptimal) { + const clientIpAnalysis = await IPAnalysis(ipAddress) + + let min = Number.POSITIVE_INFINITY + + for (let ar of availableRegions) { + //TODO the endpoint location should be saved in the database if not already present to prevent IPAnalysis call + const dist = distanceBetweenLocations(clientIpAnalysis, ar.location || (await IPAnalysis(ar.endpoint))) + + if(dist < min) { + min = dist + optimalId = ar.id + } + } + } + + return availableRegions.map(ar => ({ + id: ar.id, + name: ar.name, + custom: ar.custom, + deprecated: ar.deprecated, + optimal: ar.id === optimalId + })) +} \ No newline at end of file diff --git a/api/src/util/ipAddress.ts b/api/src/util/ipAddress.ts index 0a724daa..c6239426 100644 --- a/api/src/util/ipAddress.ts +++ b/api/src/util/ipAddress.ts @@ -60,6 +60,7 @@ const exampleData = { status: 200 }; +//TODO add function that support both ip and domain names export async function IPAnalysis(ip: string): Promise { const { ipdataApiKey } = Config.get().security; if (!ipdataApiKey) return { ...exampleData, ip }; @@ -79,3 +80,19 @@ export function getIpAdress(req: Request): string { // @ts-ignore return req.headers[Config.get().security.forwadedFor] || req.socket.remoteAddress; } + + +export function distanceBetweenLocations(loc1: any, loc2: any): number { + return distanceBetweenCoords(loc1.latitude, loc1.longitude, loc2.latitude, loc2.longitude); +} + +//Haversine function +function distanceBetweenCoords(lat1: number, lon1: number, lat2: number, lon2: number) { + const p = 0.017453292519943295; // Math.PI / 180 + const c = Math.cos; + const a = 0.5 - c((lat2 - lat1) * p) / 2 + + c(lat1 * p) * c(lat2 * p) * + (1 - c((lon2 - lon1) * p)) / 2; + + return 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km +} \ No newline at end of file diff --git a/api/tests/routes/auth/login.test.js.disabled b/api/tests/routes/auth/login.test.js.disabled new file mode 100644 index 00000000..f677cebd --- /dev/null +++ b/api/tests/routes/auth/login.test.js.disabled @@ -0,0 +1,2 @@ +const supertest = require("supertest"); +const request = supertest("http://localhost:1870"); diff --git a/api/tests/routes/auth/register.test.js b/api/tests/routes/auth/register.test.js new file mode 100644 index 00000000..f42f004a --- /dev/null +++ b/api/tests/routes/auth/register.test.js @@ -0,0 +1,25 @@ +const supertest = require("supertest"); +const request = supertest("http://localhost:3001"); + +describe("/api/register", () => { + describe("POST", () => { + test("without body", async () => { + const response = await request.post("/api/auth/register").send({}); + expect(response.statusCode).toBe(400); + }); + test("with body", async () => { + const response = await request.post("/api/auth/register").send({ + fingerprint: "805826570869932034.wR8vi8lGlFBJerErO9LG5NViJFw", + email: "qo8etzvaf@gmail.com", + username: "qp39gr98", + password: "wtp9gep9gw", + invite: null, + consent: true, + date_of_birth: "2000-04-04", + gift_code_sku_id: null, + captcha_key: null + }); + expect(response.statusCode).toBe(200); + }); + }); +}); diff --git a/api/tests/routes/ping.test.js b/api/tests/routes/ping.test.js new file mode 100644 index 00000000..6fa4b160 --- /dev/null +++ b/api/tests/routes/ping.test.js @@ -0,0 +1,12 @@ +const supertest = require("supertest"); +const request = supertest("http://localhost:3001"); + +describe("/ping", () => { + describe("GET", () => { + test("should return 200 and pong", async () => { + let response = await request.get("/api/ping"); + expect(response.text).toBe("pong"); + expect(response.statusCode).toBe(200); + }); + }); +}); diff --git a/bundle/database.db b/bundle/database.db new file mode 100644 index 00000000..2d4abd49 Binary files /dev/null and b/bundle/database.db differ diff --git a/bundle/package-lock.json b/bundle/package-lock.json index 379b1c18..59fd96ee 100644 Binary files a/bundle/package-lock.json and b/bundle/package-lock.json differ diff --git a/bundle/package.json b/bundle/package.json index 38377d15..63945135 100644 --- a/bundle/package.json +++ b/bundle/package.json @@ -5,8 +5,9 @@ "main": "src/start.js", "scripts": { "preinstall": "cd ../util && npm i && cd ../api && npm i && cd ../cdn && npm i && cd ../gateway && npm i", - "build": "npm run build:api && npm run build:cdn && npm run build:gateway && npm run build:bundle", + "build": "npm run build:util && npm run build:api && npm run build:cdn && npm run build:gateway && npm run build:bundle", "build:bundle": "npx tsc -b .", + "build:util": "cd ../util/ && npm run build", "build:api": "cd ../api/ && npm run build", "build:cdn": "cd ../cdn/ && npm run build", "build:gateway": "cd ../gateway/ && npm run build", @@ -50,6 +51,7 @@ "@fosscord/util": "file:../util", "async-exit-hook": "^2.0.1", "express": "^4.17.1", + "missing-native-js-functions": "^1.2.13", "mongodb-memory-server": "^7.3.6", "node-os-utils": "^1.3.5" } diff --git a/bundle/src/Database.ts b/bundle/src/Database.ts deleted file mode 100644 index 12febc1c..00000000 --- a/bundle/src/Database.ts +++ /dev/null @@ -1,44 +0,0 @@ -import fs from "fs"; -import { MongoMemoryServer } from "mongodb-memory-server"; -import path from "path"; -import exitHook from "async-exit-hook"; - -if (process.arch == "ia32") { - Object.defineProperty(process, "arch", { - value: "x64", - }); -} - -export async function setupDatabase() { - if (process.env.MONGO_URL) return; // exit because the user provides his own mongodb - const dbPath = path.join(__dirname, "..", "..", "db"); - const dbName = "fosscord"; - const storageEngine = "wiredTiger"; - const port = 27020; - const ip = "127.0.0.1"; - var mongod: MongoMemoryServer; - fs.mkdirSync(dbPath, { recursive: true }); - - exitHook((callback: any) => { - (async () => { - console.log(`Stopping MongoDB ...`); - await mongod.stop(); - console.log(`Stopped MongoDB`); - callback(); - })(); - }); - - console.log(`[Database] starting ...`); - mongod = new MongoMemoryServer({ - instance: { - port, - ip, - dbName, - dbPath, - storageEngine, - auth: false, // by default `mongod` is started with '--noauth', start `mongod` with '--auth' - }, - }); - await mongod.start(); - process.env.MONGO_URL = mongod.getUri(dbName); -} diff --git a/bundle/src/Server.ts b/bundle/src/Server.ts index e0586601..56c82cd1 100644 --- a/bundle/src/Server.ts +++ b/bundle/src/Server.ts @@ -6,7 +6,7 @@ import { FosscordServer as APIServer } from "@fosscord/api"; import { Server as GatewayServer } from "@fosscord/gateway"; import { CDNServer } from "@fosscord/cdn/"; import express from "express"; -import { Config } from "@fosscord/util"; +import { Config, initDatabase } from "@fosscord/util"; const app = express(); const server = http.createServer(); @@ -22,6 +22,8 @@ const cdn = new CDNServer({ server, port, production, app }); const gateway = new GatewayServer({ server, port, production }); async function main() { + await initDatabase(); + await Config.init(); await Config.set({ cdn: { endpointClient: "${location.host}", diff --git a/bundle/src/start.ts b/bundle/src/start.ts index 323995ae..843e3812 100644 --- a/bundle/src/start.ts +++ b/bundle/src/start.ts @@ -1,7 +1,6 @@ // process.env.MONGOMS_DEBUG = "true"; import cluster from "cluster"; import os from "os"; -import { setupDatabase } from "./Database"; import { initStats } from "./stats"; // TODO: add tcp socket event transmission @@ -12,7 +11,6 @@ if (cluster.isMaster && !process.env.masterStarted) { (async () => { initStats(); - await setupDatabase(); if (cores === 1) { require("./Server.js"); diff --git a/cdn/package.json b/cdn/package.json index 7e69b65d..1eb6b1a8 100644 --- a/cdn/package.json +++ b/cdn/package.json @@ -61,6 +61,7 @@ "jest": { "setupFilesAfterEnv": [ "/jest/setup.js" - ] + ], + "verbose": true } } diff --git a/cdn/src/Server.ts b/cdn/src/Server.ts index 522e11c2..f4a6b576 100644 --- a/cdn/src/Server.ts +++ b/cdn/src/Server.ts @@ -1,5 +1,5 @@ import { Server, ServerOptions } from "lambert-server"; -import { Config, db } from "@fosscord/util"; +import { Config, initDatabase } from "@fosscord/util"; import path from "path"; import avatarsRoute from "./routes/avatars"; @@ -13,8 +13,7 @@ export class CDNServer extends Server { } async start() { - // @ts-ignore - await (db as Promise); + await initDatabase(); await Config.init(); this.app.use((req, res, next) => { res.set("Access-Control-Allow-Origin", "*"); diff --git a/cdn/src/routes/external.ts b/cdn/src/routes/external.ts index 625b6bbd..10bb0f7d 100644 --- a/cdn/src/routes/external.ts +++ b/cdn/src/routes/external.ts @@ -1,45 +1,38 @@ -// @ts-nocheck -import bodyParser from "body-parser"; import { Router, Response, Request } from "express"; import fetch from "node-fetch"; -import crypto from "crypto"; import { HTTPError } from "lambert-server"; import { Snowflake } from "@fosscord/util"; import { storage } from "../util/Storage"; +import FileType from "file-type"; +import { Config } from "@fosscord/util"; + +// TODO: somehow handle the deletion of images posted to the /external route const router = Router(); - -type crawled = { - id: string; - ogTitle: string; - ogType: string; - ogDescription: string; - ogUrl: string; - cachedImage: string; -}; - const DEFAULT_FETCH_OPTIONS: any = { redirect: "follow", follow: 1, headers: { - "user-agent": "Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)", + "user-agent": "Mozilla/5.0 (compatible Fosscordbot/0.1; +https://fosscord.com)", }, size: 1024 * 1024 * 8, compress: true, method: "GET", }; -router.post("/", bodyParser.json(), async (req: Request, res: Response) => { +router.post("/", async (req: Request, res: Response) => { if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature"); + if (!req.body) throw new HTTPError("Invalid Body"); + const { url } = req.body; if (!url || typeof url !== "string") throw new HTTPError("Invalid url"); const id = Snowflake.generate(); try { - const response = await fetch(ogImage, DEFAULT_FETCH_OPTIONS); + const response = await fetch(url, DEFAULT_FETCH_OPTIONS); const buffer = await response.buffer(); await storage.set(`/external/${id}`, buffer); @@ -50,7 +43,7 @@ router.post("/", bodyParser.json(), async (req: Request, res: Response) => { } }); -router.get("/:id/", async (req: Request, res: Response) => { +router.get("/:id", async (req: Request, res: Response) => { const { id } = req.params; const file = await storage.get(`/external/${id}`); diff --git a/cdn/src/util/FileStorage.ts b/cdn/src/util/FileStorage.ts index 6e74788f..fae6eb1a 100644 --- a/cdn/src/util/FileStorage.ts +++ b/cdn/src/util/FileStorage.ts @@ -6,6 +6,8 @@ import "missing-native-js-functions"; import { Readable } from "stream"; import ExifTransformer = require("exif-be-gone"); +// TODO: split stored files into separate folders named after cloned route + function getPath(path: string) { // STORAGE_LOCATION has a default value in start.ts const root = process.env.STORAGE_LOCATION || "../"; diff --git a/cdn/tests/cdn_endpoints.test.js b/cdn/tests/cdn_endpoints.test.js new file mode 100644 index 00000000..a133d0dd --- /dev/null +++ b/cdn/tests/cdn_endpoints.test.js @@ -0,0 +1,211 @@ +const dotenv = require("dotenv"); +const path = require("path"); +const fse = require("fs-extra"); +dotenv.config(); + +// TODO: write unittest to check if FileStorage.ts is working +// TODO: write unitest to check if env vars are defined + +if (!process.env.STORAGE_PROVIDER) process.env.STORAGE_PROVIDER = "file"; +// TODO:nodejs path.join trailing slash windows compatible +if (process.env.STORAGE_PROVIDER === "file") { + if (process.env.STORAGE_LOCATION) { + if (!process.env.STORAGE_LOCATION.startsWith("/")) { + process.env.STORAGE_LOCATION = path.join(__dirname, "..", process.env.STORAGE_LOCATION, "/"); + } + } else { + process.env.STORAGE_LOCATION = path.join(__dirname, "..", "files", "/"); + } + fse.ensureDirSync(process.env.STORAGE_LOCATION); +} +const { CDNServer } = require("../dist/Server"); +const { Config } = require("@fosscord/util"); +const supertest = require("supertest"); +const request = supertest("http://localhost:3003"); +const server = new CDNServer({ port: Number(process.env.PORT) || 3003 }); + +beforeAll(async () => { + await server.start(); + return server; +}); + +afterAll(() => { + return server.stop(); +}); + +describe("/ping", () => { + describe("GET", () => { + describe("without signature specified", () => { + test("route should respond with 200", async () => { + let response = await request.get("/ping"); + expect(response.text).toBe("pong"); + }); + }); + }); +}); + +describe("/attachments", () => { + describe("POST", () => { + describe("without signature specified", () => { + test("route should respond with 400", async () => { + const response = await request.post("/attachments/123456789"); + expect(response.statusCode).toBe(400); + }); + }); + describe("with signature specified, without file specified", () => { + test("route should respond with 400", async () => { + const response = await request + .post("/attachments/123456789") + .set({ signature: Config.get().security.requestSignature }); + expect(response.statusCode).toBe(400); + }); + }); + describe("with signature specified, with file specified ", () => { + test("route should respond with Content-type: application/json, 200 and res.body.url", async () => { + const response = await request + .post("/attachments/123456789") + .set({ signature: Config.get().security.requestSignature }) + .attach("file", __dirname + "/antman.jpg"); + expect(response.statusCode).toBe(200); + expect(response.headers["content-type"]).toEqual(expect.stringContaining("json")); + expect(response.body.url).toBeDefined(); + }); + }); + }); + describe("GET", () => { + describe("getting uploaded image by url returned by POST /attachments", () => { + test("route should respond with 200", async () => { + let response = await request + .post("/attachments/123456789") + .set({ signature: Config.get().security.requestSignature }) + .attach("file", __dirname + "/antman.jpg"); + request.get(response.body.url.replace("http://localhost:3003", "")).then((x) => { + expect(x.statusCode).toBe(200); + }); + }); + }); + }); + describe("DELETE", () => { + describe("deleting uploaded image by url returned by POST /attachments", () => { + test("route should respond with res.body.success", async () => { + let response = await request + .post("/attachments/123456789") + .set({ signature: Config.get().security.requestSignature }) + .attach("file", __dirname + "/antman.jpg"); + request.delete(response.body.url.replace("http://localhost:3003", "")).then((x) => { + expect(x.body.success).toBeDefined(); + }); + }); + }); + }); +}); + +describe("/avatars", () => { + describe("POST", () => { + describe("without signature specified", () => { + test("route should respond with 400", async () => { + const response = await request.post("/avatars/123456789"); + expect(response.statusCode).toBe(400); + }); + }); + describe("with signature specified, without file specified", () => { + test("route should respond with 400", async () => { + const response = await request + .post("/avatars/123456789") + .set({ signature: Config.get().security.requestSignature }); + expect(response.statusCode).toBe(400); + }); + }); + describe("with signature specified, with file specified ", () => { + test("route should respond with Content-type: application/json, 200 and res.body.url", async () => { + const response = await request + .post("/avatars/123456789") + .set({ signature: Config.get().security.requestSignature }) + .attach("file", __dirname + "/antman.jpg"); + expect(response.statusCode).toBe(200); + expect(response.headers["content-type"]).toEqual(expect.stringContaining("json")); + expect(response.body.url).toBeDefined(); + }); + }); + }); + describe("GET", () => { + describe("getting uploaded image by url returned by POST /avatars", () => { + test("route should respond with 200", async () => { + let response = await request + .post("/avatars/123456789") + .set({ signature: Config.get().security.requestSignature }) + .attach("file", __dirname + "/antman.jpg"); + request.get(response.body.url.replace("http://localhost:3003", "")).then((x) => { + expect(x.statusCode).toBe(200); + }); + }); + }); + }); + describe("DELETE", () => { + describe("deleting uploaded image by url returned by POST /avatars", () => { + test("route should respond with res.body.success", async () => { + let response = await request + .post("/avatars/123456789") + .set({ signature: Config.get().security.requestSignature }) + .attach("file", __dirname + "/antman.jpg"); + request.delete(response.body.url.replace("http://localhost:3003", "")).then((x) => { + expect(x.body.success).toBeDefined(); + }); + }); + }); + }); +}); + +describe("/external", () => { + describe("POST", () => { + describe("without signature specified", () => { + test("route should respond with 400", async () => { + const response = await request.post("/external"); + expect(response.statusCode).toBe(400); + }); + }); + describe("with signature specified, without file specified", () => { + test("route should respond with 400", async () => { + const response = await request + .post("/external") + .set({ signature: Config.get().security.requestSignature }); + expect(response.statusCode).toBe(400); + }); + }); + describe("with signature specified, with file specified ", () => { + test("route should respond with Content-type: application/json, 200 and res.body.url", async () => { + const response = await request + .post("/external") + .set({ signature: Config.get().security.requestSignature }) + .send({ url: "https://i.ytimg.com/vi_webp/TiXzhQr5AUc/mqdefault.webp" }); + expect(response.statusCode).toBe(200); + expect(response.headers["content-type"]).toEqual(expect.stringContaining("json")); + expect(response.body.id).toBeDefined(); + }); + }); + describe("with signature specified, with falsy url specified ", () => { + test("route should respond with 400", async () => { + const response = await request + .post("/external") + .set({ signature: Config.get().security.requestSignature }) + .send({ + url: "notavalidurl.123", + }); + expect(response.statusCode).toBe(400); + }); + }); + }); + describe("GET", () => { + describe("getting uploaded image by url returned by POST /avatars", () => { + test("route should respond with 200", async () => { + let response = await request + .post("/external") + .set({ signature: Config.get().security.requestSignature }) + .send({ url: "https://i.ytimg.com/vi_webp/TiXzhQr5AUc/mqdefault.webp" }); + request.get(`external/${response.body.id}`).then((x) => { + expect(x.statusCode).toBe(200); + }); + }); + }); + }); +}); diff --git a/cdn/tests/filestorage.test.js b/cdn/tests/filestorage.test.js new file mode 100644 index 00000000..78036602 --- /dev/null +++ b/cdn/tests/filestorage.test.js @@ -0,0 +1,27 @@ +const path = require("path"); +process.env.STORAGE_LOCATION = path.join(__dirname, "..", "files", "/"); + +const { FileStorage } = require("../dist/util/FileStorage"); +const storage = new FileStorage(); +const fs = require("fs"); + +const file = fs.readFileSync(path.join(__dirname, "antman.jpg")); + +describe("FileStorage", () => { + describe("saving a file", () => { + test("saving a buffer", async () => { + await storage.set("test_saving_file", file); + }); + }); + describe("getting a file", () => { + test("getting buffer with given name", async () => { + const buffer2 = await storage.get("test_saving_file"); + expect(Buffer.compare(file, buffer2)).toBeTruthy(); + }); + }); + describe("deleting a file", () => { + test("deleting buffer with given name", async () => { + await storage.delete("test_saving_file"); + }); + }); +}); diff --git a/cdn/tests/server.test.js b/cdn/tests/server.test.js deleted file mode 100644 index 77d2b949..00000000 --- a/cdn/tests/server.test.js +++ /dev/null @@ -1,101 +0,0 @@ -const dotenv = require("dotenv"); -const path = require("path"); -const fse = require("fs-extra"); -dotenv.config(); - -if (!process.env.STORAGE_PROVIDER) process.env.STORAGE_PROVIDER = "file"; -// TODO:nodejs path.join trailing slash windows compatible -if (process.env.STORAGE_PROVIDER === "file") { - if (process.env.STORAGE_LOCATION) { - if (!process.env.STORAGE_LOCATION.startsWith("/")) { - process.env.STORAGE_LOCATION = path.join(__dirname, "..", process.env.STORAGE_LOCATION, "/"); - } - } else { - process.env.STORAGE_LOCATION = path.join(__dirname, "..", "files", "/"); - } - fse.ensureDirSync(process.env.STORAGE_LOCATION); -} - -const { CDNServer } = require("../dist/Server"); -const { db, Config } = require("@fosscord/util"); -const supertest = require("supertest"); -const request = supertest("http://localhost:3003"); -const server = new CDNServer({ port: Number(process.env.PORT) || 3003 }); - -beforeAll(async () => { - await server.start(); - return server; -}); - -afterAll(() => { - db.close(); - return server.stop(); -}); - -describe("/ping", () => { - describe("GET", () => { - describe("without signature specified", () => { - test("route should respond with 200", async () => { - let response = await request.get("/ping"); - expect(response.text).toBe("pong"); - }); - }); - }); -}); - -describe("/attachments", () => { - describe("POST", () => { - describe("without signature specified", () => { - test("route should respond with 400", async () => { - const response = await request.post("/attachments/123456789"); - expect(response.statusCode).toBe(400); - }); - }); - describe("with signature specified, without file specified", () => { - test("route should respond with 400", async () => { - const response = await request - .post("/attachments/123456789") - .set({ signature: Config.get().security.requestSignature }); - expect(response.statusCode).toBe(400); - }); - }); - describe("with signature specified, with file specified ", () => { - test("route should respond with Content-type: application/json, 200 and res.body.url", async () => { - const response = await request - .post("/attachments/123456789") - .set({ signature: Config.get().security.requestSignature }) - .attach("file", __dirname + "/antman.jpg"); - expect(response.statusCode).toBe(200); - expect(response.headers["content-type"]).toEqual(expect.stringContaining("json")); - expect(response.body.url).toBeDefined(); - attachment_url = response.body.url; - }); - }); - }); - describe("GET", () => { - describe("getting uploaded image by url returned by POST /attachments", () => { - test("route should respond with 200", async () => { - let response = await request - .post("/attachments/123456789") - .set({ signature: Config.get().security.requestSignature }) - .attach("file", __dirname + "/antman.jpg"); - request.get(response.body.url.replace("http://localhost:3003", "")).then((x) => { - expect(x.statusCode).toBe(200); - }); - }); - }); - }); - describe("DELETE", () => { - describe("deleting uploaded image by url returned by POST /attachments", () => { - test("route should respond with res.body.success", async () => { - let response = await request - .post("/attachments/123456789") - .set({ signature: Config.get().security.requestSignature }) - .attach("file", __dirname + "/antman.jpg"); - request.delete(response.body.url.replace("http://localhost:3003", "")).then((x) => { - expect(x.body.success).toBeDefined(); - }); - }); - }); - }); -}); diff --git a/gateway/package-lock.json b/gateway/package-lock.json index e76bd71c..40606ecd 100644 Binary files a/gateway/package-lock.json and b/gateway/package-lock.json differ diff --git a/gateway/src/Server.ts b/gateway/src/Server.ts index a50c24a6..944174c7 100644 --- a/gateway/src/Server.ts +++ b/gateway/src/Server.ts @@ -1,7 +1,7 @@ import "missing-native-js-functions"; import dotenv from "dotenv"; dotenv.config(); -import { Config, db, initEvent, RabbitMQ } from "@fosscord/util"; +import { closeDatabase, Config, initDatabase, initEvent, RabbitMQ } from "@fosscord/util"; import { Server as WebSocketServer } from "ws"; import { Connection } from "./events/Connection"; import http from "http"; @@ -38,15 +38,8 @@ export class Server { this.ws.on("error", console.error); } - async setupSchema() { - // TODO: adjust expireAfterSeconds -> lower - await Promise.all([db.collection("events").createIndex({ created_at: 1 }, { expireAfterSeconds: 60 })]); - } - async start(): Promise { - // @ts-ignore - await (db as Promise); - await this.setupSchema(); + await initDatabase(); await Config.init(); await initEvent(); if (!this.server.listening) { @@ -56,7 +49,7 @@ export class Server { } async stop() { - await db.close(); + closeDatabase(); this.server.close(); } } diff --git a/gateway/src/listener/listener.ts b/gateway/src/listener/listener.ts index 51082586..75ca1680 100644 --- a/gateway/src/listener/listener.ts +++ b/gateway/src/listener/listener.ts @@ -1,22 +1,21 @@ import { - db, - Event, - UserModel, + User, getPermission, Permissions, - ChannelModel, + Channel, RabbitMQ, - EVENT, listenEvent, EventOpts, ListenEventOpts, + Member, } from "@fosscord/util"; import { OPCODES } from "../util/Constants"; import { Send } from "../util/Send"; import WebSocket from "../util/WebSocket"; import "missing-native-js-functions"; -import { ConsumeMessage } from "amqplib"; -import { Channel } from "amqplib"; +import { Channel as AMQChannel } from "amqplib"; +import { In, Like } from "../../../util/node_modules/typeorm"; +import { Recipient } from "../../../util/dist/entities/Recipient"; // TODO: close connection on Invalidated Token // TODO: check intent @@ -27,15 +26,15 @@ import { Channel } from "amqplib"; // TODO: use already queried guilds/channels of Identify and don't fetch them again export async function setupListener(this: WebSocket) { - const user = await UserModel.findOne({ id: this.user_id }, { guilds: true }).exec(); - const channels = await ChannelModel.find( - { $or: [{ recipient_ids: this.user_id }, { guild_id: { $in: user.guilds } }] }, - { id: true, permission_overwrites: true } - ).exec(); - const dm_channels = channels.filter((x) => !x.guild_id); + const members = await Member.find({ where: { id: this.user_id } }); + const guild_ids = members.map((x) => x.guild_id); + const user = await User.findOneOrFail({ id: this.user_id }); + const recipients = await Recipient.find({ where: { id: this.user_id }, relations: ["channel"] }); + const channels = await Channel.find({ guild_id: In(guild_ids) }); + const dm_channels = recipients.map((x) => x.channel); const guild_channels = channels.filter((x) => x.guild_id); - const opts: { acknowledge: boolean; channel?: Channel } = { acknowledge: true }; + const opts: { acknowledge: boolean; channel?: AMQChannel } = { acknowledge: true }; const consumer = consume.bind(this); if (RabbitMQ.connection) { @@ -50,7 +49,7 @@ export async function setupListener(this: WebSocket) { this.events[channel.id] = await listenEvent(channel.id, consumer, opts); } - for (const guild of user.guilds) { + for (const guild of guild_ids) { // contains guild and dm channels getPermission(this.user_id, guild) diff --git a/gateway/src/opcodes/Identify.ts b/gateway/src/opcodes/Identify.ts index 91f7f675..958f1b73 100644 --- a/gateway/src/opcodes/Identify.ts +++ b/gateway/src/opcodes/Identify.ts @@ -1,17 +1,16 @@ import { CLOSECODES, Payload, OPCODES } from "../util/Constants"; import WebSocket from "../util/WebSocket"; import { - ChannelModel, + Channel, checkToken, - GuildModel, + Guild, Intents, - MemberDocument, - MemberModel, + Member, ReadyEventData, - UserModel, - toObject, + User, EVENTEnum, Config, + dbConnection, } from "@fosscord/util"; import { setupListener } from "../listener/listener"; import { IdentifySchema } from "../schema/Identify"; @@ -19,6 +18,8 @@ import { Send } from "../util/Send"; // import experiments from "./experiments.json"; const experiments: any = []; import { check } from "./instanceOf"; +import { Like } from "../../../util/node_modules/typeorm"; +import { Recipient } from "../../../util/dist/entities/Recipient"; // TODO: bot sharding // TODO: check priviliged intents @@ -54,17 +55,22 @@ export async function onIdentify(this: WebSocket, data: Payload) { } } - const members = toObject(await MemberModel.find({ id: this.user_id }).exec()); + const members = await Member.find({ + where: { id: this.user_id }, + relations: ["guild", "guild.channels", "guild.emojis", "guild.roles", "guild.stickers", "user", "roles"], + }); const merged_members = members.map((x: any) => { - const y = { ...x, user_id: x.id }; - delete y.settings; - delete y.id; - return [y]; - }) as MemberDocument[][]; + return [x]; + }) as Member[][]; + const guilds = members.map((x) => ({ ...x.guild, joined_at: x.joined_at })); const user_guild_settings_entries = members.map((x) => x.settings); - const channels = await ChannelModel.find({ recipient_ids: this.user_id }).exec(); - const user = await UserModel.findOne({ id: this.user_id }).exec(); + const recipients = await Recipient.find({ + where: { id: this.user_id }, + relations: ["channel", "channel.recipients"], + }); + const channels = recipients.map((x) => x.channel); + const user = await User.findOneOrFail({ id: this.user_id }); if (!user) return this.close(CLOSECODES.Authentication_failed); const public_user = { @@ -74,12 +80,9 @@ export async function onIdentify(this: WebSocket, data: Payload) { public_flags: user.public_flags, avatar: user.avatar, bot: user.bot, + bio: user.bio, }; - const guilds = await GuildModel.find({ id: { $in: user.guilds } }) - .populate({ path: "joined_at", match: { id: this.user_id } }) - .exec(); - const privateUser = { avatar: user.avatar, mobile: user.mobile, @@ -99,14 +102,15 @@ export async function onIdentify(this: WebSocket, data: Payload) { bot: user.bot, accent_color: user.accent_color || 0, banner: user.banner, + bio: user.bio, }; const d: ReadyEventData = { v: 8, user: privateUser, - user_settings: user.user_settings, + user_settings: user.settings, // @ts-ignore - guilds: toObject(guilds).map((x) => { + guilds: guilds.map((x) => { // @ts-ignore x.guild_hashes = { channels: { omitted: false, hash: "y4PV2fZ0gmo" }, @@ -118,7 +122,7 @@ export async function onIdentify(this: WebSocket, data: Payload) { }), guild_experiments: [], // TODO geo_ordered_rtc_regions: [], // TODO - relationships: user.user_data.relationships, + relationships: user.relationships, read_state: { // TODO entries: [], @@ -130,12 +134,7 @@ export async function onIdentify(this: WebSocket, data: Payload) { partial: false, // TODO partial version: 642, }, - // @ts-ignore - private_channels: toObject(channels).map((x: ChannelDocument) => { - x.recipient_ids = x.recipients.map((y: any) => y.id); - delete x.recipients; - return x; - }), + private_channels: channels, session_id: "", // TODO analytics_token: "", // TODO connected_accounts: [], // TODO @@ -144,17 +143,12 @@ export async function onIdentify(this: WebSocket, data: Payload) { consented: false, // TODO }, }, - country_code: user.user_settings.locale, + country_code: user.settings.locale, friend_suggestion_count: 0, // TODO // @ts-ignore experiments: experiments, // TODO guild_join_requests: [], // TODO what is this? - users: [ - public_user, - ...toObject(channels) - .map((x: any) => x.recipients) - .flat(), - ].unique(), // TODO + users: [public_user].unique(), // TODO merged_members: merged_members, // shard // TODO: only for bots sharding // application // TODO for applications diff --git a/gateway/src/opcodes/LazyRequest.ts b/gateway/src/opcodes/LazyRequest.ts index 63075e5a..9f514f5f 100644 --- a/gateway/src/opcodes/LazyRequest.ts +++ b/gateway/src/opcodes/LazyRequest.ts @@ -18,45 +18,43 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) { permissions.hasThrow("VIEW_CHANNEL"); // MongoDB query to retrieve all hoisted roles and join them with the members and users collection - const roles = toObject( - await db - .collection("roles") - .aggregate([ - { - $match: { - guild_id, - // hoist: true // TODO: also match @everyone role - }, + const roles = await db + .collection("roles") + .aggregate([ + { + $match: { + guild_id, + // hoist: true // TODO: also match @everyone role }, - { $sort: { position: 1 } }, - { - $lookup: { - from: "members", - let: { id: "$id" }, - pipeline: [ - { $match: { $expr: { $in: ["$$id", "$roles"] } } }, - { $limit: 100 }, - { - $lookup: { - from: "users", - let: { user_id: "$id" }, - pipeline: [ - { $match: { $expr: { $eq: ["$id", "$$user_id"] } } }, - { $project: PublicUserProjection }, - ], - as: "user", - }, + }, + { $sort: { position: 1 } }, + { + $lookup: { + from: "members", + let: { id: "$id" }, + pipeline: [ + { $match: { $expr: { $in: ["$$id", "$roles"] } } }, + { $limit: 100 }, + { + $lookup: { + from: "users", + let: { user_id: "$id" }, + pipeline: [ + { $match: { $expr: { $eq: ["$id", "$$user_id"] } } }, + { $project: PublicUserProjection }, + ], + as: "user", }, - { - $unwind: "$user", - }, - ], - as: "members", - }, + }, + { + $unwind: "$user", + }, + ], + as: "members", }, - ]) - .toArray() - ); + }, + ]) + .toArray(); const groups = roles.map((x) => ({ id: x.id === guild_id ? "online" : x.id, count: x.members.length })); const member_count = roles.reduce((a, b) => b.members.length + a, 0); diff --git a/gateway/src/schema/Activity.ts b/gateway/src/schema/Activity.ts index d7e0a30b..f1665efd 100644 --- a/gateway/src/schema/Activity.ts +++ b/gateway/src/schema/Activity.ts @@ -1,10 +1,47 @@ -import { ActivityBodySchema } from "@fosscord/util"; import { EmojiSchema } from "./Emoji"; export const ActivitySchema = { afk: Boolean, status: String, - $activities: [ActivityBodySchema], + $activities: [ + { + name: String, + type: Number, + $url: String, + $created_at: Date, + $timestamps: [ + { + $start: Number, + $end: Number, + }, + ], + $application_id: String, + $details: String, + $state: String, + $emoji: { + $name: String, + $id: String, + $amimated: Boolean, + }, + $party: { + $id: String, + $size: [Number, Number], + }, + $assets: { + $large_image: String, + $large_text: String, + $small_image: String, + $small_text: String, + }, + $secrets: { + $join: String, + $spectate: String, + $match: String, + }, + $instance: Boolean, + $flags: String, + }, + ], $since: Number, // unix time (in milliseconds) of when the client went idle, or null if the client is not idle }; @@ -42,7 +79,7 @@ export interface ActivitySchema { match?: string; // the secret for a specific instanced match }; instance?: boolean; - flags: bigint; // activity flags OR d together, describes what the payload includes + flags: string; // activity flags OR d together, describes what the payload includes } ]; since?: number; // unix time (in milliseconds) of when the client went idle, or null if the client is not idle diff --git a/util/.gitignore b/util/.gitignore index d7fc3f74..87263762 100644 --- a/util/.gitignore +++ b/util/.gitignore @@ -104,4 +104,5 @@ typings/ .DS_Store # Compiled TypeScript code -dist/ \ No newline at end of file +dist/ +database.db \ No newline at end of file diff --git a/util/.vscode/launch.json b/util/.vscode/launch.json index 07fd32ac..524622d1 100644 --- a/util/.vscode/launch.json +++ b/util/.vscode/launch.json @@ -8,10 +8,20 @@ "sourceMaps": true, "type": "node", "request": "launch", - "name": "Launch Server", + "name": "Launch Util", "program": "${workspaceFolder}/dist/index.js", "preLaunchTask": "tsc: build - tsconfig.json", "outFiles": ["${workspaceFolder}/dist/**/*.js"] + }, + { + "name": "Debug Jest Tests", + "type": "node", + "request": "launch", + "runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/jest/bin/jest.js", "--runInBand"], + "preLaunchTask": "tsc: build - tsconfig.json", + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen", + "port": 9229 } ] } diff --git a/util/package-lock.json b/util/package-lock.json index befb6563..47aca2d1 100644 Binary files a/util/package-lock.json and b/util/package-lock.json differ diff --git a/util/package.json b/util/package.json index d84897dc..39af7526 100644 --- a/util/package.json +++ b/util/package.json @@ -5,7 +5,10 @@ "main": "dist/index.js", "types": "dist/index.d.ts", "scripts": { - "postinstall": "npm run build", + "start": "npm run build && node dist/", + "patch": "patch-package", + "test": "npm run build && jest", + "postinstall": "npm run patch && npm run build", "build": "npx tsc -b ." }, "repository": { @@ -28,23 +31,32 @@ "devDependencies": { "@types/amqplib": "^0.8.1", "@types/jsonwebtoken": "^8.5.0", - "@types/mongodb": "^3.6.9", "@types/mongoose-autopopulate": "^0.10.1", - "@types/mongoose-lean-virtuals": "^0.5.1", "@types/node": "^14.17.9", - "@types/node-fetch": "^2.5.12" + "@types/node-fetch": "^2.5.12", + "jest": "^27.0.6" }, "dependencies": { - "ajv": "^8.5.0", + "ajv": "^8.6.2", "amqplib": "^0.8.0", + "class-validator": "^0.13.1", "dot-prop": "^6.0.1", "env-paths": "^2.2.1", "jsonwebtoken": "^8.5.1", - "missing-native-js-functions": "^1.2.10", - "mongodb": "^3.6.9", - "mongoose": "^5.13.7", - "mongoose-autopopulate": "^0.12.3", + "lambert-server": "^1.2.10", + "missing-native-js-functions": "^1.2.11", "node-fetch": "^2.6.1", - "typescript": "^4.1.3" + "patch-package": "^6.4.7", + "pg": "^8.7.1", + "reflect-metadata": "^0.1.13", + "sqlite3": "^5.0.2", + "typeorm": "^0.2.37", + "typescript": "^4.4.2", + "typescript-json-schema": "^0.50.1" + }, + "jest": { + "setupFilesAfterEnv": [ + "./tests/setupJest.js" + ] } } diff --git a/util/patches/ajv+8.6.2.patch b/util/patches/ajv+8.6.2.patch new file mode 100644 index 00000000..e7556179 --- /dev/null +++ b/util/patches/ajv+8.6.2.patch @@ -0,0 +1,249 @@ +diff --git a/node_modules/ajv/dist/compile/jtd/parse.js b/node_modules/ajv/dist/compile/jtd/parse.js +index 1eeb1be..7684121 100644 +--- a/node_modules/ajv/dist/compile/jtd/parse.js ++++ b/node_modules/ajv/dist/compile/jtd/parse.js +@@ -239,6 +239,9 @@ function parseType(cxt) { + gen.if(fail, () => parsingError(cxt, codegen_1.str `invalid timestamp`)); + break; + } ++ case "bigint": ++ parseBigInt(cxt); ++ break + case "float32": + case "float64": + parseNumber(cxt); +@@ -284,6 +287,15 @@ function parseNumber(cxt, maxDigits) { + skipWhitespace(cxt); + gen.if(codegen_1._ `"-0123456789".indexOf(${jsonSlice(1)}) < 0`, () => jsonSyntaxError(cxt), () => parseWith(cxt, parseJson_1.parseJsonNumber, maxDigits)); + } ++function parseBigInt(cxt, maxDigits) { ++ const {gen} = cxt ++ skipWhitespace(cxt) ++ gen.if( ++ _`"-0123456789".indexOf(${jsonSlice(1)}) < 0`, ++ () => jsonSyntaxError(cxt), ++ () => parseWith(cxt, parseJson_1.parseJsonBigInt, maxDigits) ++ ) ++} + function parseBooleanToken(bool, fail) { + return (cxt) => { + const { gen, data } = cxt; +diff --git a/node_modules/ajv/dist/compile/rules.js b/node_modules/ajv/dist/compile/rules.js +index 82a591f..1ebd8fe 100644 +--- a/node_modules/ajv/dist/compile/rules.js ++++ b/node_modules/ajv/dist/compile/rules.js +@@ -1,7 +1,7 @@ + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.getRules = exports.isJSONType = void 0; +-const _jsonTypes = ["string", "number", "integer", "boolean", "null", "object", "array"]; ++const _jsonTypes = ["string", "number", "integer", "boolean", "null", "object", "array","bigint"]; + const jsonTypes = new Set(_jsonTypes); + function isJSONType(x) { + return typeof x == "string" && jsonTypes.has(x); +@@ -13,10 +13,11 @@ function getRules() { + string: { type: "string", rules: [] }, + array: { type: "array", rules: [] }, + object: { type: "object", rules: [] }, ++ bigint: {type: "bigint", rules: []} + }; + return { +- types: { ...groups, integer: true, boolean: true, null: true }, +- rules: [{ rules: [] }, groups.number, groups.string, groups.array, groups.object], ++ types: { ...groups, integer: true, boolean: true, null: true, bigint: true }, ++ rules: [{ rules: [] }, groups.number, groups.string, groups.array, groups.object, groups.bigint], + post: { rules: [] }, + all: {}, + keywords: {}, +diff --git a/node_modules/ajv/dist/compile/validate/dataType.js b/node_modules/ajv/dist/compile/validate/dataType.js +index 6319e76..8b50b4c 100644 +--- a/node_modules/ajv/dist/compile/validate/dataType.js ++++ b/node_modules/ajv/dist/compile/validate/dataType.js +@@ -52,7 +52,7 @@ function coerceAndCheckDataType(it, types) { + return checkTypes; + } + exports.coerceAndCheckDataType = coerceAndCheckDataType; +-const COERCIBLE = new Set(["string", "number", "integer", "boolean", "null"]); ++const COERCIBLE = new Set(["string", "number", "integer", "boolean", "null","bigint"]); + function coerceToTypes(types, coerceTypes) { + return coerceTypes + ? types.filter((t) => COERCIBLE.has(t) || (coerceTypes === "array" && t === "array")) +@@ -83,6 +83,14 @@ function coerceData(it, types, coerceTo) { + }); + function coerceSpecificType(t) { + switch (t) { ++ case "bigint": ++ gen ++ .elseIf( ++ codegen_1._`${dataType} == "boolean" || ${data} === null ++ || (${dataType} == "string" && ${data} && ${data} == BigInt(${data}))` ++ ) ++ .assign(coerced, codegen_1._`BigInt(${data})`) ++ return + case "string": + gen + .elseIf(codegen_1._ `${dataType} == "number" || ${dataType} == "boolean"`) +@@ -143,6 +151,9 @@ function checkDataType(dataType, data, strictNums, correct = DataType.Correct) { + case "number": + cond = numCond(); + break; ++ case "bigint": ++ cond = codegen_1._`typeof ${data} == "bigint" && isFinite(${data})` ++ break + default: + return codegen_1._ `typeof ${data} ${EQ} ${dataType}`; + } +diff --git a/node_modules/ajv/dist/refs/json-schema-2019-09/meta/validation.json b/node_modules/ajv/dist/refs/json-schema-2019-09/meta/validation.json +index 7027a12..25679c8 100644 +--- a/node_modules/ajv/dist/refs/json-schema-2019-09/meta/validation.json ++++ b/node_modules/ajv/dist/refs/json-schema-2019-09/meta/validation.json +@@ -78,7 +78,7 @@ + "default": 0 + }, + "simpleTypes": { +- "enum": ["array", "boolean", "integer", "null", "number", "object", "string"] ++ "enum": ["array", "boolean", "integer", "null", "number", "object", "string","bigint"] + }, + "stringArray": { + "type": "array", +diff --git a/node_modules/ajv/dist/refs/json-schema-2020-12/meta/validation.json b/node_modules/ajv/dist/refs/json-schema-2020-12/meta/validation.json +index e0ae13d..57c9036 100644 +--- a/node_modules/ajv/dist/refs/json-schema-2020-12/meta/validation.json ++++ b/node_modules/ajv/dist/refs/json-schema-2020-12/meta/validation.json +@@ -78,7 +78,7 @@ + "default": 0 + }, + "simpleTypes": { +- "enum": ["array", "boolean", "integer", "null", "number", "object", "string"] ++ "enum": ["array", "boolean", "integer", "null", "number", "object", "string","bigint"] + }, + "stringArray": { + "type": "array", +diff --git a/node_modules/ajv/dist/refs/json-schema-draft-06.json b/node_modules/ajv/dist/refs/json-schema-draft-06.json +index 5410064..774435b 100644 +--- a/node_modules/ajv/dist/refs/json-schema-draft-06.json ++++ b/node_modules/ajv/dist/refs/json-schema-draft-06.json +@@ -16,7 +16,7 @@ + "allOf": [{"$ref": "#/definitions/nonNegativeInteger"}, {"default": 0}] + }, + "simpleTypes": { +- "enum": ["array", "boolean", "integer", "null", "number", "object", "string"] ++ "enum": ["array", "boolean", "integer", "null", "number", "object", "string","bigint"] + }, + "stringArray": { + "type": "array", +diff --git a/node_modules/ajv/dist/refs/json-schema-draft-07.json b/node_modules/ajv/dist/refs/json-schema-draft-07.json +index 6a74851..fc6dd7d 100644 +--- a/node_modules/ajv/dist/refs/json-schema-draft-07.json ++++ b/node_modules/ajv/dist/refs/json-schema-draft-07.json +@@ -16,7 +16,7 @@ + "allOf": [{"$ref": "#/definitions/nonNegativeInteger"}, {"default": 0}] + }, + "simpleTypes": { +- "enum": ["array", "boolean", "integer", "null", "number", "object", "string"] ++ "enum": ["array", "boolean", "integer", "null", "number", "object", "string","bigint"] + }, + "stringArray": { + "type": "array", +diff --git a/node_modules/ajv/dist/refs/jtd-schema.js b/node_modules/ajv/dist/refs/jtd-schema.js +index 1ee940a..1148887 100644 +--- a/node_modules/ajv/dist/refs/jtd-schema.js ++++ b/node_modules/ajv/dist/refs/jtd-schema.js +@@ -38,6 +38,7 @@ const typeForm = (root) => ({ + "uint16", + "int32", + "uint32", ++ "bigint", + ], + }, + }, +diff --git a/node_modules/ajv/dist/runtime/parseJson.js b/node_modules/ajv/dist/runtime/parseJson.js +index 2576a6e..e7447b1 100644 +--- a/node_modules/ajv/dist/runtime/parseJson.js ++++ b/node_modules/ajv/dist/runtime/parseJson.js +@@ -97,6 +97,71 @@ exports.parseJsonNumber = parseJsonNumber; + parseJsonNumber.message = undefined; + parseJsonNumber.position = 0; + parseJsonNumber.code = 'require("ajv/dist/runtime/parseJson").parseJsonNumber'; ++ ++function parseJsonBigInt(s, pos, maxDigits) { ++ let numStr = ""; ++ let c; ++ parseJsonBigInt.message = undefined; ++ if (s[pos] === "-") { ++ numStr += "-"; ++ pos++; ++ } ++ if (s[pos] === "0") { ++ numStr += "0"; ++ pos++; ++ } ++ else { ++ if (!parseDigits(maxDigits)) { ++ errorMessage(); ++ return undefined; ++ } ++ } ++ if (maxDigits) { ++ parseJsonBigInt.position = pos; ++ return BigInt(numStr); ++ } ++ if (s[pos] === ".") { ++ numStr += "."; ++ pos++; ++ if (!parseDigits()) { ++ errorMessage(); ++ return undefined; ++ } ++ } ++ if (((c = s[pos]), c === "e" || c === "E")) { ++ numStr += "e"; ++ pos++; ++ if (((c = s[pos]), c === "+" || c === "-")) { ++ numStr += c; ++ pos++; ++ } ++ if (!parseDigits()) { ++ errorMessage(); ++ return undefined; ++ } ++ } ++ parseJsonBigInt.position = pos; ++ return BigInt(numStr); ++ function parseDigits(maxLen) { ++ let digit = false; ++ while (((c = s[pos]), c >= "0" && c <= "9" && (maxLen === undefined || maxLen-- > 0))) { ++ digit = true; ++ numStr += c; ++ pos++; ++ } ++ return digit; ++ } ++ function errorMessage() { ++ parseJsonBigInt.position = pos; ++ parseJsonBigInt.message = pos < s.length ? `unexpected token ${s[pos]}` : "unexpected end"; ++ } ++} ++exports.parseJsonBigInt = parseJsonBigInt; ++parseJsonBigInt.message = undefined; ++parseJsonBigInt.position = 0; ++parseJsonBigInt.code = 'require("ajv/dist/runtime/parseJson").parseJsonBigInt'; ++ ++ + const escapedChars = { + b: "\b", + f: "\f", +diff --git a/node_modules/ajv/dist/vocabularies/jtd/type.js b/node_modules/ajv/dist/vocabularies/jtd/type.js +index 428bddb..fbc3070 100644 +--- a/node_modules/ajv/dist/vocabularies/jtd/type.js ++++ b/node_modules/ajv/dist/vocabularies/jtd/type.js +@@ -45,6 +45,9 @@ const def = { + cond = timestampCode(cxt); + break; + } ++ case "bigint": ++ cond = codegen_1._`typeof ${data} == "bigint" || typeof ${data} == "string"` ++ break + case "float32": + case "float64": + cond = codegen_1._ `typeof ${data} == "number"`; diff --git a/util/patches/typescript-json-schema+0.50.1.patch b/util/patches/typescript-json-schema+0.50.1.patch new file mode 100644 index 00000000..a0d479de --- /dev/null +++ b/util/patches/typescript-json-schema+0.50.1.patch @@ -0,0 +1,14 @@ +diff --git a/node_modules/typescript-json-schema/dist/typescript-json-schema.js b/node_modules/typescript-json-schema/dist/typescript-json-schema.js +index 47e1598..8397b9d 100644 +--- a/node_modules/typescript-json-schema/dist/typescript-json-schema.js ++++ b/node_modules/typescript-json-schema/dist/typescript-json-schema.js +@@ -432,6 +432,9 @@ var JsonSchemaGenerator = (function () { + else if (flags & ts.TypeFlags.Boolean) { + definition.type = "boolean"; + } ++ else if (flags & ts.TypeFlags.BigInt) { ++ definition.type = "bigint"; ++ } + else if (flags & ts.TypeFlags.Null) { + definition.type = "null"; + } diff --git a/util/src/entities/Application.ts b/util/src/entities/Application.ts new file mode 100644 index 00000000..2092cd4e --- /dev/null +++ b/util/src/entities/Application.ts @@ -0,0 +1,107 @@ +import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; +import { BaseClass } from "./BaseClass"; +import { Guild } from "./Guild"; +import { Team } from "./Team"; +import { User } from "./User"; + +@Entity("applications") +export class Application extends BaseClass { + @Column() + name: string; + + @Column({ nullable: true }) + icon?: string; + + @Column() + description: string; + + @Column({ type: "simple-array", nullable: true }) + rpc_origins?: string[]; + + @Column() + bot_public: boolean; + + @Column() + bot_require_code_grant: boolean; + + @Column({ nullable: true }) + terms_of_service_url?: string; + + @Column({ nullable: true }) + privacy_policy_url?: string; + + @JoinColumn({ name: "owner_id" }) + @ManyToOne(() => User) + owner?: User; + + @Column({ nullable: true }) + summary?: string; + + @Column() + verify_key: string; + + @JoinColumn({ name: "team_id" }) + @ManyToOne(() => Team) + team?: Team; + + @JoinColumn({ name: "guild_id" }) + @ManyToOne(() => Guild) + guild: Guild; // if this application is a game sold, this field will be the guild to which it has been linked + + @Column({ nullable: true }) + primary_sku_id?: string; // if this application is a game sold, this field will be the id of the "Game SKU" that is created, + + @Column({ nullable: true }) + slug?: string; // if this application is a game sold, this field will be the URL slug that links to the store page + + @Column({ nullable: true }) + cover_image?: string; // the application's default rich presence invite cover image hash + + @Column() + flags: string; // the application's public flags +} + +export interface ApplicationCommand { + id: string; + application_id: string; + name: string; + description: string; + options?: ApplicationCommandOption[]; +} + +export interface ApplicationCommandOption { + type: ApplicationCommandOptionType; + name: string; + description: string; + required?: boolean; + choices?: ApplicationCommandOptionChoice[]; + options?: ApplicationCommandOption[]; +} + +export interface ApplicationCommandOptionChoice { + name: string; + value: string | number; +} + +export enum ApplicationCommandOptionType { + SUB_COMMAND = 1, + SUB_COMMAND_GROUP = 2, + STRING = 3, + INTEGER = 4, + BOOLEAN = 5, + USER = 6, + CHANNEL = 7, + ROLE = 8, +} + +export interface ApplicationCommandInteractionData { + id: string; + name: string; + options?: ApplicationCommandInteractionDataOption[]; +} + +export interface ApplicationCommandInteractionDataOption { + name: string; + value?: any; + options?: ApplicationCommandInteractionDataOption[]; +} diff --git a/util/src/entities/Attachment.ts b/util/src/entities/Attachment.ts new file mode 100644 index 00000000..ca893400 --- /dev/null +++ b/util/src/entities/Attachment.ts @@ -0,0 +1,34 @@ +import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; +import { BaseClass } from "./BaseClass"; + +@Entity("attachments") +export class Attachment extends BaseClass { + @Column() + filename: string; // name of file attached + + @Column() + size: number; // size of file in bytes + + @Column() + url: string; // source url of file + + @Column() + proxy_url: string; // a proxied url of file + + @Column({ nullable: true }) + height?: number; // height of file (if image) + + @Column({ nullable: true }) + width?: number; // width of file (if image) + + @Column({ nullable: true }) + content_type?: string; + + @Column({ nullable: true }) + @RelationId((attachment: Attachment) => attachment.message) + message_id: string; + + @JoinColumn({ name: "message_id" }) + @ManyToOne(() => require("./Message").Message, (message: import("./Message").Message) => message.attachments) + message: import("./Message").Message; +} diff --git a/util/src/models/AuditLog.ts b/util/src/entities/AuditLog.ts similarity index 53% rename from util/src/models/AuditLog.ts rename to util/src/entities/AuditLog.ts index 02b2c444..ceeb21fd 100644 --- a/util/src/models/AuditLog.ts +++ b/util/src/entities/AuditLog.ts @@ -1,20 +1,67 @@ -import { Schema, Document, Types } from "mongoose"; -import db from "../util/Database"; +import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; +import { BaseClass } from "./BaseClass"; import { ChannelPermissionOverwrite } from "./Channel"; -import { PublicUser } from "./User"; +import { User } from "./User"; -export interface AuditLogResponse { - webhooks: []; // TODO: - users: PublicUser[]; - audit_log_entries: AuditLogEntries[]; - integrations: []; // TODO: +export enum AuditLogEvents { + GUILD_UPDATE = 1, + CHANNEL_CREATE = 10, + CHANNEL_UPDATE = 11, + CHANNEL_DELETE = 12, + CHANNEL_OVERWRITE_CREATE = 13, + CHANNEL_OVERWRITE_UPDATE = 14, + CHANNEL_OVERWRITE_DELETE = 15, + MEMBER_KICK = 20, + MEMBER_PRUNE = 21, + MEMBER_BAN_ADD = 22, + MEMBER_BAN_REMOVE = 23, + MEMBER_UPDATE = 24, + MEMBER_ROLE_UPDATE = 25, + MEMBER_MOVE = 26, + MEMBER_DISCONNECT = 27, + BOT_ADD = 28, + ROLE_CREATE = 30, + ROLE_UPDATE = 31, + ROLE_DELETE = 32, + INVITE_CREATE = 40, + INVITE_UPDATE = 41, + INVITE_DELETE = 42, + WEBHOOK_CREATE = 50, + WEBHOOK_UPDATE = 51, + WEBHOOK_DELETE = 52, + EMOJI_CREATE = 60, + EMOJI_UPDATE = 61, + EMOJI_DELETE = 62, + MESSAGE_DELETE = 72, + MESSAGE_BULK_DELETE = 73, + MESSAGE_PIN = 74, + MESSAGE_UNPIN = 75, + INTEGRATION_CREATE = 80, + INTEGRATION_UPDATE = 81, + INTEGRATION_DELETE = 82, } -export interface AuditLogEntries { - target_id?: string; +@Entity("audit_logs") +export class AuditLogEntry extends BaseClass { + @JoinColumn({ name: "target_id" }) + @ManyToOne(() => User) + target?: User; + + @Column({ nullable: true }) + @RelationId((auditlog: AuditLogEntry) => auditlog.user) user_id: string; - id: string; + + @JoinColumn({ name: "user_id" }) + @ManyToOne(() => User) + user: User; + + @Column({ + type: "simple-enum", + enum: AuditLogEvents, + }) action_type: AuditLogEvents; + + @Column({ type: "simple-json", nullable: true }) options?: { delete_member_days?: string; members_removed?: string; @@ -25,7 +72,12 @@ export interface AuditLogEntries { type?: string; role_name?: string; }; + + @Column() + @Column({ type: "simple-json" }) changes: AuditLogChange[]; + + @Column({ nullable: true }) reason?: string; } @@ -91,130 +143,3 @@ export interface AuditLogChangeValue { expire_grace_period?: number; user_limit?: number; } - -export interface AuditLogEntriesDocument extends Document, AuditLogEntries { - id: string; -} - -export const AuditLogChanges = { - name: String, - description: String, - icon_hash: String, - splash_hash: String, - discovery_splash_hash: String, - banner_hash: String, - owner_id: String, - region: String, - preferred_locale: String, - afk_channel_id: String, - afk_timeout: Number, - rules_channel_id: String, - public_updates_channel_id: String, - mfa_level: Number, - verification_level: Number, - explicit_content_filter: Number, - default_message_notifications: Number, - vanity_url_code: String, - $add: [{}], - $remove: [{}], - prune_delete_days: Number, - widget_enabled: Boolean, - widget_channel_id: String, - system_channel_id: String, - position: Number, - topic: String, - bitrate: Number, - permission_overwrites: [{}], - nsfw: Boolean, - application_id: String, - rate_limit_per_user: Number, - permissions: String, - color: Number, - hoist: Boolean, - mentionable: Boolean, - allow: String, - deny: String, - code: String, - channel_id: String, - inviter_id: String, - max_uses: Number, - uses: Number, - max_age: Number, - temporary: Boolean, - deaf: Boolean, - mute: Boolean, - nick: String, - avatar_hash: String, - id: String, - type: Number, - enable_emoticons: Boolean, - expire_behavior: Number, - expire_grace_period: Number, - user_limit: Number, -}; - -export const AuditLogSchema = new Schema({ - target_id: String, - user_id: { type: String, required: true }, - id: { type: String, required: true }, - action_type: { type: Number, required: true }, - options: { - delete_member_days: String, - members_removed: String, - channel_id: String, - messaged_id: String, - count: String, - id: String, - type: { type: Number }, - role_name: String, - }, - changes: [ - { - new_value: AuditLogChanges, - old_value: AuditLogChanges, - key: String, - }, - ], - reason: String, -}); - -// @ts-ignore -export const AuditLogModel = db.model("AuditLog", AuditLogSchema, "auditlogs"); - -export enum AuditLogEvents { - GUILD_UPDATE = 1, - CHANNEL_CREATE = 10, - CHANNEL_UPDATE = 11, - CHANNEL_DELETE = 12, - CHANNEL_OVERWRITE_CREATE = 13, - CHANNEL_OVERWRITE_UPDATE = 14, - CHANNEL_OVERWRITE_DELETE = 15, - MEMBER_KICK = 20, - MEMBER_PRUNE = 21, - MEMBER_BAN_ADD = 22, - MEMBER_BAN_REMOVE = 23, - MEMBER_UPDATE = 24, - MEMBER_ROLE_UPDATE = 25, - MEMBER_MOVE = 26, - MEMBER_DISCONNECT = 27, - BOT_ADD = 28, - ROLE_CREATE = 30, - ROLE_UPDATE = 31, - ROLE_DELETE = 32, - INVITE_CREATE = 40, - INVITE_UPDATE = 41, - INVITE_DELETE = 42, - WEBHOOK_CREATE = 50, - WEBHOOK_UPDATE = 51, - WEBHOOK_DELETE = 52, - EMOJI_CREATE = 60, - EMOJI_UPDATE = 61, - EMOJI_DELETE = 62, - MESSAGE_DELETE = 72, - MESSAGE_BULK_DELETE = 73, - MESSAGE_PIN = 74, - MESSAGE_UNPIN = 75, - INTEGRATION_CREATE = 80, - INTEGRATION_UPDATE = 81, - INTEGRATION_DELETE = 82, -} diff --git a/util/src/entities/Ban.ts b/util/src/entities/Ban.ts new file mode 100644 index 00000000..e8a6d648 --- /dev/null +++ b/util/src/entities/Ban.ts @@ -0,0 +1,37 @@ +import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; +import { BaseClass } from "./BaseClass"; +import { Guild } from "./Guild"; +import { User } from "./User"; + +@Entity("bans") +export class Ban extends BaseClass { + @Column({ nullable: true }) + @RelationId((ban: Ban) => ban.user) + user_id: string; + + @JoinColumn({ name: "user_id" }) + @ManyToOne(() => User) + user: User; + + @Column({ nullable: true }) + @RelationId((ban: Ban) => ban.guild) + guild_id: string; + + @JoinColumn({ name: "guild_id" }) + @ManyToOne(() => Guild) + guild: Guild; + + @Column({ nullable: true }) + @RelationId((ban: Ban) => ban.executor) + executor_id: string; + + @JoinColumn({ name: "executor_id" }) + @ManyToOne(() => User) + executor: User; + + @Column() + ip: string; + + @Column({ nullable: true }) + reason?: string; +} diff --git a/util/src/entities/BaseClass.ts b/util/src/entities/BaseClass.ts new file mode 100644 index 00000000..0856ccd1 --- /dev/null +++ b/util/src/entities/BaseClass.ts @@ -0,0 +1,77 @@ +import "reflect-metadata"; +import { BaseEntity, BeforeInsert, BeforeUpdate, EntityMetadata, FindConditions, 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 BaseClass extends BaseEntity { + @PrimaryColumn() + id: string = Snowflake.generate(); + + // @ts-ignore + constructor(public props?: any) { + super(); + this.assign(props); + } + + get construct(): any { + return this.constructor; + } + + get metadata() { + return this.construct.getRepository().metadata as EntityMetadata; + } + + assign(props: any) { + if (!props || typeof props !== "object") return; + delete props.opts; + delete props.props; + + const properties = new Set( + this.metadata.columns + .map((x: any) => x.propertyName) + .concat(this.metadata.relations.map((x) => x.propertyName)) + ); + // will not include relational properties + + for (const key in props) { + if (!properties.has(key)) continue; + // @ts-ignore + const setter = this[`set${key.capitalize()}`]; + + if (setter) { + setter.call(this, props[key]); + } else { + // @ts-ignore + this[key] = props[key]; + } + } + } + + @BeforeUpdate() + @BeforeInsert() + validate() { + this.assign(this.props); + return this; + } + + toJSON(): any { + return Object.fromEntries( + this.metadata.columns // @ts-ignore + .map((x) => [x.propertyName, this[x.propertyName]]) // @ts-ignore + .concat(this.metadata.relations.map((x) => [x.propertyName, this[x.propertyName]])) + ); + } + + static increment(conditions: FindConditions, propertyPath: string, value: number | string) { + const repository = this.getRepository(); + return repository.increment(conditions, propertyPath, value); + } + + static decrement(conditions: FindConditions, propertyPath: string, value: number | string) { + const repository = this.getRepository(); + return repository.decrement(conditions, propertyPath, value); + } +} diff --git a/util/src/entities/Channel.ts b/util/src/entities/Channel.ts new file mode 100644 index 00000000..e3586dfc --- /dev/null +++ b/util/src/entities/Channel.ts @@ -0,0 +1,171 @@ +import { Column, Entity, JoinColumn, JoinTable, ManyToMany, ManyToOne, OneToMany, RelationId } from "typeorm"; +import { BaseClass } from "./BaseClass"; +import { Guild } from "./Guild"; +import { Message } from "./Message"; +import { User } from "./User"; +import { HTTPError } from "lambert-server"; +import { emitEvent, getPermission, Snowflake } from "../util"; +import { ChannelCreateEvent } from "../interfaces"; +import { Recipient } from "./Recipient"; + +export enum ChannelType { + GUILD_TEXT = 0, // a text channel within a server + DM = 1, // a direct message between users + GUILD_VOICE = 2, // a voice channel within a server + GROUP_DM = 3, // a direct message between multiple users + GUILD_CATEGORY = 4, // an organizational category that contains up to 50 channels + GUILD_NEWS = 5, // a channel that users can follow and crosspost into their own server + GUILD_STORE = 6, // a channel in which game developers can sell their game on Discord +} + +@Entity("channels") +export class Channel extends BaseClass { + @Column() + created_at: Date; + + @Column() + name: string; + + @Column({ type: "simple-enum", enum: ChannelType }) + type: ChannelType; + + @OneToMany(() => Recipient, (recipient: Recipient) => recipient.channel, { cascade: true }) + recipients?: Recipient[]; + + @Column({ nullable: true }) + @RelationId((channel: Channel) => channel.last_message) + last_message_id: string; + + @JoinColumn({ name: "last_message_id" }) + @ManyToOne(() => Message) + last_message?: Message; + + @Column({ nullable: true }) + @RelationId((channel: Channel) => channel.guild) + guild_id?: string; + + @JoinColumn({ name: "guild_id" }) + @ManyToOne(() => Guild) + guild: Guild; + + @Column({ nullable: true }) + @RelationId((channel: Channel) => channel.parent) + parent_id: string; + + @JoinColumn({ name: "parent_id" }) + @ManyToOne(() => Channel) + parent?: Channel; + + @Column({ nullable: true }) + @RelationId((channel: Channel) => channel.owner) + owner_id: string; + + @JoinColumn({ name: "owner_id" }) + @ManyToOne(() => User) + owner: User; + + @Column({ nullable: true }) + last_pin_timestamp?: number; + + @Column({ nullable: true }) + default_auto_archive_duration?: number; + + @Column() + position: number; + + @Column({ type: "simple-json" }) + permission_overwrites: ChannelPermissionOverwrite[]; + + @Column({ nullable: true }) + video_quality_mode?: number; + + @Column({ nullable: true }) + bitrate?: number; + + @Column({ nullable: true }) + user_limit?: number; + + @Column({ nullable: true }) + nsfw?: boolean; + + @Column({ nullable: true }) + rate_limit_per_user?: number; + + @Column({ nullable: true }) + topic?: string; + + // TODO: DM channel + static async createChannel( + channel: Partial, + user_id: string = "0", + opts?: { + keepId?: boolean; + skipExistsCheck?: boolean; + skipPermissionCheck?: boolean; + skipEventEmit?: boolean; + } + ) { + if (!opts?.skipPermissionCheck) { + // Always check if user has permission first + const permissions = await getPermission(user_id, channel.guild_id); + permissions.hasThrow("MANAGE_CHANNELS"); + } + + switch (channel.type) { + case ChannelType.GUILD_TEXT: + case ChannelType.GUILD_VOICE: + if (channel.parent_id && !opts?.skipExistsCheck) { + const exists = await Channel.findOneOrFail({ id: channel.parent_id }); + if (!exists) throw new HTTPError("Parent id channel doesn't exist", 400); + if (exists.guild_id !== channel.guild_id) + throw new HTTPError("The category channel needs to be in the guild"); + } + break; + case ChannelType.GUILD_CATEGORY: + break; + case ChannelType.DM: + case ChannelType.GROUP_DM: + throw new HTTPError("You can't create a dm channel in a guild"); + // TODO: check if guild is community server + case ChannelType.GUILD_STORE: + case ChannelType.GUILD_NEWS: + default: + throw new HTTPError("Not yet supported"); + } + + if (!channel.permission_overwrites) channel.permission_overwrites = []; + // TODO: auto generate position + + channel = { + ...channel, + ...(!opts?.keepId && { id: Snowflake.generate() }), + created_at: new Date(), + position: channel.position || 0, + }; + + await Promise.all([ + Channel.insert(channel), + !opts?.skipEventEmit + ? emitEvent({ + event: "CHANNEL_CREATE", + data: channel, + guild_id: channel.guild_id, + } as ChannelCreateEvent) + : Promise.resolve(), + ]); + + return channel; + } +} + +export interface ChannelPermissionOverwrite { + allow: bigint; // for bitfields we use bigints + deny: bigint; // for bitfields we use bigints + id: string; + type: ChannelPermissionOverwriteType; +} + +export enum ChannelPermissionOverwriteType { + role = 0, + member = 1, +} diff --git a/util/src/entities/Config.ts b/util/src/entities/Config.ts new file mode 100644 index 00000000..5eb55933 --- /dev/null +++ b/util/src/entities/Config.ts @@ -0,0 +1,280 @@ +import { Column, Entity, JoinColumn, ManyToOne } from "typeorm"; +import { BaseClass } from "./BaseClass"; +import crypto from "crypto"; +import { Snowflake } from "../util/Snowflake"; + +@Entity("config") +export class ConfigEntity extends BaseClass { + @Column({ type: "simple-json" }) + value: ConfigValue; +} + +export interface RateLimitOptions { + bot?: number; + count: number; + window: number; + onyIp?: boolean; +} + +export interface Region { + id: string; + name: string; + endpoint: string; + location?: { + latitude: number; + longitude: number; + }; + vip: boolean; + custom: boolean; + deprecated: boolean; +} + +export interface KafkaBroker { + ip: string; + port: number; +} + +export interface ConfigValue { + gateway: { + endpointClient: string | null; + endpoint: string | null; + }; + cdn: { + endpointClient: string | null; + endpoint: string | null; + }; + general: { + instance_id: string; + }; + permissions: { + user: { + createGuilds: boolean; + }; + }; + limits: { + user: { + maxGuilds: number; + maxUsername: number; + maxFriends: number; + }; + guild: { + maxRoles: number; + maxMembers: number; + maxChannels: number; + maxChannelsInCategory: number; + hideOfflineMember: number; + }; + message: { + maxCharacters: number; + maxTTSCharacters: number; + maxReactions: number; + maxAttachmentSize: number; + maxBulkDelete: number; + }; + channel: { + maxPins: number; + maxTopic: number; + }; + rate: { + ip: Omit; + global: RateLimitOptions; + error: RateLimitOptions; + routes: { + guild: RateLimitOptions; + webhook: RateLimitOptions; + channel: RateLimitOptions; + auth: { + login: RateLimitOptions; + register: RateLimitOptions; + }; + // TODO: rate limit configuration for all routes + }; + }; + }; + security: { + autoUpdate: boolean | number; + requestSignature: string; + jwtSecret: string; + forwadedFor: string | null; // header to get the real user ip address + captcha: { + enabled: boolean; + service: "recaptcha" | "hcaptcha" | null; // TODO: hcaptcha, custom + sitekey: string | null; + secret: string | null; + }; + ipdataApiKey: string | null; + }; + login: { + requireCaptcha: boolean; + }; + register: { + email: { + necessary: boolean; // we have to use necessary instead of required as the cli tool uses json schema and can't use required + allowlist: boolean; + blocklist: boolean; + domains: string[]; + }; + dateOfBirth: { + necessary: boolean; + minimum: number; // in years + }; + requireCaptcha: boolean; + requireInvite: boolean; + allowNewRegistration: boolean; + allowMultipleAccounts: boolean; + blockProxies: boolean; + password: { + minLength: number; + minNumbers: number; + minUpperCase: number; + minSymbols: number; + }; + }; + regions: { + default: string; + useDefaultAsOptimal: boolean; + available: Region[]; + }; + rabbitmq: { + host: string | null; + }; + kafka: { + brokers: KafkaBroker[] | null; + }; +} + +export const DefaultConfigOptions: ConfigValue = { + gateway: { + endpointClient: null, + endpoint: null, + }, + cdn: { + endpointClient: null, + endpoint: null, + }, + general: { + instance_id: Snowflake.generate(), + }, + permissions: { + user: { + createGuilds: true, + }, + }, + limits: { + user: { + maxGuilds: 100, + maxUsername: 32, + maxFriends: 1000, + }, + guild: { + maxRoles: 250, + maxMembers: 250000, + maxChannels: 500, + maxChannelsInCategory: 50, + hideOfflineMember: 1000, + }, + message: { + maxCharacters: 2000, + maxTTSCharacters: 200, + maxReactions: 20, + maxAttachmentSize: 8388608, + maxBulkDelete: 100, + }, + channel: { + maxPins: 50, + maxTopic: 1024, + }, + rate: { + ip: { + count: 500, + window: 5, + }, + global: { + count: 20, + window: 5, + bot: 250, + }, + error: { + count: 10, + window: 5, + }, + routes: { + guild: { + count: 5, + window: 5, + }, + webhook: { + count: 10, + window: 5, + }, + channel: { + count: 10, + window: 5, + }, + auth: { + login: { + count: 5, + window: 60, + }, + register: { + count: 2, + window: 60 * 60 * 12, + }, + }, + }, + }, + }, + security: { + autoUpdate: true, + requestSignature: crypto.randomBytes(32).toString("base64"), + jwtSecret: crypto.randomBytes(256).toString("base64"), + forwadedFor: null, + // forwadedFor: "X-Forwarded-For" // nginx/reverse proxy + // forwadedFor: "CF-Connecting-IP" // cloudflare: + captcha: { + enabled: false, + service: null, + sitekey: null, + secret: null, + }, + ipdataApiKey: "eca677b284b3bac29eb72f5e496aa9047f26543605efe99ff2ce35c9", + }, + login: { + requireCaptcha: false, + }, + register: { + email: { + necessary: true, + allowlist: false, + blocklist: true, + domains: [], // TODO: efficiently save domain blocklist in database + // domains: fs.readFileSync(__dirname + "/blockedEmailDomains.txt", { encoding: "utf8" }).split("\n"), + }, + dateOfBirth: { + necessary: true, + minimum: 13, + }, + requireInvite: false, + requireCaptcha: true, + allowNewRegistration: true, + allowMultipleAccounts: true, + blockProxies: true, + password: { + minLength: 8, + minNumbers: 2, + minUpperCase: 2, + minSymbols: 0, + }, + }, + regions: { + default: "fosscord", + useDefaultAsOptimal: true, + available: [{ id: "fosscord", name: "Fosscord", endpoint: "127.0.0.1", vip: false, custom: false, deprecated: false }], + }, + rabbitmq: { + host: null, + }, + kafka: { + brokers: null, + }, +}; diff --git a/util/src/entities/ConnectedAccount.ts b/util/src/entities/ConnectedAccount.ts new file mode 100644 index 00000000..75982d01 --- /dev/null +++ b/util/src/entities/ConnectedAccount.ts @@ -0,0 +1,38 @@ +import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; +import { BaseClass } from "./BaseClass"; +import { User } from "./User"; + +@Entity("connected_accounts") +export class ConnectedAccount extends BaseClass { + @Column({ nullable: true }) + @RelationId((account: ConnectedAccount) => account.user) + user_id: string; + + @JoinColumn({ name: "user_id" }) + @ManyToOne(() => User) + user: User; + + @Column({ select: false }) + access_token: string; + + @Column({ select: false }) + friend_sync: boolean; + + @Column() + name: string; + + @Column({ select: false }) + revoked: boolean; + + @Column({ select: false }) + show_activity: boolean; + + @Column() + type: string; + + @Column() + verifie: boolean; + + @Column({ select: false }) + visibility: number; +} diff --git a/util/src/entities/Emoji.ts b/util/src/entities/Emoji.ts new file mode 100644 index 00000000..181aff2c --- /dev/null +++ b/util/src/entities/Emoji.ts @@ -0,0 +1,29 @@ +import { Column, Entity, JoinColumn, ManyToOne } from "typeorm"; +import { BaseClass } from "./BaseClass"; +import { Guild } from "./Guild"; +import { Role } from "./Role"; + +@Entity("emojis") +export class Emoji extends BaseClass { + @Column() + animated: boolean; + + @Column() + available: boolean; // whether this emoji can be used, may be false due to loss of Server Boosts + + @Column() + guild_id: string; + + @JoinColumn({ name: "guild_id" }) + @ManyToOne(() => Guild) + guild: Guild; + + @Column() + managed: boolean; + + @Column() + name: string; + + @Column() + require_colons: boolean; +} diff --git a/util/src/entities/Guild.ts b/util/src/entities/Guild.ts new file mode 100644 index 00000000..032a9415 --- /dev/null +++ b/util/src/entities/Guild.ts @@ -0,0 +1,212 @@ +import { Column, Entity, JoinColumn, ManyToMany, ManyToOne, OneToMany, OneToOne, RelationId } from "typeorm"; +import { Ban } from "./Ban"; +import { BaseClass } from "./BaseClass"; +import { Channel } from "./Channel"; +import { Emoji } from "./Emoji"; +import { Invite } from "./Invite"; +import { Member } from "./Member"; +import { Role } from "./Role"; +import { Sticker } from "./Sticker"; +import { Template } from "./Template"; +import { User } from "./User"; +import { VoiceState } from "./VoiceState"; +import { Webhook } from "./Webhook"; + +// TODO: application_command_count, application_command_counts: {1: 0, 2: 0, 3: 0} +// TODO: guild_scheduled_events +// TODO: stage_instances +// TODO: threads + +@Entity("guilds") +export class Guild extends BaseClass { + @Column({ nullable: true }) + @RelationId((guild: Guild) => guild.afk_channel) + afk_channel_id?: string; + + @JoinColumn({ name: "afk_channel_id" }) + @ManyToOne(() => Channel) + afk_channel?: Channel; + + @Column({ nullable: true }) + afk_timeout?: number; + + // * commented out -> use owner instead + // application id of the guild creator if it is bot-created + // @Column({ nullable: true }) + // application?: string; + + @JoinColumn({ name: "ban_ids" }) + @OneToMany(() => Ban, (ban: Ban) => ban.guild) + bans: Ban[]; + + @Column({ nullable: true }) + banner?: string; + + @Column({ nullable: true }) + default_message_notifications?: number; + + @Column({ nullable: true }) + description?: string; + + @Column({ nullable: true }) + discovery_splash?: string; + + @Column({ nullable: true }) + explicit_content_filter?: number; + + @Column({ type: "simple-array" }) + features: string[]; //TODO use enum + + @Column({ nullable: true }) + icon?: string; + + @Column({ nullable: true }) + large?: boolean; + + @Column({ nullable: true }) + max_members?: number; // e.g. default 100.000 + + @Column({ nullable: true }) + max_presences?: number; + + @Column({ nullable: true }) + max_video_channel_users?: number; // ? default: 25, is this max 25 streaming or watching + + @Column({ nullable: true }) + member_count?: number; + + @Column({ nullable: true }) + presence_count?: number; // users online + + @OneToMany(() => Member, (member: Member) => member.guild) + members: Member[]; + + @JoinColumn({ name: "role_ids" }) + @OneToMany(() => Role, (role: Role) => role.guild) + roles: Role[]; + + @JoinColumn({ name: "channel_ids" }) + @OneToMany(() => Channel, (channel: Channel) => channel.guild) + channels: Channel[]; + + @Column({ nullable: true }) + @RelationId((guild: Guild) => guild.template) + template_id: string; + + @JoinColumn({ name: "template_id" }) + @ManyToOne(() => Template) + template: Template; + + @JoinColumn({ name: "emoji_ids" }) + @OneToMany(() => Emoji, (emoji: Emoji) => emoji.guild) + emojis: Emoji[]; + + @JoinColumn({ name: "sticker_ids" }) + @OneToMany(() => Sticker, (sticker: Sticker) => sticker.guild) + stickers: Sticker[]; + + @JoinColumn({ name: "invite_ids" }) + @OneToMany(() => Invite, (invite: Invite) => invite.guild) + invites: Invite[]; + + @JoinColumn({ name: "voice_state_ids" }) + @OneToMany(() => VoiceState, (voicestate: VoiceState) => voicestate.guild) + voice_states: VoiceState[]; + + @JoinColumn({ name: "webhook_ids" }) + @OneToMany(() => Webhook, (webhook: Webhook) => webhook.guild) + webhooks: Webhook[]; + + @Column({ nullable: true }) + mfa_level?: number; + + @Column() + name: string; + + @Column({ nullable: true }) + @RelationId((guild: Guild) => guild.owner) + owner_id: string; + + @JoinColumn([{ name: "owner_id", referencedColumnName: "id" }]) + @ManyToOne(() => User) + owner: User; + + @Column({ nullable: true }) + preferred_locale?: string; // only community guilds can choose this + + @Column({ nullable: true }) + premium_subscription_count?: number; + + @Column({ nullable: true }) + premium_tier?: number; // nitro boost level + + @Column({ nullable: true }) + @RelationId((guild: Guild) => guild.public_updates_channel) + public_updates_channel_id: string; + + @JoinColumn({ name: "public_updates_channel_id" }) + @ManyToOne(() => Channel) + public_updates_channel?: Channel; + + @Column({ nullable: true }) + @RelationId((guild: Guild) => guild.rules_channel) + rules_channel_id?: string; + + @JoinColumn({ name: "rules_channel_id" }) + @ManyToOne(() => Channel) + rules_channel?: string; + + @Column({ nullable: true }) + region?: string; + + @Column({ nullable: true }) + splash?: string; + + @Column({ nullable: true }) + @RelationId((guild: Guild) => guild.system_channel) + system_channel_id?: string; + + @JoinColumn({ name: "system_channel_id" }) + @ManyToOne(() => Channel) + system_channel?: Channel; + + @Column({ nullable: true }) + system_channel_flags?: number; + + @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; + + @Column({ type: "simple-json" }) + welcome_screen: { + enabled: boolean; + description: string; + welcome_channels: { + description: string; + emoji_id?: string; + emoji_name: string; + channel_id: string; + }[]; + }; + + @Column({ nullable: true }) + @RelationId((guild: Guild) => guild.widget_channel) + widget_channel_id?: string; + + @JoinColumn({ name: "widget_channel_id" }) + @ManyToOne(() => Channel) + widget_channel?: Channel; + + @Column({ nullable: true }) + widget_enabled?: boolean; +} diff --git a/util/src/entities/Invite.ts b/util/src/entities/Invite.ts new file mode 100644 index 00000000..01e22294 --- /dev/null +++ b/util/src/entities/Invite.ts @@ -0,0 +1,64 @@ +import { Column, Entity, JoinColumn, ManyToOne, PrimaryColumn, RelationId } from "typeorm"; +import { BaseClass } from "./BaseClass"; +import { Channel } from "./Channel"; +import { Guild } from "./Guild"; +import { User } from "./User"; + +@Entity("invites") +export class Invite extends BaseClass { + @PrimaryColumn() + code: string; + + @Column() + temporary: boolean; + + @Column() + uses: number; + + @Column() + max_uses: number; + + @Column() + max_age: number; + + @Column() + created_at: Date; + + @Column() + expires_at: Date; + + @Column({ nullable: true }) + @RelationId((invite: Invite) => invite.guild) + guild_id: string; + + @JoinColumn({ name: "guild_id" }) + @ManyToOne(() => Guild) + guild: Guild; + + @Column({ nullable: true }) + @RelationId((invite: Invite) => invite.channel) + channel_id: string; + + @JoinColumn({ name: "channel_id" }) + @ManyToOne(() => Channel) + channel: Channel; + + @Column({ nullable: true }) + @RelationId((invite: Invite) => invite.inviter) + inviter_id: string; + + @JoinColumn({ name: "inviter_id" }) + @ManyToOne(() => User) + inviter: User; + + @Column({ nullable: true }) + @RelationId((invite: Invite) => invite.target_user) + target_user_id: string; + + @JoinColumn({ name: "target_user_id" }) + @ManyToOne(() => User) + target_user?: string; // could be used for "User specific invites" https://github.com/fosscord/fosscord/issues/62 + + @Column({ nullable: true }) + target_user_type?: number; +} diff --git a/util/src/entities/Member.ts b/util/src/entities/Member.ts new file mode 100644 index 00000000..d2d78bb9 --- /dev/null +++ b/util/src/entities/Member.ts @@ -0,0 +1,287 @@ +import { PublicUser, User } from "./User"; +import { BaseClass } from "./BaseClass"; +import { Column, Entity, JoinColumn, JoinTable, ManyToMany, ManyToOne, OneToMany, RelationId } from "typeorm"; +import { Guild } from "./Guild"; +import { Config, emitEvent } from "../util"; +import { + GuildCreateEvent, + GuildDeleteEvent, + GuildMemberAddEvent, + GuildMemberRemoveEvent, + GuildMemberUpdateEvent, +} from "../interfaces"; +import { HTTPError } from "lambert-server"; +import { Role } from "./Role"; + +@Entity("members") +export class Member extends BaseClass { + @JoinColumn({ name: "id" }) + @ManyToOne(() => User) + user: User; + + @Column({ nullable: true }) + @RelationId((member: Member) => member.guild) + guild_id: string; + + @JoinColumn({ name: "guild_id" }) + @ManyToOne(() => Guild) + guild: Guild; + + @Column({ nullable: true }) + nick?: string; + + @JoinTable({ name: "member_roles" }) + @ManyToMany(() => Role) + roles: Role[]; + + @Column() + joined_at: Date; + + @Column({ nullable: true }) + premium_since?: number; + + @Column() + deaf: boolean; + + @Column() + mute: boolean; + + @Column() + pending: boolean; + + @Column({ type: "simple-json" }) + settings: UserGuildSettings; + + // TODO: update + @Column({ type: "simple-json" }) + read_state: Record; + + static async IsInGuildOrFail(user_id: string, guild_id: string) { + if (await Member.count({ id: user_id, guild: { id: guild_id } })) return true; + throw new HTTPError("You are not member of this guild", 403); + } + + static async removeFromGuild(user_id: string, guild_id: string) { + const guild = await Guild.findOneOrFail({ select: ["owner_id"], where: { id: guild_id } }); + if (guild.owner_id === user_id) throw new Error("The owner cannot be removed of the guild"); + const member = await Member.findOneOrFail({ where: { id: user_id, guild_id }, relations: ["user"] }); + + // use promise all to execute all promises at the same time -> save time + return Promise.all([ + Member.delete({ + id: user_id, + guild_id: guild_id, + }), + Guild.decrement({ id: guild_id }, "member_count", -1), + + emitEvent({ + event: "GUILD_DELETE", + data: { + id: guild_id, + }, + user_id: user_id, + } as GuildDeleteEvent), + emitEvent({ + event: "GUILD_MEMBER_REMOVE", + data: { + guild_id: guild_id, + user: member.user, + }, + guild_id: guild_id, + } as GuildMemberRemoveEvent), + ]); + } + + static async addRole(user_id: string, guild_id: string, role_id: string) { + const [member] = await Promise.all([ + // @ts-ignore + Member.findOneOrFail({ + where: { id: user_id, guild_id: guild_id }, + relations: ["user", "roles"], // we don't want to load the role objects just the ids + select: ["roles.id"], + }), + await Role.findOneOrFail({ id: role_id, guild_id: guild_id }), + ]); + member.roles.push(new Role({ id: role_id })); + + await Promise.all([ + member.save(), + emitEvent({ + event: "GUILD_MEMBER_UPDATE", + data: { + guild_id: guild_id, + user: member.user, + roles: member.roles.map((x) => x.id), + }, + guild_id: guild_id, + } as GuildMemberUpdateEvent), + ]); + } + + static async removeRole(user_id: string, guild_id: string, role_id: string) { + const [member] = await Promise.all([ + // @ts-ignore + Member.findOneOrFail({ + where: { id: user_id, guild_id: guild_id }, + relations: ["user", "roles"], // we don't want to load the role objects just the ids + select: ["roles.id"], + }), + await Role.findOneOrFail({ id: role_id, guild_id: guild_id }), + ]); + member.roles = member.roles.filter((x) => x.id == role_id); + + await Promise.all([ + member.save(), + emitEvent({ + event: "GUILD_MEMBER_UPDATE", + data: { + guild_id: guild_id, + user: member.user, + roles: member.roles.map((x) => x.id), + }, + guild_id: guild_id, + } as GuildMemberUpdateEvent), + ]); + } + + static async changeNickname(user_id: string, guild_id: string, nickname: string) { + const member = await Member.findOneOrFail({ + where: { + id: user_id, + guild_id: guild_id, + }, + relations: ["user"], + }); + member.nick = nickname; + + await Promise.all([ + member.save(), + + emitEvent({ + event: "GUILD_MEMBER_UPDATE", + data: { + guild_id: guild_id, + user: member.user, + nick: nickname, + }, + guild_id: guild_id, + } as GuildMemberUpdateEvent), + ]); + } + + static async addToGuild(user_id: string, guild_id: string) { + const user = await User.getPublicUser(user_id); + + const { maxGuilds } = Config.get().limits.user; + const guild_count = await Member.count({ id: user_id }); + if (guild_count >= maxGuilds) { + throw new HTTPError(`You are at the ${maxGuilds} server limit.`, 403); + } + + const guild = await Guild.findOneOrFail({ + where: { + id: guild_id, + }, + relations: ["channels", "emojis", "members", "roles", "stickers"], + }); + + if (await Member.count({ id: user.id, guild: { id: guild_id } })) + throw new HTTPError("You are already a member of this guild", 400); + + const member = { + id: user_id, + guild_id: guild_id, + nick: undefined, + roles: [guild_id], // @everyone role + joined_at: new Date(), + premium_since: undefined, + deaf: false, + mute: false, + pending: false, + }; + // @ts-ignore + guild.joined_at = member.joined_at.toISOString(); + + await Promise.all([ + Member.insert({ + ...member, + roles: undefined, + read_state: {}, + settings: { + channel_overrides: [], + message_notifications: 0, + mobile_push: true, + muted: false, + suppress_everyone: false, + suppress_roles: false, + version: 0, + }, + }), + Guild.increment({ id: guild_id }, "member_count", 1), + emitEvent({ + event: "GUILD_MEMBER_ADD", + data: { + ...member, + user, + guild_id: guild_id, + }, + guild_id: guild_id, + } as GuildMemberAddEvent), + emitEvent({ + event: "GUILD_CREATE", + data: { ...guild, members: [...guild.members, member] }, + user_id, + } as GuildCreateEvent), + ]); + } +} + +export interface UserGuildSettings { + channel_overrides: { + channel_id: string; + message_notifications: number; + mute_config: MuteConfig; + muted: boolean; + }[]; + message_notifications: number; + mobile_push: boolean; + mute_config: MuteConfig; + muted: boolean; + suppress_everyone: boolean; + suppress_roles: boolean; + version: number; +} + +export interface MuteConfig { + end_time: number; + selected_time_window: number; +} + +export type PublicMemberKeys = + | "id" + | "guild_id" + | "nick" + | "roles" + | "joined_at" + | "pending" + | "deaf" + | "mute" + | "premium_since"; + +export const PublicMemberProjection: PublicMemberKeys[] = [ + "id", + "guild_id", + "nick", + "roles", + "joined_at", + "pending", + "deaf", + "mute", + "premium_since", +]; + +// @ts-ignore +export type PublicMember = Pick> & { + user: PublicUser; + roles: string[]; // only role ids not objects +}; diff --git a/util/src/entities/Message.ts b/util/src/entities/Message.ts new file mode 100644 index 00000000..542b2b55 --- /dev/null +++ b/util/src/entities/Message.ts @@ -0,0 +1,264 @@ +import { User } from "./User"; +import { Member } from "./Member"; +import { Role } from "./Role"; +import { Channel } from "./Channel"; +import { InteractionType } from "../interfaces/Interaction"; +import { Application } from "./Application"; +import { + Column, + CreateDateColumn, + Entity, + JoinColumn, + JoinTable, + ManyToMany, + ManyToOne, + OneToMany, + RelationId, + UpdateDateColumn, +} from "typeorm"; +import { BaseClass } from "./BaseClass"; +import { Guild } from "./Guild"; +import { Webhook } from "./Webhook"; +import { Sticker } from "./Sticker"; +import { Attachment } from "./Attachment"; + +export enum MessageType { + DEFAULT = 0, + RECIPIENT_ADD = 1, + RECIPIENT_REMOVE = 2, + CALL = 3, + CHANNEL_NAME_CHANGE = 4, + CHANNEL_ICON_CHANGE = 5, + CHANNEL_PINNED_MESSAGE = 6, + GUILD_MEMBER_JOIN = 7, + USER_PREMIUM_GUILD_SUBSCRIPTION = 8, + USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_1 = 9, + USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_2 = 10, + USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_3 = 11, + CHANNEL_FOLLOW_ADD = 12, + GUILD_DISCOVERY_DISQUALIFIED = 14, + GUILD_DISCOVERY_REQUALIFIED = 15, + REPLY = 19, + APPLICATION_COMMAND = 20, +} + +@Entity("messages") +export class Message extends BaseClass { + @Column() + id: string; + + @Column({ nullable: true }) + @RelationId((message: Message) => message.channel) + channel_id: string; + + @JoinColumn({ name: "channel_id" }) + @ManyToOne(() => Channel) + channel: Channel; + + @Column({ nullable: true }) + @RelationId((message: Message) => message.guild) + guild_id?: string; + + @JoinColumn({ name: "guild_id" }) + @ManyToOne(() => Guild) + guild?: Guild; + + @Column({ nullable: true }) + @RelationId((message: Message) => message.author) + author_id: string; + + @JoinColumn({ name: "author_id", referencedColumnName: "id" }) + @ManyToOne(() => User) + author?: User; + + @Column({ nullable: true }) + @RelationId((message: Message) => message.member) + member_id: string; + + @JoinColumn({ name: "member_id" }) + @ManyToOne(() => Member) + member?: Member; + + @Column({ nullable: true }) + @RelationId((message: Message) => message.webhook) + webhook_id: string; + + @JoinColumn({ name: "webhook_id" }) + @ManyToOne(() => Webhook) + webhook?: Webhook; + + @Column({ nullable: true }) + @RelationId((message: Message) => message.application) + application_id: string; + + @JoinColumn({ name: "application_id" }) + @ManyToOne(() => Application) + application?: Application; + + @Column({ nullable: true }) + content?: string; + + @Column() + @CreateDateColumn() + timestamp: Date; + + @Column() + @UpdateDateColumn() + edited_timestamp?: Date; + + @Column({ nullable: true }) + tts?: boolean; + + @Column({ nullable: true }) + mention_everyone?: boolean; + + @JoinTable({ name: "message_user_mentions" }) + @ManyToMany(() => User) + mentions: User[]; + + @JoinTable({ name: "message_role_mentions" }) + @ManyToMany(() => Role) + mention_roles: Role[]; + + @JoinTable({ name: "message_channel_mentions" }) + @ManyToMany(() => Channel) + mention_channels: Channel[]; + + @JoinTable({ name: "message_stickers" }) + @ManyToMany(() => Sticker) + sticker_items?: Sticker[]; + + @JoinColumn({ name: "attachment_ids" }) + @OneToMany(() => Attachment, (attachment: Attachment) => attachment.message) + attachments?: Attachment[]; + + @Column({ type: "simple-json" }) + embeds: Embed[]; + + @Column({ type: "simple-json" }) + reactions: Reaction[]; + + @Column({ type: "text", nullable: true }) + nonce?: string | number; + + @Column({ nullable: true }) + pinned?: boolean; + + @Column({ type: "simple-enum", enum: MessageType }) + type: MessageType; + + @Column({ type: "simple-json", nullable: true }) + activity?: { + type: number; + party_id: string; + }; + + @Column({ nullable: true }) + flags?: string; + @Column({ type: "simple-json", nullable: true }) + message_reference?: { + message_id: string; + channel_id?: string; + guild_id?: string; + }; + + @JoinColumn({ name: "message_reference_id" }) + @ManyToOne(() => Message) + referenced_message?: Message; + + @Column({ type: "simple-json", nullable: true }) + interaction?: { + id: string; + type: InteractionType; + name: string; + user_id: string; // the user who invoked the interaction + // user: User; // TODO: autopopulate user + }; + + @Column({ type: "simple-json", nullable: true }) + components?: MessageComponent[]; +} + +export interface MessageComponent { + type: number; + style?: number; + label?: string; + emoji?: PartialEmoji; + custom_id?: string; + url?: string; + disabled?: boolean; + components: MessageComponent[]; +} + +export enum MessageComponentType { + ActionRow = 1, + Button = 2, +} + +export interface Embed { + title?: string; //title of embed + type?: EmbedType; // type of embed (always "rich" for webhook embeds) + description?: string; // description of embed + url?: string; // url of embed + timestamp?: Date; // timestamp of embed content + color?: number; // color code of the embed + footer?: { + text: string; + icon_url?: string; + proxy_icon_url?: string; + }; // footer object footer information + image?: EmbedImage; // image object image information + thumbnail?: EmbedImage; // thumbnail object thumbnail information + video?: EmbedImage; // video object video information + provider?: { + name?: string; + url?: string; + }; // provider object provider information + author?: { + name?: string; + url?: string; + icon_url?: string; + proxy_icon_url?: string; + }; // author object author information + fields?: { + name: string; + value: string; + inline?: boolean; + }[]; +} + +export enum EmbedType { + rich = "rich", + image = "image", + video = "video", + gifv = "gifv", + article = "article", + link = "link", +} + +export interface EmbedImage { + url?: string; + proxy_url?: string; + height?: number; + width?: number; +} + +export interface Reaction { + count: number; + //// not saved in the database // me: boolean; // whether the current user reacted using this emoji + emoji: PartialEmoji; + user_ids: string[]; +} + +export interface PartialEmoji { + id?: string; + name: string; + animated?: boolean; +} + +export interface AllowedMentions { + parse?: ("users" | "roles" | "everyone")[]; + roles?: string[]; + users?: string[]; + replied_user?: boolean; +} diff --git a/util/src/entities/RateLimit.ts b/util/src/entities/RateLimit.ts new file mode 100644 index 00000000..fa9c32c1 --- /dev/null +++ b/util/src/entities/RateLimit.ts @@ -0,0 +1,20 @@ +import { Column, Entity } from "typeorm"; +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; + + @Column() + hits: number; + + @Column() + blocked: boolean; + + @Column() + expires_at: Date; +} diff --git a/util/src/entities/ReadState.ts b/util/src/entities/ReadState.ts new file mode 100644 index 00000000..8dd05b21 --- /dev/null +++ b/util/src/entities/ReadState.ts @@ -0,0 +1,45 @@ +import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; +import { BaseClass } from "./BaseClass"; +import { Channel } from "./Channel"; +import { Message } from "./Message"; +import { User } from "./User"; + +// for read receipts +// notification cursor and public read receipt need to be forwards-only (the former to prevent re-pinging when marked as unread, and the latter to be acceptable as a legal acknowledgement in criminal proceedings), and private read marker needs to be advance-rewind capable +// public read receipt ≥ notification cursor ≥ private fully read marker + +@Entity("read_states") +export class ReadState extends BaseClass { + @Column({ nullable: true }) + @RelationId((read_state: ReadState) => read_state.channel) + channel_id: string; + + @JoinColumn({ name: "channel_id" }) + @ManyToOne(() => Channel) + channel: Channel; + + @Column({ nullable: true }) + @RelationId((read_state: ReadState) => read_state.user) + user_id: string; + + @JoinColumn({ name: "user_id" }) + @ManyToOne(() => User) + user: User; + + @Column({ nullable: true }) + @RelationId((read_state: ReadState) => read_state.last_message) + last_message_id: string; + + @JoinColumn({ name: "last_message_id" }) + @ManyToOne(() => Message) + last_message?: Message; + + @Column({ nullable: true }) + last_pin_timestamp?: Date; + + @Column() + mention_count: number; + + @Column() + manual: boolean; +} diff --git a/util/src/entities/Recipient.ts b/util/src/entities/Recipient.ts new file mode 100644 index 00000000..75d5b94d --- /dev/null +++ b/util/src/entities/Recipient.ts @@ -0,0 +1,19 @@ +import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; +import { BaseClass } from "./BaseClass"; + +@Entity("recipients") +export class Recipient extends BaseClass { + @Column() + @RelationId((recipient: Recipient) => recipient.channel) + channel_id: string; + + @JoinColumn({ name: "channel_id" }) + @ManyToOne(() => require("./Channel").Channel) + channel: import("./Channel").Channel; + + @JoinColumn({ name: "id" }) + @ManyToOne(() => require("./User").User) + user: import("./User").User; + + // TODO: settings/mute/nick/added at/encryption keys/read_state +} diff --git a/util/src/entities/Relationship.ts b/util/src/entities/Relationship.ts new file mode 100644 index 00000000..5935f5b6 --- /dev/null +++ b/util/src/entities/Relationship.ts @@ -0,0 +1,27 @@ +import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; +import { BaseClass } from "./BaseClass"; +import { User } from "./User"; + +export enum RelationshipType { + outgoing = 4, + incoming = 3, + blocked = 2, + friends = 1, +} + +@Entity("relationships") +export class Relationship extends BaseClass { + @Column({ nullable: true }) + @RelationId((relationship: Relationship) => relationship.user) + user_id: string; + + @JoinColumn({ name: "user_id" }) + @ManyToOne(() => User) + user: User; + + @Column({ nullable: true }) + nickname?: string; + + @Column({ type: "simple-enum", enum: RelationshipType }) + type: RelationshipType; +} diff --git a/util/src/entities/Role.ts b/util/src/entities/Role.ts new file mode 100644 index 00000000..33c8d272 --- /dev/null +++ b/util/src/entities/Role.ts @@ -0,0 +1,43 @@ +import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; + +import { BaseClass } from "./BaseClass"; +import { Guild } from "./Guild"; + +@Entity("roles") +export class Role extends BaseClass { + @Column({ nullable: true }) + @RelationId((role: Role) => role.guild) + guild_id: string; + + @JoinColumn({ name: "guild_id" }) + @ManyToOne(() => Guild) + guild: Guild; + + @Column() + color: number; + + @Column() + hoist: boolean; + + @Column() + managed: boolean; + + @Column() + mentionable: boolean; + + @Column() + name: string; + + @Column() + permissions: string; + + @Column() + position: number; + + @Column({ type: "simple-json", nullable: true }) + tags?: { + bot_id?: string; + integration_id?: string; + premium_subscriber?: boolean; + }; +} diff --git a/util/src/entities/Sticker.ts b/util/src/entities/Sticker.ts new file mode 100644 index 00000000..7730a86a --- /dev/null +++ b/util/src/entities/Sticker.ts @@ -0,0 +1,42 @@ +import { Column, Entity, JoinColumn, ManyToOne } from "typeorm"; +import { BaseClass } from "./BaseClass"; +import { Guild } from "./Guild"; + +export enum StickerType { + STANDARD = 1, + GUILD = 2, +} + +export enum StickerFormatType { + PNG = 1, + APNG = 2, + LOTTIE = 3, +} + +@Entity("stickers") +export class Sticker extends BaseClass { + @Column() + name: string; + + @Column({ nullable: true }) + description?: string; + + @Column() + tags: string; + + @Column() + pack_id: string; + + @Column({ nullable: true }) + guild_id?: string; + + @JoinColumn({ name: "guild_id" }) + @ManyToOne(() => Guild) + guild?: Guild; + + @Column({ type: "simple-enum", enum: StickerType }) + type: StickerType; + + @Column({ type: "simple-enum", enum: StickerFormatType }) + format_type: StickerFormatType; +} diff --git a/util/src/entities/Team.ts b/util/src/entities/Team.ts new file mode 100644 index 00000000..beb8bf68 --- /dev/null +++ b/util/src/entities/Team.ts @@ -0,0 +1,25 @@ +import { Column, Entity, JoinColumn, ManyToMany, ManyToOne, OneToMany, RelationId } from "typeorm"; +import { BaseClass } from "./BaseClass"; +import { TeamMember } from "./TeamMember"; +import { User } from "./User"; + +@Entity("teams") +export class Team extends BaseClass { + @Column({ nullable: true }) + icon?: string; + + @JoinColumn({ name: "member_ids" }) + @OneToMany(() => TeamMember, (member: TeamMember) => member.team) + members: TeamMember[]; + + @Column() + name: string; + + @Column({ nullable: true }) + @RelationId((team: Team) => team.owner_user) + owner_user_id: string; + + @JoinColumn({ name: "owner_user_id" }) + @ManyToOne(() => User) + owner_user: User; +} diff --git a/util/src/entities/TeamMember.ts b/util/src/entities/TeamMember.ts new file mode 100644 index 00000000..6b184d08 --- /dev/null +++ b/util/src/entities/TeamMember.ts @@ -0,0 +1,33 @@ +import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; +import { BaseClass } from "./BaseClass"; +import { User } from "./User"; + +export enum TeamMemberState { + INVITED = 1, + ACCEPTED = 2, +} + +@Entity("team_members") +export class TeamMember extends BaseClass { + @Column({ type: "simple-enum", enum: TeamMemberState }) + membership_state: TeamMemberState; + + @Column({ type: "simple-array" }) + permissions: string[]; + + @Column({ nullable: true }) + @RelationId((member: TeamMember) => member.team) + team_id: string; + + @JoinColumn({ name: "team_id" }) + @ManyToOne(() => require("./Team").Team, (team: import("./Team").Team) => team.members) + team: import("./Team").Team; + + @Column({ nullable: true }) + @RelationId((member: TeamMember) => member.user) + user_id: string; + + @JoinColumn({ name: "user_id" }) + @ManyToOne(() => User) + user: User; +} diff --git a/util/src/entities/Template.ts b/util/src/entities/Template.ts new file mode 100644 index 00000000..76f77ba6 --- /dev/null +++ b/util/src/entities/Template.ts @@ -0,0 +1,44 @@ +import { Column, Entity, JoinColumn, ManyToOne, PrimaryColumn, RelationId } from "typeorm"; +import { BaseClass } from "./BaseClass"; +import { Guild } from "./Guild"; +import { User } from "./User"; + +@Entity("templates") +export class Template extends BaseClass { + @PrimaryColumn() + code: string; + + @Column() + name: string; + + @Column({ nullable: true }) + description?: string; + + @Column({ nullable: true }) + usage_count?: number; + + @Column({ nullable: true }) + @RelationId((template: Template) => template.creator) + creator_id: string; + + @JoinColumn({ name: "creator_id" }) + @ManyToOne(() => User) + creator: User; + + @Column() + created_at: Date; + + @Column() + updated_at: Date; + + @Column({ nullable: true }) + @RelationId((template: Template) => template.source_guild) + source_guild_id: string; + + @JoinColumn({ name: "source_guild_id" }) + @ManyToOne(() => Guild) + source_guild: Guild; + + @Column({ type: "simple-json" }) + serialized_source_guild: Guild; +} diff --git a/util/src/entities/User.ts b/util/src/entities/User.ts new file mode 100644 index 00000000..39f654be --- /dev/null +++ b/util/src/entities/User.ts @@ -0,0 +1,243 @@ +import { Column, Entity, FindOneOptions, JoinColumn, ManyToMany, OneToMany, RelationId } from "typeorm"; +import { BaseClass } from "./BaseClass"; +import { BitField } from "../util/BitField"; +import { Relationship } from "./Relationship"; +import { ConnectedAccount } from "./ConnectedAccount"; +import { HTTPError } from "lambert-server"; +import { Channel } from "./Channel"; + +type PublicUserKeys = + | "username" + | "discriminator" + | "id" + | "public_flags" + | "avatar" + | "accent_color" + | "banner" + | "bio" + | "bot"; +export const PublicUserProjection: PublicUserKeys[] = [ + "username", + "discriminator", + "id", + "public_flags", + "avatar", + "accent_color", + "banner", + "bio", + "bot", +]; + +// Private user data that should never get sent to the client +export type PublicUser = Pick; + +@Entity("users") +export class User extends BaseClass { + @Column() + username: string; // username max length 32, min 2 (should be configurable) + + @Column() + discriminator: string; // #0001 4 digit long string from #0001 - #9999 + + setDiscriminator(val: string) { + const number = Number(val); + if (isNaN(number)) throw new Error("invalid discriminator"); + if (number <= 0 || number > 10000) throw new Error("discriminator must be between 1 and 9999"); + this.discriminator = val.toString().padStart(4, "0"); + } + + @Column({ nullable: true }) + avatar?: string; // hash of the user avatar + + @Column({ nullable: true }) + accent_color?: number; // banner color of user + + @Column({ nullable: true }) + banner?: string; // hash of the user banner + + @Column({ nullable: true }) + phone?: string; // phone number of the user + + @Column() + desktop: boolean; // if the user has desktop app installed + + @Column() + mobile: boolean; // if the user has mobile app installed + + @Column() + premium: boolean; // if user bought nitro + + @Column() + premium_type: number; // nitro level + + @Column() + bot: boolean; // if user is bot + + @Column() + bio: string; // short description of the user (max 190 chars -> should be configurable) + + @Column() + system: boolean; // shouldn't be used, the api sents this field type true, if the generated message comes from a system generated author + + @Column() + nsfw_allowed: boolean; // if the user is older than 18 (resp. Config) + + @Column() + mfa_enabled: boolean; // if multi factor authentication is enabled + + @Column() + created_at: Date = new Date(); // registration date + + @Column() + verified: boolean; // if the user is offically verified + + @Column() + disabled: boolean; // if the account is disabled + + @Column() + deleted: boolean; // if the user was deleted + + @Column({ nullable: true }) + email?: string; // email of the user + + @Column() + flags: string; // UserFlags + + @Column() + public_flags: string; + + @JoinColumn({ name: "relationship_ids" }) + @OneToMany(() => Relationship, (relationship: Relationship) => relationship.user, { cascade: true }) + relationships: Relationship[]; + + @JoinColumn({ name: "connected_account_ids" }) + @OneToMany(() => ConnectedAccount, (account: ConnectedAccount) => account.user) + connected_accounts: ConnectedAccount[]; + + @Column({ type: "simple-json", select: false }) + data: { + valid_tokens_since: Date; // all tokens with a previous issue date are invalid + hash?: string; // hash of the password, salt is saved in password (bcrypt) + }; + + @Column({ type: "simple-array" }) + fingerprints: string[]; // array of fingerprints -> used to prevent multiple accounts + + @Column({ type: "simple-json" }) + settings: UserSettings; + + static async getPublicUser(user_id: string, opts?: FindOneOptions) { + const user = await User.findOne( + { id: user_id }, + { + ...opts, + select: [...PublicUserProjection, ...(opts?.select || [])], + } + ); + if (!user) throw new HTTPError("User not found", 404); + return user; + } +} + +export const defaultSettings: UserSettings = { + afk_timeout: 300, + allow_accessibility_detection: true, + animate_emoji: true, + animate_stickers: 0, + contact_sync_enabled: false, + convert_emoticons: false, + custom_status: { + emoji_id: undefined, + emoji_name: undefined, + expires_at: undefined, + text: undefined, + }, + default_guilds_restricted: false, + detect_platform_accounts: true, + developer_mode: false, + disable_games_tab: false, + enable_tts_command: true, + explicit_content_filter: 0, + friend_source_flags: { all: true }, + gateway_connected: false, + gif_auto_play: true, + guild_folders: [], + guild_positions: [], + inline_attachment_media: true, + inline_embed_media: true, + locale: "en", + message_display_compact: false, + native_phone_integration_enabled: true, + render_embeds: true, + render_reactions: true, + restricted_guilds: [], + show_current_game: true, + status: "offline", + stream_notifications_enabled: true, + theme: "dark", + timezone_offset: 0, + // timezone_offset: // TODO: timezone from request +}; + +export interface UserSettings { + afk_timeout: number; + allow_accessibility_detection: boolean; + animate_emoji: boolean; + animate_stickers: number; + contact_sync_enabled: boolean; + convert_emoticons: boolean; + custom_status: { + emoji_id?: string; + emoji_name?: string; + expires_at?: number; + text?: string; + }; + default_guilds_restricted: boolean; + detect_platform_accounts: boolean; + developer_mode: boolean; + disable_games_tab: boolean; + enable_tts_command: boolean; + explicit_content_filter: number; + friend_source_flags: { all: boolean }; + gateway_connected: boolean; + gif_auto_play: boolean; + // every top guild is displayed as a "folder" + guild_folders: { + color: number; + guild_ids: string[]; + id: number; + name: string; + }[]; + guild_positions: string[]; // guild ids ordered by position + inline_attachment_media: boolean; + inline_embed_media: boolean; + locale: string; // en_US + message_display_compact: boolean; + native_phone_integration_enabled: boolean; + render_embeds: boolean; + render_reactions: boolean; + restricted_guilds: string[]; + show_current_game: boolean; + status: "online" | "offline" | "dnd" | "idle"; + stream_notifications_enabled: boolean; + theme: "dark" | "white"; // dark + timezone_offset: number; // e.g -60 +} + +export class UserFlags extends BitField { + static FLAGS = { + DISCORD_EMPLOYEE: BigInt(1) << BigInt(0), + PARTNERED_SERVER_OWNER: BigInt(1) << BigInt(1), + HYPESQUAD_EVENTS: BigInt(1) << BigInt(2), + BUGHUNTER_LEVEL_1: BigInt(1) << BigInt(3), + HOUSE_BRAVERY: BigInt(1) << BigInt(6), + HOUSE_BRILLIANCE: BigInt(1) << BigInt(7), + HOUSE_BALANCE: BigInt(1) << BigInt(8), + EARLY_SUPPORTER: BigInt(1) << BigInt(9), + TEAM_USER: BigInt(1) << BigInt(10), + SYSTEM: BigInt(1) << BigInt(12), + BUGHUNTER_LEVEL_2: BigInt(1) << BigInt(14), + VERIFIED_BOT: BigInt(1) << BigInt(16), + EARLY_VERIFIED_BOT_DEVELOPER: BigInt(1) << BigInt(17), + }; +} diff --git a/util/src/entities/VoiceState.ts b/util/src/entities/VoiceState.ts new file mode 100644 index 00000000..c5040cf1 --- /dev/null +++ b/util/src/entities/VoiceState.ts @@ -0,0 +1,56 @@ +import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; +import { BaseClass } from "./BaseClass"; +import { Channel } from "./Channel"; +import { Guild } from "./Guild"; +import { User } from "./User"; + +@Entity("voice_states") +export class VoiceState extends BaseClass { + @Column({ nullable: true }) + @RelationId((voice_state: VoiceState) => voice_state.guild) + guild_id: string; + + @JoinColumn({ name: "guild_id" }) + @ManyToOne(() => Guild) + guild?: Guild; + + @Column({ nullable: true }) + @RelationId((voice_state: VoiceState) => voice_state.channel) + channel_id: string; + + @JoinColumn({ name: "channel_id" }) + @ManyToOne(() => Channel) + channel: Channel; + + @Column({ nullable: true }) + @RelationId((voice_state: VoiceState) => voice_state.user) + user_id: string; + + @JoinColumn({ name: "user_id" }) + @ManyToOne(() => User) + user: User; + + @Column() + session_id: string; + + @Column() + deaf: boolean; + + @Column() + mute: boolean; + + @Column() + self_deaf: boolean; + + @Column() + self_mute: boolean; + + @Column({ nullable: true }) + self_stream?: boolean; + + @Column() + self_video: boolean; + + @Column() + suppress: boolean; // whether this user is muted by the current user +} diff --git a/util/src/entities/Webhook.ts b/util/src/entities/Webhook.ts new file mode 100644 index 00000000..12ba0d08 --- /dev/null +++ b/util/src/entities/Webhook.ts @@ -0,0 +1,69 @@ +import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; +import { Application } from "./Application"; +import { BaseClass } from "./BaseClass"; +import { Channel } from "./Channel"; +import { Guild } from "./Guild"; +import { User } from "./User"; + +export enum WebhookType { + Incoming = 1, + ChannelFollower = 2, +} + +@Entity("webhooks") +export class Webhook extends BaseClass { + @Column() + id: string; + + @Column({ type: "simple-enum", enum: WebhookType }) + type: WebhookType; + + @Column({ nullable: true }) + name?: string; + + @Column({ nullable: true }) + avatar?: string; + + @Column({ nullable: true }) + token?: string; + + @Column({ nullable: true }) + @RelationId((webhook: Webhook) => webhook.guild) + guild_id: string; + + @JoinColumn({ name: "guild_id" }) + @ManyToOne(() => Guild) + guild: Guild; + + @Column({ nullable: true }) + @RelationId((webhook: Webhook) => webhook.channel) + channel_id: string; + + @JoinColumn({ name: "channel_id" }) + @ManyToOne(() => Channel) + channel: Channel; + + @Column({ nullable: true }) + @RelationId((webhook: Webhook) => webhook.application) + application_id: string; + + @JoinColumn({ name: "application_id" }) + @ManyToOne(() => Application) + application: Application; + + @Column({ nullable: true }) + @RelationId((webhook: Webhook) => webhook.user) + user_id: string; + + @JoinColumn({ name: "user_id" }) + @ManyToOne(() => User) + user: User; + + @Column({ nullable: true }) + @RelationId((webhook: Webhook) => webhook.guild) + source_guild_id: string; + + @JoinColumn({ name: "source_guild_id" }) + @ManyToOne(() => Guild) + source_guild: Guild; +} diff --git a/util/src/entities/index.ts b/util/src/entities/index.ts new file mode 100644 index 00000000..aa37ae2e --- /dev/null +++ b/util/src/entities/index.ts @@ -0,0 +1,25 @@ +export * from "./Application"; +export * from "./Attachment"; +export * from "./AuditLog"; +export * from "./Ban"; +export * from "./BaseClass"; +export * from "./Channel"; +export * from "./Config"; +export * from "./ConnectedAccount"; +export * from "./Emoji"; +export * from "./Guild"; +export * from "./Invite"; +export * from "./Member"; +export * from "./Message"; +export * from "./RateLimit"; +export * from "./ReadState"; +export * from "./Recipient"; +export * from "./Relationship"; +export * from "./Role"; +export * from "./Sticker"; +export * from "./Team"; +export * from "./TeamMember"; +export * from "./Template"; +export * from "./User"; +export * from "./VoiceState"; +export * from "./Webhook"; diff --git a/util/src/index.ts b/util/src/index.ts index 3565fb6b..f3bd9e9b 100644 --- a/util/src/index.ts +++ b/util/src/index.ts @@ -1,10 +1,11 @@ -export * from "./util/checkToken"; +import "reflect-metadata"; -export * as Constants from "./util/Constants"; -export * from "./models/index"; +// export * as Constants from "../util/Constants"; export * from "./util/index"; +export * from "./interfaces/index"; +export * from "./entities/index"; -import Config from "./util/Config"; -import db, { MongooseCache, toObject } from "./util/Database"; +// import Config from "../util/Config"; +// import db, { MongooseCache, toObject } from "./util/Database"; -export { Config, db, MongooseCache, toObject }; +// export { Config }; diff --git a/util/src/interfaces/Activity.ts b/util/src/interfaces/Activity.ts new file mode 100644 index 00000000..f5a3c270 --- /dev/null +++ b/util/src/interfaces/Activity.ts @@ -0,0 +1,43 @@ +export interface Activity { + name: string; + type: ActivityType; + url?: string; + created_at?: Date; + timestamps?: { + start?: number; + end?: number; + }[]; + application_id?: string; + details?: string; + state?: string; + emoji?: { + name: string; + id?: string; + amimated?: boolean; + }; + party?: { + id?: string; + size?: [number, number]; + }; + assets?: { + large_image?: string; + large_text?: string; + small_image?: string; + small_text?: string; + }; + secrets?: { + join?: string; + spectate?: string; + match?: string; + }; + instance?: boolean; + flags?: bigint; +} + +export enum ActivityType { + GAME = 0, + STREAMING = 1, + LISTENING = 2, + CUSTOM = 4, + COMPETING = 5, +} diff --git a/util/src/models/Event.ts b/util/src/interfaces/Event.ts similarity index 89% rename from util/src/models/Event.ts rename to util/src/interfaces/Event.ts index 86d0fd00..7ea1bd49 100644 --- a/util/src/models/Event.ts +++ b/util/src/interfaces/Event.ts @@ -1,15 +1,17 @@ -import { ConnectedAccount, PublicUser, Relationship, User, UserSettings } from "./User"; -import { DMChannel, Channel } from "./Channel"; -import { Guild } from "./Guild"; -import { Member, PublicMember, UserGuildSettings } from "./Member"; -import { Emoji } from "./Emoji"; -import { Presence } from "./Activity"; -import { Role } from "./Role"; -import { Invite } from "./Invite"; -import { Message, PartialEmoji } from "./Message"; -import { VoiceState } from "./VoiceState"; -import { ApplicationCommand } from "./Application"; +import { PublicUser, User, UserSettings } from "../entities/User"; +import { Channel } from "../entities/Channel"; +import { Guild } from "../entities/Guild"; +import { Member, PublicMember, UserGuildSettings } from "../entities/Member"; +import { Emoji } from "../entities/Emoji"; +import { Role } from "../entities/Role"; +import { Invite } from "../entities/Invite"; +import { Message, PartialEmoji } from "../entities/Message"; +import { VoiceState } from "../entities/VoiceState"; +import { ApplicationCommand } from "../entities/Application"; import { Interaction } from "./Interaction"; +import { ConnectedAccount } from "../entities/ConnectedAccount"; +import { Relationship } from "../entities/Relationship"; +import { Presence } from "./Presence"; export interface Event { guild_id?: string; @@ -33,17 +35,17 @@ export interface ReadyEventData { user: PublicUser & { mobile: boolean; desktop: boolean; - email: string | null; - flags: bigint; + email: string | undefined; + flags: string; mfa_enabled: boolean; nsfw_allowed: boolean; - phone: string | null; + phone: string | undefined; premium: boolean; premium_type: number; verified: boolean; bot: boolean; }; - private_channels: DMChannel[]; // this will be empty for bots + private_channels: Channel[]; // this will be empty for bots session_id: string; // resuming guilds: Guild[]; analytics_token?: string; @@ -67,12 +69,12 @@ export interface ReadyEventData { [number, [[number, [number, number]]]], { b: number; k: bigint[] }[] ][]; - guild_join_requests?: []; // ? what is this? this is new + guild_join_requests?: any[]; // ? what is this? this is new shard?: [number, number]; user_settings?: UserSettings; relationships?: Relationship[]; // TODO read_state: { - entries: []; // TODO + entries: any[]; // TODO partial: boolean; version: number; }; @@ -83,18 +85,11 @@ export interface ReadyEventData { }; application?: { id: string; - flags: bigint; + flags: string; }; merged_members?: Omit[][]; // probably all users who the user is in contact with - users?: { - avatar: string | null; - discriminator: string; - id: string; - username: string; - bot: boolean; - public_flags: bigint; - }[]; + users?: PublicUser[]; } export interface ReadyEvent extends Event { @@ -128,7 +123,9 @@ export interface ChannelPinsUpdateEvent extends Event { export interface GuildCreateEvent extends Event { event: "GUILD_CREATE"; - data: Guild; + data: Guild & { + joined_at: Date; + }; } export interface GuildUpdateEvent extends Event { @@ -257,22 +254,14 @@ export interface InviteDeleteEvent extends Event { }; } -export type MessagePayload = Omit & { - channel_id: string; - guild_id?: string; - author: PublicUser; - member: PublicMember; - mentions: (PublicUser & { member: PublicMember })[]; -}; - export interface MessageCreateEvent extends Event { event: "MESSAGE_CREATE"; - data: MessagePayload; + data: Message; } export interface MessageUpdateEvent extends Event { event: "MESSAGE_UPDATE"; - data: MessagePayload; + data: Message; } export interface MessageDeleteEvent extends Event { @@ -521,4 +510,4 @@ export type EVENT = | "RELATIONSHIP_REMOVE" | CUSTOMEVENTS; -export type CUSTOMEVENTS = "INVALIDATED"; +export type CUSTOMEVENTS = "INVALIDATED" | "RATELIMIT"; diff --git a/util/src/models/Interaction.ts b/util/src/interfaces/Interaction.ts similarity index 89% rename from util/src/models/Interaction.ts rename to util/src/interfaces/Interaction.ts index 764247a5..3cafb2d5 100644 --- a/util/src/models/Interaction.ts +++ b/util/src/interfaces/Interaction.ts @@ -1,4 +1,4 @@ -import { AllowedMentions, Embed } from "./Message"; +import { AllowedMentions, Embed } from "../entities/Message"; export interface Interaction { id: string; diff --git a/util/src/interfaces/Presence.ts b/util/src/interfaces/Presence.ts new file mode 100644 index 00000000..4a1ff038 --- /dev/null +++ b/util/src/interfaces/Presence.ts @@ -0,0 +1,10 @@ +import { ClientStatus, Status } from "./Status"; +import { Activity } from "./Activity"; + +export interface Presence { + user_id: string; + guild_id?: string; + status: Status; + activities: Activity[]; + client_status: ClientStatus; +} diff --git a/util/src/models/Status.ts b/util/src/interfaces/Status.ts similarity index 72% rename from util/src/models/Status.ts rename to util/src/interfaces/Status.ts index 5a9bf2ca..c4dab586 100644 --- a/util/src/models/Status.ts +++ b/util/src/interfaces/Status.ts @@ -5,9 +5,3 @@ export interface ClientStatus { mobile?: string; // e.g. iOS/Android web?: string; // e.g. browser, bot account } - -export const ClientStatus = { - desktop: String, - mobile: String, - web: String, -}; diff --git a/util/src/interfaces/index.ts b/util/src/interfaces/index.ts new file mode 100644 index 00000000..ab7fa429 --- /dev/null +++ b/util/src/interfaces/index.ts @@ -0,0 +1,5 @@ +export * from "./Activity"; +export * from "./Presence"; +export * from "./Interaction"; +export * from "./Event"; +export * from "./Status"; diff --git a/util/src/models/Activity.ts b/util/src/models/Activity.ts deleted file mode 100644 index 17abd1ca..00000000 --- a/util/src/models/Activity.ts +++ /dev/null @@ -1,132 +0,0 @@ -import { User } from ".."; -import { ClientStatus, Status } from "./Status"; -import { Schema, model, Types, Document } from "mongoose"; -import toBigInt from "../util/toBigInt"; - -export interface Presence { - user: User; - guild_id?: string; - status: Status; - activities: Activity[]; - client_status: ClientStatus; -} - -export interface Activity { - name: string; - type: ActivityType; - url?: string; - created_at?: Date; - timestamps?: { - start?: number; - end?: number; - }[]; - application_id?: string; - details?: string; - state?: string; - emoji?: { - name: string; - id?: string; - amimated?: boolean; - }; - party?: { - id?: string; - size?: [number, number]; - }; - assets?: { - large_image?: string; - large_text?: string; - small_image?: string; - small_text?: string; - }; - secrets?: { - join?: string; - spectate?: string; - match?: string; - }; - instance?: boolean; - flags?: bigint; -} - -export const ActivitySchema = { - name: { type: String, required: true }, - type: { type: Number, required: true }, - url: String, - created_at: Date, - timestamps: [ - { - start: Number, - end: Number, - }, - ], - application_id: String, - details: String, - state: String, - emoji: { - name: String, - id: String, - amimated: Boolean, - }, - party: { - id: String, - size: [Number, Number], - }, - assets: { - large_image: String, - large_text: String, - small_image: String, - small_text: String, - }, - secrets: { - join: String, - spectate: String, - match: String, - }, - instance: Boolean, - flags: { type: String, get: toBigInt }, -}; - -export const ActivityBodySchema = { - name: String, - type: Number, - $url: String, - $created_at: Date, - $timestamps: [ - { - $start: Number, - $end: Number, - }, - ], - $application_id: String, - $details: String, - $state: String, - $emoji: { - $name: String, - $id: String, - $amimated: Boolean, - }, - $party: { - $id: String, - $size: [Number, Number], - }, - $assets: { - $large_image: String, - $large_text: String, - $small_image: String, - $small_text: String, - }, - $secrets: { - $join: String, - $spectate: String, - $match: String, - }, - $instance: Boolean, - $flags: BigInt, -}; - -export enum ActivityType { - GAME = 0, - STREAMING = 1, - LISTENING = 2, - CUSTOM = 4, - COMPETING = 5, -} diff --git a/util/src/models/Application.ts b/util/src/models/Application.ts deleted file mode 100644 index fae6e8db..00000000 --- a/util/src/models/Application.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { Team } from "./Team"; - -export interface Application { - id: string; - name: string; - icon: string | null; - description: string; - rpc_origins: string[] | null; - bot_public: boolean; - bot_require_code_grant: boolean; - terms_of_service_url: string | null; - privacy_policy_url: string | null; - owner_id: string; - summary: string | null; - verify_key: string; - team: Team | null; - guild_id: string; // if this application is a game sold on Discord, this field will be the guild to which it has been linked - primary_sku_id: string | null; // if this application is a game sold on Discord, this field will be the id of the "Game SKU" that is created, if exists - slug: string | null; // if this application is a game sold on Discord, this field will be the URL slug that links to the store page - cover_image: string | null; // the application's default rich presence invite cover image hash - flags: number; // the application's public flags -} - -export interface ApplicationCommand { - id: string; - application_id: string; - name: string; - description: string; - options?: ApplicationCommandOption[]; -} - -export interface ApplicationCommandOption { - type: ApplicationCommandOptionType; - name: string; - description: string; - required?: boolean; - choices?: ApplicationCommandOptionChoice[]; - options?: ApplicationCommandOption[]; -} - -export interface ApplicationCommandOptionChoice { - name: string; - value: string | number; -} - -export enum ApplicationCommandOptionType { - SUB_COMMAND = 1, - SUB_COMMAND_GROUP = 2, - STRING = 3, - INTEGER = 4, - BOOLEAN = 5, - USER = 6, - CHANNEL = 7, - ROLE = 8, -} - -export interface ApplicationCommandInteractionData { - id: string; - name: string; - options?: ApplicationCommandInteractionDataOption[]; -} - -export interface ApplicationCommandInteractionDataOption { - name: string; - value?: any; - options?: ApplicationCommandInteractionDataOption[]; -} diff --git a/util/src/models/Ban.ts b/util/src/models/Ban.ts deleted file mode 100644 index f09950ee..00000000 --- a/util/src/models/Ban.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { Schema, model, Types, Document } from "mongoose"; -import db from "../util/Database"; -import { PublicUserProjection, UserModel } from "./User"; - -export interface Ban extends Document { - user_id: string; - guild_id: string; - executor_id: string; - ip: string; - reason?: string; -} - -export const BanSchema = new Schema({ - user_id: { type: String, required: true }, - guild_id: { type: String, required: true }, - executor_id: { type: String, required: true }, - reason: String, - ip: String, // ? Should we store this in here, or in the UserModel? -}); - -BanSchema.virtual("user", { - ref: UserModel, - localField: "user_id", - foreignField: "id", - justOne: true, - autopopulate: { select: PublicUserProjection }, -}); - -BanSchema.set("removeResponse", ["user_id"]); - -// @ts-ignore -export const BanModel = db.model("Ban", BanSchema, "bans"); diff --git a/util/src/models/Channel.ts b/util/src/models/Channel.ts deleted file mode 100644 index 2959decd..00000000 --- a/util/src/models/Channel.ts +++ /dev/null @@ -1,111 +0,0 @@ -import { Schema, model, Types, Document } from "mongoose"; -import db from "../util/Database"; -import toBigInt from "../util/toBigInt"; -import { PublicUserProjection, UserModel } from "./User"; - -// @ts-ignore -export interface AnyChannel extends Channel, DMChannel, TextChannel, VoiceChannel { - recipient_ids: null | string[]; -} - -export interface ChannelDocument extends Document, AnyChannel { - id: string; -} - -export const ChannelSchema = new Schema({ - id: String, - created_at: { type: Schema.Types.Date, required: true }, - name: String, // can't be required for dm channels - type: { type: Number, required: true }, - guild_id: String, - owner_id: String, - parent_id: String, - recipient_ids: [String], - position: Number, - last_message_id: String, - last_pin_timestamp: Date, - nsfw: Boolean, - rate_limit_per_user: Number, - default_auto_archive_duration: Number, - topic: String, - permission_overwrites: [ - { - allow: { type: String, get: toBigInt }, - deny: { type: String, get: toBigInt }, - id: String, - type: { type: Number }, - }, - ], -}); - -ChannelSchema.virtual("recipients", { - ref: UserModel, - localField: "recipient_ids", - foreignField: "id", - justOne: false, - autopopulate: { select: PublicUserProjection }, -}); - -ChannelSchema.set("removeResponse", ["recipient_ids"]); - -// @ts-ignore -export const ChannelModel = db.model("Channel", ChannelSchema, "channels"); - -export interface Channel { - id: string; - created_at: Date; - name: string; - type: number; -} - -export interface TextBasedChannel { - last_message_id?: string; - last_pin_timestamp?: number; - default_auto_archive_duration?: number; -} - -export interface GuildChannel extends Channel { - guild_id: string; - position: number; - parent_id?: string; - permission_overwrites: ChannelPermissionOverwrite[]; -} - -export interface ChannelPermissionOverwrite { - allow: bigint; // for bitfields we use bigints - deny: bigint; // for bitfields we use bigints - id: string; - type: ChannelPermissionOverwriteType; -} - -export enum ChannelPermissionOverwriteType { - role = 0, - member = 1, -} - -export interface VoiceChannel extends GuildChannel { - video_quality_mode?: number; - bitrate?: number; - user_limit?: number; -} - -export interface TextChannel extends GuildChannel, TextBasedChannel { - nsfw: boolean; - rate_limit_per_user: number; - topic?: string; -} -// @ts-ignore -export interface DMChannel extends Channel, TextBasedChannel { - owner_id: string; - recipient_ids: string[]; -} - -export enum ChannelType { - GUILD_TEXT = 0, // a text channel within a server - DM = 1, // a direct message between users - GUILD_VOICE = 2, // a voice channel within a server - GROUP_DM = 3, // a direct message between multiple users - GUILD_CATEGORY = 4, // an organizational category that contains up to 50 channels - GUILD_NEWS = 5, // a channel that users can follow and crosspost into their own server - GUILD_STORE = 6, // a channel in which game developers can sell their game on Discord -} diff --git a/util/src/models/Emoji.ts b/util/src/models/Emoji.ts deleted file mode 100644 index 3e5cad53..00000000 --- a/util/src/models/Emoji.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { Schema, model, Types, Document } from "mongoose"; -import db from "../util/Database"; - -export interface Emoji extends Document { - id: string; - animated: boolean; - available: boolean; - guild_id: string; - managed: boolean; - name: string; - require_colons: boolean; - url: string; - roles: string[]; // roles this emoji is whitelisted to (new discord feature?) -} - -export const EmojiSchema = new Schema({ - id: { type: String, required: true }, - animated: Boolean, - available: Boolean, - guild_id: String, - managed: Boolean, - name: String, - require_colons: Boolean, - url: String, - roles: [String], -}); - -// @ts-ignore -export const EmojiModel = db.model("Emoji", EmojiSchema, "emojis"); diff --git a/util/src/models/Guild.ts b/util/src/models/Guild.ts deleted file mode 100644 index a5dcd8e3..00000000 --- a/util/src/models/Guild.ts +++ /dev/null @@ -1,159 +0,0 @@ -import { Schema, model, Types, Document } from "mongoose"; -import db from "../util/Database"; -import { ChannelModel } from "./Channel"; -import { EmojiModel } from "./Emoji"; -import { MemberModel } from "./Member"; -import { RoleModel } from "./Role"; - -export interface GuildDocument extends Document, Guild { - id: string; -} - -export interface Guild { - id: string; - afk_channel_id?: string; - afk_timeout?: number; - application_id?: string; - banner?: string; - default_message_notifications?: number; - description?: string; - discovery_splash?: string; - explicit_content_filter?: number; - features: string[]; - icon?: string; - large?: boolean; - max_members?: number; // e.g. default 100.000 - max_presences?: number; - max_video_channel_users?: number; // ? default: 25, is this max 25 streaming or watching - member_count?: number; - presence_count?: number; // users online - // members?: Member[]; // * Members are stored in a seperate collection - // roles: Role[]; // * Role are stored in a seperate collection - // channels: GuildChannel[]; // * Channels are stored in a seperate collection - // emojis: Emoji[]; // * Emojis are stored in a seperate collection - // voice_states: []; // * voice_states are stored in a seperate collection - //TODO: - presences?: object[]; - mfa_level?: number; - name: string; - owner_id: string; - preferred_locale?: string; // only community guilds can choose this - premium_subscription_count?: number; - premium_tier?: number; // nitro boost level - public_updates_channel_id?: string; - region?: string; - rules_channel_id?: string; - splash?: string; - system_channel_flags?: number; - system_channel_id?: string; - unavailable?: boolean; - vanity_url_code?: string; - verification_level?: number; - welcome_screen: { - enabled: boolean; - description: string; - welcome_channels: { - description: string; - emoji_id?: string; - emoji_name: string; - channel_id: string; - }[]; - }; - widget_channel_id?: string; - widget_enabled?: boolean; -} - -export const GuildSchema = new Schema({ - id: { type: String, required: true }, - afk_channel_id: String, - afk_timeout: Number, - application_id: String, - banner: String, - default_message_notifications: Number, - description: String, - discovery_splash: String, - explicit_content_filter: Number, - features: { type: [String], default: [] }, - icon: String, - large: Boolean, - max_members: { type: Number, default: 100000 }, - max_presences: Number, - max_video_channel_users: { type: Number, default: 25 }, - member_count: Number, - presences: { type: [Object], default: [] }, - presence_count: Number, - mfa_level: Number, - name: { type: String, required: true }, - owner_id: { type: String, required: true }, - preferred_locale: String, - premium_subscription_count: Number, - premium_tier: Number, - public_updates_channel_id: String, - region: String, - rules_channel_id: String, - splash: String, - system_channel_flags: Number, - system_channel_id: String, - unavailable: Boolean, - vanity_url_code: String, - verification_level: Number, - voice_states: { type: [Object], default: [] }, - welcome_screen: { - enabled: Boolean, - description: String, - welcome_channels: [ - { - description: String, - emoji_id: String, - emoji_name: String, - channel_id: String, - }, - ], - }, - widget_channel_id: String, - widget_enabled: Boolean, -}); - -GuildSchema.virtual("channels", { - ref: ChannelModel, - localField: "id", - foreignField: "guild_id", - justOne: false, - autopopulate: true, -}); - -GuildSchema.virtual("roles", { - ref: RoleModel, - localField: "id", - foreignField: "guild_id", - justOne: false, - autopopulate: true, -}); - -// nested populate is needed for member users: https://gist.github.com/yangsu/5312204 -GuildSchema.virtual("members", { - ref: MemberModel, - localField: "id", - foreignField: "guild_id", - justOne: false, -}); - -GuildSchema.virtual("emojis", { - ref: EmojiModel, - localField: "id", - foreignField: "guild_id", - justOne: false, - autopopulate: true, -}); - -GuildSchema.virtual("joined_at", { - ref: MemberModel, - localField: "id", - foreignField: "guild_id", - justOne: true, -}).get((member: any, virtual: any, doc: any) => { - return member?.joined_at; -}); - -// @ts-ignore -export const GuildModel = db.model("Guild", GuildSchema, "guilds"); diff --git a/util/src/models/Invite.ts b/util/src/models/Invite.ts deleted file mode 100644 index 01f12003..00000000 --- a/util/src/models/Invite.ts +++ /dev/null @@ -1,95 +0,0 @@ -import { Schema, Document, Types } from "mongoose"; -import db from "../util/Database"; -import { ChannelModel } from "./Channel"; -import { PublicUserProjection, UserModel } from "./User"; -import { GuildModel } from "./Guild"; - -export interface Invite { - code: string; - temporary: boolean; - uses: number; - max_uses: number; - max_age: number; - created_at: Date; - expires_at: Date; - guild_id: string; - channel_id: string; - inviter_id: string; - - // ? What is this? - target_user_id?: string; - target_user_type?: number; -} - -export interface InviteDocument extends Invite, Document {} - -export const InviteSchema = new Schema({ - code: String, - temporary: Boolean, - uses: Number, - max_uses: Number, - max_age: Number, - created_at: Date, - expires_at: Date, - guild_id: String, - channel_id: String, - inviter_id: String, - - // ? What is this? - target_user_id: String, - target_user_type: Number, -}); - -InviteSchema.virtual("channel", { - ref: ChannelModel, - localField: "channel_id", - foreignField: "id", - justOne: true, - autopopulate: { - select: { - id: true, - name: true, - type: true, - }, - }, -}); - -InviteSchema.virtual("inviter", { - ref: UserModel, - localField: "inviter_id", - foreignField: "id", - justOne: true, - autopopulate: { - select: PublicUserProjection, - }, -}); - -InviteSchema.virtual("guild", { - ref: GuildModel, - localField: "guild_id", - foreignField: "id", - justOne: true, - autopopulate: { - select: { - id: true, - name: true, - splash: true, - banner: true, - description: true, - icon: true, - features: true, - verification_level: true, - vanity_url_code: true, - welcome_screen: true, - nsfw: true, - - // TODO: hide the following entries: - // channels: false, - // roles: false, - // emojis: false, - }, - }, -}); - -// @ts-ignore -export const InviteModel = db.model("Invite", InviteSchema, "invites"); diff --git a/util/src/models/Member.ts b/util/src/models/Member.ts deleted file mode 100644 index d1c9ad9b..00000000 --- a/util/src/models/Member.ts +++ /dev/null @@ -1,109 +0,0 @@ -import { PublicUser, PublicUserProjection, User, UserModel } from "./User"; -import { Schema, Types, Document } from "mongoose"; -import db from "../util/Database"; - -export const PublicMemberProjection = { - id: true, - guild_id: true, - nick: true, - roles: true, - joined_at: true, - pending: true, - deaf: true, - mute: true, - premium_since: true, -}; - -export interface Member { - id: string; - guild_id: string; - nick?: string; - roles: string[]; - joined_at: Date; - premium_since?: number; - deaf: boolean; - mute: boolean; - pending: boolean; - settings: UserGuildSettings; - read_state: Record; - // virtual - user?: User; -} - -export interface MemberDocument extends Member, Document { - id: string; -} - -export interface UserGuildSettings { - channel_overrides: { - channel_id: string; - message_notifications: number; - mute_config: MuteConfig; - muted: boolean; - }[]; - message_notifications: number; - mobile_push: boolean; - mute_config: MuteConfig; - muted: boolean; - suppress_everyone: boolean; - suppress_roles: boolean; - version: number; -} - -export interface MuteConfig { - end_time: number; - selected_time_window: number; -} - -const MuteConfig = { - end_time: Number, - selected_time_window: Number, -}; - -export const MemberSchema = new Schema({ - id: { type: String, required: true }, - guild_id: String, - nick: String, - roles: [String], - joined_at: Date, - premium_since: Number, - deaf: Boolean, - mute: Boolean, - pending: Boolean, - read_state: Object, - settings: { - channel_overrides: [ - { - channel_id: String, - message_notifications: Number, - mute_config: MuteConfig, - muted: Boolean, - }, - ], - message_notifications: Number, - mobile_push: Boolean, - mute_config: MuteConfig, - muted: Boolean, - suppress_everyone: Boolean, - suppress_roles: Boolean, - version: Number, - }, -}); - -MemberSchema.virtual("user", { - ref: UserModel, - localField: "id", - foreignField: "id", - justOne: true, - autopopulate: { - select: PublicUserProjection, - }, -}); - -// @ts-ignore -export const MemberModel = db.model("Member", MemberSchema, "members"); - -// @ts-ignore -export interface PublicMember extends Omit { - user: PublicUser; -} diff --git a/util/src/models/Message.ts b/util/src/models/Message.ts deleted file mode 100644 index 15a6f40d..00000000 --- a/util/src/models/Message.ts +++ /dev/null @@ -1,368 +0,0 @@ -import { Schema, Types, Document } from "mongoose"; -import db from "../util/Database"; -import { PublicUser, PublicUserProjection, UserModel } from "./User"; -import { MemberModel, PublicMember } from "./Member"; -import { Role, RoleModel } from "./Role"; -import { Channel } from "./Channel"; -import { Snowflake } from "../util"; -import { InteractionType } from "./Interaction"; - -export interface Message { - id: string; - channel_id: string; - guild_id?: string; - author_id?: string; - webhook_id?: string; - application_id?: string; - content?: string; - timestamp: Date; - edited_timestamp: Date | null; - tts?: boolean; - mention_everyone?: boolean; - mention_user_ids: string[]; - mention_role_ids: string[]; - mention_channels_ids: string[]; - attachments: Attachment[]; - embeds: Embed[]; - reactions: Reaction[]; - nonce?: string | number; - pinned?: boolean; - type: MessageType; - activity?: { - type: number; - party_id: string; - }; - flags?: bigint; - stickers?: any[]; - message_reference?: { - message_id: string; - channel_id?: string; - guild_id?: string; - }; - interaction?: { - id: string; - type: InteractionType; - name: string; - user_id: string; // the user who invoked the interaction - // user: User; // TODO: autopopulate user - }; - components: MessageComponent[]; - - // * mongoose virtuals: - // TODO: - // application: Application; // TODO: auto pouplate application - author?: PublicUser; - member?: PublicMember; - mentions?: (PublicUser & { - member: PublicMember; - })[]; - mention_roles?: Role[]; - mention_channels?: Channel[]; - created_at?: Date; - // thread // TODO -} - -const PartialEmoji = { - id: String, - name: { type: String, required: true }, - animated: { type: Boolean, required: true }, -}; - -const MessageComponent: any = { - type: { type: Number, required: true }, - style: Number, - label: String, - emoji: PartialEmoji, - custom_id: String, - url: String, - disabled: Boolean, - components: [Object], -}; - -export interface MessageComponent { - type: number; - style?: number; - label?: string; - emoji?: PartialEmoji; - custom_id?: string; - url?: string; - disabled?: boolean; - components: MessageComponent[]; -} - -export enum MessageComponentType { - ActionRow = 1, - Button = 2, -} - -export interface MessageDocument extends Document, Message { - id: string; -} - -export enum MessageType { - DEFAULT = 0, - RECIPIENT_ADD = 1, - RECIPIENT_REMOVE = 2, - CALL = 3, - CHANNEL_NAME_CHANGE = 4, - CHANNEL_ICON_CHANGE = 5, - CHANNEL_PINNED_MESSAGE = 6, - GUILD_MEMBER_JOIN = 7, - USER_PREMIUM_GUILD_SUBSCRIPTION = 8, - USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_1 = 9, - USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_2 = 10, - USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_3 = 11, - CHANNEL_FOLLOW_ADD = 12, - GUILD_DISCOVERY_DISQUALIFIED = 14, - GUILD_DISCOVERY_REQUALIFIED = 15, - REPLY = 19, - APPLICATION_COMMAND = 20, -} - -export interface Attachment { - id: string; // attachment id - filename: string; // name of file attached - size: number; // size of file in bytes - url: string; // source url of file - proxy_url: string; // a proxied url of file - height?: number; // height of file (if image) - width?: number; // width of file (if image) - content_type?: string; -} - -export interface Embed { - title?: string; //title of embed - type?: EmbedType; // type of embed (always "rich" for webhook embeds) - description?: string; // description of embed - url?: string; // url of embed - timestamp?: Date; // timestamp of embed content - color?: number; // color code of the embed - footer?: { - text: string; - icon_url?: string; - proxy_icon_url?: string; - }; // footer object footer information - image?: EmbedImage; // image object image information - thumbnail?: EmbedImage; // thumbnail object thumbnail information - video?: EmbedImage; // video object video information - provider?: { - name?: string; - url?: string; - }; // provider object provider information - author?: { - name?: string; - url?: string; - icon_url?: string; - proxy_icon_url?: string; - }; // author object author information - fields?: { - name: string; - value: string; - inline?: boolean; - }[]; -} - -export enum EmbedType { - rich = "rich", - image = "image", - video = "video", - gifv = "gifv", - article = "article", - link = "link", -} - -export interface EmbedImage { - url?: string; - proxy_url?: string; - height?: number; - width?: number; -} - -export interface Reaction { - count: number; - //// not saved in the database // me: boolean; // whether the current user reacted using this emoji - emoji: PartialEmoji; - user_ids: string[]; -} - -export interface PartialEmoji { - id?: string; - name: string; - animated?: boolean; -} - -export interface AllowedMentions { - parse?: ("users" | "roles" | "everyone")[]; - roles?: string[]; - users?: string[]; - replied_user?: boolean; -} - -export const Attachment = { - id: String, // attachment id - filename: String, // name of file attached - size: Number, // size of file in bytes - url: String, // source url of file - proxy_url: String, // a proxied url of file - height: Number, // height of file (if image) - width: Number, // width of file (if image) - content_type: String, -}; - -export const EmbedImage = { - url: String, - proxy_url: String, - height: Number, - width: Number, -}; - -const Reaction = { - count: Number, - user_ids: [String], - emoji: { - id: String, - name: String, - animated: Boolean, - }, -}; - -export const Embed = { - title: String, //title of embed - type: { type: String }, // type of embed (always "rich" for webhook embeds) - description: String, // description of embed - url: String, // url of embed - timestamp: Date, // timestamp of embed content - color: Number, // color code of the embed - footer: { - text: String, - icon_url: String, - proxy_icon_url: String, - }, // footer object footer information - image: EmbedImage, // image object image information - thumbnail: EmbedImage, // thumbnail object thumbnail information - video: EmbedImage, // video object video information - provider: { - name: String, - url: String, - }, // provider object provider information - author: { - name: String, - url: String, - icon_url: String, - proxy_icon_url: String, - }, // author object author information - fields: [ - { - name: String, - value: String, - inline: Boolean, - }, - ], -}; - -export const MessageSchema = new Schema({ - id: String, - channel_id: String, - author_id: String, - webhook_id: String, - guild_id: String, - application_id: String, - content: String, - timestamp: Date, - edited_timestamp: Date, - tts: Boolean, - mention_everyone: Boolean, - mention_user_ids: [String], - mention_role_ids: [String], - mention_channel_ids: [String], - attachments: [Attachment], - embeds: [Embed], - reactions: [Reaction], - nonce: Schema.Types.Mixed, // can be a long or a string - pinned: Boolean, - type: { type: Number }, - activity: { - type: { type: Number }, - party_id: String, - }, - flags: Types.Long, - stickers: [], - message_reference: { - message_id: String, - channel_id: String, - guild_id: String, - }, - components: [MessageComponent], - // virtual: - // author: { - // ref: UserModel, - // localField: "author_id", - // foreignField: "id", - // justOne: true, - // autopopulate: { select: { id: true, user_data: false } }, - // }, -}); - -MessageSchema.virtual("author", { - ref: UserModel, - localField: "author_id", - foreignField: "id", - justOne: true, - autopopulate: { select: PublicUserProjection }, -}); - -MessageSchema.virtual("member", { - ref: MemberModel, - localField: "author_id", - foreignField: "id", - justOne: true, -}); - -MessageSchema.virtual("mentions", { - ref: UserModel, - localField: "mention_user_ids", - foreignField: "id", - justOne: false, - autopopulate: { select: PublicUserProjection }, -}); - -MessageSchema.virtual("mention_roles", { - ref: RoleModel, - localField: "mention_role_ids", - foreignField: "id", - justOne: false, - autopopulate: true, -}); - -MessageSchema.virtual("mention_channels", { - ref: RoleModel, - localField: "mention_channel_ids", - foreignField: "id", - justOne: false, - autopopulate: { select: { id: true, guild_id: true, type: true, name: true } }, -}); - -MessageSchema.virtual("referenced_message", { - ref: "Message", - localField: "message_reference.message_id", - foreignField: "id", - justOne: true, - autopopulate: true, -}); - -MessageSchema.virtual("created_at").get(function (this: MessageDocument) { - return new Date(Snowflake.deconstruct(this.id).timestamp); -}); - -MessageSchema.set("removeResponse", ["mention_channel_ids", "mention_role_ids", "mention_user_ids", "author_id"]); - -// TODO: missing Application Model -// MessageSchema.virtual("application", { -// ref: Application, -// localField: "mention_role_ids", -// foreignField: "id", -// justOne: true, -// }); - -// @ts-ignore -export const MessageModel = db.model("Message", MessageSchema, "messages"); diff --git a/util/src/models/RateLimit.ts b/util/src/models/RateLimit.ts deleted file mode 100644 index 6a0e1ffd..00000000 --- a/util/src/models/RateLimit.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Schema, Document, Types } from "mongoose"; -import db from "../util/Database"; - -export interface Bucket { - id: "global" | "error" | string; // channel_239842397 | guild_238927349823 | webhook_238923423498 - user_id: string; - hits: number; - blocked: boolean; - expires_at: Date; -} - -export interface BucketDocument extends Bucket, Document { - id: string; -} - -export const BucketSchema = new Schema({ - id: { type: String, required: true }, - user_id: { type: String, required: true }, // bot, user, oauth_application, webhook - hits: { type: Number, required: true }, // Number of times the user hit this bucket - blocked: { type: Boolean, required: true }, - expires_at: { type: Date, required: true }, -}); - -// @ts-ignore -export const BucketModel = db.model("Bucket", BucketSchema, "ratelimits"); diff --git a/util/src/models/ReadState.ts b/util/src/models/ReadState.ts deleted file mode 100644 index 9c4fb323..00000000 --- a/util/src/models/ReadState.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { PublicMember } from "./Member"; -import { Schema, model, Types, Document } from "mongoose"; -import db from "../util/Database"; - -export interface ReadState extends Document { - message_id: string; - channel_id: string; - user_id: string; - last_message_id?: string; - last_pin_timestamp?: Date; - mention_count: number; - manual: boolean; -} - -export const ReadStateSchema = new Schema({ - message_id: String, - channel_id: String, - user_id: String, - last_message_id: String, - last_pin_timestamp: Date, - mention_count: Number, - manual: Boolean, -}); - -// @ts-ignore -export const ReadStateModel = db.model("ReadState", ReadStateSchema, "readstates"); diff --git a/util/src/models/Role.ts b/util/src/models/Role.ts deleted file mode 100644 index c1111c84..00000000 --- a/util/src/models/Role.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { Schema, model, Types, Document } from "mongoose"; -import db from "../util/Database"; -import toBigInt from "../util/toBigInt"; - -export interface Role { - id: string; - guild_id: string; - color: number; - hoist: boolean; - managed: boolean; - mentionable: boolean; - name: string; - permissions: bigint; - position: number; - tags?: { - bot_id?: string; - }; -} - -export interface RoleDocument extends Document, Role { - id: string; -} - -export const RoleSchema = new Schema({ - id: String, - guild_id: String, - color: Number, - hoist: Boolean, - managed: Boolean, - mentionable: Boolean, - name: String, - permissions: { type: String, get: toBigInt }, - position: Number, - tags: { - bot_id: String, - }, -}); - -RoleSchema.set("removeResponse", ["guild_id"]); - -// @ts-ignore -export const RoleModel = db.model("Role", RoleSchema, "roles"); diff --git a/util/src/models/Team.ts b/util/src/models/Team.ts deleted file mode 100644 index 795c82d2..00000000 --- a/util/src/models/Team.ts +++ /dev/null @@ -1,17 +0,0 @@ -export interface Team { - icon: string | null; - id: string; - members: { - membership_state: number; - permissions: string[]; - team_id: string; - user_id: string; - }[]; - name: string; - owner_user_id: string; -} - -export enum TeamMemberState { - INVITED = 1, - ACCEPTED = 2, -} diff --git a/util/src/models/Template.ts b/util/src/models/Template.ts deleted file mode 100644 index ad0f9104..00000000 --- a/util/src/models/Template.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { Schema, model, Types, Document } from "mongoose"; -import db from "../util/Database"; -import { PublicUser, User, UserModel, PublicUserProjection } from "./User"; -import { Guild, GuildModel } from "./Guild"; - -export interface Template extends Document { - id: string; - code: string; - name: string; - description?: string; - usage_count?: number; - creator_id: string; - creator: User; - created_at: Date; - updated_at: Date; - source_guild_id: String; - serialized_source_guild: Guild; -} - -export const TemplateSchema = new Schema({ - id: String, - code: String, - name: String, - description: String, - usage_count: Number, - creator_id: String, - created_at: Date, - updated_at: Date, - source_guild_id: String, -}); - -TemplateSchema.virtual("creator", { - ref: UserModel, - localField: "creator_id", - foreignField: "id", - justOne: true, - autopopulate: { - select: PublicUserProjection, - }, -}); - -TemplateSchema.virtual("serialized_source_guild", { - ref: GuildModel, - localField: "source_guild_id", - foreignField: "id", - justOne: true, - autopopulate: true, -}); - -// @ts-ignore -export const TemplateModel = db.model