mirror of
				https://github.com/LonamiWebs/Telethon.git
				synced 2025-10-31 16:07:44 +03:00 
			
		
		
		
	Periodically save update state (#4071)
This commit is contained in:
		
							parent
							
								
									0f7756ac68
								
							
						
					
					
						commit
						68ea208b43
					
				|  | @ -654,6 +654,19 @@ class TelegramBaseClient(abc.ABC): | ||||||
|             else: |             else: | ||||||
|                 connection._proxy = proxy |                 connection._proxy = proxy | ||||||
| 
 | 
 | ||||||
|  |     def _save_states_and_entities(self: 'TelegramClient'): | ||||||
|  |         entities = self._mb_entity_cache.get_all_entities() | ||||||
|  | 
 | ||||||
|  |         # Piggy-back on an arbitrary TL type with users and chats so the session can understand to read the entities. | ||||||
|  |         # It doesn't matter if we put users in the list of chats. | ||||||
|  |         self.session.process_entities(types.contacts.ResolvedPeer(None, [e._as_input_peer() for e in entities], [])) | ||||||
|  | 
 | ||||||
|  |         ss, cs = self._message_box.session_state() | ||||||
|  |         self.session.set_update_state(0, types.updates.State(**ss, unread_count=0)) | ||||||
|  |         now = datetime.datetime.now()  # any datetime works; channels don't need it | ||||||
|  |         for channel_id, pts in cs.items(): | ||||||
|  |             self.session.set_update_state(channel_id, types.updates.State(pts, 0, now, 0, unread_count=0)) | ||||||
|  | 
 | ||||||
|     async def _disconnect_coro(self: 'TelegramClient'): |     async def _disconnect_coro(self: 'TelegramClient'): | ||||||
|         if self.session is None: |         if self.session is None: | ||||||
|             return  # already logged out and disconnected |             return  # already logged out and disconnected | ||||||
|  | @ -684,17 +697,7 @@ class TelegramBaseClient(abc.ABC): | ||||||
|             await asyncio.wait(self._event_handler_tasks) |             await asyncio.wait(self._event_handler_tasks) | ||||||
|             self._event_handler_tasks.clear() |             self._event_handler_tasks.clear() | ||||||
| 
 | 
 | ||||||
|         entities = self._mb_entity_cache.get_all_entities() |         self._save_states_and_entities() | ||||||
| 
 |  | ||||||
|         # Piggy-back on an arbitrary TL type with users and chats so the session can understand to read the entities. |  | ||||||
|         # It doesn't matter if we put users in the list of chats. |  | ||||||
|         self.session.process_entities(types.contacts.ResolvedPeer(None, [e._as_input_peer() for e in entities], [])) |  | ||||||
| 
 |  | ||||||
|         ss, cs = self._message_box.session_state() |  | ||||||
|         self.session.set_update_state(0, types.updates.State(**ss, unread_count=0)) |  | ||||||
|         now = datetime.datetime.now()  # any datetime works; channels don't need it |  | ||||||
|         for channel_id, pts in cs.items(): |  | ||||||
|             self.session.set_update_state(channel_id, types.updates.State(pts, 0, now, 0, unread_count=0)) |  | ||||||
| 
 | 
 | ||||||
|         self.session.close() |         self.session.close() | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -467,6 +467,8 @@ class UpdateMethods: | ||||||
|             # inserted because this is a rather expensive operation |             # inserted because this is a rather expensive operation | ||||||
|             # (default's sqlite3 takes ~0.1s to commit changes). Do |             # (default's sqlite3 takes ~0.1s to commit changes). Do | ||||||
|             # it every minute instead. No-op if there's nothing new. |             # it every minute instead. No-op if there's nothing new. | ||||||
|  |             self._save_states_and_entities() | ||||||
|  | 
 | ||||||
|             self.session.save() |             self.session.save() | ||||||
| 
 | 
 | ||||||
|     async def _dispatch_update(self: 'TelegramClient', update): |     async def _dispatch_update(self: 'TelegramClient', update): | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user