channel.messages: handle at-everyone and at-here differently

- blueprints.checks: return bool on *_perm_check
This commit is contained in:
Luna Mendes 2018-11-18 02:38:30 -03:00
parent 8492b6eb33
commit 3432617988
2 changed files with 29 additions and 13 deletions

View File

@ -151,11 +151,12 @@ async def create_message(channel_id):
# TODO: check connection to the gateway # TODO: check connection to the gateway
mentions_everyone = ('@everyone' in j['content'] and can_everyone = await channel_perm_check(
await channel_perm_check(
user_id, channel_id, 'mention_everyone', False 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 is_tts = (j.get('tts', False) and
await channel_perm_check( await channel_perm_check(
@ -174,7 +175,7 @@ async def create_message(channel_id):
j['content'], j['content'],
is_tts, is_tts,
mentions_everyone, mentions_everyone or mentions_here,
int(j.get('nonce', 0)), int(j.get('nonce', 0)),
MessageType.DEFAULT.value MessageType.DEFAULT.value
@ -206,9 +207,10 @@ async def create_message(channel_id):
for member_id in member_ids: for member_id in member_ids:
uids.add(member_id) uids.add(member_id)
# if we're on an at-everyone / at-here, just update # at-here only updates the state
# the read state for everyone. # for the users that have a state
if mentions_everyone: # in the channel.
if mentions_here:
uids = [] uids = []
await app.db.execute(""" await app.db.execute("""
UPDATE user_read_state UPDATE user_read_state
@ -216,6 +218,21 @@ async def create_message(channel_id):
WHERE channel_id = $1 WHERE channel_id = $1
""", channel_id) """, 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: for user_id in uids:
await app.db.execute(""" await app.db.execute("""
UPDATE user_read_state UPDATE user_read_state

View File

@ -67,6 +67,8 @@ async def guild_perm_check(user_id, guild_id, permission: str):
if not hasperm: if not hasperm:
raise MissingPermissions('Missing permissions.') raise MissingPermissions('Missing permissions.')
return bool(hasperm)
async def channel_perm_check(user_id, channel_id, async def channel_perm_check(user_id, channel_id,
permission: str, raise_err=True): 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) base_perms = await get_permissions(user_id, channel_id)
hasperm = getattr(base_perms.bits, permission) hasperm = getattr(base_perms.bits, permission)
print(base_perms)
print(base_perms.binary)
if not hasperm and raise_err: if not hasperm and raise_err:
raise MissingPermissions('Missing permissions.') raise MissingPermissions('Missing permissions.')
return hasperm return bool(hasperm)