Example #1
0
    def test_verify(self):
        channel = MockMasterChannel()

        with self.subTest("Valid chat"):
            chat = EFBChat(channel)
            chat.chat_uid = "00001"
            chat.chat_name = "Chat"
            chat.chat_alias = "chaT"
            chat.chat_type = ChatType.User
            chat.verify()

        with self.subTest("Missing UID"):
            chat = EFBChat(channel)
            chat.chat_name = "Chat"
            chat.chat_type = ChatType.User
            with self.assertRaises(ValueError):
                chat.verify()

        with self.subTest("Wrong chat type"):
            chat = EFBChat(channel)
            chat.chat_uid = "00001"
            chat.chat_name = "Chat"
            chat.chat_type = "user"
            with self.assertRaises(ValueError):
                chat.verify()
Example #2
0
 def __init__(self):
     alice = EFBChat(self)
     alice.chat_name = "Alice"
     alice.chat_uid = "alice"
     alice.chat_type = ChatType.User
     bob = EFBChat(self)
     bob.chat_name = "Bob"
     bob.chat_alias = "Little bobby"
     bob.chat_uid = "bob"
     bob.chat_type = ChatType.User
     carol = EFBChat(self)
     carol.chat_name = "Carol"
     carol.chat_uid = "carol"
     carol.chat_type = ChatType.User
     dave = EFBChat(self)
     dave.chat_name = "デブ"  # Nah, that's a joke
     dave.chat_uid = "dave"
     dave.chat_type = ChatType.User
     wonderland = EFBChat(self)
     wonderland.chat_name = "Wonderland"
     wonderland.chat_uid = "wonderland001"
     wonderland.chat_type = ChatType.Group
     wonderland.members = [bob.copy(), carol.copy(), dave.copy()]
     for i in wonderland.members:
         i.group = wonderland
         i.is_chat = False
     self.chats: List[EFBChat] = [alice, bob, wonderland]
Example #3
0
 def __init__(self, instance_id=None):
     super().__init__(instance_id)
     alice = EFBChat(self)
     alice.chat_name = "Alice"
     alice.chat_uid = "alice"
     alice.chat_type = ChatType.User
     self.alice = alice
     bob = EFBChat(self)
     bob.chat_name = "Bob"
     bob.chat_alias = "Little bobby"
     bob.chat_uid = "bob"
     bob.chat_type = ChatType.User
     self.bob = bob
     carol = EFBChat(self)
     carol.chat_name = "Carol"
     carol.chat_uid = "carol"
     carol.chat_type = ChatType.User
     self.carol = carol
     dave = EFBChat(self)
     dave.chat_name = "デブ"  # Nah, that's a joke
     dave.chat_uid = "dave"
     dave.chat_type = ChatType.User
     self.dave = dave
     wonderland = EFBChat(self)
     wonderland.chat_name = "Wonderland"
     wonderland.chat_uid = "wonderland001"
     wonderland.chat_type = ChatType.Group
     wonderland.members = [bob.copy(), carol.copy(), dave.copy()]
     for i in wonderland.members:
         i.group = wonderland
     self.wonderland = wonderland
     self.chats: List[EFBChat] = [alice, bob, wonderland]
Example #4
0
def test_verify_valid_chat(slave_channel):
    chat = EFBChat(slave_channel)
    chat.chat_uid = "00001"
    chat.chat_name = "Chat"
    chat.chat_alias = "chaT"
    chat.chat_type = ChatType.User
    chat.verify()
Example #5
0
def test_verify_wrong_chat_type(slave_channel):
    chat = EFBChat(slave_channel)
    chat.chat_uid = "00001"
    chat.chat_name = "Chat"
    chat.chat_type = "user"
    with pytest.raises(ValueError):
        chat.verify()
Example #6
0
def test_verify_valid_chat_middleware(middleware):
    chat = EFBChat(middleware=middleware)
    chat.chat_uid = "00001"
    chat.chat_name = "Chat"
    chat.chat_alias = "chaT"
    chat.chat_type = ChatType.User
    chat.verify()
Example #7
0
def test_copy(slave_channel):
    chat = EFBChat(slave_channel)
    chat.chat_uid = "00001"
    chat.chat_name = "Chat"
    chat.chat_alias = "chaT"
    chat.chat_type = ChatType.User
    copy = chat.copy()
    assert chat == copy
    assert chat is not copy
Example #8
0
 def test_copy(self):
     channel = MockMasterChannel()
     chat = EFBChat(channel)
     chat.chat_uid = "00001"
     chat.chat_name = "Chat"
     chat.chat_alias = "chaT"
     chat.chat_type = ChatType.User
     copy = chat.copy()
     assert chat == copy
     assert chat is not copy
Example #9
0
def test_properties(slave_channel):
    chat = EFBChat(channel=slave_channel)
    chat.chat_uid = "__test__"
    chat.chat_name = "Name"
    assert chat.display_name == chat.chat_name
    assert chat.long_name == chat.chat_name
    chat.chat_alias = "Alias"
    assert chat.display_name == chat.chat_alias
    assert chat.chat_name in chat.long_name
    assert chat.chat_alias in chat.long_name
Example #10
0
def test_pickle(slave_channel):
    chat = EFBChat(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)
Example #11
0
 def test_copy(self):
     channel = MockMasterChannel()
     chat = EFBChat(channel)
     chat.chat_uid = "00001"
     chat.chat_name = "Chat"
     chat.chat_alias = "chaT"
     chat.chat_type = ChatType.User
     copy = chat.copy()
     self.assertEqual(chat, copy)
     self.assertIsNot(chat, copy)
Example #12
0
 def build_efb_chat_as_anonymous_user(self, context):
     efb_chat = EFBChat(self.channel)
     anonymous_data = context['anonymous']
     efb_chat.chat_uid = 'anonymous' + '_' + anonymous_data['flag']
     efb_chat.chat_name = '[Anonymous] ' + anonymous_data['name']
     efb_chat.chat_type = ChatType.User
     efb_chat.is_chat = False
     efb_chat.vendor_specific = {
         'is_anonymous': True,
         'anonymous_id': anonymous_data['id']
     }
     efb_chat.group = self.build_efb_chat_as_group(context)
     return efb_chat
Example #13
0
 def build_efb_chat_as_group(self, context):
     efb_chat = EFBChat(self.channel)
     efb_chat.chat_type = ChatType.Group
     is_discuss = False if context['message_type'] == 'group' else True
     chat_uid = context['discuss_id'] if is_discuss else context['group_id']
     if not is_discuss:
         efb_chat.chat_uid = 'group' + '_' + str(chat_uid)
         i = self.channel.QQClient.get_group_info(chat_uid)
         if i is not None:
             efb_chat.chat_name = i[
                 'group_name'] if 'group_name' not in context else context[
                     'group_name']
         else:
             efb_chat.chat_name = chat_uid
         efb_chat.vendor_specific = {'is_discuss': False}
         # todo Add user to efb_chat.member
     else:
         efb_chat.chat_uid = 'discuss' + '_' + str(chat_uid)
         efb_chat.chat_name = 'Discuss Group' + '_' + str(chat_uid)
         # todo Find a way to distinguish from different discuss group
         efb_chat.vendor_specific = {'is_discuss': True}
     return efb_chat
    def wxpy_chat_to_efb_chat(self,
                              chat: wxpy.Chat,
                              recursive=True) -> 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
        if chat.puid in self.efb_chat_objs:
            return self.efb_chat_objs[chat.puid]
        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}
        if isinstance(chat, wxpy.Member):
            efb_chat.chat_type = ChatType.User
            efb_chat.is_chat = False
            efb_chat.chat_alias = chat.name
            # 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)

        efb_chat.vendor_specific.update(self.generate_vendor_specific(chat))
        if efb_chat.vendor_specific.get('is_muted', False):
            efb_chat.notification = EFBChatNotificationState.MENTIONS

        # self.logger.debug('WXPY chat %s converted to EFBChat %s', chat.puid, efb_chat)

        if chat.puid:
            self.efb_chat_objs[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
Example #16
0
 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
Example #17
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
Example #18
0
 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
Example #19
0
File: chats.py Project: Z0-0Z/chat
    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
Example #20
0
import pickle
from tempfile import NamedTemporaryFile
from unittest import mock

import pytest

from ehforwarderbot import EFBMsg, EFBChat, ChatType, MsgType, coordinator
from ehforwarderbot.message import EFBMsgLinkAttribute, EFBMsgLocationAttribute, EFBMsgStatusAttribute, EFBMsgCommands, \
    EFBMsgCommand
from .mocks.master import MockMasterChannel

coordinator.master = MockMasterChannel()
chat = EFBChat(channel=coordinator.master)
chat.chat_name = "Chat 0"
chat.chat_uid = "0"
chat.chat_type = ChatType.User

media_types = (MsgType.Image, MsgType.Audio, MsgType.File, MsgType.Sticker)


def test_verify_text_msg():
    msg = EFBMsg()
    msg.deliver_to = coordinator.master
    msg.author = chat
    msg.chat = chat
    msg.type = MsgType.Text
    msg.text = "Message"
    msg.verify()


@pytest.mark.parametrize("media_type", media_types, ids=str)