channel.messages: handle role mentions and at-everyone mentions

This commit is contained in:
Luna Mendes 2018-11-18 02:25:32 -03:00
parent 9db9c75602
commit 8492b6eb33
2 changed files with 36 additions and 3 deletions

View File

@ -191,15 +191,38 @@ async def create_message(channel_id):
'MESSAGE_CREATE', payload) 'MESSAGE_CREATE', payload)
if ctype == ChannelType.GUILD_TEXT: if ctype == ChannelType.GUILD_TEXT:
for mention in payload['mentions']: # calculate the user ids we'll bump the mention count for
uid = int(mention['id']) uids = set()
# first is extracting user mentions
for mention in payload['mentions']:
uids.add(int(mention['id']))
# then role mentions
for role_mention in payload['mention_roles']:
role_id = int(role_mention)
member_ids = await app.storage.get_role_members(role_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:
uids = []
await app.db.execute("""
UPDATE user_read_state
SET mention_count = mention_count + 1
WHERE channel_id = $1
""", channel_id)
for user_id in uids:
await app.db.execute(""" await app.db.execute("""
UPDATE user_read_state UPDATE user_read_state
SET mention_count = mention_count + 1 SET mention_count = mention_count + 1
WHERE user_id = $1 WHERE user_id = $1
AND channel_id = $2 AND channel_id = $2
""", uid, channel_id) """, user_id, channel_id)
return jsonify(payload) return jsonify(payload)

View File

@ -852,3 +852,13 @@ class Storage:
res.append(emoji) res.append(emoji)
return res return res
async def get_role_members(self, role_id: int) -> List[int]:
"""Get all members with a role."""
rows = await self.db.fetch("""
SELECT user_id
FROM member_roles
WHERE role_id = $1
""", role_id)
return [r['id'] for r in rows]