add gdm channel destruction

- channels: destroy pubsub channel when deleting a guild channel
This commit is contained in:
Luna 2019-02-19 18:54:20 -03:00
parent d9283c92df
commit 39c4fc991b
2 changed files with 47 additions and 6 deletions

View File

@ -31,6 +31,9 @@ from litecord.schemas import (
from litecord.blueprints.checks import channel_check, channel_perm_check from litecord.blueprints.checks import channel_check, channel_perm_check
from litecord.system_messages import send_sys_message from litecord.system_messages import send_sys_message
from litecord.blueprints.dm_channels import (
gdm_add_recipient, gdm_remove_recipient, gdm_destroy
)
log = Logger(__name__) log = Logger(__name__)
bp = Blueprint('channels', __name__) bp = Blueprint('channels', __name__)
@ -211,6 +214,8 @@ async def close_channel(channel_id):
await app.dispatcher.dispatch_guild( await app.dispatcher.dispatch_guild(
guild_id, 'CHANNEL_DELETE', chan) guild_id, 'CHANNEL_DELETE', chan)
await app.dispatcher.remove('channel', channel_id)
return jsonify(chan) return jsonify(chan)
if ctype == ChannelType.DM: if ctype == ChannelType.DM:
@ -234,8 +239,17 @@ async def close_channel(channel_id):
return jsonify(chan) return jsonify(chan)
if ctype == ChannelType.GROUP_DM: if ctype == ChannelType.GROUP_DM:
# TODO: group dm await gdm_remove_recipient(channel_id, user_id)
pass
gdm_count = await app.db.fetchval("""
SELECT COUNT(*)
FROM group_dm_members
WHERE id = $1
""", channel_id)
if gdm_count == 0:
# destroy dm
await gdm_destroy(channel_id)
raise ChannelNotFound() raise ChannelNotFound()

View File

@ -75,7 +75,7 @@ async def _gdm_create(user_id, peer_id) -> int:
return channel_id return channel_id
async def _gdm_add_recipient(channel_id: int, peer_id: int, *, user_id=None): async def gdm_add_recipient(channel_id: int, peer_id: int, *, user_id=None):
"""Add a recipient to a Group DM. """Add a recipient to a Group DM.
Dispatches: Dispatches:
@ -96,6 +96,7 @@ async def _gdm_add_recipient(channel_id: int, peer_id: int, *, user_id=None):
await app.dispatcher.sub('channel', peer_id) await app.dispatcher.sub('channel', peer_id)
# TODO: if not user id, userid=peerid
if user_id: if user_id:
await send_sys_message( await send_sys_message(
app, channel_id, MessageType.RECIPIENT_ADD, app, channel_id, MessageType.RECIPIENT_ADD,
@ -103,7 +104,7 @@ async def _gdm_add_recipient(channel_id: int, peer_id: int, *, user_id=None):
) )
async def _gdm_remove_recipient(channel_id: int, peer_id: int, *, user_id=None): async def gdm_remove_recipient(channel_id: int, peer_id: int, *, user_id=None):
"""Remove a member from a GDM. """Remove a member from a GDM.
Dispatches: Dispatches:
@ -134,6 +135,32 @@ async def _gdm_remove_recipient(channel_id: int, peer_id: int, *, user_id=None):
) )
async def gdm_destroy(channel_id):
"""Destroy a Group DM."""
chan = await app.storage.get_channel(channel_id)
await app.db.execute("""
DELETE FROM group_dm_members
WHERE id = $1
""", channel_id)
await app.db.execute("""
DELETE FROM group_dm_channels
WHERE id = $1
""", channel_id)
await app.db.execute("""
DELETE FROM channels
WHERE id = $1
""", channel_id)
await app.dispatcher.dispatch(
'channel', channel_id, 'CHANNEL_DELETE', chan
)
await app.dispatcher.remove('channel', channel_id)
@bp.route('/<int:dm_chan>/recipients/<int:peer_id>', methods=['PUT']) @bp.route('/<int:dm_chan>/recipients/<int:peer_id>', methods=['PUT'])
async def add_to_group_dm(dm_chan, peer_id): async def add_to_group_dm(dm_chan, peer_id):
"""Adds a member to a group dm OR creates a group dm.""" """Adds a member to a group dm OR creates a group dm."""
@ -160,7 +187,7 @@ async def add_to_group_dm(dm_chan, peer_id):
user_id, other_id user_id, other_id
) )
await _gdm_add_recipient(dm_chan, peer_id, user_id=user_id) await gdm_add_recipient(dm_chan, peer_id, user_id=user_id)
return jsonify( return jsonify(
await app.storage.get_channel(dm_chan) await app.storage.get_channel(dm_chan)
@ -178,5 +205,5 @@ async def remove_from_group_dm(dm_chan, peer_id):
if owner_id != user_id: if owner_id != user_id:
raise Forbidden('You are now the owner of the group DM') raise Forbidden('You are now the owner of the group DM')
await _gdm_remove_recipient(dm_chan, peer_id) await gdm_remove_recipient(dm_chan, peer_id)
return '', 204 return '', 204