Luski.Net/Luski.net/Structures/Public/SocketAppUser.cs
2024-03-20 23:18:34 -04:00

54 lines
2.0 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!;
public async Task<SocketChannel> GetSelectedChannel(CancellationToken Token)
{
return await Server.GetChannel<SocketChannel>(SelectedChannel, Token);
}
public async Task<bool> HasAccessToCategory(SocketCategory Category, ServerPermission RequiredPerms)
{
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;
GoodPerms |= CatUserOveride.GoodPermissions;
}
RoleOveride[] CatRoleOverides = await Category.GetRoleOverrides();
foreach (RoleOveride CatRoleOveride in CatRoleOverides)
{
if (!RoleIds.Contains(CatRoleOveride.ParentRoleID)) continue;
if ((CatRoleOveride.BadPermissions & RequiredPerms) > ServerPermission.None) return false;
GoodPerms |= CatRoleOveride.GoodPermissions;
}
foreach (Role RoleID in UserRoleIDList)
{
if (((RoleID.ServerPermissions & RequiredPerms) ^ GoodPerms) > ServerPermission.None) return false;
GoodPerms |= RoleID.ServerPermissions;
}
return GoodPerms.HasPermission(RequiredPerms);
}
public string Username { get; internal set; } = default!;
}