Skip to content
This repository was archived by the owner on Dec 5, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion TLSharp.Core/TLSharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Network\Requests\AckRequest.cs" />
<Compile Include="Network\Requests\PingRequest.cs" />
<Compile Include="Types\ParticipantTypes.cs" />
<Compile Include="Types\ParticipantFilterTypes.cs" />
<Compile Include="Utils\UploadHelper.cs" />
<Compile Include="Session.cs" />
<Compile Include="TelegramClient.cs" />
Expand Down
110 changes: 60 additions & 50 deletions TLSharp.Core/TelegramClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public TelegramClient(int apiId, string apiHash,
this.handler = handler;

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))
Expand Down Expand Up @@ -110,7 +110,7 @@ public TelegramClient(int apiId, string apiHash,
}

var dc = dcOptions.First(d => d.Id == dcId);
var dataCenter = new DataCenter (dcId, dc.IpAddress, dc.Port);
var dataCenter = new DataCenter(dcId, dc.IpAddress, dc.Port);

transport = new TcpTransport(dc.IpAddress, dc.Port, handler);
session.DataCenter = dataCenter;
Expand All @@ -131,15 +131,15 @@ public TelegramClient(int apiId, string apiHash,
throw new InvalidOperationException("Not connected!");

var completed = false;
while(!completed)
while (!completed)
{
try
{
await sender.Send(request, token).ConfigureAwait(false);
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)
Expand Down Expand Up @@ -193,7 +193,7 @@ public bool IsUserAuthorized()

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);
Expand All @@ -202,7 +202,7 @@ public bool IsUserAuthorized()

return ((TLUser)request.Response.User);
}

public async Task<TLPassword> GetPasswordSetting(CancellationToken token = default(CancellationToken))
{
var request = new TLRequestGetPassword();
Expand Down Expand Up @@ -234,7 +234,7 @@ public bool IsUserAuthorized()
public async Task<TLUser> 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);
Expand All @@ -251,7 +251,7 @@ public bool IsUserAuthorized()
return (T)result;
}

internal async Task<T> SendAuthenticatedRequestAsync<T> (TLMethod methodToExecute, CancellationToken token = default(CancellationToken))
internal async Task<T> SendAuthenticatedRequestAsync<T>(TLMethod methodToExecute, CancellationToken token = default(CancellationToken))
{
if (!IsUserAuthorized())
throw new InvalidOperationException("Authorize user first!");
Expand All @@ -278,23 +278,23 @@ public bool IsUserAuthorized()

public async Task<TLImportedContacts> ImportContactsAsync(IReadOnlyList<TLInputPhoneContact> contacts, CancellationToken token = default(CancellationToken))
{
var req = new TLRequestImportContacts { Contacts = new TLVector<TLInputPhoneContact>(contacts)};
var req = new TLRequestImportContacts { Contacts = new TLVector<TLInputPhoneContact>(contacts) };

return await SendAuthenticatedRequestAsync<TLImportedContacts>(req, token)
.ConfigureAwait(false);
}

public async Task<bool> DeleteContactsAsync(IReadOnlyList<TLAbsInputUser> users, CancellationToken token = default(CancellationToken))
{
var req = new TLRequestDeleteContacts {Id = new TLVector<TLAbsInputUser>(users)};
var req = new TLRequestDeleteContacts { Id = new TLVector<TLAbsInputUser>(users) };

return await SendAuthenticatedRequestAsync<bool>(req, token)
.ConfigureAwait(false);
}

public async Task<TLLink> DeleteContactAsync(TLAbsInputUser user, CancellationToken token = default(CancellationToken))
{
var req = new TLRequestDeleteContact {Id = user};
var req = new TLRequestDeleteContact { Id = user };

return await SendAuthenticatedRequestAsync<TLLink>(req, token)
.ConfigureAwait(false);
Expand Down Expand Up @@ -337,10 +337,10 @@ public bool IsUserAuthorized()
offsetPeer = new TLInputPeerSelf();

var req = new TLRequestGetDialogs()
{
OffsetDate = offsetDate,
OffsetId = offsetId,
OffsetPeer = offsetPeer,
{
OffsetDate = offsetDate,
OffsetId = offsetId,
OffsetPeer = offsetPeer,
Limit = limit
};
return await SendAuthenticatedRequestAsync<TLAbsDialogs>(req, token)
Expand All @@ -350,44 +350,44 @@ public bool IsUserAuthorized()
public async Task<TLAbsUpdates> SendUploadedPhoto(TLAbsInputPeer peer, TLAbsInputFile file, string caption, CancellationToken token = default(CancellationToken))
{
return await SendAuthenticatedRequestAsync<TLAbsUpdates>(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);
}

public async Task<TLAbsUpdates> SendUploadedDocument(
TLAbsInputPeer peer, TLAbsInputFile file, string caption, string mimeType, TLVector<TLAbsDocumentAttribute> attributes, CancellationToken token = default(CancellationToken))
{
return await SendAuthenticatedRequestAsync<TLAbsUpdates>(new TLRequestSendMedia()
{
RandomId = Helpers.GenerateRandomLong(),
Background = false,
ClearDraft = false,
Media = new TLInputMediaUploadedDocument()
{
RandomId = Helpers.GenerateRandomLong(),
Background = false,
ClearDraft = false,
Media = new TLInputMediaUploadedDocument()
{
File = file,
Caption = caption,
MimeType = mimeType,
Attributes = attributes
},
Peer = peer
}, token)
File = file,
Caption = caption,
MimeType = mimeType,
Attributes = attributes
},
Peer = peer
}, token)
.ConfigureAwait(false);
}

public async Task<TLFile> GetFile(TLAbsInputFileLocation location, int filePartSize, int offset = 0, CancellationToken token = default(CancellationToken))
{
TLFile result = await SendAuthenticatedRequestAsync<TLFile>(new TLRequestGetFile
{
Location = location,
Limit = filePartSize,
Offset = offset
}, token)
{
Location = location,
Limit = filePartSize,
Offset = offset
}, token)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you formatted the whole file again by mistake, can you create a separate PR just to format the file please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see the next commit

.ConfigureAwait(false);
return result;
}
Expand Down Expand Up @@ -524,7 +524,7 @@ await sender.SendPingAsync(token)
/// <param name="partType">The type of the participants to get. Choose Recents not to filter</param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<TLChannelParticipants> GetParticipants(TLChannel channel, int stIdx = -1, int pageSize = -1, ParticipantTypes partType = ParticipantTypes.Recents, CancellationToken token = default(CancellationToken))
public async Task<TLChannelParticipants> GetParticipants(TLChannel channel, int stIdx = -1, int pageSize = -1, ParticipantFilterTypes partType = ParticipantFilterTypes.Recents, CancellationToken token = default(CancellationToken))
{
if (channel == null) return null;
return await GetParticipants(channel.Id, (long)channel.AccessHash, stIdx, pageSize, partType, token).ConfigureAwait(false);
Expand All @@ -541,31 +541,31 @@ await sender.SendPingAsync(token)
/// <param name="partType">The type of the participants to get. Choose Recents not to filter</param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<TLChannelParticipants> GetParticipants(int channelId, long accessHash, int stIdx = -1, int pageSize = -1, ParticipantTypes partType = ParticipantTypes.Recents, CancellationToken token = default(CancellationToken))
public async Task<TLChannelParticipants> GetParticipants(int channelId, long accessHash, int stIdx = -1, int pageSize = -1, ParticipantFilterTypes partType = ParticipantFilterTypes.Recents, CancellationToken token = default(CancellationToken))
{
TLAbsChannelParticipantsFilter filter;
switch (partType)
{
case ParticipantTypes.Admins:
case ParticipantFilterTypes.Admins:
filter = new TLChannelParticipantsAdmins();
break;

case ParticipantTypes.Kicked:
case ParticipantFilterTypes.Kicked:
filter = new TLChannelParticipantsKicked();
break;

case ParticipantTypes.Bots:
case ParticipantFilterTypes.Bots:
filter = new TLChannelParticipantsBots();
break;

case ParticipantTypes.Recents:
case ParticipantFilterTypes.Recents:
filter = new TLChannelParticipantsRecent();
break;

case ParticipantTypes.Banned:
case ParticipantTypes.Restricted:
case ParticipantTypes.Contacts:
case ParticipantTypes.Search:
case ParticipantFilterTypes.Banned:
case ParticipantFilterTypes.Restricted:
case ParticipantFilterTypes.Contacts:
case ParticipantFilterTypes.Search:
default:
throw new NotImplementedException($"{partType} not implemented yet");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's not gonna be implemented please remove them from your enum as the only purpose of enum is for this filter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's gonna be implemented as soon as the new Classes are added to the TL framework

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should i drop them anyways or leave them?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are they present in the new layers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vote, then, for only merging this PR after we have updated the layer

}
Expand All @@ -588,7 +588,7 @@ await sender.SendPingAsync(token)
ChannelId = channelId,
AccessHash = accessHash
},
Filter = new TLChannelParticipantsRecent(),
Filter = filter,
Offset = found,
Limit = pageSize
};
Expand All @@ -607,6 +607,16 @@ await sender.SendPingAsync(token)
/// <summary>
/// Invites the passed users to the specified channel
/// </summary>
/// <param name="channel">The descriptor of the channel to invite the users to</param>
/// <param name="users"></param>
/// <returns></returns>
public async Task<TLUpdates> InviteToChannel(TLChannel channel, int[] users, CancellationToken token = default(CancellationToken))
{
return await InviteToChannel(channel.Id, (long)channel.AccessHash, users, token);
}
/// <summary>
/// Invites the passed users to the specified channel
/// </summary>
/// <param name="channelId">The id of the channel to invite the users to</param>
/// <param name="accessHash">The access hash of the channel to invite the users to</param>
/// <param name="users"></param>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace TLSharp.Core.Types
{
public enum ParticipantTypes
public enum ParticipantFilterTypes
{
Recents,
Restricted,
Expand Down