From 90af904661ac5af8db83dc2e34fb4f6f2d0c9275 Mon Sep 17 00:00:00 2001 From: Lobo Metalúrgico <43734867+LoboMetalurgico@users.noreply.github.com> Date: Fri, 8 Oct 2021 23:45:12 -0300 Subject: (api): inital emojis route implementation --- api/src/routes/guilds/#guild_id/emojis.ts | 78 +++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 api/src/routes/guilds/#guild_id/emojis.ts (limited to 'api/src') diff --git a/api/src/routes/guilds/#guild_id/emojis.ts b/api/src/routes/guilds/#guild_id/emojis.ts new file mode 100644 index 00000000..f1e427e8 --- /dev/null +++ b/api/src/routes/guilds/#guild_id/emojis.ts @@ -0,0 +1,78 @@ +import { Router, Request, Response } from "express"; +import { + Config, + emitEvent, + Emoji, + GuildEmojiUpdateEvent, + Member, + User +} from "@fosscord/util"; +import { HTTPError } from "lambert-server"; +import { route } from "@fosscord/api"; + +const router = Router(); + +export interface EmojiCreateSchema { + name?: string; + image?: string; + roles?: string[]; +} + +router.get("/", route({}), async (req: Request, res: Response) => { + const guild_id = req.params.guild_id; + + await Member.IsInGuildOrFail(req.user_id, guild_id); + + const emojis = await Emoji.find({ guild_id: guild_id }); + + return res.json(emojis); +}); + +router.get("/:emoji_id", route({}), async (req: Request, res: Response) => { + const guild_id = req.params.guild_id; + const emoji_id = req.params.emoji_id; + + await Member.IsInGuildOrFail(req.user_id, guild_id); + + const emoji = await Emoji.findOneOrFail({ guild_id: guild_id, id: emoji_id }); + + return res.json(emoji); +}); + +/** WIP +router.post("/", route({ body: "EmojiCreateSchema", permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => { + const guild_id = req.params.guild_id; + const body = req.body as EmojiCreateSchema; + + const emoji_count = await Emoji.count({ guild_id: guild_id }); + const { maxEmojis } = Config.get().limits.guild; + + if (emoji_count >= maxEmojis) { + throw new HTTPError("Max emojis reached", 400); + } +}); +*/ + +router.delete("/:emoji_id", route({ permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => { + const guild_id = req.params.guild_id; + const { emoji_id } = req.params; + + await Promise.all([ + Emoji.delete({ + id: emoji_id, + guild_id: guild_id + }), + emitEvent({ + event: "GUILD_EMOJI_UPDATE", + guild_id: guild_id, + data: { + guild_id: guild_id, + emojis: await Emoji.find({ guild_id: guild_id }) + } + } as GuildEmojiUpdateEvent) + ]) + + res.sendStatus(204); +}); + +export default router; -- cgit 1.4.1 From 23827dfc774b619c1f19512e199584785e8ff525 Mon Sep 17 00:00:00 2001 From: Lobo Metalúrgico <43734867+LoboMetalurgico@users.noreply.github.com> Date: Fri, 8 Oct 2021 23:49:45 -0300 Subject: (api): fix route name --- api/src/routes/guilds/#guild_id/emoji.ts | 78 +++++++++++++++++++++++++++++++ api/src/routes/guilds/#guild_id/emojis.ts | 78 ------------------------------- 2 files changed, 78 insertions(+), 78 deletions(-) create mode 100644 api/src/routes/guilds/#guild_id/emoji.ts delete mode 100644 api/src/routes/guilds/#guild_id/emojis.ts (limited to 'api/src') diff --git a/api/src/routes/guilds/#guild_id/emoji.ts b/api/src/routes/guilds/#guild_id/emoji.ts new file mode 100644 index 00000000..f1e427e8 --- /dev/null +++ b/api/src/routes/guilds/#guild_id/emoji.ts @@ -0,0 +1,78 @@ +import { Router, Request, Response } from "express"; +import { + Config, + emitEvent, + Emoji, + GuildEmojiUpdateEvent, + Member, + User +} from "@fosscord/util"; +import { HTTPError } from "lambert-server"; +import { route } from "@fosscord/api"; + +const router = Router(); + +export interface EmojiCreateSchema { + name?: string; + image?: string; + roles?: string[]; +} + +router.get("/", route({}), async (req: Request, res: Response) => { + const guild_id = req.params.guild_id; + + await Member.IsInGuildOrFail(req.user_id, guild_id); + + const emojis = await Emoji.find({ guild_id: guild_id }); + + return res.json(emojis); +}); + +router.get("/:emoji_id", route({}), async (req: Request, res: Response) => { + const guild_id = req.params.guild_id; + const emoji_id = req.params.emoji_id; + + await Member.IsInGuildOrFail(req.user_id, guild_id); + + const emoji = await Emoji.findOneOrFail({ guild_id: guild_id, id: emoji_id }); + + return res.json(emoji); +}); + +/** WIP +router.post("/", route({ body: "EmojiCreateSchema", permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => { + const guild_id = req.params.guild_id; + const body = req.body as EmojiCreateSchema; + + const emoji_count = await Emoji.count({ guild_id: guild_id }); + const { maxEmojis } = Config.get().limits.guild; + + if (emoji_count >= maxEmojis) { + throw new HTTPError("Max emojis reached", 400); + } +}); +*/ + +router.delete("/:emoji_id", route({ permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => { + const guild_id = req.params.guild_id; + const { emoji_id } = req.params; + + await Promise.all([ + Emoji.delete({ + id: emoji_id, + guild_id: guild_id + }), + emitEvent({ + event: "GUILD_EMOJI_UPDATE", + guild_id: guild_id, + data: { + guild_id: guild_id, + emojis: await Emoji.find({ guild_id: guild_id }) + } + } as GuildEmojiUpdateEvent) + ]) + + res.sendStatus(204); +}); + +export default router; diff --git a/api/src/routes/guilds/#guild_id/emojis.ts b/api/src/routes/guilds/#guild_id/emojis.ts deleted file mode 100644 index f1e427e8..00000000 --- a/api/src/routes/guilds/#guild_id/emojis.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { Router, Request, Response } from "express"; -import { - Config, - emitEvent, - Emoji, - GuildEmojiUpdateEvent, - Member, - User -} from "@fosscord/util"; -import { HTTPError } from "lambert-server"; -import { route } from "@fosscord/api"; - -const router = Router(); - -export interface EmojiCreateSchema { - name?: string; - image?: string; - roles?: string[]; -} - -router.get("/", route({}), async (req: Request, res: Response) => { - const guild_id = req.params.guild_id; - - await Member.IsInGuildOrFail(req.user_id, guild_id); - - const emojis = await Emoji.find({ guild_id: guild_id }); - - return res.json(emojis); -}); - -router.get("/:emoji_id", route({}), async (req: Request, res: Response) => { - const guild_id = req.params.guild_id; - const emoji_id = req.params.emoji_id; - - await Member.IsInGuildOrFail(req.user_id, guild_id); - - const emoji = await Emoji.findOneOrFail({ guild_id: guild_id, id: emoji_id }); - - return res.json(emoji); -}); - -/** WIP -router.post("/", route({ body: "EmojiCreateSchema", permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => { - const guild_id = req.params.guild_id; - const body = req.body as EmojiCreateSchema; - - const emoji_count = await Emoji.count({ guild_id: guild_id }); - const { maxEmojis } = Config.get().limits.guild; - - if (emoji_count >= maxEmojis) { - throw new HTTPError("Max emojis reached", 400); - } -}); -*/ - -router.delete("/:emoji_id", route({ permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => { - const guild_id = req.params.guild_id; - const { emoji_id } = req.params; - - await Promise.all([ - Emoji.delete({ - id: emoji_id, - guild_id: guild_id - }), - emitEvent({ - event: "GUILD_EMOJI_UPDATE", - guild_id: guild_id, - data: { - guild_id: guild_id, - emojis: await Emoji.find({ guild_id: guild_id }) - } - } as GuildEmojiUpdateEvent) - ]) - - res.sendStatus(204); -}); - -export default router; -- cgit 1.4.1 From 6b44a7ada1f3fabcfbf89ae51a8e852c77f3f210 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 9 Oct 2021 14:46:59 +0200 Subject: :art: cdn now also works without setting a public endpoint --- api/src/routes/channels/#channel_id/messages/index.ts | 5 ++++- bundle/src/Server.ts | 3 --- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'api/src') diff --git a/api/src/routes/channels/#channel_id/messages/index.ts b/api/src/routes/channels/#channel_id/messages/index.ts index 1f856b80..4ec31417 100644 --- a/api/src/routes/channels/#channel_id/messages/index.ts +++ b/api/src/routes/channels/#channel_id/messages/index.ts @@ -103,6 +103,7 @@ router.get("/", async (req: Request, res: Response) => { } const messages = await Message.find(query); + const endpoint = Config.get().cdn.endpointPublic; return res.json( messages.map((x) => { @@ -115,7 +116,9 @@ router.get("/", async (req: Request, res: Response) => { // @ts-ignore if (!x.author) x.author = { discriminator: "0000", username: "Deleted User", public_flags: "0", avatar: null }; x.attachments?.forEach((x) => { - x.proxy_url = `${Config.get().cdn.endpointPublic || "http://localhost:3003"}${new URL(x.proxy_url).pathname}`; + // dynamically set attachment proxy_url in case the endpoint changed + const uri = x.proxy_url.startsWith("http") ? x.proxy_url : `https://example.org${x.proxy_url}`; + x.proxy_url = `${endpoint == null ? "http://localhost:3003" : endpoint}${new URL(uri).pathname}`; }); return x; diff --git a/bundle/src/Server.ts b/bundle/src/Server.ts index 662b9008..5dda2d02 100644 --- a/bundle/src/Server.ts +++ b/bundle/src/Server.ts @@ -30,9 +30,6 @@ async function main() { cdn: { endpointClient: "${location.host}", endpointPrivate: `http://localhost:${port}`, - ...(!Config.get().cdn.endpointPublic && { - endpointPublic: `http://localhost:${port}`, - }), }, gateway: { endpointClient: -- cgit 1.4.1 From 8d5911736ad41cdcd17654a269e95777a3219385 Mon Sep 17 00:00:00 2001 From: Lobo Metalúrgico <43734867+LoboMetalurgico@users.noreply.github.com> Date: Sat, 9 Oct 2021 10:52:43 -0300 Subject: (api): add patch to emoji route --- api/src/routes/guilds/#guild_id/emoji.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'api/src') diff --git a/api/src/routes/guilds/#guild_id/emoji.ts b/api/src/routes/guilds/#guild_id/emoji.ts index f1e427e8..f14790ed 100644 --- a/api/src/routes/guilds/#guild_id/emoji.ts +++ b/api/src/routes/guilds/#guild_id/emoji.ts @@ -18,6 +18,11 @@ export interface EmojiCreateSchema { roles?: string[]; } +export interface EmojiModifySchema { + name?: string; + roles?: string[]; +} + router.get("/", route({}), async (req: Request, res: Response) => { const guild_id = req.params.guild_id; @@ -53,6 +58,27 @@ router.post("/", route({ body: "EmojiCreateSchema", permission: "MANAGE_EMOJIS_A }); */ +router.patch("/:emoji_id", route({ body: "EmojiModifySchema", permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => { + const { emoji_id, guild_id } = req.params; + const body = req.body as EmojiModifySchema; + + const emoji = new Emoji({ ...body, id: emoji_id, guild_id: guild_id }); + + await Promise.all([ + emoji.save(), + emitEvent({ + event: "GUILD_EMOJI_UPDATE", + guild_id: guild_id, + data: { + guild_id: guild_id, + emojis: await Emoji.find({ guild_id: guild_id }) + } + } as GuildEmojiUpdateEvent) + ]) + + return res.json(emoji); +}); + router.delete("/:emoji_id", route({ permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => { const guild_id = req.params.guild_id; const { emoji_id } = req.params; -- cgit 1.4.1 From 4e684e2cd45b12184422ec9933476e553d037016 Mon Sep 17 00:00:00 2001 From: Lobo Metalúrgico <43734867+LoboMetalurgico@users.noreply.github.com> Date: Sat, 9 Oct 2021 11:36:58 -0300 Subject: (api): implement post on emoji route --- api/src/routes/guilds/#guild_id/emoji.ts | 42 +++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) (limited to 'api/src') diff --git a/api/src/routes/guilds/#guild_id/emoji.ts b/api/src/routes/guilds/#guild_id/emoji.ts index f14790ed..6b194f7b 100644 --- a/api/src/routes/guilds/#guild_id/emoji.ts +++ b/api/src/routes/guilds/#guild_id/emoji.ts @@ -1,10 +1,13 @@ import { Router, Request, Response } from "express"; import { Config, + DiscordApiErrors, emitEvent, Emoji, GuildEmojiUpdateEvent, + handleFile, Member, + Snowflake, User } from "@fosscord/util"; import { HTTPError } from "lambert-server"; @@ -15,6 +18,7 @@ const router = Router(); export interface EmojiCreateSchema { name?: string; image?: string; + require_colons?: boolean | null; roles?: string[]; } @@ -44,7 +48,6 @@ router.get("/:emoji_id", route({}), async (req: Request, res: Response) => { return res.json(emoji); }); -/** WIP router.post("/", route({ body: "EmojiCreateSchema", permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => { const guild_id = req.params.guild_id; const body = req.body as EmojiCreateSchema; @@ -52,11 +55,42 @@ router.post("/", route({ body: "EmojiCreateSchema", permission: "MANAGE_EMOJIS_A const emoji_count = await Emoji.count({ guild_id: guild_id }); const { maxEmojis } = Config.get().limits.guild; - if (emoji_count >= maxEmojis) { - throw new HTTPError("Max emojis reached", 400); + if (emoji_count >= maxEmojis) throw DiscordApiErrors.MAXIMUM_NUMBER_OF_EMOJIS_REACHED.withParams(maxEmojis); + + const id = Snowflake.generate(); + + if (!body.image) { + throw DiscordApiErrors.GENERAL_ERROR.withParams("No image provided"); } + + if (body.require_colons === null) body.require_colons = true; + + const user = await User.findOneOrFail({ id: req.user_id }); + + body.image = await handleFile(`/emojis/${id}`, body.image); + + const emoji = new Emoji({ + id: id, + guild_id: guild_id, + ...body, + user: user, + managed: false, + animated: false, // TODO: Add support animated emojis + available: true + }); + + await Promise.all([ + emoji.save(), + emitEvent({ + event: "GUILD_EMOJI_UPDATE", + guild_id: guild_id, + data: { + guild_id: guild_id, + emojis: await Emoji.find({ guild_id: guild_id }) + } + } as GuildEmojiUpdateEvent) + ]); }); -*/ router.patch("/:emoji_id", route({ body: "EmojiModifySchema", permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => { const { emoji_id, guild_id } = req.params; -- cgit 1.4.1 From da030aaff2ca77395d0ca7a39e5ca4f7abfc23cd Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 9 Oct 2021 17:33:28 +0200 Subject: :sparkles: added read state --- .../routes/channels/#channel_id/messages/#message_id/ack.ts | 8 ++++++-- gateway/src/opcodes/Identify.ts | 11 +++++++++-- util/src/entities/ReadState.ts | 13 +++++++------ util/src/util/Config.ts | 1 - 4 files changed, 22 insertions(+), 11 deletions(-) (limited to 'api/src') 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 786e4581..208c1da4 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, ReadState } from "@fosscord/util"; +import { emitEvent, getPermission, MessageAckEvent, ReadState, Snowflake } from "@fosscord/util"; import { Request, Response, Router } from "express"; import { route } from "@fosscord/api"; @@ -18,7 +18,11 @@ router.post("/", route({ body: "MessageAcknowledgeSchema" }), async (req: Reques const permission = await getPermission(req.user_id, undefined, channel_id); permission.hasThrow("VIEW_CHANNEL"); - await ReadState.update({ user_id: req.user_id, channel_id }, { user_id: req.user_id, channel_id, last_message_id: message_id }); + let read_state = await ReadState.findOne({ user_id: req.user_id, channel_id }); + if (!read_state) read_state = new ReadState({ user_id: req.user_id, channel_id }); + read_state.last_message_id = message_id; + + await read_state.save(); await emitEvent({ event: "MESSAGE_ACK", diff --git a/gateway/src/opcodes/Identify.ts b/gateway/src/opcodes/Identify.ts index 6decf21c..b81c7bf4 100644 --- a/gateway/src/opcodes/Identify.ts +++ b/gateway/src/opcodes/Identify.ts @@ -11,6 +11,7 @@ import { PublicMember, PublicUser, PrivateUserProjection, + ReadState, } from "@fosscord/util"; import { Send } from "../util/Send"; import { CLOSECODES, OPCODES } from "../util/Constants"; @@ -138,6 +139,13 @@ export async function onIdentify(this: WebSocket, data: Payload) { //We save the session and we delete it when the websocket is closed await session.save(); + const read_states = await ReadState.find({ user_id: this.user_id }); + read_states.forEach((s: any) => { + s.id = s.channel_id; + delete s.user_id; + delete s.channel_id; + }); + const privateUser = { avatar: user.avatar, mobile: user.mobile, @@ -176,8 +184,7 @@ export async function onIdentify(this: WebSocket, data: Payload) { geo_ordered_rtc_regions: [], // TODO relationships: user.relationships.map((x) => x.toPublicRelationship()), read_state: { - // TODO - entries: [], + entries: read_states, partial: false, version: 304128, }, diff --git a/util/src/entities/ReadState.ts b/util/src/entities/ReadState.ts index 68e867a0..89480e83 100644 --- a/util/src/entities/ReadState.ts +++ b/util/src/entities/ReadState.ts @@ -1,4 +1,4 @@ -import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; +import { Column, Entity, Index, JoinColumn, ManyToOne, RelationId } from "typeorm"; import { BaseClass } from "./BaseClass"; import { Channel } from "./Channel"; import { Message } from "./Message"; @@ -9,8 +9,9 @@ import { User } from "./User"; // public read receipt ≥ notification cursor ≥ private fully read marker @Entity("read_states") +@Index(["channel_id", "user_id"], { unique: true }) export class ReadState extends BaseClass { - @Column({ nullable: true }) + @Column() @RelationId((read_state: ReadState) => read_state.channel) channel_id: string; @@ -20,7 +21,7 @@ export class ReadState extends BaseClass { }) channel: Channel; - @Column({ nullable: true }) + @Column() @RelationId((read_state: ReadState) => read_state.user) user_id: string; @@ -35,15 +36,15 @@ export class ReadState extends BaseClass { last_message_id: string; @JoinColumn({ name: "last_message_id" }) - @ManyToOne(() => Message) + @ManyToOne(() => Message, { nullable: true }) last_message?: Message; @Column({ nullable: true }) last_pin_timestamp?: Date; - @Column() + @Column({ nullable: true }) mention_count: number; - @Column() + @Column({ nullable: true }) manual: boolean; } diff --git a/util/src/util/Config.ts b/util/src/util/Config.ts index eeeaa2ce..704f3f2f 100644 --- a/util/src/util/Config.ts +++ b/util/src/util/Config.ts @@ -12,7 +12,6 @@ export const Config = { if (config) return config; pairs = await ConfigEntity.find(); config = pairsToConfig(pairs); - console.log(config.guild.autoJoin); return this.set((config || {}).merge(DefaultConfigOptions)); }, -- cgit 1.4.1 From bfd74459782148f166461356d5ee9291f496b68c Mon Sep 17 00:00:00 2001 From: Lobo Metalúrgico <43734867+LoboMetalurgico@users.noreply.github.com> Date: Sat, 9 Oct 2021 11:43:27 -0300 Subject: (api): fix some issues --- api/src/routes/guilds/#guild_id/emoji.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'api/src') diff --git a/api/src/routes/guilds/#guild_id/emoji.ts b/api/src/routes/guilds/#guild_id/emoji.ts index 6b194f7b..000c9949 100644 --- a/api/src/routes/guilds/#guild_id/emoji.ts +++ b/api/src/routes/guilds/#guild_id/emoji.ts @@ -10,7 +10,6 @@ import { Snowflake, User } from "@fosscord/util"; -import { HTTPError } from "lambert-server"; import { route } from "@fosscord/api"; const router = Router(); @@ -75,7 +74,7 @@ router.post("/", route({ body: "EmojiCreateSchema", permission: "MANAGE_EMOJIS_A ...body, user: user, managed: false, - animated: false, // TODO: Add support animated emojis + animated: false, // TODO: Add support animated emojis available: true }); @@ -108,7 +107,7 @@ router.patch("/:emoji_id", route({ body: "EmojiModifySchema", permission: "MANAG emojis: await Emoji.find({ guild_id: guild_id }) } } as GuildEmojiUpdateEvent) - ]) + ]); return res.json(emoji); }); -- cgit 1.4.1 From 74bd98737acf89d9d56ee66a68694d82f591d591 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 10 Oct 2021 11:03:32 +0200 Subject: :art: clean up imports + classes --- .gitignore | 4 +- api/src/Server.ts | 3 -- api/src/routes/channels/#channel_id/invites.ts | 2 +- .../routes/channels/#channel_id/messages/index.ts | 3 +- api/src/test/jwt.ts | 37 -------------- api/src/test/jwt2.ts | 13 ----- api/src/test/password_test.ts | 12 ----- gateway/src/events/Connection.ts | 3 +- gateway/src/opcodes/LazyRequest.ts | 10 +++- util/src/entities/BaseClass.ts | 58 ++-------------------- util/src/entities/Message.ts | 3 -- util/src/entities/RateLimit.ts | 3 -- util/src/entities/User.ts | 9 ++-- util/src/entities/Webhook.ts | 3 -- util/src/index.ts | 6 --- 15 files changed, 23 insertions(+), 146 deletions(-) delete mode 100644 api/src/test/jwt.ts delete mode 100644 api/src/test/jwt2.ts delete mode 100644 api/src/test/password_test.ts (limited to 'api/src') diff --git a/.gitignore b/.gitignore index f67723af..a09d215a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,6 @@ node_modules api/assets/*.js api/assets/*.css database.db -tsconfig.tsbuildinfo \ No newline at end of file +tsconfig.tsbuildinfo +files/ +.env \ No newline at end of file diff --git a/api/src/Server.ts b/api/src/Server.ts index 12c1d6b4..19e3d245 100644 --- a/api/src/Server.ts +++ b/api/src/Server.ts @@ -1,13 +1,10 @@ -import { OptionsJson } from "body-parser"; import "missing-native-js-functions"; -import { Connection } from "mongoose"; import { Server, ServerOptions } from "lambert-server"; import { Authentication, CORS } from "./middlewares/"; import { Config, initDatabase, initEvent } from "@fosscord/util"; import { ErrorHandler } from "./middlewares/ErrorHandler"; import { BodyParser } from "./middlewares/BodyParser"; import { Router, Request, Response, NextFunction } from "express"; -import mongoose from "mongoose"; import path from "path"; import { initRateLimits } from "./middlewares/RateLimit"; import TestClient from "./middlewares/TestClient"; diff --git a/api/src/routes/channels/#channel_id/invites.ts b/api/src/routes/channels/#channel_id/invites.ts index 22420983..6d2c625d 100644 --- a/api/src/routes/channels/#channel_id/invites.ts +++ b/api/src/routes/channels/#channel_id/invites.ts @@ -2,7 +2,7 @@ import { Router, Request, Response } from "express"; import { HTTPError } from "lambert-server"; import { route } from "@fosscord/api"; import { random } from "@fosscord/api"; -import { getPermission, Channel, Invite, InviteCreateEvent, emitEvent, User, Guild, PublicInviteRelation } from "@fosscord/util"; +import { Channel, Invite, InviteCreateEvent, emitEvent, User, Guild, PublicInviteRelation } from "@fosscord/util"; import { isTextChannel } from "./messages"; const router: Router = Router(); diff --git a/api/src/routes/channels/#channel_id/messages/index.ts b/api/src/routes/channels/#channel_id/messages/index.ts index 4ec31417..26bb9e5d 100644 --- a/api/src/routes/channels/#channel_id/messages/index.ts +++ b/api/src/routes/channels/#channel_id/messages/index.ts @@ -22,7 +22,7 @@ const router: Router = Router(); export default router; -function isTextChannel(type: ChannelType): boolean { +export function isTextChannel(type: ChannelType): boolean { switch (type) { case ChannelType.GUILD_STORE: case ChannelType.GUILD_VOICE: @@ -39,7 +39,6 @@ function isTextChannel(type: ChannelType): boolean { return true; } } -module.exports.isTextChannel = isTextChannel; export interface MessageCreateSchema { content?: string; diff --git a/api/src/test/jwt.ts b/api/src/test/jwt.ts deleted file mode 100644 index bdad513b..00000000 --- a/api/src/test/jwt.ts +++ /dev/null @@ -1,37 +0,0 @@ -const jwa = require("jwa"); - -var STR64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".split(""); - -function base64url(string: string, encoding: string) { - // @ts-ignore - return Buffer.from(string, encoding).toString("base64").replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_"); -} - -function to64String(input: number, current = ""): string { - if (input < 0 && current.length == 0) { - input = input * -1; - } - var modify = input % 64; - var remain = Math.floor(input / 64); - var result = STR64[modify] + current; - return remain <= 0 ? result : to64String(remain, result); -} - -function to64Parse(input: string) { - var result = 0; - var toProc = input.split(""); - var e; - for (e in toProc) { - result = result * 64 + STR64.indexOf(toProc[e]); - } - return result; -} - -// @ts-ignore -const start = `${base64url("311129357362135041")}.${to64String(Date.now())}`; -const signature = jwa("HS256").sign(start, `test`); -const token = `${start}.${signature}`; -console.log(token); - -// MzExMTI5MzU3MzYyMTM1MDQx.XdQb_rA.907VgF60kocnOTl32MSUWGSSzbAytQ0jbt36KjLaxuY -// MzExMTI5MzU3MzYyMTM1MDQx.XdQbaPy.4vGx4L7IuFJGsRe6IL3BeybLIvbx4Vauvx12pwNsy2U diff --git a/api/src/test/jwt2.ts b/api/src/test/jwt2.ts deleted file mode 100644 index e231233d..00000000 --- a/api/src/test/jwt2.ts +++ /dev/null @@ -1,13 +0,0 @@ -import jwt from "jsonwebtoken"; - -const algorithm = "HS256"; -const iat = Math.floor(Date.now() / 1000); - -// @ts-ignore -const token = jwt.sign({ id: "311129357362135041" }, "secret", { - algorithm, -}); -console.log(token); - -const decoded = jwt.verify(token, "secret", { algorithms: [algorithm] }); -console.log(decoded); diff --git a/api/src/test/password_test.ts b/api/src/test/password_test.ts deleted file mode 100644 index 983b18ae..00000000 --- a/api/src/test/password_test.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { checkPassword } from "@fosscord/api"; - -console.log(checkPassword("123456789012345")); -// -> 0.25 -console.log(checkPassword("ABCDEFGHIJKLMOPQ")); -// -> 0.25 -console.log(checkPassword("ABC123___...123")); -// -> -console.log(checkPassword("")); -// -> -// console.log(checkPassword("")); -// // -> diff --git a/gateway/src/events/Connection.ts b/gateway/src/events/Connection.ts index c1a6b618..2cf22f7d 100644 --- a/gateway/src/events/Connection.ts +++ b/gateway/src/events/Connection.ts @@ -24,6 +24,7 @@ export async function Connection( request: IncomingMessage ) { try { + // @ts-ignore socket.on("close", Close); // @ts-ignore socket.on("message", Message); @@ -68,12 +69,10 @@ export async function Connection( }); socket.readyTimeout = setTimeout(() => { - Session.delete({ session_id: socket.session_id }); //should we await? return socket.close(CLOSECODES.Session_timed_out); }, 1000 * 30); } catch (error) { console.error(error); - Session.delete({ session_id: socket.session_id }); //should we await? return socket.close(CLOSECODES.Unknown_error); } } diff --git a/gateway/src/opcodes/LazyRequest.ts b/gateway/src/opcodes/LazyRequest.ts index d37e32da..f5fd561a 100644 --- a/gateway/src/opcodes/LazyRequest.ts +++ b/gateway/src/opcodes/LazyRequest.ts @@ -41,6 +41,7 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) { const items = []; for (const role of roles) { + // @ts-ignore const [role_members, other_members] = partition(members, (m: Member) => m.roles.find((r) => r.id === role.id) ); @@ -53,9 +54,12 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) { groups.push(group); for (const member of role_members) { - member.roles = member.roles.filter((x) => x.id !== guild_id); + member.roles = member.roles.filter((x: Role) => x.id !== guild_id); items.push({ - member: { ...member, roles: member.roles.map((x) => x.id) }, + member: { + ...member, + roles: member.roles.map((x: Role) => x.id), + }, }); } members = other_members; @@ -84,7 +88,9 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) { } function partition(array: T[], isValid: Function) { + // @ts-ignore return array.reduce( + // @ts-ignore ([pass, fail], elem) => { return isValid(elem) ? [[...pass, elem], fail] diff --git a/util/src/entities/BaseClass.ts b/util/src/entities/BaseClass.ts index beccf04b..43b69c76 100644 --- a/util/src/entities/BaseClass.ts +++ b/util/src/entities/BaseClass.ts @@ -1,19 +1,8 @@ import "reflect-metadata"; -import { - BaseEntity, - BeforeInsert, - BeforeUpdate, - EntityMetadata, - FindConditions, - ObjectIdColumn, - PrimaryColumn, -} from "typeorm"; +import { BaseEntity, EntityMetadata, FindConditions, ObjectIdColumn, PrimaryColumn } from "typeorm"; import { Snowflake } from "../util/Snowflake"; import "missing-native-js-functions"; -// TODO use class-validator https://typeorm.io/#/validation with class annotators (isPhone/isEmail) combined with types from typescript-json-schema -// btw. we don't use class-validator for everything, because we need to explicitly set the type instead of deriving it from typescript also it doesn't easily support nested objects - export class BaseClassWithoutId extends BaseEntity { constructor(props?: any) { super(); @@ -38,11 +27,12 @@ export class BaseClassWithoutId extends BaseEntity { .concat(this.metadata.relations.map((x) => x.propertyName)) ); // will not include relational properties + console.log(properties); for (const key in props) { if (!properties.has(key)) continue; // @ts-ignore - const setter = this[`set${key.capitalize()}`]; + const setter = this[`set${key.capitalize()}`]; // use setter function if it exists if (setter) { setter.call(this, props[key]); @@ -53,12 +43,6 @@ export class BaseClassWithoutId extends BaseEntity { } } - @BeforeUpdate() - @BeforeInsert() - validate() { - return this; - } - toJSON(): any { return Object.fromEntries( this.metadata.columns // @ts-ignore @@ -76,42 +60,6 @@ export class BaseClassWithoutId extends BaseEntity { const repository = this.getRepository(); return repository.decrement(conditions, propertyPath, value); } - - // static async delete(criteria: FindConditions, options?: RemoveOptions) { - // if (!criteria) throw new Error("You need to specify delete criteria"); - - // const repository = this.getRepository(); - // const promises = repository.metadata.relations.map(async (x) => { - // if (x.orphanedRowAction !== "delete") return; - - // const foreignKey = - // x.foreignKeys.find((key) => key.entityMetadata === repository.metadata) || - // x.inverseRelation?.foreignKeys[0]; // find foreign key for this entity - // if (!foreignKey) { - // throw new Error( - // `Foreign key not found for entity ${repository.metadata.name} in relation ${x.propertyName}` - // ); - // } - // const id = (criteria as any)[foreignKey.referencedColumnNames[0]]; - // if (!id) throw new Error("id missing in criteria options " + foreignKey.referencedColumnNames); - - // if (x.relationType === "many-to-many") { - // return getConnection() - // .createQueryBuilder() - // .relation(this, x.propertyName) - // .of(id) - // .remove({ [foreignKey.columnNames[0]]: id }); - // } else if ( - // x.relationType === "one-to-one" || - // x.relationType === "many-to-one" || - // x.relationType === "one-to-many" - // ) { - // return (x.inverseEntityMetadata.target as any).delete({ [foreignKey.columnNames[0]]: id }); - // } - // }); - // await Promise.all(promises); - // return super.delete(criteria, options); - // } } export const PrimaryIdColumn = process.env.DATABASE?.startsWith("mongodb") ? ObjectIdColumn : PrimaryColumn; diff --git a/util/src/entities/Message.ts b/util/src/entities/Message.ts index 04c3c7aa..c7f7a1d1 100644 --- a/util/src/entities/Message.ts +++ b/util/src/entities/Message.ts @@ -46,9 +46,6 @@ export enum MessageType { @Entity("messages") export class Message extends BaseClass { - @Column() - id: string; - @Column({ nullable: true }) @RelationId((message: Message) => message.channel) channel_id: string; diff --git a/util/src/entities/RateLimit.ts b/util/src/entities/RateLimit.ts index fa9c32c1..f5916f6b 100644 --- a/util/src/entities/RateLimit.ts +++ b/util/src/entities/RateLimit.ts @@ -3,9 +3,6 @@ import { BaseClass } from "./BaseClass"; @Entity("rate_limits") export class RateLimit extends BaseClass { - @Column() - id: "global" | "error" | string; // channel_239842397 | guild_238927349823 | webhook_238923423498 - @Column() // no relation as it also executor_id: string; diff --git a/util/src/entities/User.ts b/util/src/entities/User.ts index 97564af3..662ab031 100644 --- a/util/src/entities/User.ts +++ b/util/src/entities/User.ts @@ -198,7 +198,7 @@ export class User extends BaseClass { // randomly generates a discriminator between 1 and 9999 and checks max five times if it already exists // if it all five times already exists, abort with USERNAME_TOO_MANY_USERS error // else just continue - // TODO: is there any better way to generate a random discriminator only once, without checking if it already exists in the mongodb database? + // TODO: is there any better way to generate a random discriminator only once, without checking if it already exists in the database? for (let tries = 0; tries < 5; tries++) { discriminator = Math.randomIntBetween(1, 9999).toString().padStart(4, "0"); exists = await User.findOne({ where: { discriminator, username: username }, select: ["id"] }); @@ -219,7 +219,7 @@ export class User extends BaseClass { // if nsfw_allowed is null/undefined it'll require date_of_birth to set it to true/false const language = req.language === "en" ? "en-US" : req.language || "en-US"; - const user = await new User({ + const user = new User({ created_at: new Date(), username: username, discriminator, @@ -246,7 +246,10 @@ export class User extends BaseClass { }, settings: { ...defaultSettings, locale: language }, fingerprints: [], - }).save(); + }); + + console.log(user); + await user.save(); if (Config.get().guild.autoJoin.enabled) { for (const guild of Config.get().guild.autoJoin.guilds || []) { diff --git a/util/src/entities/Webhook.ts b/util/src/entities/Webhook.ts index 8382435f..9d8609ae 100644 --- a/util/src/entities/Webhook.ts +++ b/util/src/entities/Webhook.ts @@ -12,9 +12,6 @@ export enum WebhookType { @Entity("webhooks") export class Webhook extends BaseClass { - @Column() - id: string; - @Column({ type: "simple-enum", enum: WebhookType }) type: WebhookType; diff --git a/util/src/index.ts b/util/src/index.ts index fc00d46b..ae0f7e54 100644 --- a/util/src/index.ts +++ b/util/src/index.ts @@ -1,12 +1,6 @@ import "reflect-metadata"; -// export * as Constants from "../util/Constants"; export * from "./util/index"; export * from "./interfaces/index"; export * from "./entities/index"; export * from "./dtos/index"; - -// import Config from "../util/Config"; -// import db, { MongooseCache, toObject } from "./util/Database"; - -// export { Config }; -- cgit 1.4.1 From 57c9813f2fca9b8f4e02f09d71515bc7f59b4010 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 10 Oct 2021 11:08:55 +0200 Subject: :bug: convert bigint literal to object --- api/src/routes/channels/#channel_id/permissions.ts | 4 ++-- api/src/routes/guilds/#guild_id/roles.ts | 9 +++++++-- api/src/routes/guilds/templates/index.ts | 2 +- bundle/package.json | 2 +- gateway/src/opcodes/Identify.ts | 2 +- 5 files changed, 12 insertions(+), 7 deletions(-) (limited to 'api/src') diff --git a/api/src/routes/channels/#channel_id/permissions.ts b/api/src/routes/channels/#channel_id/permissions.ts index 6ebf721a..2eded853 100644 --- a/api/src/routes/channels/#channel_id/permissions.ts +++ b/api/src/routes/channels/#channel_id/permissions.ts @@ -44,8 +44,8 @@ router.put( }; channel.permission_overwrites!.push(overwrite); } - overwrite.allow = String(req.permission!.bitfield & (BigInt(body.allow) || 0n)); - overwrite.deny = String(req.permission!.bitfield & (BigInt(body.deny) || 0n)); + overwrite.allow = String(req.permission!.bitfield & (BigInt(body.allow) || BigInt("0"))); + overwrite.deny = String(req.permission!.bitfield & (BigInt(body.deny) || BigInt("0"))); await Promise.all([ channel.save(), diff --git a/api/src/routes/guilds/#guild_id/roles.ts b/api/src/routes/guilds/#guild_id/roles.ts index d1d60906..0a57c6a2 100644 --- a/api/src/routes/guilds/#guild_id/roles.ts +++ b/api/src/routes/guilds/#guild_id/roles.ts @@ -57,7 +57,7 @@ router.post("/", route({ body: "RoleModifySchema", permission: "MANAGE_ROLES" }) ...body, guild_id: guild_id, managed: false, - permissions: String(req.permission!.bitfield & (body.permissions || 0n)), + permissions: String(req.permission!.bitfield & (body.permissions || BigInt("0"))), tags: undefined }); @@ -105,7 +105,12 @@ router.patch("/:role_id", route({ body: "RoleModifySchema", permission: "MANAGE_ const { role_id, guild_id } = req.params; const body = req.body as RoleModifySchema; - const role = new Role({ ...body, id: role_id, guild_id, permissions: String(req.permission!.bitfield & (body.permissions || 0n)) }); + const role = new Role({ + ...body, + id: role_id, + guild_id, + permissions: String(req.permission!.bitfield & (body.permissions || BigInt("0"))) + }); await Promise.all([ role.save(), diff --git a/api/src/routes/guilds/templates/index.ts b/api/src/routes/guilds/templates/index.ts index b5e243e9..86316d23 100644 --- a/api/src/routes/guilds/templates/index.ts +++ b/api/src/routes/guilds/templates/index.ts @@ -47,7 +47,7 @@ router.post("/:code", route({ body: "GuildTemplateCreateSchema" }), async (req: managed: true, mentionable: true, name: "@everyone", - permissions: 2251804225n, + permissions: BigInt("2251804225"), position: 0, tags: null }).save() diff --git a/bundle/package.json b/bundle/package.json index 05cefaab..3bed5b07 100644 --- a/bundle/package.json +++ b/bundle/package.json @@ -94,4 +94,4 @@ "ws": "^7.4.2", "cheerio": "^1.0.0-rc.10" } -} +} \ No newline at end of file diff --git a/gateway/src/opcodes/Identify.ts b/gateway/src/opcodes/Identify.ts index b81c7bf4..5d5b72d1 100644 --- a/gateway/src/opcodes/Identify.ts +++ b/gateway/src/opcodes/Identify.ts @@ -41,7 +41,7 @@ export async function onIdentify(this: WebSocket, data: Payload) { return this.close(CLOSECODES.Authentication_failed); } this.user_id = decoded.id; - if (!identify.intents) identify.intents = 0b11111111111111n; + if (!identify.intents) identify.intents = BigInt("0b11111111111111"); this.intents = new Intents(identify.intents); if (identify.shard) { this.shard_id = identify.shard[0]; -- cgit 1.4.1 From 3b4e026eadd1334f3f075774613ed7e8c4203b40 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 10 Oct 2021 12:35:46 +0200 Subject: :bug: fix tsc compiler --- api/src/Server.ts | 3 ++- bundle/scripts/build.js | 36 +++++++++++++++++++++++++----------- bundle/tsconfig.json | 21 ++++++--------------- cdn/src/Server.ts | 14 ++++++++++---- util/src/util/TraverseDirectory.ts | 10 ++++++++++ util/src/util/index.ts | 1 + 6 files changed, 54 insertions(+), 31 deletions(-) create mode 100644 util/src/util/TraverseDirectory.ts (limited to 'api/src') diff --git a/api/src/Server.ts b/api/src/Server.ts index 19e3d245..1f11a295 100644 --- a/api/src/Server.ts +++ b/api/src/Server.ts @@ -11,6 +11,7 @@ import TestClient from "./middlewares/TestClient"; import { initTranslation } from "./middlewares/Translation"; import morgan from "morgan"; import { initInstance } from "./util/Instance"; +import { registerRoutes } from "@fosscord/util"; export interface FosscordServerOptions extends ServerOptions {} @@ -72,7 +73,7 @@ export class FosscordServer extends Server { await initRateLimits(api); await initTranslation(api); - this.routes = await this.registerRoutes(path.join(__dirname, "routes", "/")); + this.routes = await registerRoutes(this, path.join(__dirname, "routes", "/")); api.use("*", (error: any, req: Request, res: Response, next: NextFunction) => { if (error) return next(error); diff --git a/bundle/scripts/build.js b/bundle/scripts/build.js index 2a82ec57..69111c88 100644 --- a/bundle/scripts/build.js +++ b/bundle/scripts/build.js @@ -2,18 +2,32 @@ const { execSync } = require("child_process"); const path = require("path"); const fse = require("fs-extra"); -const api = path.join(__dirname, "..", "..", "api"); -const dist = path.join(__dirname, "..", "dist"); +fse.copySync(path.join(__dirname, "..", "..", "api", "assets"), path.join(__dirname, "..", "dist", "api", "assets")); +fse.copySync( + path.join(__dirname, "..", "..", "api", "client_test"), + path.join(__dirname, "..", "dist", "api", "client_test") +); +fse.copySync(path.join(__dirname, "..", "..", "api", "locales"), path.join(__dirname, "..", "dist", "api", "locales")); +fse.copySync(path.join(__dirname, "..", "..", "api", "src"), path.join(__dirname, "..", "dist", "api", "src")); +fse.copySync(path.join(__dirname, "..", "..", "util", "src"), path.join(__dirname, "..", "dist", "util", "src")); +fse.copySync(path.join(__dirname, "..", "..", "cdn", "src"), path.join(__dirname, "..", "dist", "cdn", "src")); +fse.copySync(path.join(__dirname, "..", "..", "gateway", "src"), path.join(__dirname, "..", "dist", "gateway", "src")); +fse.copySync(path.join(__dirname, "..", "..", "bundle", "src"), path.join(__dirname, "..", "dist", "bundle", "src")); -fse.copySync(path.join(api, "assets"), path.join(dist, "api", "assets")); -fse.copySync(path.join(api, "client_test"), path.join(dist, "api", "client_test")); -fse.copySync(path.join(api, "locales"), path.join(dist, "api", "locales")); +console.log("Copying src files done"); +console.log("Compiling src files ..."); console.log( - execSync("node " + path.join(__dirname, "..", "node_modules", "typescript", "lib", "tsc.js") + " -p .", { - cwd: path.join(__dirname, ".."), - shell: true, - env: process.env, - encoding: "utf8", - }) + execSync( + "node " + + path.join(__dirname, "..", "node_modules", "typescript", "lib", "tsc.js") + + " -p " + + path.join(__dirname, ".."), + { + cwd: path.join(__dirname, ".."), + shell: true, + env: process.env, + encoding: "utf8", + } + ) ); diff --git a/bundle/tsconfig.json b/bundle/tsconfig.json index 1b2949da..4e8db342 100644 --- a/bundle/tsconfig.json +++ b/bundle/tsconfig.json @@ -1,16 +1,11 @@ { - "include": [ - "../api/src/**/*.ts", - "../gateway/src/**/*.ts", - "../cdn/src/**/*.ts", - "../util/src/**/*.ts", - "src/**/*.ts" - ], + "include": ["dist/**/*.ts"], + "exclude": [], "compilerOptions": { /* Visit https://aka.ms/tsconfig.json to read more about this file */ /* Basic Options */ - "incremental": true /* Enable incremental compilation */, + "incremental": false /* Enable incremental compilation */, "target": "ES6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, "lib": ["ES2021"] /* Specify library files to be included in the compilation. */, @@ -22,7 +17,7 @@ "sourceMap": false /* Generates corresponding '.map' file. */, // "outFile": "./", /* Concatenate and emit output to single file. */ "outDir": "./dist/" /* Redirect output structure to the directory. */, - "rootDir": "../" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */, + "rootDir": "./dist/" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */, // "composite": true, /* Enable project compilation */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ // "removeComments": true, /* Do not emit comments to output. */ @@ -73,16 +68,12 @@ "emitDecoratorMetadata": true, "experimentalDecorators": true, "resolveJsonModule": true, - "baseUrl": "..", + "baseUrl": "./dist/", "paths": { "@fosscord/api": ["api/src/index"], - "@fosscord/api/*": ["api/src/*"], "@fosscord/gateway": ["gateway/src/index"], - "@fosscord/gateway/*": ["gateway/src/*"], "@fosscord/cdn": ["cdn/src/index"], - "@fosscord/cdn/*": ["cdn/src/*"], - "@fosscord/util": ["util/src/index"], - "@fosscord/util/*": ["util/src/*"] + "@fosscord/util": ["util/src/index"] }, "plugins": [{ "transform": "@zerollup/ts-transform-paths" }] } diff --git a/cdn/src/Server.ts b/cdn/src/Server.ts index 590eda6f..cac34a80 100644 --- a/cdn/src/Server.ts +++ b/cdn/src/Server.ts @@ -1,5 +1,5 @@ import { Server, ServerOptions } from "lambert-server"; -import { Config, initDatabase } from "@fosscord/util"; +import { Config, initDatabase, registerRoutes } from "@fosscord/util"; import path from "path"; import avatarsRoute from "./routes/avatars"; import bodyParser from "body-parser"; @@ -23,13 +23,19 @@ export class CDNServer extends Server { "Content-security-policy", "default-src * data: blob: filesystem: about: ws: wss: 'unsafe-inline' 'unsafe-eval'; script-src * data: blob: 'unsafe-inline' 'unsafe-eval'; connect-src * data: blob: 'unsafe-inline'; img-src * data: blob: 'unsafe-inline'; frame-src * data: blob: ; style-src * data: blob: 'unsafe-inline'; font-src * data: blob: 'unsafe-inline';" ); - res.set("Access-Control-Allow-Headers", req.header("Access-Control-Request-Headers") || "*"); - res.set("Access-Control-Allow-Methods", req.header("Access-Control-Request-Methods") || "*"); + res.set( + "Access-Control-Allow-Headers", + req.header("Access-Control-Request-Headers") || "*" + ); + res.set( + "Access-Control-Allow-Methods", + req.header("Access-Control-Request-Methods") || "*" + ); next(); }); this.app.use(bodyParser.json({ inflate: true, limit: "10mb" })); - await this.registerRoutes(path.join(__dirname, "routes/")); + await registerRoutes(this, path.join(__dirname, "routes/")); this.app.use("/icons/", avatarsRoute); this.log("verbose", "[Server] Route /icons registered"); diff --git a/util/src/util/TraverseDirectory.ts b/util/src/util/TraverseDirectory.ts new file mode 100644 index 00000000..275b7dcc --- /dev/null +++ b/util/src/util/TraverseDirectory.ts @@ -0,0 +1,10 @@ +import { Server, traverseDirectory } from "lambert-server"; + +const DEFAULT_FILTER = /^([^\.].*)(? Date: Sun, 10 Oct 2021 14:10:27 +0200 Subject: :bug: fix emojis route --- api/assets/schemas.json | 346 ++++++++++++++++++++++++++---- api/src/routes/guilds/#guild_id/emoji.ts | 137 ------------ api/src/routes/guilds/#guild_id/emojis.ts | 121 +++++++++++ 3 files changed, 431 insertions(+), 173 deletions(-) delete mode 100644 api/src/routes/guilds/#guild_id/emoji.ts create mode 100644 api/src/routes/guilds/#guild_id/emojis.ts (limited to 'api/src') diff --git a/api/assets/schemas.json b/api/assets/schemas.json index c2149836..4f1ab9a8 100644 --- a/api/assets/schemas.json +++ b/api/assets/schemas.json @@ -2887,47 +2887,321 @@ }, "$schema": "http://json-schema.org/draft-07/schema#" }, - "EmojiListResponse": { - "type": "array", - "items": { - "type": "object", - "properties": { - "animated": { - "type": "boolean" - }, - "available": { - "type": "boolean" - }, - "id": { - "type": "string" - }, - "managed": { - "type": "boolean" - }, - "name": { - "type": "string" - }, - "require_colons": { - "type": "boolean" - }, - "guild_id": { + "EmojiCreateSchema": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "image": { + "type": "string" + }, + "require_colons": { + "type": [ + "null", + "boolean" + ] + }, + "roles": { + "type": "array", + "items": { "type": "string" - }, - "roles": { - "type": "array", - "items": { + } + } + }, + "definitions": { + "ChannelPermissionOverwriteType": { + "enum": [ + 0, + 1 + ], + "type": "number" + }, + "Embed": { + "type": "object", + "properties": { + "title": { "type": "string" + }, + "type": { + "enum": [ + "article", + "gifv", + "image", + "link", + "rich", + "video" + ], + "type": "string" + }, + "description": { + "type": "string" + }, + "url": { + "type": "string" + }, + "timestamp": { + "type": "string", + "format": "date-time" + }, + "color": { + "type": "integer" + }, + "footer": { + "type": "object", + "properties": { + "text": { + "type": "string" + }, + "icon_url": { + "type": "string" + }, + "proxy_icon_url": { + "type": "string" + } + }, + "required": [ + "text" + ] + }, + "image": { + "$ref": "#/definitions/EmbedImage" + }, + "thumbnail": { + "$ref": "#/definitions/EmbedImage" + }, + "video": { + "$ref": "#/definitions/EmbedImage" + }, + "provider": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "url": { + "type": "string" + } + } + }, + "author": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "url": { + "type": "string" + }, + "icon_url": { + "type": "string" + }, + "proxy_icon_url": { + "type": "string" + } + } + }, + "fields": { + "type": "array", + "items": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "value": { + "type": "string" + }, + "inline": { + "type": "boolean" + } + }, + "required": [ + "name", + "value" + ] + } } } }, - "required": [ - "animated", - "available", - "id", - "managed", - "name", - "require_colons" - ] + "EmbedImage": { + "type": "object", + "properties": { + "url": { + "type": "string" + }, + "proxy_url": { + "type": "string" + }, + "height": { + "type": "integer" + }, + "width": { + "type": "integer" + } + } + }, + "ChannelModifySchema": { + "type": "object", + "properties": { + "name": { + "maxLength": 100, + "type": "string" + }, + "type": { + "enum": [ + 0, + 1, + 10, + 11, + 12, + 13, + 2, + 3, + 4, + 5, + 6 + ], + "type": "number" + }, + "topic": { + "type": "string" + }, + "icon": { + "type": [ + "null", + "string" + ] + }, + "bitrate": { + "type": "integer" + }, + "user_limit": { + "type": "integer" + }, + "rate_limit_per_user": { + "type": "integer" + }, + "position": { + "type": "integer" + }, + "permission_overwrites": { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "type": { + "$ref": "#/definitions/ChannelPermissionOverwriteType" + }, + "allow": { + "type": "string" + }, + "deny": { + "type": "string" + } + }, + "required": [ + "allow", + "deny", + "id", + "type" + ] + } + }, + "parent_id": { + "type": "string" + }, + "id": { + "type": "string" + }, + "nsfw": { + "type": "boolean" + }, + "rtc_region": { + "type": "string" + }, + "default_auto_archive_duration": { + "type": "integer" + } + } + }, + "UserPublic": { + "type": "object", + "properties": { + "username": { + "type": "string" + }, + "discriminator": { + "type": "string" + }, + "id": { + "type": "string" + }, + "public_flags": { + "type": "integer" + }, + "avatar": { + "type": "string" + }, + "accent_color": { + "type": "integer" + }, + "banner": { + "type": "string" + }, + "bio": { + "type": "string" + }, + "bot": { + "type": "boolean" + } + }, + "required": [ + "bio", + "bot", + "discriminator", + "id", + "public_flags", + "username" + ] + }, + "PublicConnectedAccount": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "type": { + "type": "string" + }, + "verifie": { + "type": "boolean" + } + }, + "required": [ + "name", + "type", + "verifie" + ] + } + }, + "$schema": "http://json-schema.org/draft-07/schema#" + }, + "EmojiModifySchema": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "roles": { + "type": "array", + "items": { + "type": "string" + } + } }, "definitions": { "ChannelPermissionOverwriteType": { diff --git a/api/src/routes/guilds/#guild_id/emoji.ts b/api/src/routes/guilds/#guild_id/emoji.ts deleted file mode 100644 index 000c9949..00000000 --- a/api/src/routes/guilds/#guild_id/emoji.ts +++ /dev/null @@ -1,137 +0,0 @@ -import { Router, Request, Response } from "express"; -import { - Config, - DiscordApiErrors, - emitEvent, - Emoji, - GuildEmojiUpdateEvent, - handleFile, - Member, - Snowflake, - User -} from "@fosscord/util"; -import { route } from "@fosscord/api"; - -const router = Router(); - -export interface EmojiCreateSchema { - name?: string; - image?: string; - require_colons?: boolean | null; - roles?: string[]; -} - -export interface EmojiModifySchema { - name?: string; - roles?: string[]; -} - -router.get("/", route({}), async (req: Request, res: Response) => { - const guild_id = req.params.guild_id; - - await Member.IsInGuildOrFail(req.user_id, guild_id); - - const emojis = await Emoji.find({ guild_id: guild_id }); - - return res.json(emojis); -}); - -router.get("/:emoji_id", route({}), async (req: Request, res: Response) => { - const guild_id = req.params.guild_id; - const emoji_id = req.params.emoji_id; - - await Member.IsInGuildOrFail(req.user_id, guild_id); - - const emoji = await Emoji.findOneOrFail({ guild_id: guild_id, id: emoji_id }); - - return res.json(emoji); -}); - -router.post("/", route({ body: "EmojiCreateSchema", permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => { - const guild_id = req.params.guild_id; - const body = req.body as EmojiCreateSchema; - - const emoji_count = await Emoji.count({ guild_id: guild_id }); - const { maxEmojis } = Config.get().limits.guild; - - if (emoji_count >= maxEmojis) throw DiscordApiErrors.MAXIMUM_NUMBER_OF_EMOJIS_REACHED.withParams(maxEmojis); - - const id = Snowflake.generate(); - - if (!body.image) { - throw DiscordApiErrors.GENERAL_ERROR.withParams("No image provided"); - } - - if (body.require_colons === null) body.require_colons = true; - - const user = await User.findOneOrFail({ id: req.user_id }); - - body.image = await handleFile(`/emojis/${id}`, body.image); - - const emoji = new Emoji({ - id: id, - guild_id: guild_id, - ...body, - user: user, - managed: false, - animated: false, // TODO: Add support animated emojis - available: true - }); - - await Promise.all([ - emoji.save(), - emitEvent({ - event: "GUILD_EMOJI_UPDATE", - guild_id: guild_id, - data: { - guild_id: guild_id, - emojis: await Emoji.find({ guild_id: guild_id }) - } - } as GuildEmojiUpdateEvent) - ]); -}); - -router.patch("/:emoji_id", route({ body: "EmojiModifySchema", permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => { - const { emoji_id, guild_id } = req.params; - const body = req.body as EmojiModifySchema; - - const emoji = new Emoji({ ...body, id: emoji_id, guild_id: guild_id }); - - await Promise.all([ - emoji.save(), - emitEvent({ - event: "GUILD_EMOJI_UPDATE", - guild_id: guild_id, - data: { - guild_id: guild_id, - emojis: await Emoji.find({ guild_id: guild_id }) - } - } as GuildEmojiUpdateEvent) - ]); - - return res.json(emoji); -}); - -router.delete("/:emoji_id", route({ permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => { - const guild_id = req.params.guild_id; - const { emoji_id } = req.params; - - await Promise.all([ - Emoji.delete({ - id: emoji_id, - guild_id: guild_id - }), - emitEvent({ - event: "GUILD_EMOJI_UPDATE", - guild_id: guild_id, - data: { - guild_id: guild_id, - emojis: await Emoji.find({ guild_id: guild_id }) - } - } as GuildEmojiUpdateEvent) - ]) - - res.sendStatus(204); -}); - -export default router; diff --git a/api/src/routes/guilds/#guild_id/emojis.ts b/api/src/routes/guilds/#guild_id/emojis.ts new file mode 100644 index 00000000..ff565cd4 --- /dev/null +++ b/api/src/routes/guilds/#guild_id/emojis.ts @@ -0,0 +1,121 @@ +import { Router, Request, Response } from "express"; +import { Config, DiscordApiErrors, emitEvent, Emoji, GuildEmojisUpdateEvent, handleFile, Member, Snowflake, User } from "@fosscord/util"; +import { route } from "@fosscord/api"; + +const router = Router(); + +export interface EmojiCreateSchema { + name?: string; + image: string; + require_colons?: boolean | null; + roles?: string[]; +} + +export interface EmojiModifySchema { + name?: string; + roles?: string[]; +} + +router.get("/", route({}), async (req: Request, res: Response) => { + const { guild_id } = req.params; + + await Member.IsInGuildOrFail(req.user_id, guild_id); + + const emojis = await Emoji.find({ where: { guild_id: guild_id }, relations: ["user"] }); + + return res.json(emojis); +}); + +router.get("/:emoji_id", route({}), async (req: Request, res: Response) => { + const { guild_id, emoji_id } = req.params; + + await Member.IsInGuildOrFail(req.user_id, guild_id); + + const emoji = await Emoji.findOneOrFail({ where: { guild_id: guild_id, id: emoji_id }, relations: ["user"] }); + + return res.json(emoji); +}); + +router.post("/", route({ body: "EmojiCreateSchema", permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => { + const { guild_id } = req.params; + const body = req.body as EmojiCreateSchema; + + const emoji_count = await Emoji.count({ guild_id: guild_id }); + const { maxEmojis } = Config.get().limits.guild; + + if (emoji_count >= maxEmojis) throw DiscordApiErrors.MAXIMUM_NUMBER_OF_EMOJIS_REACHED.withParams(maxEmojis); + + const id = Snowflake.generate(); + + if (body.require_colons == null) body.require_colons = true; + + const user = await User.findOneOrFail({ id: req.user_id }); + + body.image = (await handleFile(`/emojis/${id}`, body.image)) as string; + + const emoji = await new Emoji({ + id: id, + guild_id: guild_id, + ...body, + user: user, + managed: false, + animated: false, // TODO: Add support animated emojis + available: true, + roles: [] + }).save(); + + await emitEvent({ + event: "GUILD_EMOJIS_UPDATE", + guild_id: guild_id, + data: { + guild_id: guild_id, + emojis: await Emoji.find({ guild_id: guild_id }) + } + } as GuildEmojisUpdateEvent); + + return res.status(201).json(emoji); +}); + +router.patch( + "/:emoji_id", + route({ body: "EmojiModifySchema", permission: "MANAGE_EMOJIS_AND_STICKERS" }), + async (req: Request, res: Response) => { + const { emoji_id, guild_id } = req.params; + const body = req.body as EmojiModifySchema; + + const emoji = await new Emoji({ ...body, id: emoji_id, guild_id: guild_id }).save(); + + await emitEvent({ + event: "GUILD_EMOJIS_UPDATE", + guild_id: guild_id, + data: { + guild_id: guild_id, + emojis: await Emoji.find({ guild_id: guild_id }) + } + } as GuildEmojisUpdateEvent); + + return res.json(emoji); + } +); + +router.delete("/:emoji_id", route({ permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => { + const { emoji_id, guild_id } = req.params; + + await Emoji.delete({ + id: emoji_id, + guild_id: guild_id + }); + + await emitEvent({ + event: "GUILD_EMOJIS_UPDATE", + guild_id: guild_id, + data: { + guild_id: guild_id, + emojis: await Emoji.find({ guild_id: guild_id }) + } + } as GuildEmojisUpdateEvent); + + res.sendStatus(204); +}); + +export default router; -- cgit 1.4.1 From 79aee5145b6b289a001caae516477c8ab641bb8f Mon Sep 17 00:00:00 2001 From: The Arcane Brony Date: Sun, 10 Oct 2021 17:55:48 +0200 Subject: Fix duplicate key --- api/assets/schemas.json | 2 +- api/src/routes/guilds/#guild_id/vanity-url.ts | 9 +++------ api/src/routes/invites/index.ts | 1 - bundle/scripts/build.js | 27 +++++++++++++++++++++------ util/src/entities/Guild.ts | 8 -------- util/src/entities/Invite.ts | 3 +++ 6 files changed, 28 insertions(+), 22 deletions(-) (limited to 'api/src') diff --git a/api/assets/schemas.json b/api/assets/schemas.json index 4f1ab9a8..a12925e0 100644 --- a/api/assets/schemas.json +++ b/api/assets/schemas.json @@ -4744,7 +4744,7 @@ "type": "string" }, "permissions": { - "type": "bigint" + "type": "array" }, "color": { "type": "integer" diff --git a/api/src/routes/guilds/#guild_id/vanity-url.ts b/api/src/routes/guilds/#guild_id/vanity-url.ts index 7f2cea9e..061b317c 100644 --- a/api/src/routes/guilds/#guild_id/vanity-url.ts +++ b/api/src/routes/guilds/#guild_id/vanity-url.ts @@ -10,10 +10,10 @@ const InviteRegex = /\W/g; router.get("/", route({ permission: "MANAGE_GUILD" }), async (req: Request, res: Response) => { const { guild_id } = req.params; - const guild = await Guild.findOneOrFail({ where: { id: guild_id }, relations: ["vanity_url"] }); - if (!guild.vanity_url) return res.json({ code: null }); + const invite = await Invite.findOne({ where: {guild_id: guild_id, vanity_url: true} }); + if (!invite) return res.json({ code: null }); - return res.json({ code: guild.vanity_url_code, uses: guild.vanity_url.uses }); + return res.json({ code: invite.code, uses: invite.uses }); }); export interface VanityUrlSchema { @@ -33,12 +33,9 @@ router.patch("/", route({ body: "VanityUrlSchema", permission: "MANAGE_GUILD" }) const invite = await Invite.findOne({ code }); if (invite) throw new HTTPError("Invite already exists"); - const guild = await Guild.findOneOrFail({ id: guild_id }); const { id } = await Channel.findOneOrFail({ guild_id, type: ChannelType.GUILD_TEXT }); Promise.all([ - Guild.update({ id: guild_id }, { vanity_url_code: code }), - Invite.delete({ code: guild.vanity_url_code }), new Invite({ code: code, uses: 0, diff --git a/api/src/routes/invites/index.ts b/api/src/routes/invites/index.ts index 0fcf7c86..185311bc 100644 --- a/api/src/routes/invites/index.ts +++ b/api/src/routes/invites/index.ts @@ -33,7 +33,6 @@ router.delete("/:code", route({}), async (req: Request, res: Response) => { await Promise.all([ Invite.delete({ code }), - Guild.update({ vanity_url_code: code }, { vanity_url_code: undefined }), emitEvent({ event: "INVITE_DELETE", guild_id: guild_id, diff --git a/bundle/scripts/build.js b/bundle/scripts/build.js index 69111c88..a9798eff 100644 --- a/bundle/scripts/build.js +++ b/bundle/scripts/build.js @@ -1,6 +1,22 @@ const { execSync } = require("child_process"); const path = require("path"); const fse = require("fs-extra"); +const { getSystemErrorMap } = require("util"); +const { argv } = require("process"); + +const dirs = ["api", "util", "cdn", "gateway", "bundle"]; + +const verbose = argv.includes("verbose") || argv.includes("v"); + +if(argv.includes("clean")){ + dirs.forEach(a=>{ + var d = "../"+a+"/dist"; + if(fse.existsSync(d)) { + fse.rmSync(d,{recursive: true}); + if(verbose) console.log(`Deleted ${d}!`); + } + }); +} fse.copySync(path.join(__dirname, "..", "..", "api", "assets"), path.join(__dirname, "..", "dist", "api", "assets")); fse.copySync( @@ -8,13 +24,12 @@ fse.copySync( path.join(__dirname, "..", "dist", "api", "client_test") ); fse.copySync(path.join(__dirname, "..", "..", "api", "locales"), path.join(__dirname, "..", "dist", "api", "locales")); -fse.copySync(path.join(__dirname, "..", "..", "api", "src"), path.join(__dirname, "..", "dist", "api", "src")); -fse.copySync(path.join(__dirname, "..", "..", "util", "src"), path.join(__dirname, "..", "dist", "util", "src")); -fse.copySync(path.join(__dirname, "..", "..", "cdn", "src"), path.join(__dirname, "..", "dist", "cdn", "src")); -fse.copySync(path.join(__dirname, "..", "..", "gateway", "src"), path.join(__dirname, "..", "dist", "gateway", "src")); -fse.copySync(path.join(__dirname, "..", "..", "bundle", "src"), path.join(__dirname, "..", "dist", "bundle", "src")); +dirs.forEach(a=>{ + fse.copySync("../"+a+"/src", "dist/"+a+"/src"); + if(verbose) console.log(`Copied ${"../"+a+"/dist"} -> ${"dist/"+a+"/src"}!`); +}); -console.log("Copying src files done"); +console.log("Copying src files done"); console.log("Compiling src files ..."); console.log( diff --git a/util/src/entities/Guild.ts b/util/src/entities/Guild.ts index 35595191..157f0921 100644 --- a/util/src/entities/Guild.ts +++ b/util/src/entities/Guild.ts @@ -257,14 +257,6 @@ export class Guild extends BaseClass { @Column({ nullable: true }) unavailable?: boolean; - @Column({ nullable: true }) - @RelationId((guild: Guild) => guild.vanity_url) - vanity_url_code?: string; - - @JoinColumn({ name: "vanity_url_code" }) - @ManyToOne(() => Invite) - vanity_url?: Invite; - @Column({ nullable: true }) verification_level?: number; diff --git a/util/src/entities/Invite.ts b/util/src/entities/Invite.ts index 82556fab..d6b8f2f8 100644 --- a/util/src/entities/Invite.ts +++ b/util/src/entities/Invite.ts @@ -71,6 +71,9 @@ export class Invite extends BaseClass { @Column({ nullable: true }) target_user_type?: number; + @Column({ nullable: true}) + vanity_url?: boolean; + static async joinGuild(user_id: string, code: string) { const invite = await Invite.findOneOrFail({ code }); if (invite.uses++ >= invite.max_uses && invite.max_uses !== 0) await Invite.delete({ code }); -- cgit 1.4.1 From a54711f4aebd8715c768092396e5b9a7f1f20362 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 10 Oct 2021 18:28:38 +0200 Subject: :bug: fix vanity url --- api/src/routes/guilds/#guild_id/vanity-url.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'api/src') diff --git a/api/src/routes/guilds/#guild_id/vanity-url.ts b/api/src/routes/guilds/#guild_id/vanity-url.ts index 061b317c..63173345 100644 --- a/api/src/routes/guilds/#guild_id/vanity-url.ts +++ b/api/src/routes/guilds/#guild_id/vanity-url.ts @@ -10,7 +10,7 @@ const InviteRegex = /\W/g; router.get("/", route({ permission: "MANAGE_GUILD" }), async (req: Request, res: Response) => { const { guild_id } = req.params; - const invite = await Invite.findOne({ where: {guild_id: guild_id, vanity_url: true} }); + const invite = await Invite.findOne({ where: { guild_id: guild_id, vanity_url: true } }); if (!invite) return res.json({ code: null }); return res.json({ code: invite.code, uses: invite.uses }); @@ -35,15 +35,7 @@ router.patch("/", route({ body: "VanityUrlSchema", permission: "MANAGE_GUILD" }) const { id } = await Channel.findOneOrFail({ guild_id, type: ChannelType.GUILD_TEXT }); - Promise.all([ - new Invite({ - code: code, - uses: 0, - created_at: new Date(), - guild_id, - channel_id: id - }).save() - ]); + await Invite.update({ vanity_url: true, guild_id }, { code: code, channel_id: id }); return res.json({ code: code }); }); -- cgit 1.4.1 From aa25dac99f9ab2c595d9deeb4067f753796438a1 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 10 Oct 2021 19:00:50 +0200 Subject: :bug: fix modify role.permissions --- api/assets/schemas.json | 5 +- api/src/routes/guilds/#guild_id/roles.ts | 6 +- util/src/migrations/migrate_db_engine.js | 104 ++++++++++++++++++++++++ util/src/migrations/migrate_db_engine.ts | 131 ------------------------------- 4 files changed, 111 insertions(+), 135 deletions(-) create mode 100644 util/src/migrations/migrate_db_engine.js delete mode 100644 util/src/migrations/migrate_db_engine.ts (limited to 'api/src') diff --git a/api/assets/schemas.json b/api/assets/schemas.json index 4f1ab9a8..2ceaa923 100644 --- a/api/assets/schemas.json +++ b/api/assets/schemas.json @@ -2909,6 +2909,9 @@ } } }, + "required": [ + "image" + ], "definitions": { "ChannelPermissionOverwriteType": { "enum": [ @@ -4744,7 +4747,7 @@ "type": "string" }, "permissions": { - "type": "bigint" + "type": "string" }, "color": { "type": "integer" diff --git a/api/src/routes/guilds/#guild_id/roles.ts b/api/src/routes/guilds/#guild_id/roles.ts index 0a57c6a2..b1875598 100644 --- a/api/src/routes/guilds/#guild_id/roles.ts +++ b/api/src/routes/guilds/#guild_id/roles.ts @@ -17,7 +17,7 @@ const router: Router = Router(); export interface RoleModifySchema { name?: string; - permissions?: bigint; + permissions?: string; color?: number; hoist?: boolean; // whether the role should be displayed separately in the sidebar mentionable?: boolean; // whether the role should be mentionable @@ -57,7 +57,7 @@ router.post("/", route({ body: "RoleModifySchema", permission: "MANAGE_ROLES" }) ...body, guild_id: guild_id, managed: false, - permissions: String(req.permission!.bitfield & (body.permissions || BigInt("0"))), + permissions: String(req.permission!.bitfield & BigInt(body.permissions || "0")), tags: undefined }); @@ -109,7 +109,7 @@ router.patch("/:role_id", route({ body: "RoleModifySchema", permission: "MANAGE_ ...body, id: role_id, guild_id, - permissions: String(req.permission!.bitfield & (body.permissions || BigInt("0"))) + permissions: String(req.permission!.bitfield & BigInt(body.permissions || "0")) }); await Promise.all([ diff --git a/util/src/migrations/migrate_db_engine.js b/util/src/migrations/migrate_db_engine.js new file mode 100644 index 00000000..7b8b5784 --- /dev/null +++ b/util/src/migrations/migrate_db_engine.js @@ -0,0 +1,104 @@ +const { config } = require("dotenv"); +config(); +const { createConnection } = require("typeorm"); +const { initDatabase } = require("../../dist/util/Database"); +require("missing-native-js-functions"); +const { + Application, + Attachment, + Ban, + Channel, + ConnectedAccount, + Emoji, + Guild, + Invite, + Member, + Message, + ReadState, + Recipient, + Relationship, + Role, + Sticker, + Team, + TeamMember, + Template, + User, + VoiceState, + Webhook, +} = require("../../dist/entities/index"); + +async function main() { + if (!process.env.TO) throw new Error("TO database env connection string not set"); + + // manually arrange them because of foreign key + const entities = [ + User, + Guild, + Channel, + Invite, + Role, + Ban, + Application, + Emoji, + ConnectedAccount, + Member, + ReadState, + Recipient, + Relationship, + Sticker, + Team, + TeamMember, + Template, + VoiceState, + Webhook, + Message, + Attachment, + ]; + + const newDB = await initDatabase(); + + const type = process.env.TO.includes("://") ? process.env.TO.split(":")[0]?.replace("+srv", "") : "sqlite"; + const isSqlite = type.includes("sqlite"); + + // @ts-ignore + const oldDB = await createConnection({ + type, + url: isSqlite ? undefined : process.env.TO, + database: isSqlite ? process.env.TO : undefined, + entities, + name: "old", + }); + let i = 0; + + try { + for (const entity of entities) { + const entries = await oldDB.manager.find(entity); + // @ts-ignore + console.log("migrating " + entries.length + " " + entity.name + " ..."); + + for (const entry of entries) { + console.log(i++); + + try { + await newDB.manager.insert(entity, entry); + } catch (error) { + try { + if (!entry.id) throw new Error("object doesn't have a unique id: " + entry); + await newDB.manager.update(entity, { id: entry.id }, entry); + } catch (error) { + console.error("couldn't migrate " + i + " " + entity.name, error); + } + } + } + // @ts-ignore + console.log("migrated " + entries.length + " " + entity.name); + } + } catch (error) { + console.error(error.message); + } + + console.log("SUCCESS migrated all data"); + await newDB.close(); +} + +main().caught(); diff --git a/util/src/migrations/migrate_db_engine.ts b/util/src/migrations/migrate_db_engine.ts deleted file mode 100644 index 527e7b53..00000000 --- a/util/src/migrations/migrate_db_engine.ts +++ /dev/null @@ -1,131 +0,0 @@ -import { config } from "dotenv"; -config(); -import { BaseEntity, createConnection, EntityTarget } from "typeorm"; -import { initDatabase } from "../util/Database"; -import "missing-native-js-functions"; -import { - Application, - Attachment, - Ban, - Channel, - ConnectedAccount, - defaultSettings, - Emoji, - Guild, - Invite, - Member, - Message, - ReadState, - Recipient, - Relationship, - Role, - Sticker, - Team, - TeamMember, - Template, - User, - VoiceState, - Webhook, -} from ".."; - -async function main() { - if (!process.env.TO) throw new Error("TO database env connection string not set"); - - // manually arrange them because of foreign key - const entities = [ - User, - Guild, - Channel, - Invite, - Role, - Ban, - Application, - Emoji, - ConnectedAccount, - Member, - ReadState, - Recipient, - Relationship, - Sticker, - Team, - TeamMember, - Template, - VoiceState, - Webhook, - Message, - Attachment, - ]; - - const newDB = await initDatabase(); - - const type = process.env.TO.includes("://") ? process.env.TO.split(":")[0]?.replace("+srv", "") : "sqlite"; - const isSqlite = type.includes("sqlite"); - - // @ts-ignore - const oldDB = await createConnection({ - type, - url: isSqlite ? undefined : process.env.TO, - database: isSqlite ? process.env.TO : undefined, - entities, - name: "old", - }); - let i = 0; - - try { - for (const e of entities) { - const entity = e as EntityTarget; - const entries = await oldDB.manager.find(entity); - // @ts-ignore - console.log("migrating " + entries.length + " " + entity.name + " ..."); - - for (const entry of entries) { - console.log(i++); - - if (entry instanceof User) { - if (entry.bio == null) entry.bio = ""; - if (entry.rights == null) entry.rights = "0"; - if (entry.disabled == null) entry.disabled = false; - if (entry.fingerprints == null) entry.fingerprints = []; - if (entry.deleted == null) entry.deleted = false; - if (entry.data == null) { - entry.data = { - valid_tokens_since: new Date(0), - hash: undefined, - }; - // @ts-ignore - if (entry.user_data) { - // TODO: relationships - entry.data = { - // @ts-ignore - valid_tokens_since: entry.user_data.valid_tokens_since, // @ts-ignore - hash: entry.user_data.hash, - }; - } - } - // @ts-ignore - if (entry.settings == null) { - entry.settings = defaultSettings; - // @ts-ignore - if (entry.user_data) entry.settings = entry.user_settings; - } - } - - // try { - await newDB.manager.insert(entity, entry); - // } catch (error) { - // if (!entry.id) throw new Error("object doesn't have a unique id: " + entry); - // await newDB.manager.update(entity, { id: entry.id }, entry); - // } - } - // @ts-ignore - console.log("migrated " + entries.length + " " + entity.name); - } - } catch (error) { - console.error((error as any).message); - } - - console.log("SUCCESS migrated all data"); - await newDB.close(); -} - -main().caught(); -- cgit 1.4.1