def execute(self, cmd: Command, msg: ReliableMessage) -> Optional[Content]: assert isinstance(cmd, SearchCommand), 'command error: %s' % cmd # message message = cmd.get('message') print('online users response: %s' % message) # users users = cmd.get('users') if users is not None: print(' users:', json_encode(users)) return None
def execute(self, cmd: Command, msg: ReliableMessage) -> Optional[Content]: assert isinstance(cmd, Command), 'command error: %s' % cmd # submit device token for APNs token = cmd.get('device_token') if token is not None: self.database.save_device_token(token=token, identifier=msg.sender) return ReceiptCommand(message='Token received')
def do_search(self, keywords: str): if self.client is None: self.info('login first') return cmd: Command = Command.new(command='search') cmd['keywords'] = keywords self.client.send_command(cmd=cmd)
def __search(self, keywords: list) -> Optional[Content]: results = self.database.search(keywords=keywords) users = list(results.keys()) response = Command.new(command='search') response['message'] = '%d user(s) found' % len(users) response['users'] = users response['results'] = results return response
def do_show(self, name: str): if self.client is None: self.info('login first') return if 'users' == name: cmd: Command = Command.new(command='users') self.client.send_command(cmd=cmd) else: self.info('I don\'t understand.')
def contacts_command(self, identifier: ID) -> Command: cmd = self.__contacts_commands.get(identifier) if cmd is None: path = self.__contacts_command_path(identifier=identifier) self.info('Loading stored contacts command from: %s' % path) dictionary = self.read_json(path=path) if dictionary is not None: cmd = Command(dictionary) self.__contacts_commands[identifier] = cmd return cmd
def execute(self, cmd: Command, msg: ReliableMessage) -> Optional[Content]: assert isinstance(cmd, SearchCommand), 'command error: %s' % cmd # keywords keywords = cmd.get('keywords') if keywords is None: return TextContent(text='Search command error') keywords = keywords.split(' ') # search in database results = self.database.search(keywords=keywords) users = list(results.keys()) return SearchCommand(users=users, results=results)
def __get(self, sender: ID) -> Content: stored: Command = self.database.block_command(identifier=sender) if stored is not None: # response the stored block command directly return stored else: # return TextContent.new(text='Sorry, block-list of %s not found.' % sender) # TODO: here should response an empty HistoryCommand: 'block' res = Command.new(command='block') res['list'] = [] return res
def __get(self, sender: ID) -> Content: stored: Command = self.database.mute_command(identifier=sender) if stored is not None: # response the stored mute command directly return stored else: # return TextContent.new(text='Sorry, mute-list of %s not found.' % sender) # TODO: here should response an empty HistoryCommand: 'mute' res = Command(command=MuteCommand.MUTE) res['list'] = [] return res
def execute(self, cmd: Command, msg: ReliableMessage) -> Optional[Content]: assert isinstance(cmd, DocumentCommand), 'command error: %s' % cmd identifier = cmd.identifier doc = cmd.document if doc is None: doc_type = cmd.get('doc_type') if doc_type is None: doc_type = '*' return self.__get(identifier=identifier, doc_type=doc_type) else: # check meta meta = cmd.meta return self.__put(identifier=identifier, meta=meta, document=doc)
def __process_old_report(self, cmd: Command, sender: ID) -> Optional[Content]: # compatible with v1.0 state = cmd.get('state') if state is not None: session = self.messenger.current_session(identifier=sender) if 'background' == state: session.active = False elif 'foreground' == state: # welcome back! self.receptionist.add_guest(identifier=session.identifier) session.active = True else: session.active = True return ReceiptCommand.new(message='Client state received')
def contacts_command(self, identifier: ID) -> Optional[Command]: # try from memory cache cmd = self.__contacts_commands.get(identifier) if cmd is None: # try from local storage path = self.__contacts_command_path(identifier=identifier) self.info('Loading stored contacts command from: %s' % path) dictionary = self.read_json(path=path) if dictionary is None: cmd = self.__empty else: cmd = Command(dictionary) self.__contacts_commands[identifier] = cmd if cmd is not self.__empty: return cmd
return self.__users @users.setter def users(self, value: List[ID]): if value is None: self.pop('users', None) else: self['users'] = ID.revert(members=value) self.__users = value # # User's meta map # @property def results(self) -> dict: return self.get('results') @results.setter def results(self, value: dict): if value is None: self.pop('results', None) else: self['results'] = value # register Command.register(command=SearchCommand.SEARCH, factory=CommandFactoryBuilder(command_class=SearchCommand)) Command.register(command=SearchCommand.ONLINE_USERS, factory=CommandFactoryBuilder(command_class=SearchCommand))
cmd: Optional[dict] = None, title: Optional[str] = None): if cmd is None: super().__init__(command=ReportCommand.REPORT) else: super().__init__(cmd=cmd) if title is not None: self['title'] = title # # report title # @property def title(self) -> str: return self.get('title') @title.setter def title(self, value: str): self['title'] = value # register Command.register(command=ReportCommand.REPORT, factory=CommandFactoryBuilder(command_class=ReportCommand)) Command.register(command='broadcast', factory=CommandFactoryBuilder(command_class=ReportCommand)) Command.register(command=ReportCommand.ONLINE, factory=CommandFactoryBuilder(command_class=ReportCommand)) Command.register(command=ReportCommand.OFFLINE, factory=CommandFactoryBuilder(command_class=ReportCommand))
Create search command :param content: command info :param keywords: search number, ID, or 'users' :param users: user ID list :param results: user meta map :return: SearchCommand object """ if content is None: # create empty content content = {} # new SearchCommand(dict) if keywords is None: command = cls.SEARCH elif keywords == cls.SEARCH or keywords == cls.ONLINE_USERS: command = keywords else: command = cls.SEARCH content['keywords'] = keywords if users is not None: content['users'] = users if results is not None: content['results'] = results return super().new(content=content, command=command) # register command class Command.register(command=SearchCommand.SEARCH, command_class=SearchCommand) Command.register(command=SearchCommand.ONLINE_USERS, command_class=SearchCommand)
def __random_users(self, max_count=20) -> Optional[Content]: users = self.session_server.random_users(max_count=max_count) response = Command.new(command='users') response['message'] = '%d user(s) connected' % len(users) response['users'] = users return response