From e2ffaa119d9a686b52d7155e282fbd363d880527 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 2 Sep 2021 00:08:16 -0300 Subject: [PATCH] fix subscription state for new dms --- litecord/blueprints/dm_channels.py | 15 ++++++++++++--- litecord/blueprints/dms.py | 4 +++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/litecord/blueprints/dm_channels.py b/litecord/blueprints/dm_channels.py index 3a21829..041c288 100644 --- a/litecord/blueprints/dm_channels.py +++ b/litecord/blueprints/dm_channels.py @@ -17,6 +17,8 @@ along with this program. If not, see . """ +from typing import Iterable + from quart import Blueprint, current_app as app, jsonify from logbook import Logger @@ -55,6 +57,14 @@ async def _raw_gdm_remove(channel_id, user_id): ) +async def gdm_pubsub(channel_id: int, recipients: Iterable[int]): + for recipient_id in recipients: + await app.dispatcher.channel.sub_many( + channel_id, + [state.session_id for state in app.state_manager.user_states(recipient_id)], + ) + + async def gdm_create(user_id, peer_id) -> int: """Create a group dm, given two users. @@ -83,8 +93,7 @@ async def gdm_create(user_id, peer_id) -> int: await _raw_gdm_add(channel_id, user_id) await _raw_gdm_add(channel_id, peer_id) - await app.dispatcher.channel.sub(channel_id, user_id) - await app.dispatcher.channel.sub(channel_id, peer_id) + await gdm_pubsub(channel_id, (user_id, peer_id)) chan = await app.storage.get_channel(channel_id) await app.dispatcher.channel.dispatch(channel_id, ("CHANNEL_CREATE", chan)) @@ -108,7 +117,7 @@ async def gdm_add_recipient(channel_id: int, peer_id: int, *, user_id=None): await dispatch_user(peer_id, ("CHANNEL_CREATE", gdm_recipient_view(chan, peer_id))) await app.dispatcher.channel.dispatch(channel_id, ("CHANNEL_UPDATE", chan)) - await app.dispatcher.channel.sub(peer_id) + await gdm_pubsub(channel_id, (peer_id,)) if user_id: await send_sys_message(channel_id, MessageType.RECIPIENT_ADD, user_id, peer_id) diff --git a/litecord/blueprints/dms.py b/litecord/blueprints/dms.py index 1bf33a1..0e18e38 100644 --- a/litecord/blueprints/dms.py +++ b/litecord/blueprints/dms.py @@ -30,7 +30,7 @@ from ..enums import ChannelType from .auth import token_check -from litecord.blueprints.dm_channels import gdm_create, gdm_add_recipient +from litecord.blueprints.dm_channels import gdm_create, gdm_add_recipient, gdm_pubsub from litecord.common.channels import try_dm_state log = Logger(__name__) @@ -73,6 +73,7 @@ async def create_dm(user_id: int, recipient_id: int): ) if dm_id: + await gdm_pubsub(dm_id, (user_id, recipient_id)) return await jsonify_dm(dm_id, user_id) # if no dm was found, create a new one @@ -105,6 +106,7 @@ async def create_dm(user_id: int, recipient_id: int): # until the user sends a message. await try_dm_state(user_id, dm_id) + await gdm_pubsub(dm_id, (user_id, recipient_id)) return await jsonify_dm(dm_id, user_id)