Skip to content

Commit

Permalink
contacts.getContactIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
aykutalparslan committed Apr 8, 2023
1 parent d5ea888 commit c7505ea
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Ferrite.Services;
using Ferrite.TL;
using Ferrite.TL.slim;
using VectorOfLong = Ferrite.TL.slim.VectorOfLong;

namespace Ferrite.Core.Execution.Functions.BaseLayer.Contacts;

public class GetContactIdsFunc : ITLFunction
{
private readonly IContactsService _contacts;

public GetContactIdsFunc(IContactsService contacts)
{
_contacts = contacts;
}
public async ValueTask<TLBytes?> Process(TLBytes q, TLExecutionContext ctx)
{
var contacts = await _contacts.GetContactIds(ctx.AuthKeyId, q);
return RpcResultGenerator.Generate(ToVectorOfLong(contacts), ctx.MessageId);
}

private static TLBytes ToVectorOfLong(ICollection<long> collection)
{
VectorOfLong v = new ();
foreach (var s in collection)
{
v.Append(s);
}

var vBytes = v.ToReadOnlySpan().ToArray();
return new TLBytes(vBytes, 0, vBytes.Length);
}
}
15 changes: 14 additions & 1 deletion Ferrite.Services/ContactsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,20 @@ public ContactsService(IUnitOfWork unitOfWork, ISearchEngine search,

public async Task<ICollection<long>> GetContactIds(long authKeyId, TLBytes q)
{
return new List<long>();
var result = new List<long>();
var auth = await _unitOfWork.AuthorizationRepository.GetAuthorizationAsync(authKeyId);
if (auth == null)
{
return result;
}

var contactList = _unitOfWork.ContactsRepository.GetContacts(auth.Value.AsAuthInfo().UserId);
foreach (var c in contactList)
{
result.Add(c.AsContact().UserId);
}

return result;
}

public async Task<ICollection<TLContactStatus>> GetStatuses(long authKeyId)
Expand Down
4 changes: 4 additions & 0 deletions Ferrite/ServerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ private static void RegisterContactsMethods(ContainerBuilder builder)
.Keyed<ITLFunction>(
new FunctionKey(DefaultLayer, Constructors.layer150_ResolveUsername))
.SingleInstance();
builder.RegisterType<GetContactIdsFunc>()
.Keyed<ITLFunction>(
new FunctionKey(DefaultLayer, Constructors.layer150_GetContactIDs))
.SingleInstance();
}

private static void RegisterUsersMethods(ContainerBuilder builder)
Expand Down

0 comments on commit c7505ea

Please sign in to comment.