diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 7dad81a..9bddb95 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -201,7 +201,6 @@ namespace TLSharp.Core public async Task MakeAuthWithPasswordAsync(TLPassword password, string password_str) { - byte[] password_Bytes = Encoding.UTF8.GetBytes(password_str); IEnumerable rv = password.CurrentSalt.Concat(password_Bytes).Concat(password.CurrentSalt); @@ -227,6 +226,7 @@ namespace TLSharp.Core return ((TLUser)request.Response.User); } + public async Task SendRequestAsync(TLMethod methodToExecute) { await RequestWithDcMigration(methodToExecute); @@ -236,72 +236,59 @@ namespace TLSharp.Core return (T)result; } - public async Task UpdateUsernameAsync(string username) + private async Task SendAuthenticatedRequestAsync (TLMethod methodToExecute) { if (!IsUserAuthorized()) throw new InvalidOperationException("Authorize user first!"); + return await SendRequestAsync(methodToExecute); + } + + public async Task UpdateUsernameAsync(string username) + { var req = new TLRequestUpdateUsername { Username = username }; - return await SendRequestAsync(req); + return await SendAuthenticatedRequestAsync(req); } public async Task CheckUsernameAsync(string username) { - if (!IsUserAuthorized()) - throw new InvalidOperationException("Authorize user first!"); - var req = new TLRequestCheckUsername { Username = username }; - return await SendRequestAsync(req); + return await SendAuthenticatedRequestAsync(req); } public async Task ImportContactsAsync(IReadOnlyList contacts) { - if (!IsUserAuthorized()) - throw new InvalidOperationException("Authorize user first!"); - var req = new TLRequestImportContacts { Contacts = new TLVector(contacts)}; - return await SendRequestAsync(req); + return await SendAuthenticatedRequestAsync(req); } public async Task DeleteContactsAsync(IReadOnlyList users) { - if (!IsUserAuthorized()) - throw new InvalidOperationException("Authorize user first!"); - var req = new TLRequestDeleteContacts {Id = new TLVector(users)}; - return await SendRequestAsync(req); + return await SendAuthenticatedRequestAsync(req); } public async Task DeleteContactAsync(TLAbsInputUser user) { - if (!IsUserAuthorized()) - throw new InvalidOperationException("Authorize user first!"); - var req = new TLRequestDeleteContact {Id = user}; - return await SendRequestAsync(req); + return await SendAuthenticatedRequestAsync(req); } public async Task GetContactsAsync() { - if (!IsUserAuthorized()) - throw new InvalidOperationException("Authorize user first!"); - var req = new TLRequestGetContacts() { Hash = "" }; - return await SendRequestAsync(req); + return await SendAuthenticatedRequestAsync(req); } public async Task SendMessageAsync(TLAbsInputPeer peer, string message) { - if (!IsUserAuthorized()) - throw new InvalidOperationException("Authorize user first!"); - - return await SendRequestAsync( + return await SendAuthenticatedRequestAsync( new TLRequestSendMessage() { Peer = peer, @@ -322,9 +309,6 @@ namespace TLSharp.Core public async Task GetUserDialogsAsync(int offsetDate = 0, int offsetId = 0, TLAbsInputPeer offsetPeer = null, int limit = 100) { - if (!IsUserAuthorized()) - throw new InvalidOperationException("Authorize user first!"); - if (offsetPeer == null) offsetPeer = new TLInputPeerSelf(); @@ -335,7 +319,7 @@ namespace TLSharp.Core OffsetPeer = offsetPeer, Limit = limit }; - return await SendRequestAsync(req); + return await SendAuthenticatedRequestAsync(req); } public async Task SendUploadedPhoto(TLAbsInputPeer peer, TLAbsInputFile file, string caption) @@ -388,9 +372,6 @@ namespace TLSharp.Core public async Task GetHistoryAsync(TLAbsInputPeer peer, int offsetId = 0, int offsetDate = 0, int addOffset = 0, int limit = 100, int maxId = 0, int minId = 0) { - if (!IsUserAuthorized()) - throw new InvalidOperationException("Authorize user first!"); - var req = new TLRequestGetHistory() { Peer = peer, @@ -401,7 +382,7 @@ namespace TLSharp.Core MaxId = maxId, MinId = minId }; - return await SendRequestAsync(req); + return await SendAuthenticatedRequestAsync(req); } ///