Merge branch 'support/embed-links' into 'master'

Embed Links permission support

Closes #49

See merge request litecord/litecord!43
This commit is contained in:
Luna 2019-07-23 19:34:42 +00:00
commit cf16a14e94
5 changed files with 40 additions and 28 deletions

View File

@ -32,6 +32,7 @@ from litecord.enums import MessageType, ChannelType, GUILD_CHANS
from litecord.snowflake import get_snowflake
from litecord.schemas import validate, MESSAGE_CREATE
from litecord.utils import pg_set_json
from litecord.permissions import get_permissions
from litecord.embed.sanitizer import fill_embed
from litecord.embed.messages import process_url_embed
@ -360,6 +361,14 @@ async def msg_add_attachment(message_id: int, channel_id: int,
return attachment_id
async def _spawn_embed(app_, payload, **kwargs):
app_.sched.spawn(
process_url_embed(
app_.config, app_.storage, app_.dispatcher, app_.session,
payload, **kwargs)
)
@bp.route('/<int:channel_id>/messages', methods=['POST'])
async def _create_message(channel_id):
"""Create a message."""
@ -424,12 +433,9 @@ async def _create_message(channel_id):
'MESSAGE_CREATE', payload)
# spawn url processor for embedding of images
app.sched.spawn(
process_url_embed(
app.config, app.storage, app.dispatcher, app.session,
payload
)
)
perms = await get_permissions(user_id, channel_id)
if perms.bits.embed_links:
await _spawn_embed(app, payload)
# update read state for the author
await app.db.execute("""
@ -487,17 +493,14 @@ async def edit_message(channel_id, message_id):
# the artificial delay keeps consistency between the events, since
# it makes more sense for the MESSAGE_UPDATE with new content to come
# BEFORE the MESSAGE_UPDATE with the new embeds (based on content)
app.sched.spawn(
process_url_embed(
app.config, app.storage, app.dispatcher, app.session,
{
perms = await get_permissions(user_id, channel_id)
if perms.bits.embed_links:
await _spawn_embed(app, {
'id': message_id,
'channel_id': channel_id,
'content': j['content'],
'embeds': old_message['embeds']
}, delay=0.2
)
)
}, delay=0.2)
# only set new timestamp upon actual update
if updated:

View File

@ -278,26 +278,25 @@ async def _mass_chan_update(guild_id, channel_ids: List[Optional[int]]):
async def _process_overwrites(channel_id: int, overwrites: list):
for overwrite in overwrites:
# 0 for user overwrite, 1 for role overwrite
target_type = 0 if overwrite['type'] == 'user' else 1
# 0 for member overwrite, 1 for role overwrite
target_type = 0 if overwrite['type'] == 'member' else 1
target_role = None if target_type == 0 else overwrite['id']
target_user = overwrite['id'] if target_type == 0 else None
col_name = 'target_user' if target_type == 0 else 'target_role'
constraint_name = f'channel_overwrites_{col_name}_uniq'
await app.db.execute(
"""
f"""
INSERT INTO channel_overwrites
(channel_id, target_type, target_role,
target_user, allow, deny)
VALUES
($1, $2, $3, $4, $5, $6)
ON CONFLICT ON CONSTRAINT channel_overwrites_uniq
ON CONFLICT ON CONSTRAINT {constraint_name}
DO
UPDATE
SET allow = $5, deny = $6
WHERE channel_overwrites.channel_id = $1
AND channel_overwrites.target_type = $2
AND channel_overwrites.target_role = $3
AND channel_overwrites.target_user = $4
""",
channel_id, target_type,
target_role, target_user,

View File

@ -156,7 +156,7 @@ async def process_url_embed(config, storage, dispatcher,
if not new_embeds:
return
log.debug('made {} thumbnail embeds for mid {}',
log.debug('made {} embeds for mid {}',
len(new_embeds), message_id)
await msg_update_embeds(payload, new_embeds, storage, dispatcher)

View File

@ -403,9 +403,9 @@ class Storage:
drow = dict(row)
target_type = drow['target_type']
drow['type'] = 'user' if target_type == 0 else 'role'
drow['type'] = 'member' if target_type == 0 else 'role'
# if type is 0, the overwrite is for a user
# if type is 0, the overwrite is for a member
# if type is 1, the overwrite is for a role
drow['id'] = {
0: drow['target_user'],

View File

@ -0,0 +1,10 @@
ALTER TABLE channel_overwrites
DROP CONSTRAINT IF EXISTS channel_overwrites_uniq;
ALTER TABLE channel_overwrites
ADD CONSTRAINT channel_overwrites_target_role_uniq
UNIQUE (channel_id, target_role);
ALTER TABLE channel_overwrites
ADD CONSTRAINT channel_overwrites_target_user_uniq
UNIQUE (channel_id, target_user);