Fix #335 GetAllDialogs infinite loop when last dialogs' messages are unavailable
Some checks failed
Dev build / build (push) Has been cancelled

This commit is contained in:
Wizou 2025-09-25 02:04:16 +02:00
parent 4578dea3a3
commit 3f1036a559
2 changed files with 7 additions and 5 deletions

View file

@ -8,7 +8,7 @@
This library allows you to connect to Telegram and control a user programmatically (or a bot, but [WTelegramBot](https://www.nuget.org/packages/WTelegramBot) is much easier for that).
All the Telegram Client APIs (MTProto) are supported so you can do everything the user could do with a full Telegram GUI client.
Library was developed solely by one unemployed guy. [Donations are welcome](https://buymeacoffee.com/wizou).
Library was developed solely by one unemployed guy. [Donations](https://buymeacoffee.com/wizou) or [Patreon memberships are welcome](https://patreon.com/wizou).
This ReadMe is a **quick but important tutorial** to learn the fundamentals about this library. Please read it all.

View file

@ -535,18 +535,20 @@ namespace WTelegram
case Messages_DialogsSlice mds:
var dialogList = new List<DialogBase>();
var messageList = new List<MessageBase>();
while (dialogs.Dialogs.Length != 0)
int skip = 0;
while (dialogs.Dialogs.Length > skip)
{
dialogList.AddRange(dialogs.Dialogs);
dialogList.AddRange(skip == 0 ? dialogs.Dialogs : dialogs.Dialogs[skip..]);
messageList.AddRange(dialogs.Messages);
skip = 0;
int last = dialogs.Dialogs.Length - 1;
var lastDialog = dialogs.Dialogs[last];
retryDate:
var lastPeer = dialogs.UserOrChat(lastDialog).ToInputPeer();
var lastMsgId = lastDialog.TopMessage;
retryDate:
var lastDate = dialogs.Messages.LastOrDefault(m => m.Peer.ID == lastDialog.Peer.ID && m.ID == lastDialog.TopMessage)?.Date ?? default;
if (lastDate == default)
if (--last < 0) break; else { lastDialog = dialogs.Dialogs[last]; goto retryDate; }
if (--last < 0) break; else { ++skip; lastDialog = dialogs.Dialogs[last]; goto retryDate; }
dialogs = await this.Messages_GetDialogs(lastDate, lastMsgId, lastPeer, folder_id: folder_id);
if (dialogs is not Messages_Dialogs md) break;
foreach (var (key, value) in md.chats) mds.chats[key] = value;