mirror of https://gitlab.com/litecord/litecord.git
Merge branch 'message-flags' into 'master'
add message flags Closes #57 See merge request litecord/litecord!46
This commit is contained in:
commit
96591c1f20
|
|
@ -25,7 +25,7 @@ from quart import Blueprint, request, current_app as app, jsonify
|
||||||
from logbook import Logger
|
from logbook import Logger
|
||||||
|
|
||||||
from litecord.auth import token_check
|
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.errors import ChannelNotFound, Forbidden, BadRequest
|
||||||
from litecord.schemas import (
|
from litecord.schemas import (
|
||||||
validate, CHAN_UPDATE, CHAN_OVERWRITE, SEARCH_CHANNEL, GROUP_DM_UPDATE,
|
validate, CHAN_UPDATE, CHAN_OVERWRITE, SEARCH_CHANNEL, GROUP_DM_UPDATE,
|
||||||
|
|
@ -615,12 +615,43 @@ 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
|
||||||
|
# route or code wants it.
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
FROM messages
|
||||||
|
WHERE id = $1
|
||||||
|
""", message_id)
|
||||||
|
|
||||||
|
|
||||||
|
async def _msg_set_flags(message_id: int, new_flags: int):
|
||||||
|
flags = await _msg_get_flags(message_id)
|
||||||
|
flags |= new_flags
|
||||||
|
await _msg_update_flags(message_id, flags)
|
||||||
|
|
||||||
|
|
||||||
|
async def _msg_unset_flags(message_id: int, unset_flags: int):
|
||||||
|
flags = await _msg_get_flags(message_id)
|
||||||
|
flags &= ~unset_flags
|
||||||
|
await _msg_update_flags(message_id, flags)
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/<int:channel_id>/messages/<int:message_id>/suppress-embeds',
|
@bp.route('/<int:channel_id>/messages/<int:message_id>/suppress-embeds',
|
||||||
methods=['POST'])
|
methods=['POST'])
|
||||||
async def suppress_embeds(channel_id: int, message_id: int):
|
async def suppress_embeds(channel_id: int, message_id: int):
|
||||||
"""Toggle the embeds in a message.
|
"""Toggle the embeds in a message.
|
||||||
|
|
||||||
Either the author of the message or a channel member with the
|
Either the author of the message or a channel member with the
|
||||||
Manage Messages permission can run this route.
|
Manage Messages permission can run this route.
|
||||||
"""
|
"""
|
||||||
|
|
@ -654,11 +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)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
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,
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -164,6 +164,15 @@ class UserFlags(Flags):
|
||||||
premium_early = 512
|
premium_early = 512
|
||||||
|
|
||||||
|
|
||||||
|
class MessageFlags(Flags):
|
||||||
|
"""Message flags."""
|
||||||
|
none = 0
|
||||||
|
|
||||||
|
crossposted = 1 << 0
|
||||||
|
is_crosspost = 1 << 1
|
||||||
|
suppress_embeds = 1 << 2
|
||||||
|
|
||||||
|
|
||||||
class StatusType(EasyEnum):
|
class StatusType(EasyEnum):
|
||||||
"""All statuses there can be in a presence."""
|
"""All statuses there can be in a presence."""
|
||||||
ONLINE = 'online'
|
ONLINE = 'online'
|
||||||
|
|
|
||||||
|
|
@ -876,7 +876,7 @@ class Storage:
|
||||||
row = await self.fetchrow_with_json("""
|
row = await self.fetchrow_with_json("""
|
||||||
SELECT id::text, channel_id::text, author_id, content,
|
SELECT id::text, channel_id::text, author_id, content,
|
||||||
created_at AS timestamp, edited_at AS edited_timestamp,
|
created_at AS timestamp, edited_at AS edited_timestamp,
|
||||||
tts, mention_everyone, nonce, message_type, embeds
|
tts, mention_everyone, nonce, message_type, embeds, flags
|
||||||
FROM messages
|
FROM messages
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
""", message_id)
|
""", message_id)
|
||||||
|
|
@ -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
|
||||||
res.pop('member')
|
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')
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
pin_id = await self.db.fetchval("""
|
pin_id = await self.db.fetchval("""
|
||||||
SELECT message_id
|
SELECT message_id
|
||||||
|
|
@ -961,6 +968,9 @@ class Storage:
|
||||||
if guild_id:
|
if guild_id:
|
||||||
res['guild_id'] = str(guild_id)
|
res['guild_id'] = str(guild_id)
|
||||||
|
|
||||||
|
if res['flags'] == 0:
|
||||||
|
res.pop('flags')
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
async def get_invite(self, invite_code: str) -> Optional[Dict]:
|
async def get_invite(self, invite_code: str) -> Optional[Dict]:
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
ALTER TABLE messages
|
||||||
|
ADD COLUMN flags bigint DEFAULT 0;
|
||||||
|
|
@ -30,6 +30,7 @@ async def find_user(username, discrim, ctx) -> int:
|
||||||
WHERE username = $1 AND discriminator = $2
|
WHERE username = $1 AND discriminator = $2
|
||||||
""", username, discrim)
|
""", username, discrim)
|
||||||
|
|
||||||
|
|
||||||
async def set_user_staff(user_id, ctx):
|
async def set_user_staff(user_id, ctx):
|
||||||
"""Give a single user staff status."""
|
"""Give a single user staff status."""
|
||||||
old_flags = await ctx.db.fetchval("""
|
old_flags = await ctx.db.fetchval("""
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue