From 2d4aa3e8f90222c9271b7bffa59824af3d14605e Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Fri, 31 Jan 2020 14:39:19 +0800 Subject: [PATCH] style: more consistent naming removing underscores There were less places with underscores in field names than places without them, so we now are consistent with the most abundant occurrence. --- TLSharp.Core/DataCenterIPVersion.cs | 30 ----- TLSharp.Core/TLSharp.Core.csproj | 1 - TLSharp.Core/TelegramClient.cs | 173 +++++++--------------------- 3 files changed, 41 insertions(+), 163 deletions(-) delete mode 100644 TLSharp.Core/DataCenterIPVersion.cs diff --git a/TLSharp.Core/DataCenterIPVersion.cs b/TLSharp.Core/DataCenterIPVersion.cs deleted file mode 100644 index 9c908ac..0000000 --- a/TLSharp.Core/DataCenterIPVersion.cs +++ /dev/null @@ -1,30 +0,0 @@ -namespace TLSharp.Core -{ - /// - /// When the Telegram server responds with a set of addresses to connect to, DataCenterIPVersion indicates a preference - /// for how to choose the IP address to connect to - /// - public enum DataCenterIPVersion - { - /// - /// Prefers IPv6 addresses if any is passed by Telegram - /// - Default = 0, - /// - /// Takes only IPv4 addresses - /// - OnlyIPv4 = 1, - /// - /// Takes only IPv6 addresses - /// - OnlyIPv6 = 2, - /// - /// Connection to IPv4 addresses is preferred to IPv6 addresses - /// - PreferIPv4 = 3, - /// - /// Connection to IPv6 addresses is preferred to IPv4 addresses - /// - PreferIPv6 = 4, - } -} diff --git a/TLSharp.Core/TLSharp.Core.csproj b/TLSharp.Core/TLSharp.Core.csproj index 07a3270..c09f394 100644 --- a/TLSharp.Core/TLSharp.Core.csproj +++ b/TLSharp.Core/TLSharp.Core.csproj @@ -48,7 +48,6 @@ - diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index d4906a8..46e0ad4 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.Configuration; -using System.Data; using System.Linq; using System.Security.Cryptography; using System.Text; @@ -24,7 +22,6 @@ using TLAuthorization = TeleSharp.TL.Auth.TLAuthorization; namespace TLSharp.Core { - public class TelegramClient : IDisposable { private MtProtoSender sender; @@ -34,24 +31,14 @@ namespace TLSharp.Core private Session session; private List dcOptions; private TcpClientConnectionHandler handler; - private DataCenterIPVersion dcIpVersion; public Session Session { get { return session; } } - /// - /// Creates a new TelegramClient - /// - /// The API ID provided by Telegram. Get one at https://my.telegram.org - /// The API Hash provided by Telegram. Get one at https://my.telegram.org - /// An ISessionStore object that will handle the session - /// The name of the session that tracks login info about this TelegramClient connection - /// A delegate to invoke when a connection is needed and that will return a TcpClient that will be used to connect - /// Indicates the preferred IpAddress version to use to connect to a Telegram server public TelegramClient(int apiId, string apiHash, - ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null, DataCenterIPVersion dcIpVersion = DataCenterIPVersion.Default) + ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null) { if (apiId == default(int)) throw new MissingApiConfigurationException("API_ID"); @@ -64,10 +51,9 @@ namespace TLSharp.Core this.apiHash = apiHash; this.apiId = apiId; this.handler = handler; - this.dcIpVersion = dcIpVersion; session = Session.TryLoadOrCreateNew(store, sessionUserId); - transport = new TcpTransport(session.DataCenter.Address, session.DataCenter.Port, this.handler); + transport = new TcpTransport (session.DataCenter.Address, session.DataCenter.Port, this.handler); } public async Task ConnectAsync(bool reconnect = false, CancellationToken token = default(CancellationToken)) @@ -115,22 +101,8 @@ namespace TLSharp.Core exported = await SendRequestAsync(exportAuthorization, token).ConfigureAwait(false); } - var dcs = dcOptions.Where(d => d.Id == dcId - && ( - (dcIpVersion == DataCenterIPVersion.Default) // any - || (d.Ipv6 && dcIpVersion == DataCenterIPVersion.OnlyIPv6) // selects only ipv6 addresses - || (!d.Ipv6 && dcIpVersion == DataCenterIPVersion.OnlyIPv4) // selects only ipv4 addresses - || dcIpVersion == DataCenterIPVersion.PreferIPv4 // we can take both types of address - || dcIpVersion == DataCenterIPVersion.PreferIPv6 // we can take both types of address - ) - ).OrderBy(d => d.Ipv6); - - if (dcs.Count() == 0) - throw new Exception($"Telegram server didn't provide us with any IPAddress that matches your preferences. If you chose OnlyIPvX, try switch to PreferIPvX instead."); - - var dc = dcIpVersion == DataCenterIPVersion.PreferIPv4 ? dcs.First() : dcs.Last(); // ipv4 addresses are at the beginning of the list because it was ordered - - var dataCenter = new DataCenter(dcId, dc.IpAddress, dc.Port); + var dc = dcOptions.First(d => d.Id == dcId); + var dataCenter = new DataCenter (dcId, dc.IpAddress, dc.Port); transport = new TcpTransport(dc.IpAddress, dc.Port, handler); session.DataCenter = dataCenter; @@ -151,7 +123,7 @@ namespace TLSharp.Core throw new InvalidOperationException("Not connected!"); var completed = false; - while (!completed) + while(!completed) { try { @@ -159,7 +131,7 @@ namespace TLSharp.Core await sender.Receive(request, token).ConfigureAwait(false); completed = true; } - catch (DataCenterMigrationException e) + catch(DataCenterMigrationException e) { if (session.DataCenter.DataCenterId.HasValue && session.DataCenter.DataCenterId.Value == e.DC) @@ -213,7 +185,7 @@ namespace TLSharp.Core if (String.IsNullOrWhiteSpace(code)) throw new ArgumentNullException(nameof(code)); - + var request = new TLRequestSignIn() { PhoneNumber = phoneNumber, PhoneCodeHash = phoneCodeHash, PhoneCode = code }; await RequestWithDcMigration(request, token).ConfigureAwait(false); @@ -222,7 +194,7 @@ namespace TLSharp.Core return ((TLUser)request.Response.User); } - + public async Task GetPasswordSetting(CancellationToken token = default(CancellationToken)) { var request = new TLRequestGetPassword(); @@ -254,7 +226,7 @@ namespace TLSharp.Core public async Task SignUpAsync(string phoneNumber, string phoneCodeHash, string code, string firstName, string lastName, CancellationToken token = default(CancellationToken)) { var request = new TLRequestSignUp() { PhoneNumber = phoneNumber, PhoneCode = code, PhoneCodeHash = phoneCodeHash, FirstName = firstName, LastName = lastName }; - + await RequestWithDcMigration(request, token).ConfigureAwait(false); OnUserAuthenticated((TLUser)request.Response.User); @@ -271,7 +243,7 @@ namespace TLSharp.Core return (T)result; } - internal async Task SendAuthenticatedRequestAsync(TLMethod methodToExecute, CancellationToken token = default(CancellationToken)) + internal async Task SendAuthenticatedRequestAsync (TLMethod methodToExecute, CancellationToken token = default(CancellationToken)) { if (!IsUserAuthorized()) throw new InvalidOperationException("Authorize user first!"); @@ -298,7 +270,7 @@ namespace TLSharp.Core public async Task ImportContactsAsync(IReadOnlyList contacts, CancellationToken token = default(CancellationToken)) { - var req = new TLRequestImportContacts { Contacts = new TLVector(contacts) }; + var req = new TLRequestImportContacts { Contacts = new TLVector(contacts)}; return await SendAuthenticatedRequestAsync(req, token) .ConfigureAwait(false); @@ -306,7 +278,7 @@ namespace TLSharp.Core public async Task DeleteContactsAsync(IReadOnlyList users, CancellationToken token = default(CancellationToken)) { - var req = new TLRequestDeleteContacts { Id = new TLVector(users) }; + var req = new TLRequestDeleteContacts {Id = new TLVector(users)}; return await SendAuthenticatedRequestAsync(req, token) .ConfigureAwait(false); @@ -314,7 +286,7 @@ namespace TLSharp.Core public async Task DeleteContactAsync(TLAbsInputUser user, CancellationToken token = default(CancellationToken)) { - var req = new TLRequestDeleteContact { Id = user }; + var req = new TLRequestDeleteContact {Id = user}; return await SendAuthenticatedRequestAsync(req, token) .ConfigureAwait(false); @@ -357,10 +329,10 @@ namespace TLSharp.Core offsetPeer = new TLInputPeerSelf(); var req = new TLRequestGetDialogs() - { - OffsetDate = offsetDate, - OffsetId = offsetId, - OffsetPeer = offsetPeer, + { + OffsetDate = offsetDate, + OffsetId = offsetId, + OffsetPeer = offsetPeer, Limit = limit }; return await SendAuthenticatedRequestAsync(req, token) @@ -370,13 +342,13 @@ namespace TLSharp.Core public async Task SendUploadedPhoto(TLAbsInputPeer peer, TLAbsInputFile file, string caption, CancellationToken token = default(CancellationToken)) { return await SendAuthenticatedRequestAsync(new TLRequestSendMedia() - { - RandomId = Helpers.GenerateRandomLong(), - Background = false, - ClearDraft = false, - Media = new TLInputMediaUploadedPhoto() { File = file, Caption = caption }, - Peer = peer - }, token) + { + RandomId = Helpers.GenerateRandomLong(), + Background = false, + ClearDraft = false, + Media = new TLInputMediaUploadedPhoto() { File = file, Caption = caption }, + Peer = peer + }, token) .ConfigureAwait(false); } @@ -384,30 +356,30 @@ namespace TLSharp.Core TLAbsInputPeer peer, TLAbsInputFile file, string caption, string mimeType, TLVector attributes, CancellationToken token = default(CancellationToken)) { return await SendAuthenticatedRequestAsync(new TLRequestSendMedia() - { - RandomId = Helpers.GenerateRandomLong(), - Background = false, - ClearDraft = false, - Media = new TLInputMediaUploadedDocument() { - File = file, - Caption = caption, - MimeType = mimeType, - Attributes = attributes - }, - Peer = peer - }, token) + RandomId = Helpers.GenerateRandomLong(), + Background = false, + ClearDraft = false, + Media = new TLInputMediaUploadedDocument() + { + File = file, + Caption = caption, + MimeType = mimeType, + Attributes = attributes + }, + Peer = peer + }, token) .ConfigureAwait(false); } public async Task GetFile(TLAbsInputFileLocation location, int filePartSize, int offset = 0, CancellationToken token = default(CancellationToken)) { TLFile result = await SendAuthenticatedRequestAsync(new TLRequestGetFile - { - Location = location, - Limit = filePartSize, - Offset = offset - }, token) + { + Location = location, + Limit = filePartSize, + Offset = offset + }, token) .ConfigureAwait(false); return result; } @@ -434,69 +406,6 @@ namespace TLSharp.Core .ConfigureAwait(false); } - /// - /// Authenticates a Bot - /// - /// The token of the bot to authenticate - /// - /// The TLUser descriptor - public async Task MakeAuthBotAsync(string botAuthToken, CancellationToken token = default(CancellationToken)) - { - if (String.IsNullOrWhiteSpace(botAuthToken)) - { - throw new ArgumentNullException(nameof(botAuthToken)); - } - - var request = new TLRequestImportBotAuthorization() { BotAuthToken = botAuthToken, ApiId = apiId, ApiHash = apiHash }; - - await RequestWithDcMigration(request, token).ConfigureAwait(false); - - OnUserAuthenticated(((TLUser)request.Response.User)); - - return ((TLUser)request.Response.User); - } - - /// - /// Gets the full information of a specified chat - /// - /// The ID of the chat we want the info of - /// - /// - public async Task GetFullChat(int chatId, CancellationToken token = default(CancellationToken)) - { - var req = new TLRequestGetFullChat() { ChatId = chatId }; - var fchat = await SendRequestAsync(req).ConfigureAwait(false); - - return fchat; - } - - /// - /// Gets the list of chats opened by the authenticated user. - /// Throws an exception if the authenticated user is a bot. - /// - /// - /// The list of chats opened by the authenticated user - public async Task GetAllChats(CancellationToken token = default(CancellationToken)) - { - return await GetAllChats(null, token); - } - - /// - /// Gets the list of chats opened by the authenticated user except the passed ones. - /// Throws an exception if the authenticated user is a bot. - /// - /// The IDs of the chats that we don't want to be returned - /// - /// The list of chats opened by the authenticated user - public async Task GetAllChats(int[] exceptIds = null, CancellationToken token = default(CancellationToken)) - { - var ichats = new TeleSharp.TL.TLVector(); // we can't pass a null argument to the TLRequestGetChats - if (exceptIds != null) - Array.ForEach(exceptIds, x => ichats.Add(x)); - var chatInfo = await SendRequestAsync(new TLRequestGetChats() { Id = ichats }).ConfigureAwait(false); - return chatInfo; - } - /// /// Serch user or chat. API: contacts.search#11f812d8 q:string limit:int = contacts.Found; ///