pubsub.channel: add gdm recipient view for chan create/update

This commit is contained in:
Luna 2019-02-16 23:39:47 -03:00
parent 24cc9e1d25
commit 8a76b5a407
1 changed files with 35 additions and 1 deletions

View File

@ -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)