From 8678c84355deab4432eb9d6854c16441bb0f7a2f Mon Sep 17 00:00:00 2001 From: Luna Mendes Date: Sat, 27 Oct 2018 22:16:03 -0300 Subject: [PATCH] roles: add GET /api/v6/guilds/:id/roles - guild: add DELETE /api/v6/guilds/:id/bans/:uid --- litecord/blueprints/guild/roles.py | 22 +++++++++++++++++----- litecord/blueprints/guilds.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/litecord/blueprints/guild/roles.py b/litecord/blueprints/guild/roles.py index c6cb7d2..aaece26 100644 --- a/litecord/blueprints/guild/roles.py +++ b/litecord/blueprints/guild/roles.py @@ -4,19 +4,31 @@ from quart import Blueprint, request, current_app as app, jsonify from litecord.auth import token_check -# from litecord.blueprints.checks import guild_check -from litecord.blueprints.checks import guild_owner_check -from litecord.snowflake import get_snowflake -from litecord.utils import dict_get - +from litecord.blueprints.checks import ( + guild_check, guild_owner_check +) from litecord.schemas import ( validate, ROLE_CREATE, ROLE_UPDATE, ROLE_UPDATE_POSITION ) +from litecord.snowflake import get_snowflake +from litecord.utils import dict_get + DEFAULT_EVERYONE_PERMS = 104324161 bp = Blueprint('guild_roles', __name__) +@bp.route('//roles', methods=['GET']) +async def get_guild_roles(guild_id): + """Get all roles in a guild.""" + user_id = await token_check() + await guild_check(user_id, guild_id) + + return jsonify( + await app.storage.get_role_data(guild_id) + ) + + async def create_role(guild_id, name: str, **kwargs): """Create a role in a guild.""" new_role_id = get_snowflake() diff --git a/litecord/blueprints/guilds.py b/litecord/blueprints/guilds.py index 23ee91c..84eb129 100644 --- a/litecord/blueprints/guilds.py +++ b/litecord/blueprints/guilds.py @@ -112,6 +112,10 @@ async def create_guild(): # create the default @everyone role (everyone has it by default, # so we don't insert that in the table) + + # we also don't use create_role because the id of the role + # is the same as the id of the guild, and create_role + # generates a new snowflake. await app.db.execute(""" INSERT INTO roles (id, guild_id, name, position, permissions) VALUES ($1, $2, $3, $4, $5) @@ -314,6 +318,31 @@ async def create_ban(guild_id, member_id): return '', 204 +@bp.route('//bans/', methods=['DELETE']) +async def remove_ban(guild_id, banned_id): + user_id = await token_check() + + # TODO: check BAN_MEMBERS permission + await guild_owner_check(guild_id, user_id) + + res = await app.db.execute(""" + DELETE FROM bans + WHERE guild_id = $1 AND user_id = $@ + """, guild_id, banned_id) + + # we don't really need to dispatch GUILD_BAN_REMOVE + # when no bans were actually removed. + if res == 'DELETE 0': + return '', 204 + + await app.dispatcher.dispatch_guild(guild_id, 'GUILD_BAN_REMOVE', { + 'guild_id': str(guild_id), + 'user': await app.storage.get_user(banned_id) + }) + + return '', 204 + + @bp.route('//messages/search') async def search_messages(guild_id): """Search messages in a guild.