Created
November 22, 2016 21:02
-
-
Save EliAndrewC/cbfdb7a259916ac9493c18f36360e44e to your computer and use it in GitHub Desktop.
Example of customized subscriptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
from sideboard.lib import notify, listify, threadlocal | |
__all__ = ['get', 'send'] | |
ecards = defaultdict(list) | |
class user_subscribes(object): | |
def __init__(self, func): | |
self.func = func | |
@property | |
def subscribes(self): | |
params = listify(threadlocal.get('message', {}).get('params', [])) | |
if isinstance(params, dict): | |
return ['ecards.' + params['username']] | |
else: | |
return ['ecards.' + params[0]] | |
def __call__(self, *args, **kwargs): | |
return self.func(*args, **kwargs) | |
@user_subscribes | |
def get(username): | |
return ecards[username] | |
def send(username, message): | |
ecards[username].append(message) | |
notify('ecards.' + username) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment