From 3858e20080b3ed34f4c26818e338c5bec96b8aa5 Mon Sep 17 00:00:00 2001 From: Luna Mendes Date: Sat, 22 Sep 2018 20:18:48 -0300 Subject: [PATCH] Add user notes - blueprints.users: fix get_other - blueprints.users: add dummy get_user_settings, patch_current_settings, get_consent, get_harvest and get_library - enums: add ExplicitFilter - storage: add Storage.fetch_notes - schema.sql: add incomplete user_settings table --- README.md | 1 + litecord/blueprints/users.py | 95 +++++++++++++++++++++++++++++++++-- litecord/enums.py | 6 +++ litecord/gateway/websocket.py | 3 +- litecord/storage.py | 11 ++++ schema.sql | 42 ++++++++++++++++ 6 files changed, 153 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 697763e..f471d33 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ $ createdb litecord $ psql -f schema.sql litecord # Configure litecord: +# edit config.py as you wish $ cp config.example.py config.py # Install all packages: diff --git a/litecord/blueprints/users.py b/litecord/blueprints/users.py index 6c080fd..2eca969 100644 --- a/litecord/blueprints/users.py +++ b/litecord/blueprints/users.py @@ -15,8 +15,8 @@ async def get_me(): return jsonify(user) -@bp.route('/', methods=['GET']) -async def get_other(): +@bp.route('/', methods=['GET']) +async def get_other(target_id): """Get any user, given the user ID.""" user_id = await token_check() @@ -28,7 +28,7 @@ async def get_other(): if not bot: raise Forbidden('Only bots can use this endpoint') - other = await app.storage.get_user(user_id) + other = await app.storage.get_user(target_id) return jsonify(other) @@ -109,3 +109,92 @@ async def get_dms(): # @bp.route('/@me/channels', methods=['POST']) async def start_dm(): pass + + +@bp.route('/@me/notes/', methods=['PUT']) +async def put_note(target_id: int): + """Put a note to a user.""" + user_id = await token_check() + + j = await request.get_json() + note = str(j['note']) + + try: + await app.db.execute(""" + INSERT INTO notes (user_id, target_id, note) + VALUES ($1, $2, $3) + """, user_id, target_id, note) + except UniqueViolationError: + await app.db.execute(""" + UPDATE notes + SET note = $3 + WHERE user_id = $1 AND target_id = $2 + """, user_id, target_id, note) + + return '', 204 + + +@bp.route('/@me/settings', methods=['GET']) +async def get_user_settings(): + # TODO: for now, just return hardcoded defaults, + # once we get the user_settings table working + # we can move to that. + await token_check() + + return jsonify({ + 'afk_timeout': 300, + 'animate_emoji': True, + 'convert_emoticons': False, + 'default_guilds_restricted': True, + 'detect_platform_accounts': False, + 'developer_mode': True, + 'disable_games_tab': True, + 'enable_tts_command': False, + 'explicit_content_filter': 2, + 'friend_source_flags': { + 'mutual_friends': True + }, + 'gif_auto_play': True, + 'guild_positions': [], + 'inline_attachment_media': True, + 'inline_embed_media': True, + 'locale': 'en-US', + 'message_display_compact': False, + 'render_embeds': True, + 'render_reactions': True, + 'restricted_guilds': [], + 'show_current_game': True, + 'status': 'online', + 'theme': 'dark', + 'timezone_offset': 420, + }) + + +@bp.route('/@me/settings', methods=['PATCH']) +async def patch_current_settings(): + return '', 204 + + +@bp.route('/@me/consent', methods=['GET']) +async def get_consent(): + """Always disable data collection.""" + return jsonify({ + 'usage_statistics': { + 'consented': False, + }, + 'personalization': { + 'consented': False, + } + }) + + +@bp.route('/@me/harvest', methods=['GET']) +async def get_harvest(): + """Dummy route""" + return '', 204 + + +@bp.route('/@me/library', methods=['GET']) +async def get_library(): + """Probably related to Discord Store?""" + return jsonify([]) diff --git a/litecord/enums.py b/litecord/enums.py index 2030a2c..1fdf584 100644 --- a/litecord/enums.py +++ b/litecord/enums.py @@ -71,3 +71,9 @@ class StatusType(EasyEnum): IDLE = 'idle' INVISIBLE = 'invisible' OFFLINE = 'offline' + + +class ExplicitFilter(EasyEnum): + EDGE = 0 + FRIENDS = 1 + SAFE = 2 diff --git a/litecord/gateway/websocket.py b/litecord/gateway/websocket.py index e4cc4af..b209682 100644 --- a/litecord/gateway/websocket.py +++ b/litecord/gateway/websocket.py @@ -172,8 +172,7 @@ class GatewayWebsocket: # TODO 'user_guild_settings': [], - # TODO - 'notes': {}, + 'notes': await self.storage.fetch_notes(self.state.user_id), 'friend_suggestion_count': 0, # TODO diff --git a/litecord/storage.py b/litecord/storage.py index 1f94dee..3be90d3 100644 --- a/litecord/storage.py +++ b/litecord/storage.py @@ -380,3 +380,14 @@ class Storage: res['pinned'] = False return res + + async def fetch_notes(self, user_id: int) -> dict: + """Fetch a users' notes""" + note_rows = await self.db.fetch(""" + SELECT target_id, note + FROM notes + WHERE user_id = $1 + """, user_id) + + return {str(row['target_id']): row['note'] + for row in note_rows} diff --git a/schema.sql b/schema.sql index f8cad86..269cce3 100644 --- a/schema.sql +++ b/schema.sql @@ -78,6 +78,48 @@ CREATE TABLE IF NOT EXISTS users ( PRIMARY KEY (id, username, discriminator) ); +/* + +CREATE TABLE IF NOT EXISTS user_settings ( + id bigint REFERENCES users (id), + afk_timeout int DEFAULT 300, + animate_emoji bool DEFAULT true, + convert_emoticons bool DEFAULT false, + default_guilds_restricted bool DEFAULT false, + detect_platform_accounts bool DEFAULT false, + + -- smirk emoji + developer_mode bool DEFAULT true, + + disable_games_tab bool DEFAULT true, + enable_tts_command bool DEFAULT false, + explicit_content_filter int DEFAULT 2, + + friend_source_everyone bool DEFAULT true, + friend_source_mutuals bool DEFAULT true, + friend_source_guilds bool DEFAULT true, + + gif_auto_play bool DEFAULT true, + + -- TODO: guild_positions + -- TODO: restricted_guilds + + inline_attachment_media bool DEFAULT true, + inline_embed_media bool DEFAULT true, + locale text DEFAULT 'en-US', + message_display_compact bool DEFAULT false, + render_embeds bool DEFAULT true, + render_reactions bool DEFAULT true, + show_current_game bool DEFAULT true, + + status text DEFAULT 'online' NOT NULL, + theme text DEFAULT 'dark' NOT NULL, + + timezone_offset int DEFAULT 0, + +); + +*/ CREATE TABLE IF NOT EXISTS notes ( user_id bigint REFERENCES users (id),