From 3d96ccda35ba083e5c89b19e077cd75775874900 Mon Sep 17 00:00:00 2001 From: Luna Mendes Date: Fri, 12 Oct 2018 23:32:54 -0300 Subject: [PATCH] blueprints.users: dispatch USER_UPDATE to guilds and friends - dispatcher: add EventDispatcher.dispatch_many --- litecord/blueprints/users.py | 15 ++++++++++++++- litecord/dispatcher.py | 10 +++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/litecord/blueprints/users.py b/litecord/blueprints/users.py index f1b094f..169a98d 100644 --- a/litecord/blueprints/users.py +++ b/litecord/blueprints/users.py @@ -189,10 +189,23 @@ async def patch_me(): WHERE id = $2 """, new_hash, user_id) - # TODO: dispatch USER_UPDATE to guilds and users + user.pop('password_hash') await app.dispatcher.dispatch_user( user_id, 'USER_UPDATE', user) + public_user = await app.storage.get_user(user_id) + + guild_ids = await app.storage.get_user_guilds(user_id) + friend_ids = await app.storage.get_friend_ids(user_id) + + await app.dispatcher.dispatch_many( + 'guild', guild_ids, 'USER_UPDATE', public_user + ) + + await app.dispatcher.dispatch_many( + 'friend', friend_ids, 'USER_UPDATE', public_user + ) + return jsonify(user) diff --git a/litecord/dispatcher.py b/litecord/dispatcher.py index 6a042d6..6db0a59 100644 --- a/litecord/dispatcher.py +++ b/litecord/dispatcher.py @@ -1,5 +1,5 @@ import collections -from typing import Any +from typing import List, Any from logbook import Logger @@ -62,6 +62,14 @@ class EventDispatcher: key = backend.KEY_TYPE(key) return await backend.dispatch(key, *args, **kwargs) + async def dispatch_many(self, backend_str: str, + keys: List[Any], *args, **kwargs): + """Dispatch to multiple keys in a single backend.""" + log.info('MULTI DISPATCH: {!r}, {} keys', + backend_str, len(keys)) + for key in keys: + await self.dispatch(backend_str, key, *args, **kwargs) + async def reset(self, backend_str: str, key: Any): """Reset the bucket in the given backend.""" backend = self.backends[backend_str]