chats and users fields are now serialized as Dictionary for easier access

This commit is contained in:
Wizou 2021-10-20 19:12:50 +02:00
parent 5e2ddf41f6
commit 718e96a763
10 changed files with 145 additions and 116 deletions

View file

@ -32,26 +32,26 @@ namespace WTelegramClientTest
var chats = await client.Messages_GetAllChats(null); // chats = groups/channels (does not include users dialogs)
Console.WriteLine("This user has joined the following:");
foreach (var chat in chats.chats)
foreach (var (id, chat) in chats.chats)
switch (chat)
{
case Chat smallgroup when (smallgroup.flags & Chat.Flags.deactivated) == 0:
Console.WriteLine($"{smallgroup.id}: Small group: {smallgroup.title} with {smallgroup.participants_count} members");
Console.WriteLine($"{id}: Small group: {smallgroup.title} with {smallgroup.participants_count} members");
break;
case Channel channel when (channel.flags & Channel.Flags.broadcast) != 0:
Console.WriteLine($"{channel.id}: Channel {channel.username}: {channel.title}");
Console.WriteLine($"{id}: Channel {channel.username}: {channel.title}");
//Console.WriteLine($" → access_hash = {channel.access_hash:X}");
break;
case Channel group: // no broadcast flag => it's a big group, also called supergroup or megagroup
Console.WriteLine($"{group.id}: Group {group.username}: {group.title}");
Console.WriteLine($"{id}: Group {group.username}: {group.title}");
//Console.WriteLine($" → access_hash = {group.access_hash:X}");
break;
}
Console.Write("Type a chat ID to send a message: ");
long id = long.Parse(Console.ReadLine());
var target = chats.chats.First(chat => chat.ID == id);
Console.WriteLine($"Sending a message in chat {target.ID}: {target.Title}");
long chatId = long.Parse(Console.ReadLine());
var target = chats.chats[chatId];
Console.WriteLine($"Sending a message in chat {chatId}: {target.Title}");
// Next line implicitely creates an adequate InputPeer from ChatBase: (with the access_hash if these is one)
InputPeer peer = target;
await client.SendMessageAsync(peer, "Hello, World");

View file

@ -24,12 +24,11 @@ namespace WTelegramClientTest
if (dialogsBase is Messages_Dialogs dialogs)
while (dialogs.dialogs.Length != 0)
{
foreach (var user in dialogs.users) users[user.ID] = user;
foreach (var chat in dialogs.chats) chats[chat.ID] = chat;
foreach (var (id, user) in dialogs.users) users[id] = user;
foreach (var (id, chat) in dialogs.chats) chats[id] = chat;
var lastDialog = (Dialog)dialogs.dialogs[^1];
var lastMsg = dialogs.messages.LastOrDefault(m => m.Peer.ID == lastDialog.peer.ID && m.ID == lastDialog.top_message);
InputPeer offsetPeer = lastDialog.peer is PeerUser pu ? dialogs.users.First(u => u.ID == pu.ID)
: dialogs.chats.First(u => u.ID == lastDialog.peer.ID);
InputPeer offsetPeer = lastDialog.peer is PeerUser pu ? dialogs.users[pu.ID] : dialogs.chats[lastDialog.peer.ID];
dialogs = (Messages_Dialogs)await client.Messages_GetDialogs(lastMsg?.Date ?? default, lastDialog.top_message, offsetPeer, 500, 0);
}
Console.ReadKey();
@ -53,13 +52,13 @@ namespace WTelegramClientTest
case UpdateShortSentMessage: Console.WriteLine($"You sent a message"); break;
case UpdateShort updateShort: DisplayUpdate(updateShort.update); break;
case Updates u:
foreach (var user in u.users) users[user.ID] = user;
foreach (var chat in u.chats) chats[chat.ID] = chat;
foreach (var (id, user) in u.users) users[id] = user;
foreach (var (id, chat) in u.chats) chats[id] = chat;
foreach (var update in u.updates) DisplayUpdate(update);
break;
case UpdatesCombined uc:
foreach (var user in uc.users) users[user.ID] = user;
foreach (var chat in uc.chats) chats[chat.ID] = chat;
foreach (var (id, user) in uc.users) users[id] = user;
foreach (var (id, chat) in uc.chats) chats[id] = chat;
foreach (var update in uc.updates) DisplayUpdate(update);
break;
default: Console.WriteLine(arg.GetType().Name); break;