From 8a76b5a40749a799a3f78405b44abcac01e13156 Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 16 Feb 2019 23:39:47 -0300 Subject: [PATCH] pubsub.channel: add gdm recipient view for chan create/update --- litecord/pubsub/channel.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/litecord/pubsub/channel.py b/litecord/pubsub/channel.py index ef23a6a..3f805b4 100644 --- a/litecord/pubsub/channel.py +++ b/litecord/pubsub/channel.py @@ -22,10 +22,32 @@ from typing import Any from logbook import Logger from .dispatcher import DispatcherWithState +from litecord.enums import ChannelType +from litecord.utils import index_by_func log = Logger(__name__) +def _recipient_view(orig: dict, user_id: int) -> dict: + """Create a copy of the original channel object that doesn't + show the user we are dispatching it to. + + this only applies to group dms and discords' api design that says + a group dms' recipients must not show the original user. + """ + # make a copy or the original channel object + data = dict(orig) + + idx = index_by_func( + lambda user: user['id'] == str(user_id), + data['recipients'] + ) + + data['recipients'].pop(idx) + + return data + + class ChannelDispatcher(DispatcherWithState): """Main channel Pub/Sub logic.""" KEY_TYPE = int @@ -62,7 +84,19 @@ class ChannelDispatcher(DispatcherWithState): await self.unsub(channel_id, user_id) continue - cur_sess = await self._dispatch_states(states, event, data) + cur_sess = 0 + + if event in ('CHANNEL_CREATE', 'CHANNEL_UPDATE') \ + and data.get('type') == ChannelType.GROUP_DM.value: + # we edit the channel payload so it doesn't show + # the user as a recipient + + new_data = _recipient_view(data, user_id) + cur_sess = await self._dispatch_states( + states, event, new_data) + else: + cur_sess = await self._dispatch_states( + states, event, data) sessions.extend(cur_sess) dispatched += len(cur_sess)