From 8e3b5d79abee024e2e39d6841ee44b1524771a03 Mon Sep 17 00:00:00 2001 From: Luna Mendes Date: Fri, 19 Oct 2018 12:17:00 -0300 Subject: [PATCH] blueprints.users: use UPSERT when adding/patching a note - blueprints.users: move get_mutual_friends to relationships blueprint --- litecord/blueprints/relationships.py | 35 ++++++++++++++++- litecord/blueprints/users.py | 59 +++++++--------------------- 2 files changed, 49 insertions(+), 45 deletions(-) diff --git a/litecord/blueprints/relationships.py b/litecord/blueprints/relationships.py index d8ed973..43788d9 100644 --- a/litecord/blueprints/relationships.py +++ b/litecord/blueprints/relationships.py @@ -25,7 +25,6 @@ async def _sub_friend(user_id, peer_id): await app.dispatcher.sub('friend', peer_id, user_id) - async def make_friend(user_id: int, peer_id: int, rel_type=RelationshipType.FRIEND.value): _friend = RelationshipType.FRIEND.value @@ -240,3 +239,37 @@ async def remove_relationship(peer_id: int): await _unsub_friend(user_id, peer_id) return '', 204 + + +@bp.route('//relationships', methods=['GET']) +async def get_mutual_friends(peer_id: int): + """Fetch a users' mutual friends with the current user.""" + user_id = await token_check() + _friend = RelationshipType.FRIEND.value + + peer = await app.storage.get_user(peer_id) + + if not peer: + return '', 204 + + # NOTE: maybe this could be better with pure SQL calculations + # but it would be beyond my current SQL knowledge, so... + user_rels = await app.storage.get_relationships(user_id) + peer_rels = await app.storage.get_relationships(peer_id) + + user_friends = {rel['user']['id'] + for rel in user_rels if rel['type'] == _friend} + peer_friends = {rel['user']['id'] + for rel in peer_rels if rel['type'] == _friend} + + # get the intersection, then map them to Storage.get_user() calls + mutual_ids = user_friends | peer_friends + + mutual_friends = [] + + for friend_id in mutual_ids: + mutual_friends.append( + await app.storage.get_user(int(friend_id)) + ) + + return jsonify(mutual_friends) diff --git a/litecord/blueprints/users.py b/litecord/blueprints/users.py index 313356d..e9f2656 100644 --- a/litecord/blueprints/users.py +++ b/litecord/blueprints/users.py @@ -352,17 +352,16 @@ async def put_note(target_id: int): 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) + # UPSERTs are beautiful + await app.db.execute(""" + INSERT INTO notes (user_id, target_id, note) + VALUES ($1, $2, $3) + + ON CONFLICT DO UPDATE SET + note = $3 + WHERE + user_id = $1 AND target_id = $2 + """, user_id, target_id, note) await app.dispatcher.dispatch_user(user_id, 'USER_NOTE_UPDATE', { 'id': str(target_id), @@ -382,6 +381,11 @@ async def get_user_settings(): @bp.route('/@me/settings', methods=['PATCH']) async def patch_current_settings(): + """Patch the users' current settings. + + More information on what settings exist + is at Storage.get_user_settings and the schema.sql file. + """ user_id = await token_check() j = validate(await request.get_json(), USER_SETTINGS) @@ -485,39 +489,6 @@ async def get_profile(peer_id: int): }) -@bp.route('//relationships', methods=['GET']) -async def get_mutual_friends(peer_id: int): - user_id = await token_check() - _friend = RelationshipType.FRIEND.value - - peer = await app.storage.get_user(peer_id) - - if not peer: - return '', 204 - - # NOTE: maybe this could be better with pure SQL calculations - # but it would be beyond my current SQL knowledge, so... - user_rels = await app.storage.get_relationships(user_id) - peer_rels = await app.storage.get_relationships(peer_id) - - user_friends = {rel['user']['id'] - for rel in user_rels if rel['type'] == _friend} - peer_friends = {rel['user']['id'] - for rel in peer_rels if rel['type'] == _friend} - - # get the intersection, then map them to Storage.get_user() calls - mutual_ids = user_friends | peer_friends - - mutual_friends = [] - - for friend_id in mutual_ids: - mutual_friends.append( - await app.storage.get_user(int(friend_id)) - ) - - return jsonify(mutual_friends) - - @bp.route('/@me/guilds//settings', methods=['PATCH']) async def patch_guild_settings(guild_id: int): """Update the users' guild settings for a given guild.