Luski.Net/Luski.net/Structures/Public/SocketAppUser.cs
2024-08-27 10:57:22 -04:00

78 lines
2.7 KiB
C#
Executable File

using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Luski.Shared.PublicServers.V1.Enums;
using Luski.Shared.PublicServers.V1.Shared;
namespace Luski.net.Structures.Public;
public class SocketAppUser : SocketUser
{
public long SelectedChannel { get; init; } = default!;
private List<ServerProfile>? _serverProfiles;
public async Task<SocketChannel> GetSelectedChannel(CancellationToken Token)
{
return await Server.GetChannel<SocketChannel>(SelectedChannel, Token);
}
public async Task<ServerProfile[]> GetProfiles(CancellationToken Token)
{
if (_serverProfiles is null)
{
_serverProfiles = (await Server.GetMyProfiles(Token)).ToList();
}
return _serverProfiles.ToArray();
}
public async Task<bool> HasAccessToCategory(SocketCategory Category, ServerPermission RequiredPerms = ServerPermission.ViewThis)
{
if (Category.Server != Server) return false;
if (Server.OwnerID == Id) return true;
Role[] UserRoleIDList = await GetRoles();
RequiredPerms |= ServerPermission.ViewThis;
ServerPermission GoodPerms = ServerPermission.None;
UserOverride[] CatUserOverides = await Category.GetUserOverrides();
foreach (UserOverride CatUserOveride in CatUserOverides)
{
if (CatUserOveride.UserID != Id) continue;
if ((CatUserOveride.BadPermissions & RequiredPerms) > ServerPermission.None) return false;
if ((CatUserOveride.GoodPermissions & RequiredPerms) == RequiredPerms) return true;
GoodPerms |= CatUserOveride.GoodPermissions;
break;
}
RoleOverride[] CatRoleOverides = await Category.GetRoleOverrides();
int bad_index = -1;
int good_index = -1;
foreach (RoleOverride CatRoleOveride in CatRoleOverides)
{
if (!RoleIds.Contains(CatRoleOveride.ParentRoleID)) continue;
var index = (await CatRoleOveride.GetRole()).Index;
if ((CatRoleOveride.BadPermissions & RequiredPerms) > ServerPermission.None)
{
if (bad_index < index)
bad_index = index;
}
else good_index = index;
GoodPerms |= CatRoleOveride.GoodPermissions;
}
if (bad_index > good_index) return false;
foreach (Role RoleID in UserRoleIDList)
{
GoodPerms |= RoleID.ServerPermissions;
}
return GoodPerms.HasPermission(RequiredPerms);
}
public string Username { get; internal set; } = default!;
}