diff --git a/api/package-lock.json b/api/package-lock.json index de8891880..4e65e8860 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 b34f1b309..48a857b18 100644 --- a/api/package.json +++ b/api/package.json @@ -93,7 +93,7 @@ "picocolors": "^1.0.0", "proxy-agent": "^5.0.0", "supertest": "^6.1.6", - "typeorm": "^0.2.45" + "typeorm": "^0.3.7" }, "jest": { "setupFiles": [ 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 6eacf2497..9e8cad235 100644 --- a/api/src/routes/channels/#channel_id/messages/bulk-delete.ts +++ b/api/src/routes/channels/#channel_id/messages/bulk-delete.ts @@ -17,15 +17,15 @@ export interface BulkDeleteSchema { // https://discord.com/developers/docs/resources/channel#bulk-delete-messages router.post("/", route({ body: "BulkDeleteSchema" }), async (req: Request, res: Response) => { const { channel_id } = req.params; - const channel = await Channel.findOneOrFail({ id: channel_id }); + const channel = await Channel.findOneByOrFail({ id: channel_id }); if (!channel.guild_id) throw new HTTPError("Can't bulk delete dm channel messages", 400); const rights = await getRights(req.user_id); rights.hasThrow("SELF_DELETE_MESSAGES"); - + let superuser = rights.has("MANAGE_MESSAGES"); const permission = await getPermission(req.user_id, channel?.guild_id, channel_id); - + const { maxBulkDelete } = Config.get().limits.message; const { messages } = req.body as { messages: string[] }; @@ -35,7 +35,7 @@ router.post("/", route({ body: "BulkDeleteSchema" }), async (req: Request, res: if (messages.length > maxBulkDelete) throw new HTTPError(`You cannot delete more than ${maxBulkDelete} messages`); } - await Message.delete(messages.map((x) => ({ id: x }))); + await Message.delete({ id: In(messages) }); await emitEvent({ event: "MESSAGE_DELETE_BULK", diff --git a/api/src/routes/channels/#channel_id/purge.ts b/api/src/routes/channels/#channel_id/purge.ts index 622e06e5a..3a6997b24 100644 --- a/api/src/routes/channels/#channel_id/purge.ts +++ b/api/src/routes/channels/#channel_id/purge.ts @@ -2,24 +2,9 @@ import { HTTPError } from "lambert-server"; import { route } from "@fosscord/api"; import { isTextChannel } from "./messages"; import { FindManyOptions, Between, Not } from "typeorm"; -import { - Attachment, - Channel, - Config, - Embed, - DiscordApiErrors, - emitEvent, - FosscordApiErrors, - getPermission, - getRights, - Message, - MessageDeleteBulkEvent, - Snowflake, - uploadFile -} from "@fosscord/util"; +import { Channel, Config, emitEvent, getPermission, getRights, Message, MessageDeleteBulkEvent } from "@fosscord/util"; import { Router, Response, Request } from "express"; -import multer from "multer"; -import { handleMessage, postHandleMessage } from "@fosscord/api"; +import { In } from "typeorm"; const router: Router = Router(); @@ -27,58 +12,58 @@ export default router; export interface PurgeSchema { before: string; - after: string + after: string; } /** TODO: apply the delete bit by bit to prevent client and database stress **/ -router.post("/", route({ /*body: "PurgeSchema",*/ }), async (req: Request, res: Response) => { - const { channel_id } = req.params; - const channel = await Channel.findOneOrFail({ id: channel_id }); - - if (!channel.guild_id) throw new HTTPError("Can't purge dm channels", 400); - isTextChannel(channel.type); +router.post("/",route({ /*body: "PurgeSchema",*/ }), async (req: Request, res: Response) => { + const { channel_id } = req.params; + const channel = await Channel.findOneOrFail({ where: { id: channel_id } }); - const rights = await getRights(req.user_id); - if (!rights.has("MANAGE_MESSAGES")) { - const permissions = await getPermission(req.user_id, channel.guild_id, channel_id); - permissions.hasThrow("MANAGE_MESSAGES"); - permissions.hasThrow("MANAGE_CHANNELS"); + if (!channel.guild_id) throw new HTTPError("Can't purge dm channels", 400); + isTextChannel(channel.type); + + const rights = await getRights(req.user_id); + if (!rights.has("MANAGE_MESSAGES")) { + const permissions = await getPermission(req.user_id, channel.guild_id, channel_id); + permissions.hasThrow("MANAGE_MESSAGES"); + permissions.hasThrow("MANAGE_CHANNELS"); + } + + const { before, after } = req.body as PurgeSchema; + + // TODO: send the deletion event bite-by-bite to prevent client stress + + let query: FindManyOptions & { where: { id?: any } } = { + order: { id: "ASC" }, + // take: limit, + where: { + channel_id, + id: Between(after, before), // the right way around + author_id: rights.has("SELF_DELETE_MESSAGES") ? undefined : Not(req.user_id) + // if you lack the right of self-deletion, you can't delete your own messages, even in purges + }, + relations: ["author", "webhook", "application", "mentions", "mention_roles", "mention_channels", "sticker_items", "attachments"] + }; + + const messages = await Message.find(query); + const endpoint = Config.get().cdn.endpointPublic; + + if (messages.length == 0) { + res.sendStatus(304); + return; + } + + await Message.delete({ id: In(messages) }); + + await emitEvent({ + event: "MESSAGE_DELETE_BULK", + channel_id, + data: { ids: messages.map((x) => x.id), channel_id, guild_id: channel.guild_id } + } as MessageDeleteBulkEvent); + + res.sendStatus(204); } - - const { before, after } = req.body as PurgeSchema; - - // TODO: send the deletion event bite-by-bite to prevent client stress - - let query: FindManyOptions & { where: { id?: any; }; } = { - order: { id: "ASC" }, - // take: limit, - where: { - channel_id, - id: Between(after, before), // the right way around - author_id: rights.has("SELF_DELETE_MESSAGES") ? undefined : Not(req.user_id) - // if you lack the right of self-deletion, you can't delete your own messages, even in purges - }, - relations: ["author", "webhook", "application", "mentions", "mention_roles", "mention_channels", "sticker_items", "attachments"] - }; - - - const messages = await Message.find(query); - const endpoint = Config.get().cdn.endpointPublic; - - if (messages.length == 0) { - res.sendStatus(304); - return; - } - - await Message.delete(messages.map((x) => ({ id: x }))); - - await emitEvent({ - event: "MESSAGE_DELETE_BULK", - channel_id, - data: { ids: messages.map(x => x.id), channel_id, guild_id: channel.guild_id } - } as MessageDeleteBulkEvent); - - res.sendStatus(204); -}); +); diff --git a/api/src/routes/discoverable-guilds.ts b/api/src/routes/discoverable-guilds.ts index 984916f41..fb7b844f4 100644 --- a/api/src/routes/discoverable-guilds.ts +++ b/api/src/routes/discoverable-guilds.ts @@ -16,19 +16,24 @@ router.get("/", route({}), async (req: Request, res: Response) => { if (categories == undefined) { guilds = showAllGuilds ? await Guild.find({ take: Math.abs(Number(limit || configLimit)) }) - : await Guild.find({ where: `"features" LIKE '%DISCOVERABLE%'`, take: Math.abs(Number(limit || configLimit)) }); + : await Guild.find({ where: { features: Like("%DISCOVERABLE%") }, take: Math.abs(Number(limit || configLimit)) }); } else { guilds = showAllGuilds - ? await Guild.find({ where: `"primary_category_id" = ${categories}`, take: Math.abs(Number(limit || configLimit)) }) - : await Guild.find({ - where: `"primary_category_id" = ${categories} AND "features" LIKE '%DISCOVERABLE%'`, - take: Math.abs(Number(limit || configLimit)) - }); + ? await Guild.find({ where: { primary_category_id: Number(categories) }, take: Math.abs(Number(limit || configLimit)) }) + : await Guild.find({ + where: { primary_category_id: Number(categories), features: Like("%DISCOVERABLE%") }, + take: Math.abs(Number(limit || configLimit)) + }); } const total = guilds ? guilds.length : undefined; - res.send({ total: total, guilds: guilds, offset: Number(offset || Config.get().guild.discovery.offset), limit: Number(limit || configLimit) }); + res.send({ + total: total, + guilds: guilds, + offset: Number(offset || Config.get().guild.discovery.offset), + limit: Number(limit || configLimit) + }); }); export default router; diff --git a/bundle/package-lock.json b/bundle/package-lock.json index 3f2d85ba9..b9277e0b8 100644 Binary files a/bundle/package-lock.json and b/bundle/package-lock.json differ diff --git a/util/package-lock.json b/util/package-lock.json index a566836e4..2405456ee 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 246ec6b68..e6dc8e2df 100644 --- a/util/package.json +++ b/util/package.json @@ -32,7 +32,7 @@ "@types/amqplib": "^0.8.1", "@types/jsonwebtoken": "^8.5.0", "@types/multer": "^1.4.7", - "@types/node": "^14.17.9", + "@types/node": "^14.18.22", "@types/node-fetch": "^2.5.12", "jest": "^27.0.6", "ts-node": "^10.2.1" @@ -50,7 +50,7 @@ "picocolors": "^1.0.0", "proxy-agent": "^5.0.0", "reflect-metadata": "^0.1.13", - "typeorm": "^0.2.45", + "typeorm": "^0.3.7", "typescript": "^4.4.2", "typescript-json-schema": "^0.50.1" }, diff --git a/util/src/entities/BaseClass.ts b/util/src/entities/BaseClass.ts index 9f23de7cb..7ee27e304 100644 --- a/util/src/entities/BaseClass.ts +++ b/util/src/entities/BaseClass.ts @@ -50,12 +50,20 @@ export class BaseClassWithoutId extends BaseEntity { ); } - static increment(conditions: FindOptionsWhere, propertyPath: string, value: number | string) { + static increment( + conditions: FindOptionsWhere, + propertyPath: string, + value: number | string + ) { const repository = this.getRepository(); return repository.increment(conditions, propertyPath, value); } - static decrement(conditions: FindOptionsWhere, propertyPath: string, value: number | string) { + static decrement( + conditions: FindOptionsWhere, + propertyPath: string, + value: number | string + ) { const repository = this.getRepository(); return repository.decrement(conditions, propertyPath, value); } diff --git a/util/src/entities/User.ts b/util/src/entities/User.ts index bd70780dd..7d5dc5a6e 100644 --- a/util/src/entities/User.ts +++ b/util/src/entities/User.ts @@ -1,4 +1,4 @@ -import { Column, Entity, FindOneOptions, JoinColumn, OneToMany } from "typeorm"; +import { Column, Entity, FindOneOptions, FindOptionsSelectByString, JoinColumn, ManyToMany, OneToMany, RelationId } from "typeorm"; import { BaseClass } from "./BaseClass"; import { BitField } from "../util/BitField"; import { Relationship } from "./Relationship"; @@ -90,7 +90,7 @@ export class User extends BaseClass { @Column() premium: boolean; // if user bought individual premium - + @Column() premium_type: number; // individual premium level @@ -105,7 +105,7 @@ export class User extends BaseClass { @Column({ select: false }) nsfw_allowed: boolean; // if the user can do age-restricted actions (NSFW channels/guilds/commands) - + @Column({ select: false }) mfa_enabled: boolean; // if multi factor authentication is enabled @@ -170,11 +170,14 @@ export class User extends BaseClass { @Column({ type: "simple-json", select: false }) settings: UserSettings; - + // workaround to prevent fossord-unaware clients from deleting settings not used by them @Column({ type: "simple-json", select: false }) extended_settings: string; + @Column({ type: "simple-json" }) + notes: { [key: string]: string }; //key is ID of user + toPublicUser() { const user: any = {}; PublicUserProjection.forEach((x) => { @@ -184,19 +187,17 @@ export class User extends BaseClass { } static async getPublicUser(user_id: string, opts?: FindOneOptions) { - return await User.findOneOrFail( - { id: user_id }, - { - ...opts, - select: [...PublicUserProjection, ...(opts?.select || [])], - } - ); + return await User.findOneOrFail({ + where: { id: user_id }, + select: [...PublicUserProjection, ...((opts?.select as FindOptionsSelectByString) || [])], + ...opts, + }); } private static async generateDiscriminator(username: string): Promise { if (Config.get().register.incrementingDiscriminators) { // discriminator will be incrementally generated - + // First we need to figure out the currently highest discrimnator for the given username and then increment it const users = await User.find({ where: { username }, select: ["discriminator"] }); const highestDiscriminator = Math.max(0, ...users.map((u) => Number(u.discriminator))); @@ -268,6 +269,8 @@ export class User extends BaseClass { premium_type: 2, bio: "", mfa_enabled: false, + totp_secret: "", + totp_backup_codes: [], verified: true, disabled: false, deleted: false,