From d1b10e74094369b1d84d57e33d7ffef4695c3ce9 Mon Sep 17 00:00:00 2001 From: Luna Date: Wed, 14 Jul 2021 23:41:19 -0300 Subject: [PATCH] fix new joins not subscribing to channels --- litecord/common/guilds.py | 7 ++++++- litecord/gateway/websocket.py | 16 ++-------------- litecord/pubsub/guild.py | 22 ++++++++++++++++++++-- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/litecord/common/guilds.py b/litecord/common/guilds.py index 4dd9e81..368ba07 100644 --- a/litecord/common/guilds.py +++ b/litecord/common/guilds.py @@ -329,7 +329,12 @@ async def add_member(guild_id: int, user_id: int, *, basic=False): # pubsub changes for new member await app.lazy_guild.new_member(guild_id, user_id) - states = await app.dispatcher.guild.sub_user(guild_id, user_id) + + # TODO how to remove repetition between this and websocket's subscribe_all? + states, channels = await app.dispatcher.guild.sub_user(guild_id, user_id) + for channel_id in channel_ids: + for state in states: + await app.dispatcher.channel.sub(channel_id, state.session_id) guild = await app.storage.get_guild_full(guild_id, user_id, 250) for state in states: diff --git a/litecord/gateway/websocket.py b/litecord/gateway/websocket.py index 05376f3..2c70120 100644 --- a/litecord/gateway/websocket.py +++ b/litecord/gateway/websocket.py @@ -506,20 +506,8 @@ class GatewayWebsocket: channel_ids: List[int] = [] for guild_id in guild_ids: - await app.dispatcher.guild.sub(guild_id, session_id) - - # instead of calculating which channels to subscribe to - # inside guild dispatcher, we calculate them in here, so that - # we remove complexity of the dispatcher. - - guild_chan_ids = await app.storage.get_channel_ids(guild_id) - for channel_id in guild_chan_ids: - perms = await get_permissions( - self.state.user_id, channel_id, storage=self.storage - ) - - if perms.bits.read_messages: - channel_ids.append(channel_id) + _, channels = await app.dispatcher.guild.sub_user(guild_id, session_id) + channel_ids.extend(channels) log.info("subscribing to {} guild channels", len(channel_ids)) for channel_id in channel_ids: diff --git a/litecord/pubsub/guild.py b/litecord/pubsub/guild.py index d7477a0..eeaea16 100644 --- a/litecord/pubsub/guild.py +++ b/litecord/pubsub/guild.py @@ -25,6 +25,8 @@ from logbook import Logger from .dispatcher import DispatcherWithState, GatewayEvent from litecord.gateway.state import GatewayState from litecord.enums import EVENTS_TO_INTENTS +from litecord.permissions import get_permissions + log = Logger(__name__) @@ -48,12 +50,28 @@ def can_dispatch(event_type, event_data, state) -> bool: class GuildDispatcher(DispatcherWithState[int, str, GatewayEvent, List[str]]): """Guild backend for Pub/Sub.""" - async def sub_user(self, guild_id: int, user_id: int) -> List[GatewayState]: + async def sub_user( + self, guild_id: int, user_id: int + ) -> Tuple[List[GatewayState], List[int]]: states = app.state_manager.fetch_states(user_id, guild_id) for state in states: await self.sub(guild_id, state.session_id) - return states + # instead of calculating which channels to subscribe to + # inside guild dispatcher, we calculate them in here, so that + # we remove complexity of the dispatcher. + + guild_chan_ids = await app.storage.get_channel_ids(guild_id) + channel_ids = [] + for channel_id in guild_chan_ids: + perms = await get_permissions( + self.state.user_id, channel_id, storage=self.storage + ) + + if perms.bits.read_messages: + channel_ids.append(channel_id) + + return states, channel_ids async def dispatch_filter( self, guild_id: int, filter_function, event: GatewayEvent