diff --git a/litecord/blueprints/channel/dm_checks.py b/litecord/blueprints/channel/dm_checks.py new file mode 100644 index 0000000..f8ca616 --- /dev/null +++ b/litecord/blueprints/channel/dm_checks.py @@ -0,0 +1,26 @@ +from quart import current_app as app + +from litecord.errors import Forbidden +from litecord.enums import RelationshipType + + +class ForbiddenDM(Forbidden): + error_code = 50007 + + +async def dm_pre_check(user_id: int, channel_id: int, peer_id: int): + """Check if the user can DM the peer.""" + # first step is checking if there is a block in any direction + blockrow = await app.db.fetchrow(""" + SELECT rel_type + FROM relationships + WHERE rel_type = $3 + AND user_id IN ($1, $2) + AND peer_id IN ($1, $2) + """, user_id, peer_id, RelationshipType.BLOCK.value) + + if blockrow is not None: + raise ForbiddenDM() + + # TODO: check mutual guilds and guild settings for + # each user diff --git a/litecord/blueprints/channel/messages.py b/litecord/blueprints/channel/messages.py index 144c14c..683d4ea 100644 --- a/litecord/blueprints/channel/messages.py +++ b/litecord/blueprints/channel/messages.py @@ -14,6 +14,7 @@ from litecord.schemas import validate, MESSAGE_CREATE from litecord.utils import pg_set_json from litecord.embed.sanitizer import fill_embed, proxify, fetch_metadata +from litecord.blueprints.channel.dm_checks import dm_pre_check log = Logger(__name__) @@ -307,6 +308,10 @@ async def _create_message(channel_id): # TODO: check connection to the gateway + if ctype == ChannelType.DM: + # guild_id is the dm's peer_id + await dm_pre_check(user_id, channel_id, guild_id) + can_everyone = await channel_perm_check( user_id, channel_id, 'mention_everyone', False )