@@ -164,8 +164,9 @@ async def _load_participants(self, chat_identifier: int) -> None:
164164 last_name = participant_user .last_name ,
165165 username = participant_user .username ,
166166 is_premium = participant_user .premium or False ,
167- language_code = participant_user .lang_code
168- or core_settings .default_language ,
167+ language_code = (
168+ participant_user .lang_code or core_settings .default_language
169+ ),
169170 )
170171 )
171172 self .telegram_chat_user_service .create_or_update (
@@ -192,26 +193,33 @@ async def index(self, chat: ChatPeerType) -> None:
192193 logger .info (f"Creating a new chat invite link for the chat { chat_id !r} ..." )
193194 invite_link = await self .telethon_service .get_invite_link (chat )
194195 self .telegram_chat_service .refresh_invite_link (chat_id , invite_link .link )
195- logger .info (f"Chat { chat_id !r} created successfully" )
196196 await self ._load_participants (telegram_chat .id )
197197
198- async def fetch_and_push_profile_photo (self , chat : ChatPeerType ) -> Path | None :
198+ async def fetch_and_push_profile_photo (
199+ self ,
200+ chat : ChatPeerType ,
201+ current_logo_path : str | None ,
202+ ) -> Path | None :
199203 """
200204 Fetches the profile photo of a chat and uploads it for hosting. This function
201205 handles the download of the profile photo from the given chat and then pushes
202206 it to a CDN service for further access. If the profile photo exists, it will
203207 be returned as a Path object; otherwise, None is returned.
204208
205209 :param chat: The chat from which the profile photo is to be fetched.
210+ :param current_logo_path: The current logo path in the database.
206211 :return: The local path of the fetched profile photo or None
207212 """
208- logo_path = await self .telethon_service .download_profile_photo (chat )
213+ logo_path = await self .telethon_service .download_profile_photo (
214+ entity = chat ,
215+ current_logo_path = current_logo_path ,
216+ )
209217 if logo_path :
210218 await self .cdn_service .upload_file (
211219 file_path = logo_path ,
212220 object_name = logo_path .name ,
213221 )
214- logger .info (f"Profile photo for chat { chat .id !r} uploaded" )
222+ logger .info (f"New profile photo for chat { chat .id !r} uploaded" )
215223 return logo_path
216224
217225 async def _create (
@@ -230,7 +238,9 @@ async def _create(
230238 :return: A DTO containing the details of the created Telegram chat.
231239 :raises TelegramChatAlreadyExists: If the chat already exists in the database.
232240 """
233- logo_path = await self .fetch_and_push_profile_photo (chat )
241+ logo_path = await self .fetch_and_push_profile_photo (
242+ chat , current_logo_path = None
243+ )
234244 try :
235245 chat_id = get_peer_id (chat , add_mark = True )
236246 telegram_chat = self .telegram_chat_service .create (
@@ -325,7 +335,7 @@ async def _refresh(self, chat: TelegramChat) -> TelegramChat:
325335 :raises TelegramChatNotSufficientPrivileges: If the bot lacks functionality privileges within the chat
326336 """
327337 try :
328- chat_entity , logo_path = await self ._get_chat_data (chat .id )
338+ chat_entity = await self ._get_chat_data (chat .id )
329339
330340 except (
331341 TelegramChatNotSufficientPrivileges , # happens when bot has no rights to function in the chat
@@ -343,10 +353,16 @@ async def _refresh(self, chat: TelegramChat) -> TelegramChat:
343353 self .telegram_chat_service .delete (chat_id = chat .id )
344354 raise
345355
356+ logo_path = await self .fetch_and_push_profile_photo (
357+ chat_entity , current_logo_path = chat .logo_path
358+ )
359+
346360 chat = self .telegram_chat_service .update (
347361 chat = chat ,
348362 entity = chat_entity ,
349- logo_path = logo_path .name ,
363+ # If a new logo was downloaded - use it,
364+ # otherwise fallback to the current one
365+ logo_path = logo_path .name if logo_path else chat .logo_path ,
350366 )
351367 await self .index (chat_entity )
352368 logger .info (f"Chat { chat .id !r} refreshed successfully" )
0 commit comments