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

78 lines
2.7 KiB
C#
Raw Permalink Normal View History

2024-03-20 23:18:34 -04:00
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2024-03-20 23:18:34 -04:00
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!;
2024-08-27 10:57:22 -04:00
private List<ServerProfile>? _serverProfiles;
public async Task<SocketChannel> GetSelectedChannel(CancellationToken Token)
{
return await Server.GetChannel<SocketChannel>(SelectedChannel, Token);
}
2024-08-27 10:57:22 -04:00
public async Task<ServerProfile[]> GetProfiles(CancellationToken Token)
{
if (_serverProfiles is null)
{
_serverProfiles = (await Server.GetMyProfiles(Token)).ToList();
}
return _serverProfiles.ToArray();
}
2024-03-31 23:57:12 -04:00
public async Task<bool> HasAccessToCategory(SocketCategory Category, ServerPermission RequiredPerms = ServerPermission.ViewThis)
2024-03-20 23:18:34 -04:00
{
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;
2024-08-27 10:57:22 -04:00
if ((CatUserOveride.GoodPermissions & RequiredPerms) == RequiredPerms) return true;
2024-03-20 23:18:34 -04:00
GoodPerms |= CatUserOveride.GoodPermissions;
2024-08-27 10:57:22 -04:00
break;
2024-03-20 23:18:34 -04:00
}
2024-03-31 23:57:12 -04:00
RoleOverride[] CatRoleOverides = await Category.GetRoleOverrides();
int bad_index = -1;
int good_index = -1;
foreach (RoleOverride CatRoleOveride in CatRoleOverides)
2024-03-20 23:18:34 -04:00
{
if (!RoleIds.Contains(CatRoleOveride.ParentRoleID)) continue;
2024-03-31 23:57:12 -04:00
var index = (await CatRoleOveride.GetRole()).Index;
if ((CatRoleOveride.BadPermissions & RequiredPerms) > ServerPermission.None)
{
if (bad_index < index)
bad_index = index;
}
else good_index = index;
2024-03-20 23:18:34 -04:00
GoodPerms |= CatRoleOveride.GoodPermissions;
}
2024-03-31 23:57:12 -04:00
if (bad_index > good_index) return false;
2024-03-20 23:18:34 -04:00
foreach (Role RoleID in UserRoleIDList)
{
GoodPerms |= RoleID.ServerPermissions;
}
return GoodPerms.HasPermission(RequiredPerms);
}
public string Username { get; internal set; } = default!;
}