diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-10-15 00:35:18 +0200 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-10-15 00:35:18 +0200 |
commit | 0994b47a6702c8957ba8ffcfe1720d3c70fabebe (patch) | |
tree | 24365d30cff2dbb8e3fe38ab62eeb6bc14e335ec /api/src | |
parent | :zap: speed up message query (diff) | |
download | server-0994b47a6702c8957ba8ffcfe1720d3c70fabebe.tar.xz |
:bug: fix prune
Diffstat (limited to 'api/src')
-rw-r--r-- | api/src/routes/guilds/#guild_id/prune.ts | 56 |
1 files changed, 32 insertions, 24 deletions
diff --git a/api/src/routes/guilds/#guild_id/prune.ts b/api/src/routes/guilds/#guild_id/prune.ts index cddbcee7..92809985 100644 --- a/api/src/routes/guilds/#guild_id/prune.ts +++ b/api/src/routes/guilds/#guild_id/prune.ts @@ -1,7 +1,6 @@ import { Router, Request, Response } from "express"; import { Guild, Member, Snowflake } from "@fosscord/util"; -import { HTTPError } from "lambert-server"; -import { LessThan } from "typeorm"; +import { LessThan, IsNull } from "typeorm"; import { route } from "@fosscord/api"; const router = Router(); @@ -16,59 +15,68 @@ export const inactiveMembers = async (guild_id: string, user_id: string, days: n where: [ { guild_id, - last_message_id: LessThan(minId.toString()), + last_message_id: LessThan(minId.toString()) }, + { + last_message_id: IsNull() + } ], relations: ["roles"] }); + console.log(members); if (!members.length) return []; //I'm sure I can do this in the above db query ( and it would probably be better to do so ), but oh well. - if (roles.length && members.length) - members = members.filter(user => user.roles?.some(role => roles.includes(role.id))); + if (roles.length && members.length) members = members.filter((user) => user.roles?.some((role) => roles.includes(role.id))); - const me = await Member.findOne({ id: user_id, guild_id }, { relations: ["roles"] }); - if (!me) throw new HTTPError("You are not member of this guild", 403); - const myHighestRole = Math.max(...(me.roles?.map(x => x.position) || [])) + const me = await Member.findOneOrFail({ id: user_id, guild_id }, { relations: ["roles"] }); + const myHighestRole = Math.max(...(me.roles?.map((x) => x.position) || [])); - const guild = await Guild.findOneOrFail({ where: { id: guild_id } }) + const guild = await Guild.findOneOrFail({ where: { id: guild_id } }); - members = members.filter(member => - member.id !== guild.owner_id && //can't kick owner - member.roles?.some(role => - role.position < myHighestRole || //roles higher than me can't be kicked - me.id === guild.owner_id //owner can kick anyone - ) - ) + members = members.filter( + (member) => + member.id !== guild.owner_id && //can't kick owner + member.roles?.some( + (role) => + role.position < myHighestRole || //roles higher than me can't be kicked + me.id === guild.owner_id //owner can kick anyone + ) + ); return members; -} +}; -router.get("/", async (req: Request, res: Response) => { +router.get("/", route({ permission: "KICK_MEMBERS" }), async (req: Request, res: Response) => { const days = parseInt(req.query.days as string); var roles = req.query.include_roles; - if (typeof roles === 'string') roles = [roles]; //express will return array otherwise + if (typeof roles === "string") roles = [roles]; //express will return array otherwise const members = await inactiveMembers(req.params.guild_id, req.user_id, days, roles as string[]); res.send({ pruned: members.length }); }); +export interface PruneSchema { + /** + * @min 0 + */ + days: number; +} + router.post("/", route({ permission: "KICK_MEMBERS" }), async (req: Request, res: Response) => { const days = parseInt(req.body.days); var roles = req.query.include_roles; - if (typeof roles === 'string') roles = [roles]; + if (typeof roles === "string") roles = [roles]; const { guild_id } = req.params; const members = await inactiveMembers(guild_id, req.user_id, days, roles as string[]); - for (var curr of members) { - await Member.removeFromGuild(curr.id, guild_id); - } + await Promise.all(members.map((x) => Member.removeFromGuild(x.id, guild_id))); res.send({ purged: members.length }); -}) +}); export default router; |