def query_meta(self, identifier: ID) -> bool: now = time.time() last = self.__meta_queries.get(identifier, 0) if (now - last) < self.EXPIRES: return False self.__meta_queries[identifier] = now # query from DIM network cmd = MetaCommand(identifier=identifier) return self.__send_command(cmd=cmd)
def __get(self, identifier: ID) -> Content: # query meta for ID meta = self.facebook.meta(identifier=identifier) if meta is None: # meta not found text = 'Sorry, meta for %s not found.' % identifier return TextContent(text=text) # response return MetaCommand.response(identifier=identifier, meta=meta)
def __response_meta(self) -> MetaCommand: gid = self.__group.identifier meta = self.__group.meta profile = self.__group.profile if profile is None: cmd = MetaCommand.response(identifier=gid, meta=meta) else: cmd = ProfileCommand.response(identifier=gid, profile=profile, meta=meta) return cmd
def query_meta(self, identifier: str) -> (int, Optional[Content]): # check ID identifier = self.identifier(identifier) if identifier is None: return 400, None # Bad Request # get meta meta = self.meta(identifier) if meta is None: return 404, None # Not Found # OK return 200, MetaCommand.new(identifier=identifier, meta=meta)
def invite(self, invite_list: List[ID]) -> bool: """ Invite new members to this group (only existed member/assistant can do this) :param invite_list: new members ID list :return: True on success """ facebook = self.facebook owner = facebook.owner(self.group) assistants = facebook.assistants(self.group) members = facebook.members(self.group) assert assistants is not None, 'failed to get assistants for group: %s' % self.group # 0. send 'meta/profile' command to new members meta = facebook.meta(self.group) profile = facebook.document(identifier=self.group) if profile is None or profile.get('data') is None: cmd = MetaCommand.response(identifier=self.group, meta=meta) else: cmd = DocumentCommand.response(document=profile, meta=meta, identifier=self.group) self.__send_group_command(cmd=cmd, members=invite_list) # 1. send 'invite' command with new members to existed members cmd = GroupCommand.invite(group=self.group, members=invite_list) # 1.1. send to existed members self.__send_group_command(cmd=cmd, members=members) # 1.2. send to assistants self.__send_group_command(cmd=cmd, members=assistants) # 1.3. send to owner if owner is not None and owner not in members: self.__send_group_command(cmd=cmd, members=[owner]) # 2. update local storage self.add_members(invite_list) # 3. send 'invite' command with all members to new members members = facebook.members(self.group) cmd = GroupCommand.invite(group=self.group, members=members) self.__send_group_command(cmd=cmd, members=invite_list) return True
def query_meta(self, identifier: ID) -> bool: cmd = MetaCommand.new(identifier=identifier) return self.send_command(cmd=cmd)