TDLib中UpdateNewChat事件出现非当前成员聊天问题咨询
TDLib中LoadChats方法触发UpdateNewChat事件的异常问题
在TDLib中调用LoadChats方法时,遇到UpdateNewChat事件相关异常:偶尔会收到已退出、从未加入的聊天(包括归档、已删除、曾退出的公开聊天),以及与我所属频道关联的群组的更新。
复现步骤
- 调用LoadChats方法;
- 观察UpdateNewChat、UpdateChatPosition事件。
预期与实际行为
预期行为:仅接收当前拥有成员身份的聊天更新。
实际行为:收到非当前成员或已退出的聊天更新。
环境信息
- tdlib.native版本: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<long, TdApi.Chat> MainChatList { get; set; } = []; public static ConcurrentDictionary<long, TdApi.Chat> ArchiveChatList { get; set; } = []; public static ConcurrentDictionary<long, TdApi.Chat> UnkownChatList { get; set; } = []; }
Helper类
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); }
内容的提问来源于stack exchange,提问作者Reza
相关产品推荐
相关产品推荐

