fix ts errors

This commit is contained in:
Flam3rboy
2022-08-09 23:28:27 +02:00
committed by TheArcaneBrony
parent 450e7d9378
commit 14e0842dfb
10 changed files with 94 additions and 93 deletions
BIN
View File
Binary file not shown.
+1 -1
View File
@@ -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": [
@@ -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",
+50 -65
View File
@@ -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<Message> & { 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<Message> & { 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);
});
);
+12 -7
View File
@@ -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;
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+2 -2
View File
@@ -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"
},
+10 -2
View File
@@ -50,12 +50,20 @@ export class BaseClassWithoutId extends BaseEntity {
);
}
static increment<T extends BaseClass>(conditions: FindOptionsWhere<T>, propertyPath: string, value: number | string) {
static increment<T extends BaseClass>(
conditions: FindOptionsWhere<T>,
propertyPath: string,
value: number | string
) {
const repository = this.getRepository();
return repository.increment(conditions, propertyPath, value);
}
static decrement<T extends BaseClass>(conditions: FindOptionsWhere<T>, propertyPath: string, value: number | string) {
static decrement<T extends BaseClass>(
conditions: FindOptionsWhere<T>,
propertyPath: string,
value: number | string
) {
const repository = this.getRepository();
return repository.decrement(conditions, propertyPath, value);
}
+15 -12
View File
@@ -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<User>) {
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<User>) || [])],
...opts,
});
}
private static async generateDiscriminator(username: string): Promise<string | undefined> {
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,