From 3432617988d35149bdfb3d1caf2eb639088bc1a6 Mon Sep 17 00:00:00 2001 From: Luna Mendes Date: Sun, 18 Nov 2018 02:38:30 -0300 Subject: [PATCH] channel.messages: handle at-everyone and at-here differently - blueprints.checks: return bool on *_perm_check --- litecord/blueprints/channel/messages.py | 35 ++++++++++++++++++------- litecord/blueprints/checks.py | 7 +++-- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/litecord/blueprints/channel/messages.py b/litecord/blueprints/channel/messages.py index b5cdaea..84b6b2e 100644 --- a/litecord/blueprints/channel/messages.py +++ b/litecord/blueprints/channel/messages.py @@ -151,11 +151,12 @@ async def create_message(channel_id): # TODO: check connection to the gateway - mentions_everyone = ('@everyone' in j['content'] and - await channel_perm_check( - user_id, channel_id, 'mention_everyone', False - ) - ) + can_everyone = await channel_perm_check( + user_id, channel_id, 'mention_everyone', False + ) + + mentions_everyone = ('@everyone' in j['content']) and can_everyone + mentions_here = ('@here' in j['content']) and can_everyone is_tts = (j.get('tts', False) and await channel_perm_check( @@ -174,7 +175,7 @@ async def create_message(channel_id): j['content'], is_tts, - mentions_everyone, + mentions_everyone or mentions_here, int(j.get('nonce', 0)), MessageType.DEFAULT.value @@ -206,9 +207,10 @@ async def create_message(channel_id): for member_id in member_ids: uids.add(member_id) - # if we're on an at-everyone / at-here, just update - # the read state for everyone. - if mentions_everyone: + # at-here only updates the state + # for the users that have a state + # in the channel. + if mentions_here: uids = [] await app.db.execute(""" UPDATE user_read_state @@ -216,6 +218,21 @@ async def create_message(channel_id): WHERE channel_id = $1 """, channel_id) + # at-here updates the read state + # for all users, including the ones + # that might not have read permissions + # to the channel. + if mentions_everyone: + uids = [] + + member_ids = await app.storage.get_member_ids(guild_id) + + await app.db.executemany(""" + UPDATE user_read_state + SET mention_count = mention_count + 1 + WHERE channel_id = $1 AND user_id = $2 + """, [(channel_id, uid) for uid in member_ids]) + for user_id in uids: await app.db.execute(""" UPDATE user_read_state diff --git a/litecord/blueprints/checks.py b/litecord/blueprints/checks.py index e051c11..be51b94 100644 --- a/litecord/blueprints/checks.py +++ b/litecord/blueprints/checks.py @@ -67,6 +67,8 @@ async def guild_perm_check(user_id, guild_id, permission: str): if not hasperm: raise MissingPermissions('Missing permissions.') + return bool(hasperm) + async def channel_perm_check(user_id, channel_id, permission: str, raise_err=True): @@ -74,10 +76,7 @@ async def channel_perm_check(user_id, channel_id, base_perms = await get_permissions(user_id, channel_id) hasperm = getattr(base_perms.bits, permission) - print(base_perms) - print(base_perms.binary) - if not hasperm and raise_err: raise MissingPermissions('Missing permissions.') - return hasperm + return bool(hasperm)