From 49ed61438ca7bc9460a14d4a144214444ac19977 Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 20 Jul 2019 13:05:47 -0300 Subject: [PATCH] pubsub: add indirection layer to access flags --- litecord/pubsub/channel.py | 5 +++++ litecord/pubsub/dispatcher.py | 7 +++++++ litecord/pubsub/guild.py | 4 ++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/litecord/pubsub/channel.py b/litecord/pubsub/channel.py index 5b3cf55..8ad47b5 100644 --- a/litecord/pubsub/channel.py +++ b/litecord/pubsub/channel.py @@ -84,6 +84,11 @@ class ChannelDispatcher(DispatcherWithFlags): await self.unsub(channel_id, user_id) continue + # skip typing events for users that don't want it + if event.startswith('TYPING_') and \ + not self.flags_get(channel_id, user_id, 'typing', True): + continue + cur_sess = [] if event in ('CHANNEL_CREATE', 'CHANNEL_UPDATE') \ diff --git a/litecord/pubsub/dispatcher.py b/litecord/pubsub/dispatcher.py index 7d40c07..493162a 100644 --- a/litecord/pubsub/dispatcher.py +++ b/litecord/pubsub/dispatcher.py @@ -150,3 +150,10 @@ class DispatcherWithFlags(DispatcherWithState): """Unsubscribe a user from the guild.""" await super().unsub(key, identifier) self.flags[key].pop(identifier) + + def flags_get(self, key, identifier, field: str, default): + """Get a single field from the flags store.""" + # yes, i know its simply an indirection from the main flags store, + # but i'd rather have this than change every call if i ever change + # the structure of the flags store. + return self.flags[key][identifier].get(field, default) diff --git a/litecord/pubsub/guild.py b/litecord/pubsub/guild.py index 7b56fcf..54d77e4 100644 --- a/litecord/pubsub/guild.py +++ b/litecord/pubsub/guild.py @@ -107,8 +107,8 @@ class GuildDispatcher(DispatcherWithFlags): # note that this does not equate to any unsubscription # of the channel. - flags = self.flags[guild_id][user_id] - if event.startswith('PRESENCE_') and not flags.get('presence', True): + if event.startswith('PRESENCE_') and \ + not self.flags_get(guild_id, user_id, 'presence', True): continue # filter the ones that matter