2023-08-21 10:58:17 -04:00
|
|
|
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;
|
2024-03-20 23:18:34 -04:00
|
|
|
using Luski.Shared.PublicServers.V1.Enums;
|
|
|
|
using Luski.Shared.PublicServers.V1.ServerToClient.HTTP;
|
2024-08-27 10:57:22 -04:00
|
|
|
using Luski.Shared.PublicServers.V1.Shared;
|
2023-08-21 10:58:17 -04:00
|
|
|
|
|
|
|
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!;
|
2024-08-27 10:57:22 -04:00
|
|
|
public long ServerProfile { get; init; }
|
2023-08-21 10:58:17 -04:00
|
|
|
public long[] RoleIds { get; init; } = default!;
|
|
|
|
private List<Role>? RawRoles = null;
|
2024-03-20 23:18:34 -04:00
|
|
|
|
2023-08-21 10:58:17 -04:00
|
|
|
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());
|
|
|
|
}
|
2024-08-27 10:57:22 -04:00
|
|
|
|
|
|
|
public async Task<bool> 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);
|
|
|
|
}
|
2023-08-21 10:58:17 -04:00
|
|
|
|
|
|
|
public Task<PublicKeyInfo[]> GetUserKeys(CancellationToken CancellationToken)
|
|
|
|
{
|
2024-03-20 23:18:34 -04:00
|
|
|
KeysGetSTC data = Server.GetFromServer($"Keys/UserKeys/{Id}", KeysGetSTCContext.Default.KeysGetSTC, CancellationToken).Result;
|
2023-08-21 10:58:17 -04:00
|
|
|
List<PublicKeyInfo> pki = new();
|
2024-03-20 23:18:34 -04:00
|
|
|
foreach (KeyGetSTC key in data.Keys)
|
2023-08-21 10:58:17 -04:00
|
|
|
{
|
|
|
|
pki.Add(new()
|
|
|
|
{
|
2024-03-20 23:18:34 -04:00
|
|
|
Id = key.ID,
|
|
|
|
Owner = key.Owner,
|
|
|
|
EncryptionType = key.EncryptionType,
|
|
|
|
Data = Convert.FromBase64String(key.Data)
|
2023-08-21 10:58:17 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return Task.FromResult(pki.ToArray());
|
|
|
|
}
|
|
|
|
}
|