Exemple #1
0
def test_pickle(slave_channel):
    chat = EFBChat(channel=slave_channel)
    chat.chat_uid = "00001"
    chat.chat_name = "Chat"
    chat.chat_alias = "chaT"
    chat.chat_type = ChatType.User
    chat_dup = pickle.loads(pickle.dumps(chat))
    for attr in ("module_name", "module_id", "channel_emoji", "chat_name",
                 "chat_type", "chat_alias", "chat_uid", "is_chat"):
        assert getattr(chat, attr) == getattr(chat_dup, attr)
Exemple #2
0
    def wxpy_chat_to_efb_chat(self, chat: wxpy.Chat, recursive=True) -> Optional[EFBChat]:
        self.logger.debug("Converting WXPY chat %r, %sin recursive mode", chat, '' if recursive else 'not ')
        self.logger.debug("WXPY chat with ID: %s, name: %s, alias: %s;", chat.puid, chat.nick_name, chat.alias)
        if chat is None:
            return self.MISSING_USER
        efb_chat = EFBChat(self.channel)
        efb_chat.chat_uid = chat.puid or "__invalid__"
        efb_chat.chat_name = ews_utils.wechat_string_unescape(chat.nick_name)
        efb_chat.chat_alias = None
        efb_chat.chat_type = ChatType.System
        efb_chat.vendor_specific = {'is_mp': False,
                                    'wxpy_object': chat}
        if isinstance(chat, wxpy.Member):
            efb_chat.chat_type = ChatType.User
            efb_chat.is_chat = False
            efb_chat.chat_alias = chat.display_name or efb_chat.chat_alias
            self.logger.debug("[WXPY: %s] Display name: %s;", chat.puid, chat.display_name)
            if recursive:
                efb_chat.group = self.wxpy_chat_to_efb_chat(chat.group, False)
        elif isinstance(chat, wxpy.Group):
            efb_chat.chat_type = ChatType.Group
            for i in chat.members:
                efb_chat.members.append(self.wxpy_chat_to_efb_chat(i, False))
                efb_chat.members[-1].group = efb_chat
        elif isinstance(chat, wxpy.MP):
            efb_chat.chat_type = ChatType.User
            efb_chat.vendor_specific['is_mp'] = True
        elif isinstance(chat, wxpy.User):
            efb_chat.chat_type = ChatType.User
            efb_chat.chat_alias = chat.remark_name or efb_chat.chat_alias
            self.logger.debug("[WXPY: %s] Remark name: %s;", chat.puid, chat.remark_name)
        if chat == chat.bot.self:
            efb_chat.self()

        efb_chat.chat_alias = efb_chat.chat_alias and ews_utils.wechat_string_unescape(efb_chat.chat_alias)

        self.logger.debug('WXPY chat %s converted to EFBChat %s', chat.puid, efb_chat)
        return efb_chat
 def get_chat_from_db(self, channel_id: str,
                      chat_id: str) -> Optional[EFBChat]:
     d = self.db.get_slave_chat_info(slave_channel_id=channel_id,
                                     slave_chat_uid=chat_id)
     if d:
         chat = EFBChat(coordinator.slaves[channel_id])
         chat.chat_name = d.slave_chat_name
         chat.chat_alias = d.slave_chat_alias
         chat.chat_uid = d.slave_chat_uid
         chat.chat_type = ChatType(d.slave_chat_type)
         return chat
     else:
         chat = coordinator.slaves[channel_id].get_chat(chat_id)
         if chat:
             self._db_update_slave_chats_cache([chat])
             return chat
 def get_efb_chats(self):
     chat_list = requests.get(user_url % (self.hostname, self.port)).json()
     result = []
     for chat_item in chat_list:
         efb_chat = EFBChat(self.channel)
         efb_chat.chat_uid = chat_item["userId"]
         title = chat_item["title"]
         if title.startswith("[Group] "):
             efb_chat.chat_name = title[8:]
             efb_chat.chat_type = ChatType.Group
         else:
             efb_chat.chat_name = title
             efb_chat.chat_type = ChatType.User
         efb_chat.chat_alias = None
         efb_chat.is_chat = True
         efb_chat.vendor_specific = {'is_anonymous': False}
         result.append(efb_chat)
     return result
Exemple #5
0
 def build_efb_chat_as_user(self, context, is_chat):
     efb_chat = EFBChat(self.channel)
     uid = context['user_id']
     efb_chat.chat_uid = 'private' + '_' + str(uid)
     chat_name = ''
     if 'nickname' not in context:
         i: dict = self.channel.QQClient.get_stranger_info(uid)
         chat_name = i['nickname']
     else:
         chat_name = context['nickname']
     efb_chat.chat_name = chat_name
     efb_chat.chat_alias = None if 'alias' not in context else context[
         'alias']
     efb_chat.chat_type = ChatType.User
     efb_chat.is_chat = is_chat
     efb_chat.vendor_specific = {'is_anonymous': False}
     if not is_chat and context['message_type'] != 'private':
         efb_chat.group = self.build_efb_chat_as_group(context)
     return efb_chat
 def get_chat_from_db(self, channel_id: ModuleID,
                      chat_id: ChatID) -> Optional[EFBChat]:
     if channel_id not in coordinator.slaves:
         return None
     d = self.db.get_slave_chat_info(slave_channel_id=channel_id,
                                     slave_chat_uid=chat_id)
     if d:
         chat = EFBChat(coordinator.slaves[channel_id])
         chat.chat_name = d.slave_chat_name
         chat.chat_alias = d.slave_chat_alias
         chat.chat_uid = d.slave_chat_uid
         chat.chat_type = ChatType(d.slave_chat_type)
         return chat
     else:
         try:
             chat = coordinator.slaves[channel_id].get_chat(chat_id)
             if chat:
                 self._db_update_slave_chats_cache([chat])
                 return chat
             return None
         except EFBChatNotFound:
             return None