admin_api.features: delete vanity invites when guild loses feature

- guilds: make _vanity_inv -> vanity_invite exportable function
This commit is contained in:
Luna 2019-03-13 05:07:10 -03:00
parent 6763f2c501
commit 977fa95ff2
2 changed files with 18 additions and 3 deletions

View File

@ -23,6 +23,7 @@ from quart import Blueprint, current_app as app, jsonify, request
from litecord.auth import admin_check
from litecord.errors import BadRequest
from litecord.schemas import validate, FEATURES
from litecord.blueprints.guilds import vanity_invite
bp = Blueprint('features_admin', __name__)
@ -39,6 +40,19 @@ async def _features(guild_id: int):
async def _update_features(guild_id: int, features: list):
if 'VANITY_URL' not in features:
existing_inv = await vanity_invite(guild_id)
await app.db.execute("""
DELETE FROM vanity_invites
WHERE guild_id = $1
""", guild_id)
await app.db.execute("""
DELETE FROM invites
WHERE code = $1
""", existing_inv)
await app.db.execute("""
UPDATE guilds
SET features = $1

View File

@ -376,7 +376,8 @@ async def ack_guild(guild_id):
return '', 204
async def _vanity_inv(guild_id) -> Optional[str]:
async def vanity_invite(guild_id: int) -> Optional[str]:
"""Get the vanity invite for a guild."""
return await app.db.fetchval("""
SELECT code FROM vanity_invites
WHERE guild_id = $1
@ -389,7 +390,7 @@ async def get_vanity_url(guild_id: int):
user_id = await token_check()
await guild_perm_check(user_id, guild_id, 'manage_guild')
inv_code = await _vanity_inv(guild_id)
inv_code = await vanity_invite(guild_id)
if inv_code is None:
return jsonify({'code': None})
@ -415,7 +416,7 @@ async def change_vanity_url(guild_id: int):
# store old vanity in a variable to delete it from
# invites table
old_vanity = await _vanity_inv(guild_id)
old_vanity = await vanity_invite(guild_id)
if old_vanity == inv_code:
raise BadRequest('can not change to same invite')