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
This commit is contained in:
Luna 2019-03-15 04:29:35 -03:00
parent 44b81ea2f6
commit b6e21a2501
4 changed files with 17 additions and 8 deletions

View File

@ -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"""

View File

@ -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:

View File

@ -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,

View File

@ -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]