Luski.Net/Luski.net/Structures/Public/SocketUser.cs

64 lines
2.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Luski.net.Classes;
using Luski.net.Enums;
using Luski.net.Interfaces;
using Luski.net.JsonTypes;
using Luski.net.JsonTypes.BaseTypes;
namespace Luski.net.Structures.Public;
public class SocketUser : IUser
{
public PublicServer Server { get; init; } = default!;
public long Id { get; init; } = default!;
public string DisplayName { get; init; } = default!;
public virtual UserStatus Status { get; internal set; } = default!;
public PictureType PictureType { get; init; } = default!;
public long[] RoleIds { get; init; } = default!;
private List<Role>? RawRoles = null;
public async Task<Role[]> GetRoles()
{
if (RawRoles is null)
{
RawRoles = new();
foreach (long r in RoleIds)
{
Role f = await Server.GetRole(r);
RawRoles.Add(f);
}
RawRoles.Sort(new RoleComparer());
}
return RawRoles.ToArray();
}
public async Task<Stream> GetAvatar(CancellationToken CancellationToken)
{
bool isc = System.IO.File.Exists(Server.Storage.GetStorageDirectory(StorageDirectory.Avatars) + Id.ToString());
if (!isc) await Server.GetFromServer($"socketuserprofile/Avatar/{Id}", Server.Storage.GetStorageDirectory(StorageDirectory.Avatars) + Id.ToString(), CancellationToken);
return Server.Storage.GetResourceStream(StorageDirectory.Avatars, Id.ToString());
}
public Task<PublicKeyInfo[]> GetUserKeys(CancellationToken CancellationToken)
{
UserKeysGetRequest data = Server.GetFromServer($"Keys/UserKeys/{Id}", UserKeysGetRequestContext.Default.UserKeysGetRequest, CancellationToken).Result;
List<PublicKeyInfo> pki = new();
foreach (UserKeyGetRequest key in data.keys)
{
pki.Add(new()
{
Id = key.id,
Owner = key.owner,
EncryptionType = key.encryption_type,
Data = Convert.FromBase64String(key.key_data)
});
}
return Task.FromResult(pki.ToArray());
}
}