Skip to content

Commit a7e295b

Browse files
committed
fix: locking before create new topic
1 parent 2c6e3b9 commit a7e295b

1 file changed

Lines changed: 26 additions & 18 deletions

File tree

efb_telegram_master/slave_message.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import logging
66
import os
77
import tempfile
8+
import threading
89
import traceback
910
import urllib.parse
11+
from collections import defaultdict
1012
from pathlib import Path
1113
from typing import Tuple, Optional, TYPE_CHECKING, List, IO, Union
1214

@@ -53,6 +55,7 @@ def __init__(self, channel: 'TelegramChannel'):
5355
self.db: 'DatabaseManager' = channel.db
5456
self.chat_dest_cache: ChatDestinationCache = channel.chat_dest_cache
5557
self.chat_manager: ChatObjectCacheManager = channel.chat_manager
58+
self._topic_creation_locks = defaultdict(threading.Lock)
5659

5760
def is_silent(self, msg: Message) -> Optional[bool]:
5861
"""Determine if a message shall be sent silently.
@@ -267,6 +270,7 @@ def get_slave_msg_dest(self, msg: Message) -> Tuple[str, Tuple[Optional[Telegram
267270
tg_dest = TelegramChatID(int(utils.chat_id_str_to_id(tg_chat)[1]))
268271
elif not isinstance(chat, SystemChat) and self.channel.topic_group:
269272
thread_id = self.db.get_topic_thread_id(slave_uid=chat_uid, topic_chat_id=self.channel.topic_group)
273+
topic_is_deleted = False
270274
if thread_id:
271275
try:
272276
self.bot.reopen_forum_topic(
@@ -281,26 +285,30 @@ def get_slave_msg_dest(self, msg: Message) -> Tuple[str, Tuple[Optional[Telegram
281285
pass
282286
else:
283287
self.logger.error('Failed to reopen topic, Reason: %s', e)
288+
topic_is_deleted = True
284289
thread_id = None
285290
if not thread_id:
286-
try:
287-
topic: ForumTopic = self.bot.create_forum_topic(
288-
chat_id=self.channel.topic_group,
289-
name=chat.chat_title
290-
)
291-
tg_dest = self.channel.topic_group
292-
thread_id = topic.message_thread_id
293-
self.db.remove_topic_assoc(
294-
topic_chat_id=self.channel.topic_group,
295-
slave_uid=chat_uid,
296-
)
297-
self.db.add_topic_assoc(
298-
topic_chat_id=self.channel.topic_group,
299-
message_thread_id=thread_id,
300-
slave_uid=chat_uid,
301-
)
302-
except telegram.error.BadRequest as e:
303-
self.logger.error('Failed to create topic, Reason: %s', e)
291+
with self._topic_creation_locks[self.channel.topic_group]:
292+
thread_id = not topic_is_deleted and self.db.get_topic_thread_id(slave_uid=chat_uid, topic_chat_id=self.channel.topic_group)
293+
if not thread_id:
294+
try:
295+
topic: ForumTopic = self.bot.create_forum_topic(
296+
chat_id=self.channel.topic_group,
297+
name=chat.chat_title
298+
)
299+
tg_dest = self.channel.topic_group
300+
thread_id = topic.message_thread_id
301+
self.db.remove_topic_assoc(
302+
topic_chat_id=self.channel.topic_group,
303+
slave_uid=chat_uid,
304+
)
305+
self.db.add_topic_assoc(
306+
topic_chat_id=self.channel.topic_group,
307+
message_thread_id=thread_id,
308+
slave_uid=chat_uid,
309+
)
310+
except telegram.error.BadRequest as e:
311+
self.logger.error('Failed to create topic, Reason: %s', e)
304312
else:
305313
singly_linked = False
306314

0 commit comments

Comments
 (0)