def assert_is_linked(channel: TelegramChannel, slave_chats: Iterable[Chat], telegram_chat_id: int): master_str = chat_id_to_str(channel.channel_id, str(telegram_chat_id)) chats_str = set(channel.db.get_chat_assoc(master_uid=master_str)) slave_ids = {chat_id_to_str(chat=i) for i in slave_chats} # print("ASSERT_IS_LINKED", chats_str, slave_ids) assert chats_str == slave_ids, f"expecting {slave_ids} linked, found {chats_str}"
def test_chat_id_str_conversion(): channel_id = "__channel_id__" chat_id = "__chat_id__" group_id = "__group_id__" assert (channel_id, chat_id, None) == chat_id_str_to_id( chat_id_to_str( channel_id=channel_id, chat_uid=chat_id)), "Converting channel-chat ID without group ID" assert (channel_id, chat_id, group_id) == chat_id_str_to_id( chat_id_to_str( channel_id=channel_id, chat_uid=chat_id, group_id=group_id)), "Converting channel-chat ID with group ID"
def test_source_chat_pagination(channel, slave): storage_id = (TelegramChatID(0), TelegramMessageID(3)) source_chats = [utils.chat_id_to_str(chat=slave.group)] legends, buttons = channel.chat_binding.slave_chats_pagination(storage_id, source_chats=source_chats) legend = "\n".join(legends) assert slave.channel_emoji in legend assert slave.channel_name in legend assert len(buttons) == 2
def link_chats(channel: TelegramChannel, slave_chats: Iterable[Chat], telegram_chat_id: int): """Link a list of remote chats to a Telegram chat and revert the changes upon finishing. """ # Link the chats db = channel.db slave_ids = [chat_id_to_str(chat=i) for i in slave_chats] master_str = chat_id_to_str(channel.channel_id, str(telegram_chat_id)) backup = db.get_chat_assoc(master_uid=master_str) db.remove_chat_assoc(master_uid=master_str) for i in slave_ids: db.add_chat_assoc(master_str, i, multiple_slave=True) yield # Unlink the chats and revert back db.remove_chat_assoc(master_uid=master_str) for i in backup: db.add_chat_assoc(master_str, i, multiple_slave=True)
def test_chat_pagination(self): with self.subTest('Full channel pagination'): legend, buttons = self.master.chat_binding.slave_chats_pagination((0, 1)) self.assertIn(self.slave.channel_emoji, "\n".join(legend)) self.assertIn(self.slave.channel_name, "\n".join(legend)) self.assertEqual(min(self.master.flag("chats_per_page"), len(self.slave.get_chats())), len(buttons) - 1) with self.subTest('Pagination with filter'): legend, buttons = self.master.chat_binding.slave_chats_pagination((0, 2), pattern="wonderland") self.assertIn(self.slave.channel_emoji, "\n".join(legend)) self.assertIn(self.slave.channel_name, "\n".join(legend)) self.assertEqual(2, len(buttons)) with self.subTest('Pagination with list'): source_chats = [utils.chat_id_to_str(chat=self.slave.get_chat('wonderland001'))] legend, buttons = self.master.chat_binding.slave_chats_pagination((0, 3), source_chats=source_chats) self.assertIn(self.slave.channel_emoji, "\n".join(legend)) self.assertIn(self.slave.channel_name, "\n".join(legend)) self.assertEqual(2, len(buttons))
def unlink_all_chats(channel: TelegramChannel, telegram_chat_id: int): master_str = chat_id_to_str(channel.channel_id, str(telegram_chat_id)) channel.db.remove_chat_assoc(master_uid=master_str)
import os from efb_telegram_master import utils from ehforwarderbot import coordinator etm = coordinator.master ews = coordinator.slaves['blueset.wechat'] chats = ews.get_chats() to_link = [i.chat_uid for i in chats if i.vendor_specific.get('is_mp', False)] tg_grp_id = os.environ["TG_MP_GROUP_ID"] for i in to_link: etm.db.add_chat_assoc(master_uid=utils.chat_id_to_str( etm.channel_id, tg_grp_id), slave_uid=utils.chat_id_to_str(ews.channel_id, i), multiple_slave=True)