From 4f6e59e936dd9dfc10bebe7ac10dea8c0d96e6ec Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Wed, 10 Aug 2022 02:53:29 -0600 Subject: Add basic developer panel functionality - doesn't work yet > > > Co-authored-by: TheArcaneBrony --- api/src/routes/applications/#id/bot/index.ts | 83 ++++++++++++++++++++++++++++ api/src/routes/applications/#id/index.ts | 27 +++++++++ api/src/routes/applications/#id/skus.ts | 11 ++++ api/src/routes/applications/index.ts | 28 +++++++++- 4 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 api/src/routes/applications/#id/bot/index.ts create mode 100644 api/src/routes/applications/#id/index.ts create mode 100644 api/src/routes/applications/#id/skus.ts (limited to 'api/src') diff --git a/api/src/routes/applications/#id/bot/index.ts b/api/src/routes/applications/#id/bot/index.ts new file mode 100644 index 00000000..82c09c4d --- /dev/null +++ b/api/src/routes/applications/#id/bot/index.ts @@ -0,0 +1,83 @@ +import { Request, Response, Router } from "express"; +import { route } from "@fosscord/api"; +import { Application, Config, FieldErrors, generateToken, OrmUtils, Snowflake, trimSpecial, User } from "@fosscord/util"; +import { HTTPError } from "lambert-server"; +import { verifyToken } from "node-2fa"; + +const router: Router = Router(); + +router.post("/", route({}), async (req: Request, res: Response) => { + const app = await Application.findOne({where: {id: req.params.id}}); + if(!app) return res.status(404); + const username = trimSpecial(app.name); + const discriminator = await User.generateDiscriminator(username); + if (!discriminator) { + // We've failed to generate a valid and unused discriminator + throw FieldErrors({ + username: { + code: "USERNAME_TOO_MANY_USERS", + message: req?.t("auth:register.USERNAME_TOO_MANY_USERS"), + }, + }); + } + + const user = OrmUtils.mergeDeep(new User(), { + created_at: new Date(), + username: username, + discriminator, + id: app.id, + bot: true, + system: false, + premium_since: new Date(), + desktop: false, + mobile: false, + premium: true, + premium_type: 2, + bio: app.description, + mfa_enabled: false, + totp_secret: "", + totp_backup_codes: [], + verified: true, + disabled: false, + deleted: false, + email: null, + rights: Config.get().register.defaultRights, + nsfw_allowed: true, + public_flags: "0", + flags: "0", + data: { + hash: null, + valid_tokens_since: new Date(), + }, + settings: {}, + extended_settings: {}, + fingerprints: [], + notes: {}, + }); + await user.save(); + app.bot = user; + await app.save(); + res.send().status(204) +}); + +router.post("/reset", route({}), async (req: Request, res: Response) => { + let bot = await User.findOne({where: {id: req.params.id}}); + let owner = await User.findOne({where: {id: req.user_id}}); + if(!bot) return res.status(404); + if(owner?.totp_secret && (!req.body.code || verifyToken(owner.totp_secret, req.body.code))) { + throw new HTTPError(req.t("auth:login.INVALID_TOTP_CODE"), 60008); + } + bot.data = { hash: undefined, valid_tokens_since: new Date() }; + await bot.save(); + let token = await generateToken(bot.id); + res.json({token}).status(200); +}); + +router.patch("/", route({}), async (req: Request, res: Response) => { + delete req.body.icon; + let app = OrmUtils.mergeDeep(await User.findOne({where: {id: req.params.id}}), req.body); + await app.save(); + res.json(app).status(200); +}); + +export default router; \ No newline at end of file diff --git a/api/src/routes/applications/#id/index.ts b/api/src/routes/applications/#id/index.ts new file mode 100644 index 00000000..be8c3ba4 --- /dev/null +++ b/api/src/routes/applications/#id/index.ts @@ -0,0 +1,27 @@ +import { Request, Response, Router } from "express"; +import { route } from "@fosscord/api"; +import { Application, OrmUtils, Team, trimSpecial, User } from "@fosscord/util"; + +const router: Router = Router(); + +router.get("/", route({}), async (req: Request, res: Response) => { + //TODO + let results = await Application.findOne({where: {id: req.params.id}, relations: ["owner", "bot"] }); + //debugger; + res.json(results).status(200); +}); + +router.patch("/", route({}), async (req: Request, res: Response) => { + delete req.body.icon; + let app = OrmUtils.mergeDeep(await Application.findOne({where: {id: req.params.id}, relations: ["owner", "bot"]}), req.body); + if(app.bot) { + app.bot.bio = req.body.description + app.bot?.save(); + } + if(req.body.tags) app.tags = req.body.tags; + await app.save(); + debugger; + res.json(app).status(200); +}); + +export default router; \ No newline at end of file diff --git a/api/src/routes/applications/#id/skus.ts b/api/src/routes/applications/#id/skus.ts new file mode 100644 index 00000000..5b667f36 --- /dev/null +++ b/api/src/routes/applications/#id/skus.ts @@ -0,0 +1,11 @@ +import { Request, Response, Router } from "express"; +import { route } from "@fosscord/api"; +import { Application, OrmUtils, Team, trimSpecial, User } from "@fosscord/util"; + +const router: Router = Router(); + +router.get("/", route({}), async (req: Request, res: Response) => { + res.json([]).status(200); +}); + +export default router; \ No newline at end of file diff --git a/api/src/routes/applications/index.ts b/api/src/routes/applications/index.ts index 28ce42da..c9be1131 100644 --- a/api/src/routes/applications/index.ts +++ b/api/src/routes/applications/index.ts @@ -1,11 +1,35 @@ import { Request, Response, Router } from "express"; import { route } from "@fosscord/api"; +import { Application, OrmUtils, Team, trimSpecial, User } from "@fosscord/util"; const router: Router = Router(); +export interface ApplicationCreateSchema { + name: string; + team_id?: string | number; +} + router.get("/", route({}), async (req: Request, res: Response) => { //TODO - res.send([]).status(200); + let results = await Application.find({where: {owner: {id: req.user_id}}, relations: ["owner"] }); + res.json(results).status(200); +}); + +router.post("/", route({}), async (req: Request, res: Response) => { + const body = req.body as ApplicationCreateSchema; + const user = await User.findOne({where: {id: req.user_id}}) + if(!user) res.status(420); + let app = OrmUtils.mergeDeep(new Application(), { + name: trimSpecial(body.name), + description: "", + bot_public: true, + bot_require_code_grant: false, + owner: user, + verify_key: "IMPLEMENTME", + flags: "" + }); + await app.save(); + res.json(app).status(200); }); -export default router; +export default router; \ No newline at end of file -- cgit 1.5.1 From 4fe40c2a2016cce4278fbb40093c38145b36e0ea Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Wed, 10 Aug 2022 03:52:03 -0600 Subject: It works now... I guess Co-authored-by: TheArcaneBrony --- api/src/routes/applications/#id/index.ts | 1 - api/src/routes/applications/index.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'api/src') diff --git a/api/src/routes/applications/#id/index.ts b/api/src/routes/applications/#id/index.ts index be8c3ba4..a16b18a9 100644 --- a/api/src/routes/applications/#id/index.ts +++ b/api/src/routes/applications/#id/index.ts @@ -7,7 +7,6 @@ const router: Router = Router(); router.get("/", route({}), async (req: Request, res: Response) => { //TODO let results = await Application.findOne({where: {id: req.params.id}, relations: ["owner", "bot"] }); - //debugger; res.json(results).status(200); }); diff --git a/api/src/routes/applications/index.ts b/api/src/routes/applications/index.ts index c9be1131..41ce35b5 100644 --- a/api/src/routes/applications/index.ts +++ b/api/src/routes/applications/index.ts @@ -11,7 +11,7 @@ export interface ApplicationCreateSchema { router.get("/", route({}), async (req: Request, res: Response) => { //TODO - let results = await Application.find({where: {owner: {id: req.user_id}}, relations: ["owner"] }); + let results = await Application.find({where: {owner: {id: req.user_id}}, relations: ["owner", "bot"] }); res.json(results).status(200); }); -- cgit 1.5.1 From e041945eccbd3f5241e4ef62239d207c180bd466 Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Wed, 10 Aug 2022 04:29:14 -0600 Subject: I'm probably forgetting a lot of things, but hey, it works --- api/src/routes/applications/#id/index.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'api/src') diff --git a/api/src/routes/applications/#id/index.ts b/api/src/routes/applications/#id/index.ts index a16b18a9..24df2d88 100644 --- a/api/src/routes/applications/#id/index.ts +++ b/api/src/routes/applications/#id/index.ts @@ -5,7 +5,6 @@ import { Application, OrmUtils, Team, trimSpecial, User } from "@fosscord/util"; const router: Router = Router(); router.get("/", route({}), async (req: Request, res: Response) => { - //TODO let results = await Application.findOne({where: {id: req.params.id}, relations: ["owner", "bot"] }); res.json(results).status(200); }); @@ -23,4 +22,10 @@ router.patch("/", route({}), async (req: Request, res: Response) => { res.json(app).status(200); }); +router.post("/delete", route({}), async (req: Request, res: Response) => { + await Application.delete(req.params.id); + res.send().status(200); +}); + + export default router; \ No newline at end of file -- cgit 1.5.1 From f79baa7b2cb58106ad2ffb69ce6166ce821c5d29 Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Wed, 10 Aug 2022 04:35:10 -0600 Subject: Whoops, missed one --- api/src/routes/applications/#id/index.ts | 1 - 1 file changed, 1 deletion(-) (limited to 'api/src') diff --git a/api/src/routes/applications/#id/index.ts b/api/src/routes/applications/#id/index.ts index 24df2d88..0aced582 100644 --- a/api/src/routes/applications/#id/index.ts +++ b/api/src/routes/applications/#id/index.ts @@ -18,7 +18,6 @@ router.patch("/", route({}), async (req: Request, res: Response) => { } if(req.body.tags) app.tags = req.body.tags; await app.save(); - debugger; res.json(app).status(200); }); -- cgit 1.5.1 From 5974acc91e80aae47006ad54808bc83169cd0d86 Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Wed, 10 Aug 2022 04:50:29 -0600 Subject: Whoops --- api/src/routes/applications/#id/bot/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'api/src') diff --git a/api/src/routes/applications/#id/bot/index.ts b/api/src/routes/applications/#id/bot/index.ts index 82c09c4d..a8a24d13 100644 --- a/api/src/routes/applications/#id/bot/index.ts +++ b/api/src/routes/applications/#id/bot/index.ts @@ -74,7 +74,7 @@ router.post("/reset", route({}), async (req: Request, res: Response) => { }); router.patch("/", route({}), async (req: Request, res: Response) => { - delete req.body.icon; + delete req.body.avatar; let app = OrmUtils.mergeDeep(await User.findOne({where: {id: req.params.id}}), req.body); await app.save(); res.json(app).status(200); -- cgit 1.5.1 From d20a42af18ff641f365bcee2069cb660cc6dbe35 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Wed, 10 Aug 2022 16:07:01 +0200 Subject: Make teams nullable --- api/src/routes/applications/index.ts | 3 +-- util/src/entities/Application.ts | 1 + .../mariadb/1660131942703-apps_nullable_team.ts | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 util/src/migrations/mariadb/1660131942703-apps_nullable_team.ts (limited to 'api/src') diff --git a/api/src/routes/applications/index.ts b/api/src/routes/applications/index.ts index 41ce35b5..033dcc51 100644 --- a/api/src/routes/applications/index.ts +++ b/api/src/routes/applications/index.ts @@ -23,10 +23,9 @@ router.post("/", route({}), async (req: Request, res: Response) => { name: trimSpecial(body.name), description: "", bot_public: true, - bot_require_code_grant: false, owner: user, verify_key: "IMPLEMENTME", - flags: "" + flags: 0 }); await app.save(); res.json(app).status(200); diff --git a/util/src/entities/Application.ts b/util/src/entities/Application.ts index 28381579..103f8e84 100644 --- a/util/src/entities/Application.ts +++ b/util/src/entities/Application.ts @@ -104,6 +104,7 @@ export class Application extends BaseClass { @JoinColumn({ name: "team_id" }) @ManyToOne(() => Team, { onDelete: "CASCADE", + nullable: true }) team?: Team; diff --git a/util/src/migrations/mariadb/1660131942703-apps_nullable_team.ts b/util/src/migrations/mariadb/1660131942703-apps_nullable_team.ts new file mode 100644 index 00000000..ac445772 --- /dev/null +++ b/util/src/migrations/mariadb/1660131942703-apps_nullable_team.ts @@ -0,0 +1,18 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class appsNullableTeam1660131942703 implements MigrationInterface { + name = 'appsNullableTeam1660131942703' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + DROP INDEX \`IDX_2ce5a55796fe4c2f77ece57a64\` ON \`applications\` + `); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + CREATE UNIQUE INDEX \`IDX_2ce5a55796fe4c2f77ece57a64\` ON \`applications\` (\`bot_user_id\`) + `); + } + +} -- cgit 1.5.1 From 443c7d8a1ee81add4b549b00fc27fefe6e50279c Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Fri, 12 Aug 2022 23:28:12 -0600 Subject: Made nitro stuff make some sense --- api/src/routes/applications/#id/bot/index.ts | 8 ++++---- bundle/package.json | 2 +- util/src/entities/Member.ts | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) (limited to 'api/src') diff --git a/api/src/routes/applications/#id/bot/index.ts b/api/src/routes/applications/#id/bot/index.ts index a8a24d13..a51c9526 100644 --- a/api/src/routes/applications/#id/bot/index.ts +++ b/api/src/routes/applications/#id/bot/index.ts @@ -28,13 +28,13 @@ router.post("/", route({}), async (req: Request, res: Response) => { id: app.id, bot: true, system: false, - premium_since: new Date(), + premium_since: 0, desktop: false, mobile: false, - premium: true, - premium_type: 2, + premium: false, + premium_type: 0, bio: app.description, - mfa_enabled: false, + mfa_enabled: true, totp_secret: "", totp_backup_codes: [], verified: true, diff --git a/bundle/package.json b/bundle/package.json index 6adb569c..b67598b8 100644 --- a/bundle/package.json +++ b/bundle/package.json @@ -99,4 +99,4 @@ "typescript": "^4.1.2", "ws": "^8.8.1" } -} +} \ No newline at end of file diff --git a/util/src/entities/Member.ts b/util/src/entities/Member.ts index d4395e03..baac58ed 100644 --- a/util/src/entities/Member.ts +++ b/util/src/entities/Member.ts @@ -257,7 +257,7 @@ export class Member extends BaseClassWithoutId { nick: undefined, roles: [guild_id], // @everyone role joined_at: new Date(), - premium_since: new Date(), + premium_since: null, deaf: false, mute: false, pending: false, -- cgit 1.5.1 From d9760fe9146ba87a8296a566abbb4aa7ab61c750 Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Fri, 12 Aug 2022 23:44:50 -0600 Subject: Handle avatar uploads --- api/src/routes/applications/#id/bot/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'api/src') diff --git a/api/src/routes/applications/#id/bot/index.ts b/api/src/routes/applications/#id/bot/index.ts index a51c9526..562c31fe 100644 --- a/api/src/routes/applications/#id/bot/index.ts +++ b/api/src/routes/applications/#id/bot/index.ts @@ -1,6 +1,6 @@ import { Request, Response, Router } from "express"; import { route } from "@fosscord/api"; -import { Application, Config, FieldErrors, generateToken, OrmUtils, Snowflake, trimSpecial, User } from "@fosscord/util"; +import { Application, Config, FieldErrors, generateToken, OrmUtils, Snowflake, trimSpecial, User, handleFile } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { verifyToken } from "node-2fa"; @@ -74,7 +74,7 @@ router.post("/reset", route({}), async (req: Request, res: Response) => { }); router.patch("/", route({}), async (req: Request, res: Response) => { - delete req.body.avatar; + if (req.body.avatar) req.body.avatar = await handleFile(`/avatars/${req.params.id}`, req.body.avatar as string); let app = OrmUtils.mergeDeep(await User.findOne({where: {id: req.params.id}}), req.body); await app.save(); res.json(app).status(200); -- cgit 1.5.1