From bff5700d064c63bcace6e294db9c7822b3e8be9f Mon Sep 17 00:00:00 2001 From: Luna Mendes Date: Sat, 29 Sep 2018 23:43:02 -0300 Subject: [PATCH] blueprints.users: add user setting patch impl - schemas: add explicit type - schemas: add USER_SETTINGS - run: make Access-Control-Allow-Methods the same as Allow header when possible --- litecord/blueprints/users.py | 15 ++++++++- litecord/schemas.py | 60 ++++++++++++++++++++++++++++++++++-- run.py | 4 ++- 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/litecord/blueprints/users.py b/litecord/blueprints/users.py index e1c5719..507b33b 100644 --- a/litecord/blueprints/users.py +++ b/litecord/blueprints/users.py @@ -3,6 +3,7 @@ from asyncpg import UniqueViolationError from ..auth import token_check from ..errors import Forbidden, BadRequest +from ..schemas import validate, USER_SETTINGS bp = Blueprint('user', __name__) @@ -144,7 +145,19 @@ async def get_user_settings(): @bp.route('/@me/settings', methods=['PATCH']) async def patch_current_settings(): - return '', 204 + user_id = await token_check() + j = validate(await request.get_json(), USER_SETTINGS) + + for key in j: + await app.db.execute(f""" + UPDATE user_settings + SET {key}=$1 + """, j[key]) + + settings = await app.storage.get_user_settings(user_id) + await app.dispatcher.dispatch_user( + user_id, 'USER_SETTINGS_UPDATE', settings) + return jsonify(settings) @bp.route('/@me/consent', methods=['GET', 'POST']) diff --git a/litecord/schemas.py b/litecord/schemas.py index a7ce4d0..1d54ee4 100644 --- a/litecord/schemas.py +++ b/litecord/schemas.py @@ -4,7 +4,7 @@ from cerberus import Validator from logbook import Logger from .errors import BadRequest -from .enums import ActivityType, StatusType +from .enums import ActivityType, StatusType, ExplicitFilter log = Logger(__name__) @@ -49,6 +49,14 @@ class LitecordValidator(Validator): return value in statuses + def _validate_type_explicit(self, value: str) -> bool: + try: + val = int(value) + except (TypeError, ValueError): + return False + + return val in ExplicitFilter.values() + def validate(reqjson, schema, raise_err: bool = True): validator = LitecordValidator(schema) @@ -80,7 +88,7 @@ GUILD_UPDATE = { 'type': 'msg_notifications', 'required': False, }, - 'explicit_content_filter': {'type': 'explicit_content', 'required': False}, + 'explicit_content_filter': {'type': 'explicit', 'required': False}, 'afk_channel_id': {'type': 'snowflake', 'required': False}, 'afk_timeout': {'type': 'number', 'required': False}, @@ -169,7 +177,8 @@ GW_ACTIVITY = { GW_STATUS_UPDATE = { 'status': {'type': 'status_external', 'required': False}, - 'activities': {'type': 'list', 'schema': GW_ACTIVITY}, + 'activities': { + 'type': 'list', 'required': False, 'schema': GW_ACTIVITY}, 'afk': {'type': 'boolean', 'required': False}, 'since': {'type': 'number', 'required': True, 'nullable': True}, @@ -208,3 +217,48 @@ INVITE = { 'temporary': {'type': 'boolean', 'required': False, 'default': False}, 'unique': {'type': 'boolean', 'required': False, 'default': True}, } + +USER_SETTINGS = { + 'afk_timeout': { + 'type': 'number', 'required': False, 'min': 0, 'max': 3000}, + + 'animate_emoji': {'type': 'boolean', 'required': False}, + 'convert_emoticons': {'type': 'boolean', 'required': False}, + 'default_guilds_restricted': {'type': 'boolean', 'required': False}, + 'detect_platform_accounts': {'type': 'boolean', 'required': False}, + 'developer_mode': {'type': 'boolean', 'required': False}, + 'disable_games_tab': {'type': 'boolean', 'required': False}, + 'enable_tts_command': {'type': 'boolean', 'required': False}, + + 'explicit_content_filter': {'type': 'explicit', 'required': False}, + + 'friend_source': { + 'type': 'dict', + 'required': False, + 'schema': { + 'all': {'type': 'boolean', 'required': False}, + 'mutual_guilds': {'type': 'boolean', 'required': False}, + 'mutual_friends': {'type': 'boolean', 'required': False}, + } + }, + 'guild_positions': { + 'type': 'list', + 'required': False, + 'schema': {'type': 'snowflake'} + }, + 'restricted_guilds': { + 'type': 'list', + 'required': False, + 'schema': {'type': 'snowflake'} + }, + + 'gif_auto_play': {'type': 'boolean', 'required': False}, + 'inline_attachment_media': {'type': 'boolean', 'required': False}, + 'inline_embed_media': {'type': 'boolean', 'required': False}, + 'message_display_compact': {'type': 'boolean', 'required': False}, + 'render_embeds': {'type': 'boolean', 'required': False}, + 'render_reactions': {'type': 'boolean', 'required': False}, + 'show_current_game': {'type': 'boolean', 'required': False}, + + 'timezone_offset': {'type': 'number', 'required': False}, +} diff --git a/run.py b/run.py index 3bb7036..f3d09a3 100644 --- a/run.py +++ b/run.py @@ -75,7 +75,9 @@ async def app_after_request(resp): 'Authorization, ' 'Origin, ' 'If-None-Match') - resp.headers['Access-Control-Allow-Methods'] = '*' + # resp.headers['Access-Control-Allow-Methods'] = '*' + resp.headers['Access-Control-Allow-Methods'] = \ + resp.headers.get('allow', '*') return resp