From b6e21a2501c472712b872a624b35147f25057c35 Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 15 Mar 2019 04:29:35 -0300 Subject: [PATCH] guilds: handle banner changes like user avatar changes just like user avatars, the client can just send the icon hash, we need to check that first beforehand. - guilds: add description update - utils: move to_update() from blueprints.users to there --- litecord/blueprints/guilds.py | 8 ++++++-- litecord/blueprints/users.py | 7 ++----- litecord/schemas.py | 5 ++++- litecord/utils.py | 5 +++++ 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/litecord/blueprints/guilds.py b/litecord/blueprints/guilds.py index 4e11755..ebdca11 100644 --- a/litecord/blueprints/guilds.py +++ b/litecord/blueprints/guilds.py @@ -35,6 +35,7 @@ from ..schemas import ( ) from .channels import channel_ack from .checks import guild_check, guild_owner_check, guild_perm_check +from litecord.utils import to_update from litecord.errors import BadRequest @@ -293,14 +294,17 @@ async def _update_guild(guild_id): await _guild_update_icon('splash', guild_id, j['splash']) - if 'banner' in j: + # small guild to work with to_update() + guild = await app.storage.get_guild(guild_id) + + if to_update(j, guild, 'banner'): if not await app.storage.has_feature(guild_id, 'VERIFIED'): raise BadRequest('guild is not verified') await _guild_update_icon('banner', guild_id, j['banner']) fields = ['verification_level', 'default_message_notifications', - 'explicit_content_filter', 'afk_timeout'] + 'explicit_content_filter', 'afk_timeout', 'description'] for field in [f for f in fields if f in j]: await app.db.execute(f""" diff --git a/litecord/blueprints/users.py b/litecord/blueprints/users.py index 261b0a7..eff1be9 100644 --- a/litecord/blueprints/users.py +++ b/litecord/blueprints/users.py @@ -32,10 +32,11 @@ from litecord.auth import token_check, hash_data, check_username_usage from litecord.blueprints.guild.mod import remove_member from litecord.enums import PremiumType -from litecord.images import parse_data_uri +from litecord.images import parse_data_uri, ImageError from litecord.permissions import base_permissions from litecord.blueprints.auth import check_password +from litecord.utils import to_update bp = Blueprint('user', __name__) log = Logger(__name__) @@ -185,10 +186,6 @@ async def _try_discrim_patch(user_id, new_discrim: str): }) -def to_update(j: dict, user: dict, field: str): - return field in j and j[field] and j[field] != user[field] - - async def _check_pass(j, user): # Do not do password checks on unclaimed accounts if user['email'] is None: diff --git a/litecord/schemas.py b/litecord/schemas.py index 893d46e..6796ed8 100644 --- a/litecord/schemas.py +++ b/litecord/schemas.py @@ -281,7 +281,10 @@ GUILD_UPDATE = { 'icon': {'type': 'b64_icon', 'required': False, 'nullable': True}, 'splash': {'type': 'b64_icon', 'required': False, 'nullable': True}, - 'banner': {'type': 'b64_icon', 'required': False, 'nullable': True}, + + # TODO: does splash also respect when its just a string pointing to the + # hash, just like in USER_UPDATE.avatar? + 'banner': {'type': 'string', 'required': False, 'nullable': True}, 'description': { 'type': 'string', 'required': False, diff --git a/litecord/utils.py b/litecord/utils.py index 513fafa..d19df06 100644 --- a/litecord/utils.py +++ b/litecord/utils.py @@ -174,3 +174,8 @@ def yield_chunks(input_list: Sequence[Any], chunk_size: int): # make the chunks for idx in range(0, len(input_list), chunk_size): yield input_list[idx:idx + chunk_size] + +def to_update(j: dict, orig: dict, field: str) -> bool: + """Compare values to check if j[field] is actually updating + the value in orig[field]. Useful for icon checks.""" + return field in j and j[field] and j[field] != orig[field]