channels: update local message var when changing flags

- embed.messages: propagate payload.flags when updating msg embeds
 - enums: fix typo
 - channels: fix flag helper functions
 - storage: only fill res.member when user_id is given
 - storage: sentinel value is 0 instead of none for flags removal
This commit is contained in:
Luna 2019-09-01 19:18:54 -03:00
parent e0d253f36f
commit ef6361dbda
5 changed files with 49 additions and 28 deletions

View File

@ -615,39 +615,36 @@ async def _search_channel(channel_id):
return jsonify(await search_result_from_list(rows)) return jsonify(await search_result_from_list(rows))
# NOTE that those functions stay here until some other # NOTE that those functions stay here until some other
# route or code wants it. # route or code wants it.
async def _msg_set_flags(message_id: int, new_flags: int):
flags = await app.db.fetchval("""
async def _msg_update_flags(message_id: int, flags: int):
await app.db.execute("""
UPDATE messages
SET flags = $1
WHERE id = $2
""", flags, message_id)
async def _msg_get_flags(message_id: int):
return await app.db.fetchval("""
SELECT flags SELECT flags
FROM messages FROM messages
WHERE id = $1 WHERE id = $1
""", message_id) """, message_id)
flags |= new_flags
await app.db.execute(""" async def _msg_set_flags(message_id: int, new_flags: int):
UPDATE messages flags = await _msg_get_flags(message_id)
SET flags = $1 flags |= new_flags
WHERE id = $1 await _msg_update_flags(message_id, flags)
""", flags.value, message_id)
async def _msg_unset_flags(message_id: int, unset_flags: int): async def _msg_unset_flags(message_id: int, unset_flags: int):
flags = await app.db.fetchval(""" flags = await _msg_get_flags(message_id)
SELECT flags
FROM messages
WHERE id = $1
""", message_id)
flags &= ~unset_flags flags &= ~unset_flags
await _msg_update_flags(message_id, flags)
await app.db.execute("""
UPDATE messages
SET flags = $1
WHERE id = $1
""", flags.value, message_id)
@bp.route('/<int:channel_id>/messages/<int:message_id>/suppress-embeds', @bp.route('/<int:channel_id>/messages/<int:message_id>/suppress-embeds',
@ -688,13 +685,27 @@ async def suppress_embeds(channel_id: int, message_id: int):
url_embeds = sum( url_embeds = sum(
1 for embed in message['embeds'] if embed['type'] == 'url') 1 for embed in message['embeds'] if embed['type'] == 'url')
# NOTE for any future self. discord doing flags an optional thing instead
# of just giving 0 is a pretty bad idea because now i have to deal with
# that behavior here, and likely in every other message update thing
if suppress and url_embeds: if suppress and url_embeds:
# delete all embeds then dispatch an update # delete all embeds then dispatch an update
await _msg_set_flags(message_id, MessageFlags.suppress_embeds) await _msg_set_flags(message_id, MessageFlags.suppress_embeds)
message['flags'] = \
message.get('flags', 0) | MessageFlags.suppress_embeds
await msg_update_embeds(message, [], app.storage, app.dispatcher) await msg_update_embeds(message, [], app.storage, app.dispatcher)
elif not suppress and not url_embeds: elif not suppress and not url_embeds:
# spawn process_url_embed to restore the embeds, if any # spawn process_url_embed to restore the embeds, if any
await _msg_unset_flags(message_id, MessageFlags.suppress_embeds) await _msg_unset_flags(message_id, MessageFlags.suppress_embeds)
try:
message.pop('flags')
except KeyError:
pass
app.sched.spawn( app.sched.spawn(
process_url_embed( process_url_embed(
app.config, app.storage, app.dispatcher, app.session, app.config, app.storage, app.dispatcher, app.session,

View File

@ -82,6 +82,9 @@ async def msg_update_embeds(payload, new_embeds, storage, dispatcher):
if 'guild_id' in payload: if 'guild_id' in payload:
update_payload['guild_id'] = payload['guild_id'] update_payload['guild_id'] = payload['guild_id']
if 'flags' in payload:
update_payload['flags'] = payload['flags']
await dispatcher.dispatch( await dispatcher.dispatch(
'channel', channel_id, 'MESSAGE_UPDATE', update_payload) 'channel', channel_id, 'MESSAGE_UPDATE', update_payload)

View File

@ -170,7 +170,7 @@ class MessageFlags(Flags):
crossposted = 1 << 0 crossposted = 1 << 0
is_crosspost = 1 << 1 is_crosspost = 1 << 1
suppresss_embeds = 1 << 2 suppress_embeds = 1 << 2
class StatusType(EasyEnum): class StatusType(EasyEnum):

View File

@ -942,10 +942,17 @@ class Storage:
# if message is not from a dm, guild_id is None and so, _member_basic # if message is not from a dm, guild_id is None and so, _member_basic
# will just return None # will just return None
res['member'] = await self._member_basic_with_roles(guild_id, user_id)
if res['member'] is None: # user id can be none, though, and we need to watch out for that
if user_id is not None:
res['member'] = await self._member_basic_with_roles(
guild_id, user_id)
if res.get('member') is None:
try:
res.pop('member') res.pop('member')
except KeyError:
pass
pin_id = await self.db.fetchval(""" pin_id = await self.db.fetchval("""
SELECT message_id SELECT message_id
@ -961,7 +968,7 @@ class Storage:
if guild_id: if guild_id:
res['guild_id'] = str(guild_id) res['guild_id'] = str(guild_id)
if res['flags'] is None: if res['flags'] == 0:
res.pop('flags') res.pop('flags')
return res return res

View File

@ -1,2 +1,2 @@
ALTER TABLE messages ALTER TABLE messages
ADD COLUMN flags bigint DEFAULT NULL; ADD COLUMN flags bigint DEFAULT 0;