From 8026bd79dca8ac089fd41a8423da9382feea83fb Mon Sep 17 00:00:00 2001 From: David Woakes Date: Tue, 19 Jul 2016 14:47:08 +0100 Subject: [PATCH] Fix issues with GetDialogs Default value of max_id = 0 returns all dialogs Should return a class with lists of dialog, messages, chats and users. Add UserForeignConstructor to list of constructors Add missing detail to DialogConstructor (PeerNotifySettings) Unpack datastream completely to avoid issue with Ionic exception causing problems in userForeign parsing Use more recent code for Dialog construction --- TLSharp.Core/MTProto/TL.cs | 23 ++++++++++++++++++---- TLSharp.Core/Network/MtProtoSender.cs | 15 ++++++++++---- TLSharp.Core/Requests/GetDialogsRequest.cs | 9 +++++---- TLSharp.Core/TelegramClient.cs | 10 ++++++++-- 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/TLSharp.Core/MTProto/TL.cs b/TLSharp.Core/MTProto/TL.cs index 2a0e5a7..cee1be6 100644 --- a/TLSharp.Core/MTProto/TL.cs +++ b/TLSharp.Core/MTProto/TL.cs @@ -537,6 +537,9 @@ namespace TLSharp.Core.MTProto {0x427425e7, typeof (AudioConstructor)}, {0x36f8c871, typeof (DocumentEmptyConstructor)}, {0xf9a39f4f, typeof (DocumentConstructor)}, + {0xab3a99ac, typeof (DialogConstructor)}, + {0xd9ccc4ef, typeof (UserRequestConstructor)}, + {0x075cf7a8, typeof (UserForeignConstructor)}, }; public static TLObject Parse(BinaryReader reader, uint code) @@ -1035,9 +1038,9 @@ namespace TLSharp.Core.MTProto return new MessageActionChatDeleteUserConstructor(user_id); } - public static Dialog dialog(Peer peer, int top_message, int unread_count) + public static Dialog dialog(Peer peer, int top_message, int unread_count, PeerNotifySettings peerNotifySettings) { - return new DialogConstructor(peer, top_message, unread_count); + return new DialogConstructor(peer, top_message, unread_count, peerNotifySettings); } public static Photo photoEmpty(long id) @@ -5957,23 +5960,33 @@ namespace TLSharp.Core.MTProto } } + public class MessageDialogs + { + public int? Count { get; set; } + public List Dialogs { get; set; } + public List Messages { get; set; } + public List Chats { get; set; } + public List Users { get; set; } + } public class DialogConstructor : Dialog { public Peer peer; public int top_message; public int unread_count; + public PeerNotifySettings peerNotifySettings; public DialogConstructor() { } - public DialogConstructor(Peer peer, int top_message, int unread_count) + public DialogConstructor(Peer peer, int top_message, int unread_count, PeerNotifySettings peerNotifySettings) { this.peer = peer; this.top_message = top_message; this.unread_count = unread_count; + this.peerNotifySettings = peerNotifySettings; } @@ -5984,10 +5997,11 @@ namespace TLSharp.Core.MTProto public override void Write(BinaryWriter writer) { - writer.Write(0x214a8cdf); + writer.Write(0xab3a99ac); this.peer.Write(writer); writer.Write(this.top_message); writer.Write(this.unread_count); + this.peerNotifySettings.Write(writer); } public override void Read(BinaryReader reader) @@ -5995,6 +6009,7 @@ namespace TLSharp.Core.MTProto this.peer = TL.Parse(reader); this.top_message = reader.ReadInt32(); this.unread_count = reader.ReadInt32(); + this.peerNotifySettings = TL.Parse(reader); } public override string ToString() diff --git a/TLSharp.Core/Network/MtProtoSender.cs b/TLSharp.Core/Network/MtProtoSender.cs index 1dfa02e..3f9ff94 100644 --- a/TLSharp.Core/Network/MtProtoSender.cs +++ b/TLSharp.Core/Network/MtProtoSender.cs @@ -295,11 +295,18 @@ namespace TLSharp.Core.Network { // gzip_packed byte[] packedData = Serializers.Bytes.read(messageReader); - using (var packedStream = new MemoryStream(packedData, false)) - using (var zipStream = new GZipStream(packedStream, CompressionMode.Decompress)) - using (var compressedReader = new BinaryReader(zipStream)) + using (var ms = new MemoryStream()) { - request.OnResponse(compressedReader); + using (var packedStream = new MemoryStream(packedData, false)) + using (var zipStream = new GZipStream(packedStream, CompressionMode.Decompress)) + { + zipStream.CopyTo(ms); + ms.Position = 0; + } + using (var compressedReader = new BinaryReader(ms)) + { + request.OnResponse(compressedReader); + } } } catch (ZlibException ex) diff --git a/TLSharp.Core/Requests/GetDialogsRequest.cs b/TLSharp.Core/Requests/GetDialogsRequest.cs index afa55a3..7ab35ed 100644 --- a/TLSharp.Core/Requests/GetDialogsRequest.cs +++ b/TLSharp.Core/Requests/GetDialogsRequest.cs @@ -11,6 +11,7 @@ namespace TLSharp.Core.Requests int _max_id; int _limit; + public int count; public List dialogs; public List messages; public List chats; @@ -35,7 +36,7 @@ namespace TLSharp.Core.Requests { bool dialogsSlice = reader.ReadUInt32() == 0x71e094f3; // else dialogs#15ba6c40 - if (dialogsSlice) reader.ReadInt32(); // count + if (dialogsSlice) count = reader.ReadInt32(); // count // dialogs var result = reader.ReadUInt32(); // vector#1cb5c415 @@ -48,7 +49,7 @@ namespace TLSharp.Core.Requests dialogs.Add(dialog_element); } // messages - var count = reader.ReadInt32(); + result = reader.ReadUInt32(); // vector#1cb5c415 int messages_len = reader.ReadInt32(); messages = new List(messages_len); for (int message_index = 0; message_index < messages_len; message_index++) @@ -58,7 +59,7 @@ namespace TLSharp.Core.Requests messages.Add(messages_element); } // chats - count = reader.ReadInt32(); + result = reader.ReadUInt32(); // vector#1cb5c415 int chats_len = reader.ReadInt32(); chats = new List(chats_len); for (int chat_index = 0; chat_index < chats_len; chat_index++) @@ -68,7 +69,7 @@ namespace TLSharp.Core.Requests chats.Add(chats_element); } // users - count = reader.ReadInt32(); + result = reader.ReadUInt32(); // vector#1cb5c415 int users_len = reader.ReadInt32(); users = new List(users_len); for (int users_index = 0; users_index < users_len; users_index++) diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 4364e7f..71dc520 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -250,13 +250,19 @@ namespace TLSharp.Core return Tuple.Create(request.type, request.bytes); } - public async Task> GetDialogs(int offset, int limit, int max_id = -1) + public async Task GetDialogs(int offset, int limit, int max_id = 0) { var request = new GetDialogsRequest(offset, max_id, limit); await _sender.Send(request); await _sender.Recieve(request); - return request.dialogs; + return new MessageDialogs + { + Dialogs = request.dialogs, + Messages = request.messages, + Chats = request.chats, + Users = request.users, + }; } public async Task GetUserFull(int user_id)