Шаги по воспроизведению:
- Вызовите метод LoadChats.
- Наблюдайте за событиями UpdateNewChat, UpdateChatPosition.
Фактическое поведение: Получение обновления для чатов, в которых я не являюсь участником или которые покинул в прошлом.
Среда:
tdlib.родная версия: 1.8.29(из nuget)
Версия tdlib: 1.8.29 (из nuget)
Язык программирования : C# 11
(.NET8)
Вопрос:
Что именно отправляет событие UpdateNewChat и как это работает? В документации упоминается, что это событие должно обрабатываться при загрузке или создании чата, но не объясняется, откуда берутся эти чаты. Я ожидал получать только те чаты, в которых я сейчас участвую (даже если они заархивированы). Ожидается ли такое поведение или может возникнуть проблема с тем, как я обрабатываю это событие? Почему это событие приведет к появлению чатов, в которых я больше не участвую?
Мой обработчик обновлений:
internal static class CommonUpdateHandlerMethods
{
public static async void UpdateChatListHanlder(object sender, TdApi.Update e)
{
var client = (TdClient)sender;
switch (e)
{
case TdApi.Update.UpdateNewChat newChat:
{
await UpdateNewChat(client, newChat.Chat.Id);
break;
}
case TdApi.Update.UpdateChatPosition chatPos:
{
if (chatPos.Position.Order == CommonConstants.TdApi.ShouldRemoveChat)//ShouldRemoveChat = 0
{
switch (chatPos.Position.List)
{
case TdApi.ChatList.ChatListArchive:
{
CommonData.ArchiveChatList.Remove(chatPos.ChatId, out _);
break;
}
case TdApi.ChatList.ChatListMain:
{
CommonData.MainChatList.Remove(chatPos.ChatId, out _);
break;
}
}
}
else
{
await UpdateNewChat(client, chatPos.ChatId);
}
break;
}
}
}
private static async Task UpdateNewChat(TdClient client, long newChatId)
{
var chat = await client.GetChatAsync(newChatId);
if (chat.ChatLists.Any(list => list is TdApi.ChatList.ChatListArchive))
{
CommonData.ArchiveChatList.AddOrUpdate(chat.Id, chat, (id, chat) => chat);
}
else if (chat.ChatLists.Any(list => list is TdApi.ChatList.ChatListMain))
{
CommonData.MainChatList.AddOrUpdate(chat.Id, chat, (id, chat) => chat);
}
else
{
CommonData.UnkownChatList.AddOrUpdate(chat.Id, chat, (id, chat) => chat);
}
}
}
Класс CommonData:
internal static class CommonData
{
public static ConcurrentDictionary MainChatList { get; set; } = [];
public static ConcurrentDictionary ArchiveChatList { get; set; } = [];
public static ConcurrentDictionary UnkownChatList { get; set; } = [];
}
Вспомогательный класс:
internal static class Helper
{
private static async Task InnerLoadChatsAsync(TdClient client, TdApi.ChatList chatList, int delay)
{
try
{
while (true)
{
await client.LoadChatsAsync(chatList, CommonConstants.DefaultLimit);
Thread.Sleep(delay);
}
}
catch (Exception ex)
{
if (ex is TdException tde && tde.Error.Code == CommonConstants.ErrorCodes.NotFound)//NotFound = 404
return;
else
throw;
}
}
internal static async Task LoadChatsAsync(TdClient client, int delay = CommonConstants.DefaultLoadChatDelay)//DefaultLoadChatDelay = 100
{
await InnerLoadChatsAsync(client, new TdApi.ChatList.ChatListArchive(), delay);
await InnerLoadChatsAsync(client, new TdApi.ChatList.ChatListMain(), delay);
}
}
Мой метод тестирования:
public static async Task TestLoadChats(TdClient client)
{
await Helper.LoadChatsAsync(client);
Console.WriteLine("CHATM[{0}]", CommonData.MainChatList.Count);
Console.WriteLine("CHATA[{0}]", CommonData.ArchiveChatList.Count);
using var sw = new StreamWriter("finfo.txt");
foreach (var chat in CommonData.UnkownChatList)
{
sw.WriteLine("{0}\n {1}\n {2}\n--------------------------------------", chat.Key, chat.Value.Title, chat.Value.Type.DataType);
}
Console.WriteLine("CHATU[{0}]", CommonData.UnkownChatList.Count);
}
Подробнее здесь: https://stackoverflow.com/questions/788 ... t-in-tdlib
Мобильная версия