mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
Add contacts.GetContacts method to client
This commit is contained in:
parent
397f20298e
commit
089f80fcce
|
|
@ -5960,6 +5960,12 @@ namespace TLSharp.Core.MTProto
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ContactsContacts
|
||||||
|
{
|
||||||
|
public IList<Contact> Contacts { get; set; }
|
||||||
|
public IList<User> Users { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
public class MessageDialogs
|
public class MessageDialogs
|
||||||
{
|
{
|
||||||
public int? Count { get; set; }
|
public int? Count { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -1,72 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using TLSharp.Core.MTProto;
|
|
||||||
|
|
||||||
namespace TLSharp.Core.Requests
|
|
||||||
{
|
|
||||||
public class GetContactRequest : MTProtoRequest
|
|
||||||
{
|
|
||||||
private List<int> currentContacts { get; set; }
|
|
||||||
|
|
||||||
public List<Contact> contacts;
|
|
||||||
public List<UserContactConstructor> users;
|
|
||||||
|
|
||||||
public GetContactRequest(List<int> currentContactsID = null)
|
|
||||||
{
|
|
||||||
currentContacts = currentContactsID;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void OnSend(BinaryWriter writer)
|
|
||||||
{
|
|
||||||
writer.Write(0x22c6aa08);
|
|
||||||
if (currentContacts == null)
|
|
||||||
Serializers.String.write(writer, "");
|
|
||||||
else
|
|
||||||
{
|
|
||||||
string hash = "";
|
|
||||||
foreach (var currentUserID in currentContacts)
|
|
||||||
{
|
|
||||||
var md5 = Utils.Helpers.md5(currentUserID.ToString());
|
|
||||||
hash += md5 + ",";
|
|
||||||
}
|
|
||||||
hash = hash.Length > 0 ? hash.Remove(hash.LastIndexOf(','), 1) : Utils.Helpers.md5( hash);
|
|
||||||
Serializers.String.write(writer, hash);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void OnResponse(BinaryReader reader)
|
|
||||||
{
|
|
||||||
var code = reader.ReadUInt32();
|
|
||||||
var result = reader.ReadInt32(); // vector code
|
|
||||||
int contact_len = reader.ReadInt32();
|
|
||||||
this.contacts = new List<Contact>(contact_len);
|
|
||||||
for (int imported_index = 0; imported_index < contact_len; imported_index++)
|
|
||||||
{
|
|
||||||
Contact imported_element;
|
|
||||||
imported_element = TL.Parse<Contact>(reader);
|
|
||||||
this.contacts.Add(imported_element);
|
|
||||||
}
|
|
||||||
reader.ReadInt32(); // vector code
|
|
||||||
int users_len = reader.ReadInt32();
|
|
||||||
this.users = new List<UserContactConstructor>(users_len);
|
|
||||||
for (int users_index = 0; users_index < users_len; users_index++)
|
|
||||||
{
|
|
||||||
UserContactConstructor users_element;
|
|
||||||
users_element = TL.Parse<UserContactConstructor>(reader);
|
|
||||||
this.users.Add(users_element);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void OnException(Exception exception)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
public override bool Confirmed { get { return true; } }
|
|
||||||
private readonly bool responded;
|
|
||||||
public override bool Responded { get { return responded; } }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
85
TLSharp.Core/Requests/GetContactsRequest.cs
Normal file
85
TLSharp.Core/Requests/GetContactsRequest.cs
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TLSharp.Core.MTProto;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
|
namespace TLSharp.Core.Requests
|
||||||
|
{
|
||||||
|
public class GetContactsRequest : MTProtoRequest
|
||||||
|
{
|
||||||
|
private List<int> CurrentContacts { get; set; }
|
||||||
|
|
||||||
|
public List<Contact> Contacts;
|
||||||
|
public List<User> Users;
|
||||||
|
|
||||||
|
public GetContactsRequest(IList<int> currentContacts = null)
|
||||||
|
{
|
||||||
|
if (currentContacts != null)
|
||||||
|
{
|
||||||
|
CurrentContacts = currentContacts.ToList();
|
||||||
|
CurrentContacts.Sort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnSend(BinaryWriter writer)
|
||||||
|
{
|
||||||
|
writer.Write(0x22c6aa08);
|
||||||
|
if (CurrentContacts == null)
|
||||||
|
Serializers.String.write(writer, "");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// create CSV of contactids and calculate md5 hash
|
||||||
|
string hash;
|
||||||
|
var list = string.Join(",", CurrentContacts);
|
||||||
|
using (var md5 = MD5.Create())
|
||||||
|
{
|
||||||
|
var retVal = md5.ComputeHash(Encoding.UTF8.GetBytes(list));
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
foreach (var t in retVal)
|
||||||
|
{
|
||||||
|
sb.Append(t.ToString("x2"));
|
||||||
|
}
|
||||||
|
hash = sb.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
Serializers.String.write(writer, hash);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnResponse(BinaryReader reader)
|
||||||
|
{
|
||||||
|
var code = reader.ReadUInt32();
|
||||||
|
// if contactsNotModified then exit
|
||||||
|
if (code == 0xb74ba9d2) return;
|
||||||
|
|
||||||
|
reader.ReadInt32(); // vector code
|
||||||
|
var contactLen = reader.ReadInt32();
|
||||||
|
Contacts = new List<Contact>(contactLen);
|
||||||
|
for (var importedIndex = 0; importedIndex < contactLen; importedIndex++)
|
||||||
|
{
|
||||||
|
var importedElement = TL.Parse<Contact>(reader);
|
||||||
|
this.Contacts.Add(importedElement);
|
||||||
|
}
|
||||||
|
reader.ReadInt32(); // vector code
|
||||||
|
var usersLen = reader.ReadInt32();
|
||||||
|
Users = new List<User>(usersLen);
|
||||||
|
for (var usersIndex = 0; usersIndex < usersLen; usersIndex++)
|
||||||
|
{
|
||||||
|
var usersElement = TL.Parse<User>(reader);
|
||||||
|
this.Users.Add(usersElement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnException(Exception exception)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
public override bool Confirmed { get { return true; } }
|
||||||
|
private readonly bool _responded;
|
||||||
|
public override bool Responded { get { return _responded; } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -69,6 +69,7 @@
|
||||||
<Compile Include="Requests\AuthSendSmsRequest.cs" />
|
<Compile Include="Requests\AuthSendSmsRequest.cs" />
|
||||||
<Compile Include="Requests\AuthSignInRequest.cs" />
|
<Compile Include="Requests\AuthSignInRequest.cs" />
|
||||||
<Compile Include="Requests\AuthSignUpRequest.cs" />
|
<Compile Include="Requests\AuthSignUpRequest.cs" />
|
||||||
|
<Compile Include="Requests\GetContactsRequest.cs" />
|
||||||
<Compile Include="Requests\GetDialogsRequest.cs" />
|
<Compile Include="Requests\GetDialogsRequest.cs" />
|
||||||
<Compile Include="Requests\GetFileRequest.cs" />
|
<Compile Include="Requests\GetFileRequest.cs" />
|
||||||
<Compile Include="Requests\GetHistoryRequest.cs" />
|
<Compile Include="Requests\GetHistoryRequest.cs" />
|
||||||
|
|
|
||||||
|
|
@ -105,8 +105,6 @@ namespace TLSharp.Core
|
||||||
request = new AuthSendCodeRequest(phoneNumber, (int)tokenDestination, _apiId, _apiHash, "en");
|
request = new AuthSendCodeRequest(phoneNumber, (int)tokenDestination, _apiId, _apiHash, "en");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
await _sender.Send(request);
|
await _sender.Send(request);
|
||||||
await _sender.Receive(request);
|
await _sender.Receive(request);
|
||||||
|
|
||||||
|
|
@ -282,5 +280,19 @@ namespace TLSharp.Core
|
||||||
|
|
||||||
return regex.IsMatch(number);
|
return regex.IsMatch(number);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<ContactsContacts> GetContacts(IList<int> contactIds = null)
|
||||||
|
{
|
||||||
|
var request = new GetContactsRequest(contactIds);
|
||||||
|
await _sender.Send(request);
|
||||||
|
await _sender.Receive(request);
|
||||||
|
|
||||||
|
return new ContactsContacts
|
||||||
|
{
|
||||||
|
Contacts = request.Contacts,
|
||||||
|
Users = request.Users,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue