using System; using System.Collections.Generic; using System.IO; 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.Shared.PublicServers.V1.Enums; using Luski.Shared.PublicServers.V1.ServerToClient.HTTP; using Luski.Shared.PublicServers.V1.Shared; namespace Luski.net.Structures.Public; public class SocketUser : IUser { public PublicServer Server { get; init; } = default!; public long Id { get; init; } = default!; public virtual UserStatus Status { get; internal set; } = default!; public long ServerProfile { get; init; } public long[] RoleIds { get; init; } = default!; private List? RawRoles = null; public async Task 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 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 async Task HasPermissions(ServerPermission RequiredPerms) { if (Server.OwnerID == Id) return true; Role[] UserRoleIDList = await GetRoles(); ServerPermission op = ServerPermission.None; foreach (Role RoleID in UserRoleIDList) { op |= RoleID.ServerPermissions; } return op.HasPermission(RequiredPerms); } public Task GetUserKeys(CancellationToken CancellationToken) { KeysGetSTC data = Server.GetFromServer($"Keys/UserKeys/{Id}", KeysGetSTCContext.Default.KeysGetSTC, CancellationToken).Result; List pki = new(); foreach (KeyGetSTC key in data.Keys) { pki.Add(new() { Id = key.ID, Owner = key.Owner, EncryptionType = key.EncryptionType, Data = Convert.FromBase64String(key.Data) }); } return Task.FromResult(pki.ToArray()); } }