From 27b5836132f7445f829cda3fc04da54757c34a2f Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 26 Oct 2019 11:52:26 -0300 Subject: [PATCH] invites: check maximum invite count --- litecord/blueprints/invites.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/litecord/blueprints/invites.py b/litecord/blueprints/invites.py index 0a9f7d7..ea82ece 100644 --- a/litecord/blueprints/invites.py +++ b/litecord/blueprints/invites.py @@ -214,6 +214,31 @@ async def use_invite(user_id, invite_code): pass +async def _check_max_invites(guild_id, channel_id): + """Check that the maximum invite count (1000) isn't being blown.""" + if guild_id is not None: + invite_count = await app.db.fetchval( + """ + SELECT COUNT(*) + FROM invites + WHERE guild_id = $1 + """, + guild_id, + ) + else: + invite_count = await app.db.fetchval( + """ + SELECT COUNT(*) + FROM invites + WHERE channel_id = $1 + """, + channel_id, + ) + + if invite_count >= 1000: + raise BadRequest(30016) + + @bp.route("/channels//invites", methods=["POST"]) async def create_invite(channel_id): """Create an invite to a channel.""" @@ -241,6 +266,8 @@ async def create_invite(channel_id): else: guild_id = None + await _check_max_invites(guild_id, channel_id) + await app.db.execute( """ INSERT INTO invites