From 6defce0f9ee6deae7a7faf1ab5b348c6fa60b5bc Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 6 Dec 2018 17:09:40 -0300 Subject: [PATCH] channel.dm_checks: add friend check override - user_storage: add UserStorage.are_friends_with --- litecord/blueprints/channel/dm_checks.py | 8 +++++++ litecord/user_storage.py | 28 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/litecord/blueprints/channel/dm_checks.py b/litecord/blueprints/channel/dm_checks.py index 3161f1a..155f5a1 100644 --- a/litecord/blueprints/channel/dm_checks.py +++ b/litecord/blueprints/channel/dm_checks.py @@ -22,8 +22,16 @@ async def dm_pre_check(user_id: int, channel_id: int, peer_id: int): if blockrow is not None: raise ForbiddenDM() + # check if theyre friends + friends = await app.user_storage.are_friends_with(user_id, peer_id) + + # if they're mutual friends, we don't do mutual guild checking + if friends: + return + # now comes the fun part, which is guild settings. mutual_guilds = await app.user_storage.get_mutual_guilds(user_id, peer_id) + mutual_guilds = set(mutual_guilds) # user_settings.restricted_guilds gives us the dms a user doesn't # want dms from, so we use that setting from both user and peer diff --git a/litecord/user_storage.py b/litecord/user_storage.py index 0c823c3..4389607 100644 --- a/litecord/user_storage.py +++ b/litecord/user_storage.py @@ -301,3 +301,31 @@ class UserStorage: mutual_guilds = [r['guild_id'] for r in mutual_guilds] return mutual_guilds + + async def are_friends_with(self, user_id: int, peer_id: int) -> bool: + """Return if two people are friends. + + This returns false even if there is a friend request. + """ + return await self.db.fetchval(""" + SELECT + ( + SELECT EXISTS( + SELECT rel_type + FROM relationships + WHERE user_id = $1 + AND peer_id = $2 + AND rel_type = 1 + ) + ) + AND + ( + SELECT EXISTS( + SELECT rel_type + FROM relationships + WHERE user_id = $2 + AND peer_id = $1 + AND rel_type = 1 + ) + ) + """, user_id, peer_id)