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

77 lines
2.6 KiB
C#
Raw Normal View History

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;
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; }
public long[] RoleIds { get; init; } = default!;
private List<Role>? RawRoles = null;
2024-03-20 23:18:34 -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);
}
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;
List<PublicKeyInfo> pki = new();
2024-03-20 23:18:34 -04:00
foreach (KeyGetSTC key in data.Keys)
{
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)
});
}
return Task.FromResult(pki.ToArray());
}
}