|
22 | 22 | from .message import ETMMsg |
23 | 23 | from .msg_type import TGMsgType |
24 | 24 | from .utils import TelegramChatID, EFBChannelChatIDStr, TgChatMsgIDStr, message_id_to_str, \ |
25 | | - chat_id_to_str, OldMsgID, chat_id_str_to_id, TelegramMessageID |
| 25 | + chat_id_to_str, OldMsgID, chat_id_str_to_id, TelegramMessageID, TelegramTopicID |
26 | 26 |
|
27 | 27 | if TYPE_CHECKING: |
28 | 28 | from . import TelegramChannel |
@@ -72,8 +72,6 @@ class MsgLog(BaseModel): |
72 | 72 | """Editable message ID from Telegram if ``master_msg_id`` is not editable |
73 | 73 | and a separate one is sent. |
74 | 74 | """ |
75 | | - master_message_thread_id = TextField(null=True) |
76 | | - """Message thread ID from Telegram""" |
77 | 75 | slave_message_id = TextField() |
78 | 76 | """Message from slave channel.""" |
79 | 77 | text = TextField() |
@@ -204,8 +202,6 @@ def __init__(self, channel: 'TelegramChannel'): |
204 | 202 | self._migrate(2) |
205 | 203 | elif "file_unique_id" not in msg_log_columns: |
206 | 204 | self._migrate(3) |
207 | | - elif "master_message_thread_id" not in msg_log_columns: |
208 | | - self._migrate(4) |
209 | 205 | self.logger.debug("Database migration finished...") |
210 | 206 |
|
211 | 207 | def stop_worker(self): |
@@ -256,12 +252,6 @@ def _migrate(i: int): |
256 | 252 | migrate( |
257 | 253 | migrator.add_column("msglog", "file_unique_id", MsgLog.file_unique_id) |
258 | 254 | ) |
259 | | - if i <= 4: |
260 | | - # Migration 4: Add column for message thread ID to message log table |
261 | | - # 2025APR12 |
262 | | - migrate( |
263 | | - migrator.add_column("msglog", "master_message_thread_id", MsgLog.master_message_thread_id) |
264 | | - ) |
265 | 255 |
|
266 | 256 | def add_chat_assoc(self, master_uid: EFBChannelChatIDStr, |
267 | 257 | slave_uid: EFBChannelChatIDStr, |
@@ -389,80 +379,111 @@ def get_chat_assoc(master_uid: Optional[EFBChannelChatIDStr] = None, |
389 | 379 | except DoesNotExist: |
390 | 380 | return [] |
391 | 381 |
|
392 | | - def add_topic_assoc(self, message_thread_id: EFBChannelChatIDStr, |
393 | | - slave_uid: EFBChannelChatIDStr, |
394 | | - topic_chat_id: int): |
| 382 | + def add_topic_assoc(self, topic_chat_id: TelegramChatID, |
| 383 | + message_thread_id: EFBChannelChatIDStr, |
| 384 | + slave_uid: EFBChannelChatIDStr, ): |
395 | 385 | """ |
396 | 386 | Add topic associations (topic links). |
397 | 387 | One Master channel with many Slave channel. |
398 | 388 |
|
399 | 389 | Args: |
400 | | - message_thread_id (str): thread UID in topic |
401 | | - slave_uid (str): Slave channel UID ("%(channel_id)s.%(chat_id)s") |
| 390 | + topic_chat_id (TelegramChatID): The topic group chat ID |
| 391 | + message_thread_id (EFBChannelChatIDStr): The topic thread ID |
| 392 | + slave_uid (EFBChannelChatIDStr): Slave channel UID ("%(channel_id)s.%(chat_id)s") |
402 | 393 | """ |
403 | 394 | return TopicAssoc.create(topic_chat_id=topic_chat_id, message_thread_id=message_thread_id, slave_uid=slave_uid) |
404 | 395 |
|
405 | 396 | @staticmethod |
406 | | - def get_topic_thread_id(topic_chat_id: int, |
407 | | - slave_uid: Optional[EFBChannelChatIDStr] |
408 | | - ) -> int: |
| 397 | + def get_topic_thread_id(slave_uid: EFBChannelChatIDStr) -> TelegramTopicID: |
409 | 398 | """ |
410 | 399 | Get topic association (topic link) information. |
411 | 400 | Only one parameter is to be provided. |
412 | 401 |
|
413 | 402 | Args: |
414 | | - topic_chat_id (int): The topic UID |
415 | | - slave_uid (str): Slave channel UID ("%(channel_id)s.%(chat_id)s") |
| 403 | + slave_uid (EFBChannelChatIDStr): Slave channel UID ("%(channel_id)s.%(chat_id)s") |
416 | 404 |
|
417 | 405 | Returns: |
418 | 406 | The message thread_id |
419 | 407 | """ |
420 | 408 | try: |
421 | 409 | assoc = TopicAssoc.select(TopicAssoc.message_thread_id)\ |
422 | | - .where(TopicAssoc.slave_uid == slave_uid, TopicAssoc.topic_chat_id == topic_chat_id)\ |
| 410 | + .where(TopicAssoc.slave_uid == slave_uid)\ |
423 | 411 | .order_by(TopicAssoc.id.desc()).first() |
424 | 412 | if assoc: |
425 | 413 | return int(assoc.message_thread_id) |
426 | 414 | except DoesNotExist: |
427 | 415 | return None |
428 | 416 |
|
429 | 417 | @staticmethod |
430 | | - def get_topic_slave(topic_chat_id: int, |
431 | | - message_thread_id: int |
| 418 | + def get_topic_slave(topic_chat_id: TelegramTopicID, |
| 419 | + message_thread_id: Optional[EFBChannelChatIDStr] = None, |
432 | 420 | ) -> Optional[EFBChannelChatIDStr]: |
433 | 421 | """ |
434 | 422 | Get topic association (topic link) information. |
435 | 423 | Only one parameter is to be provided. |
436 | 424 |
|
437 | 425 | Args: |
438 | | - topic_chat_id (int): The topic UID |
439 | | - message_thread_id (int): The message thread ID |
| 426 | + topic_chat_id (TelegramTopicID): The topic UID |
| 427 | + message_thread_id (TelegramTopicID): The message thread ID |
440 | 428 |
|
441 | 429 | Returns: |
442 | 430 | Slave channel UID ("%(channel_id)s.%(chat_id)s") |
443 | 431 | """ |
444 | 432 | try: |
445 | | - return TopicAssoc.select(TopicAssoc.slave_uid)\ |
446 | | - .where(TopicAssoc.message_thread_id == message_thread_id, TopicAssoc.topic_chat_id == topic_chat_id).first().slave_uid |
| 433 | + if message_thread_id: |
| 434 | + return TopicAssoc.select(TopicAssoc.slave_uid)\ |
| 435 | + .where(TopicAssoc.message_thread_id == message_thread_id, TopicAssoc.topic_chat_id == topic_chat_id).first().slave_uid |
| 436 | + else: |
| 437 | + return TopicAssoc.select(TopicAssoc.slave_uid)\ |
| 438 | + .where(TopicAssoc.topic_chat_id == topic_chat_id).first().slave_uid |
447 | 439 | except DoesNotExist: |
448 | 440 | return None |
449 | | - except AttributeError: # Handle case where .slave_uid doesn't exist on the result |
| 441 | + except AttributeError: |
450 | 442 | return None |
451 | 443 |
|
452 | 444 | @staticmethod |
453 | | - def remove_topic_assoc(topic_chat_id: int, slave_uid: Optional[EFBChannelChatIDStr] = None): |
| 445 | + def get_topic_slaves(topic_chat_id: TelegramChatID) -> Optional[List[Tuple[EFBChannelChatIDStr, TelegramTopicID]]]: |
| 446 | + """ |
| 447 | + Get topic association (topic link) information. |
| 448 | + Only one parameter is to be provided. |
| 449 | +
|
| 450 | + Args: |
| 451 | + topic_chat_id (TelegramChatID): The topic UID |
| 452 | +
|
| 453 | + Returns: |
| 454 | + List[Tuple[EFBChannelChatIDStr, TelegramTopicID]]: A list of tuples containing slave channel UID and message thread ID |
| 455 | + """ |
| 456 | + try: |
| 457 | + query = TopicAssoc.select(TopicAssoc.slave_uid, TopicAssoc.message_thread_id)\ |
| 458 | + .where(TopicAssoc.topic_chat_id == topic_chat_id).order_by(TopicAssoc.id.desc()) |
| 459 | + return [(row.slave_uid, int(row.message_thread_id)) for row in query] |
| 460 | + except DoesNotExist: |
| 461 | + return None |
| 462 | + except AttributeError: |
| 463 | + return None |
| 464 | + |
| 465 | + @staticmethod |
| 466 | + def remove_topic_assoc(topic_chat_id: Optional[TelegramChatID] = None, |
| 467 | + message_thread_id: Optional[EFBChannelChatIDStr] = None, |
| 468 | + slave_uid: Optional[EFBChannelChatIDStr] = None): |
454 | 469 | """ |
455 | 470 | Remove topic association (topic link). |
456 | 471 |
|
457 | 472 | Args: |
458 | | - topic_chat_id (int): The topic group chat ID |
459 | | - slave_uid (str): Slave channel UID ("%(channel_id)s.%(chat_id)s") |
| 473 | + topic_chat_id (TelegramChatID): The topic group chat ID |
| 474 | + message_thread_id (EFBChannelChatIDStr): The topic thread ID |
| 475 | + slave_uid (EFBChannelChatIDStr): Slave channel UID ("%(channel_id)s.%(chat_id)s") |
460 | 476 | """ |
461 | 477 | try: |
462 | | - return TopicAssoc.delete().where( |
463 | | - (TopicAssoc.topic_chat_id == str(topic_chat_id)) & |
464 | | - (TopicAssoc.slave_uid == str(slave_uid)) |
465 | | - ).execute() |
| 478 | + if bool(topic_chat_id and message_thread_id) == bool(slave_uid): |
| 479 | + raise ValueError("Please provide either topic_chat_id and message_thread_id or slave_uid.") |
| 480 | + elif topic_chat_id and message_thread_id: |
| 481 | + return TopicAssoc.delete().where( |
| 482 | + (TopicAssoc.topic_chat_id == str(topic_chat_id)) & |
| 483 | + (TopicAssoc.message_thread_id == str(message_thread_id)) |
| 484 | + ).execute() |
| 485 | + elif slave_uid: |
| 486 | + return TopicAssoc.delete().where(TopicAssoc.slave_uid == slave_uid).execute() |
466 | 487 | except DoesNotExist: |
467 | 488 | return 0 |
468 | 489 |
|
@@ -494,7 +515,6 @@ def add_or_update_message_log(self, |
494 | 515 |
|
495 | 516 | row.master_msg_id = master_msg_id |
496 | 517 | row.master_msg_id_alt = master_msg_id_alt |
497 | | - row.master_message_thread_id = str(master_message.message_thread_id) if master_message.message_thread_id else None |
498 | 518 | row.text = msg.text |
499 | 519 | row.slave_origin_uid = chat_id_to_str(chat=msg.chat) |
500 | 520 | row.slave_member_uid = chat_id_to_str(chat=msg.author) |
|
0 commit comments