From e0d253f36f1ebead76fbead39eee479f41f527f9 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 1 Sep 2019 18:03:29 -0300 Subject: [PATCH] add message flags updating on suppress embeds --- litecord/blueprints/channels.py | 38 ++++++++++++++++++++++++++++++++- litecord/enums.py | 9 ++++++++ manage/cmd/users.py | 1 + 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/litecord/blueprints/channels.py b/litecord/blueprints/channels.py index 11c18a5..fa3310a 100644 --- a/litecord/blueprints/channels.py +++ b/litecord/blueprints/channels.py @@ -25,7 +25,7 @@ from quart import Blueprint, request, current_app as app, jsonify from logbook import Logger from litecord.auth import token_check -from litecord.enums import ChannelType, GUILD_CHANS, MessageType +from litecord.enums import ChannelType, GUILD_CHANS, MessageType, MessageFlags from litecord.errors import ChannelNotFound, Forbidden, BadRequest from litecord.schemas import ( validate, CHAN_UPDATE, CHAN_OVERWRITE, SEARCH_CHANNEL, GROUP_DM_UPDATE, @@ -616,6 +616,40 @@ async def _search_channel(channel_id): return jsonify(await search_result_from_list(rows)) +# NOTE that those functions stay here until some other +# route or code wants it. +async def _msg_set_flags(message_id: int, new_flags: int): + flags = await app.db.fetchval(""" + SELECT flags + FROM messages + WHERE id = $1 + """, message_id) + + flags |= new_flags + + await app.db.execute(""" + UPDATE messages + SET flags = $1 + WHERE id = $1 + """, flags.value, message_id) + + +async def _msg_unset_flags(message_id: int, unset_flags: int): + flags = await app.db.fetchval(""" + SELECT flags + FROM messages + WHERE id = $1 + """, message_id) + + flags &= ~unset_flags + + await app.db.execute(""" + UPDATE messages + SET flags = $1 + WHERE id = $1 + """, flags.value, message_id) + + @bp.route('//messages//suppress-embeds', methods=['POST']) async def suppress_embeds(channel_id: int, message_id: int): @@ -656,9 +690,11 @@ async def suppress_embeds(channel_id: int, message_id: int): if suppress and url_embeds: # delete all embeds then dispatch an update + await _msg_set_flags(message_id, MessageFlags.suppress_embeds) await msg_update_embeds(message, [], app.storage, app.dispatcher) elif not suppress and not url_embeds: # spawn process_url_embed to restore the embeds, if any + await _msg_unset_flags(message_id, MessageFlags.suppress_embeds) app.sched.spawn( process_url_embed( app.config, app.storage, app.dispatcher, app.session, diff --git a/litecord/enums.py b/litecord/enums.py index 98e036c..3facc43 100644 --- a/litecord/enums.py +++ b/litecord/enums.py @@ -164,6 +164,15 @@ class UserFlags(Flags): premium_early = 512 +class MessageFlags(Flags): + """Message flags.""" + none = 0 + + crossposted = 1 << 0 + is_crosspost = 1 << 1 + suppresss_embeds = 1 << 2 + + class StatusType(EasyEnum): """All statuses there can be in a presence.""" ONLINE = 'online' diff --git a/manage/cmd/users.py b/manage/cmd/users.py index 4d37757..d835c6a 100644 --- a/manage/cmd/users.py +++ b/manage/cmd/users.py @@ -30,6 +30,7 @@ async def find_user(username, discrim, ctx) -> int: WHERE username = $1 AND discriminator = $2 """, username, discrim) + async def set_user_staff(user_id, ctx): """Give a single user staff status.""" old_flags = await ctx.db.fetchval("""