From 51180d5e05d7db4ac87503d0a98825d18c06c511 Mon Sep 17 00:00:00 2001 From: gabixdev Date: Fri, 19 Jul 2019 20:42:22 +0200 Subject: [PATCH 1/2] Fixed invites --- litecord/blueprints/invites.py | 35 +++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/litecord/blueprints/invites.py b/litecord/blueprints/invites.py index 4fe6054..0931d30 100644 --- a/litecord/blueprints/invites.py +++ b/litecord/blueprints/invites.py @@ -48,6 +48,8 @@ class UnknownInvite(BadRequest): class InvalidInvite(Forbidden): error_code = 50020 +class AlreadyInvited(BaseException): + pass def gen_inv_code() -> str: """Generate an invite code. @@ -70,7 +72,7 @@ async def invite_precheck(user_id: int, guild_id: int): """, user_id, guild_id) if joined is not None: - raise BadRequest('You are already in the guild') + raise AlreadyInvited('You are already in the guild') banned = await app.db.fetchval(""" SELECT reason @@ -87,7 +89,7 @@ async def invite_precheck_gdm(user_id: int, channel_id: int): is_member = await gdm_is_member(channel_id, user_id) if is_member: - raise BadRequest('You are already in the Group DM') + raise AlreadyInvited('You are already in the Group DM') async def _inv_check_age(inv: dict): @@ -171,20 +173,22 @@ async def use_invite(user_id, invite_code): # NOTE: if group dm invite, guild_id is null. guild_id = inv['guild_id'] - if guild_id is None: - channel_id = inv['channel_id'] - await invite_precheck_gdm(user_id, inv['channel_id']) - await gdm_add_recipient(channel_id, user_id) - else: - await invite_precheck(user_id, guild_id) - await _guild_add_member(guild_id, user_id) - - await app.db.execute(""" - UPDATE invites - SET uses = uses + 1 - WHERE code = $1 - """, invite_code) + try: + if guild_id is None: + channel_id = inv['channel_id'] + await invite_precheck_gdm(user_id, inv['channel_id']) + await gdm_add_recipient(channel_id, user_id) + else: + await invite_precheck(user_id, guild_id) + await _guild_add_member(guild_id, user_id) + await app.db.execute(""" + UPDATE invites + SET uses = uses + 1 + WHERE code = $1 + """, invite_code) + except AlreadyInvited: + pass @bp.route('/channels//invites', methods=['POST']) async def create_invite(channel_id): @@ -323,6 +327,7 @@ async def get_channel_invites(channel_id: int): @bp.route('/invite/', methods=['POST']) +@bp.route('/invites/', methods=['POST']) async def _use_invite(invite_code): """Use an invite.""" user_id = await token_check() From 409d3fcc8fc1f89c8709c94b3966c3527fab2c90 Mon Sep 17 00:00:00 2001 From: gabixdev Date: Fri, 19 Jul 2019 20:53:09 +0200 Subject: [PATCH 2/2] Remove unnecessary _2 routes --- litecord/blueprints/invites.py | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/litecord/blueprints/invites.py b/litecord/blueprints/invites.py index 0931d30..1b3506a 100644 --- a/litecord/blueprints/invites.py +++ b/litecord/blueprints/invites.py @@ -230,6 +230,7 @@ async def create_invite(channel_id): return jsonify(invite) +@bp.route('/invite/', methods=['GET']) @bp.route('/invites/', methods=['GET']) async def get_invite(invite_code: str): inv = await app.storage.get_invite(invite_code) @@ -243,12 +244,6 @@ async def get_invite(invite_code: str): return jsonify(inv) - -@bp.route('/invite/', methods=['GET']) -async def get_invite_2(invite_code: str): - return await get_invite(invite_code) - - async def delete_invite(invite_code: str): """Delete an invite.""" await app.db.fetchval(""" @@ -256,7 +251,7 @@ async def delete_invite(invite_code: str): WHERE code = $1 """, invite_code) - +@bp.route('/invite/', methods=['DELETE']) @bp.route('/invites/', methods=['DELETE']) async def _delete_invite(invite_code: str): user_id = await token_check() @@ -276,12 +271,6 @@ async def _delete_invite(invite_code: str): await delete_invite(invite_code) return jsonify(inv) - -@bp.route('/invite/', methods=['DELETE']) -async def _delete_invite_2(invite_code: str): - return await _delete_invite(invite_code) - - async def _get_inv(code): inv = await app.storage.get_invite(code) meta = await app.storage.get_invite_metadata(code)