From 82ccdd83075bec8d95a57c429f4458b3d2e3e51c Mon Sep 17 00:00:00 2001 From: JacobTech Date: Sun, 31 Mar 2024 23:57:12 -0400 Subject: [PATCH] Overrides & Members --- Luski.net/API.cs | 2 +- Luski.net/Luski.net.csproj | 4 +- Luski.net/PublicServer.Account.cs | 11 +++ Luski.net/PublicServer.cs | 49 ++++++++++++- Luski.net/Structures/Public/Role.cs | 11 ++- .../{RoleOveride.cs => RoleOverride.cs} | 14 ++-- Luski.net/Structures/Public/SocketAppUser.cs | 21 ++++-- Luski.net/Structures/Public/SocketCategory.cs | 4 +- Luski.net/Structures/Public/SocketChannel.cs | 72 +++++++++++++++++-- 9 files changed, 159 insertions(+), 29 deletions(-) rename Luski.net/Structures/Public/{RoleOveride.cs => RoleOverride.cs} (65%) diff --git a/Luski.net/API.cs b/Luski.net/API.cs index 306244d..ec74a72 100644 --- a/Luski.net/API.cs +++ b/Luski.net/API.cs @@ -107,7 +107,7 @@ public class API { IEnumerable isl = InternalServers.Where(a => (a.Domain == Domain && a.ApiVersion == Version)); if (isl.Any()) return isl.First(); - s = await PublicServer.GetServer(Domain, Version, Secure); + s = await PublicServer.GetServer(Domain, Version, Secure, GenerateEncryption, LogConsole); } catch (Exception e) { diff --git a/Luski.net/Luski.net.csproj b/Luski.net/Luski.net.csproj index ad568a1..b4b67ea 100755 --- a/Luski.net/Luski.net.csproj +++ b/Luski.net/Luski.net.csproj @@ -13,11 +13,11 @@ https://github.com/JacobTech-com/Luski.net True 1.0.0 - 2.0.0-alpha65 + 2.0.0-alpha76 - + diff --git a/Luski.net/PublicServer.Account.cs b/Luski.net/PublicServer.Account.cs index 0542b7a..6cbc47a 100644 --- a/Luski.net/PublicServer.Account.cs +++ b/Luski.net/PublicServer.Account.cs @@ -203,6 +203,17 @@ public partial class PublicServer FailedSystems.Add(new("Auto Status", "Failed to set status on the server", e)); } #endregion + + #region GetRoles + try + { + _ = await GetRoles(); + } + catch (Exception e) + { + FailedSystems.Add(new("Get Roles", "Failed to get roles on the server", e)); + } + #endregion #region Local Storage Cleanup try diff --git a/Luski.net/PublicServer.cs b/Luski.net/PublicServer.cs index 6412524..e2e6b7c 100644 --- a/Luski.net/PublicServer.cs +++ b/Luski.net/PublicServer.cs @@ -29,6 +29,7 @@ public partial class PublicServer : Server public List chans { get; } = new(); public List cats { get; } = new(); public List roles { get; } = new(); + public List roleso { get; } = new(); public SocketAppUser User { get; private set; } = null!; @@ -210,6 +211,47 @@ public partial class PublicServer : Server roles.Add(role); return role; } + + public async Task GetRoleOverride(long id) + { + RoleOverride[] r = roleso.Where(s => s.ID == id).ToArray(); + if (r.Length > 0) return r[0]; + UserRoleOverrideSTC s = await GetFromServer("SocketOverrides/RoleOverride/" + id.ToString(), UserRoleOverrideSTCContext.Default.UserRoleOverrideSTC, CancellationToken.None); + + RoleOverride role = new() + { + ID = s.Id, + BadPermissions = s.BadPermissions, + GoodPermissions = s.GoodPermissions, + ParentRoleID = s.RoleID, + Server = this + }; + roleso.Add(role); + return role; + } + + public async Task GetRoles() + { + RolesSTC s = await GetFromServer("SocketRole/GetAll", RolesSTCContext.Default.RolesSTC, CancellationToken.None); + roles.Clear(); + foreach (var ServerRole in s.Roles) + { + roles.Add(new Role() + { + Server = this, + ID = ServerRole.ID, + Color = new(ServerRole.Color), + Description = ServerRole.Description, + DisplayName = ServerRole.DisplayName, + MembersListID = ServerRole.Members, + Name = ServerRole.Name, + Index = ServerRole.Index, + ServerPermissions = ServerRole.ServerPermissions + }); + } + + return roles.ToArray(); + } public async Task SendMessage(TChannel channel, string msg, SocketMessage? ReplyTo = null, SocketChannelProfile? FakeProfile = null) where TChannel : SocketChannel, new() @@ -344,6 +386,7 @@ public partial class PublicServer : Server Server = this, Color = new(request.Color) }; + chans.Add(bob); return bob; } @@ -364,7 +407,7 @@ public partial class PublicServer : Server new ChannelPostCTS() { Name = Convert.ToBase64String(Encoding.UTF8.GetBytes(Name)), - Description = Convert.ToBase64String(Encoding.UTF8.GetBytes(Description)), + Description = Convert.ToBase64String(Encoding.UTF8.GetBytes(Decription)), EncoderTypes = new[] { EncoderType.UTF16 }, EncryptionKeys = new long[] { 0 }, DescriptionEncoderType = EncoderType.UTF8, @@ -385,7 +428,7 @@ public partial class PublicServer : Server { ID = res.ID, CategoryID = res.Parent, - Description = Description, + Description = Decription, DescriptionEncoderType = res.DescriptionEncoderType, DescriptionEncryptionKey = res.DescriptionEncryptionKey, EncoderTypes = res.EncoderTypes, @@ -439,7 +482,7 @@ public partial class PublicServer : Server } Tuser u = new(); - if (u is SocketUser) + if (typeof(Tuser).FullName == typeof(SocketUser).FullName) { u = new() { diff --git a/Luski.net/Structures/Public/Role.cs b/Luski.net/Structures/Public/Role.cs index bed89d0..9a8e332 100644 --- a/Luski.net/Structures/Public/Role.cs +++ b/Luski.net/Structures/Public/Role.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using Luski.Shared.PublicServers.V1.Enums; @@ -17,13 +18,17 @@ public class Role public required long[] MembersListID { get; init; } = default!; private List? RawUsers = null; - public Task GetMembers() + public async Task GetMembers() { if (RawUsers is null) { - + RawUsers = new(); + foreach (long m in MembersListID) + { + RawUsers.Add(await Server.GetUser(m, CancellationToken.None)); + } } - return Task.FromResult(RawUsers!.ToArray()); + return RawUsers!.ToArray(); } } \ No newline at end of file diff --git a/Luski.net/Structures/Public/RoleOveride.cs b/Luski.net/Structures/Public/RoleOverride.cs similarity index 65% rename from Luski.net/Structures/Public/RoleOveride.cs rename to Luski.net/Structures/Public/RoleOverride.cs index 27dd63a..a83cd7a 100644 --- a/Luski.net/Structures/Public/RoleOveride.cs +++ b/Luski.net/Structures/Public/RoleOverride.cs @@ -4,7 +4,7 @@ using Luski.Shared.PublicServers.V1.Shared; namespace Luski.net.Structures.Public; -public class RoleOveride +public class RoleOverride { public long ID { get; init; } public long ParentRoleID { get; init; } @@ -12,15 +12,11 @@ public class RoleOveride public ServerPermission GoodPermissions { get; set; } public ServerPermission BadPermissions { get; set; } - private Role? Parent = null; + + public required PublicServer Server { get; init; } - public Task GetRole() + public async Task GetRole() { - if (Parent is null) - { - - } - - return Task.FromResult(Parent)!; + return await Server.GetRole(ParentRoleID); } } \ No newline at end of file diff --git a/Luski.net/Structures/Public/SocketAppUser.cs b/Luski.net/Structures/Public/SocketAppUser.cs index 0b91c08..9300ac6 100755 --- a/Luski.net/Structures/Public/SocketAppUser.cs +++ b/Luski.net/Structures/Public/SocketAppUser.cs @@ -16,7 +16,7 @@ public class SocketAppUser : SocketUser return await Server.GetChannel(SelectedChannel, Token); } - public async Task HasAccessToCategory(SocketCategory Category, ServerPermission RequiredPerms) + public async Task HasAccessToCategory(SocketCategory Category, ServerPermission RequiredPerms = ServerPermission.ViewThis) { if (Category.Server != Server) return false; if (Server.OwnerID == Id) return true; @@ -33,18 +33,29 @@ public class SocketAppUser : SocketUser GoodPerms |= CatUserOveride.GoodPermissions; } - RoleOveride[] CatRoleOverides = await Category.GetRoleOverrides(); - foreach (RoleOveride CatRoleOveride in CatRoleOverides) + + RoleOverride[] CatRoleOverides = await Category.GetRoleOverrides(); + int bad_index = -1; + int good_index = -1; + + foreach (RoleOverride CatRoleOveride in CatRoleOverides) { if (!RoleIds.Contains(CatRoleOveride.ParentRoleID)) continue; - if ((CatRoleOveride.BadPermissions & RequiredPerms) > ServerPermission.None) return false; + 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) { - if (((RoleID.ServerPermissions & RequiredPerms) ^ GoodPerms) > ServerPermission.None) return false; GoodPerms |= RoleID.ServerPermissions; } return GoodPerms.HasPermission(RequiredPerms); diff --git a/Luski.net/Structures/Public/SocketCategory.cs b/Luski.net/Structures/Public/SocketCategory.cs index fe0511a..630ba13 100644 --- a/Luski.net/Structures/Public/SocketCategory.cs +++ b/Luski.net/Structures/Public/SocketCategory.cs @@ -17,7 +17,7 @@ public class SocketCategory internal long[] Channels { get; set; } internal long[] Categories { get; set; } SocketCategory? RawParent = null; - List? RawRoleOverides = null; + List? RawRoleOverides = null; List? RawUserOverides = null; List? RawChan = null; List? RawCat = null; @@ -79,7 +79,7 @@ public class SocketCategory public string Name { get; internal set; } public string Description { get; internal set; } - public Task GetRoleOverrides() + public Task GetRoleOverrides() { if (RawRoleOverides is null) { diff --git a/Luski.net/Structures/Public/SocketChannel.cs b/Luski.net/Structures/Public/SocketChannel.cs index 938f26e..ad4b9fc 100644 --- a/Luski.net/Structures/Public/SocketChannel.cs +++ b/Luski.net/Structures/Public/SocketChannel.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Threading; using System.Threading.Tasks; using JacobTechEncryption; @@ -20,7 +21,7 @@ public class SocketChannel internal long[] RoleOverrides { get; set; } internal long[] UserOverrides { get; set; } SocketCategory? RawParent = null; - List? RawRoleOverides = null; + List? RawRoleOverides = null; List? RawUserOverides = null; public PictureType PictureType { get; internal set; } @@ -336,16 +337,20 @@ public class SocketChannel public DateTime Epoch { get; internal set; } public string Name { get; internal set; } public string Description { get; internal set; } - public Task GetRoleOverides() + public async Task GetRoleOverrides() { if (RawRoleOverides is null) { RawRoleOverides = new(); + foreach (long ro in RoleOverrides) + { + RawRoleOverides.Add(await Server.GetRoleOverride(ro)); + } } - return Task.FromResult(RawRoleOverides!.ToArray()); + return RawRoleOverides!.ToArray(); } - public Task GetUserOveride() + public Task GetUserOverride() { if (RawUserOverides is null) { @@ -354,6 +359,65 @@ public class SocketChannel return Task.FromResult(RawUserOverides!.ToArray()); } + + public async Task GetMembers() + { + ServerPermission req = ServerPermission.ViewThis; + List GoodMembers = new(); + List GoodRoles = new(); + List BadRoles = new(); + List GoodPeople = new(); + + foreach (UserOverride cro in await GetUserOverride()) + { + if ((cro.GoodPermissions & req) == req) GoodMembers.Add(cro.UserID); + } + + foreach (RoleOverride ro in (await GetRoleOverrides())) + { + if ((ro.GoodPermissions & req) == req) + { + GoodRoles.Add(ro.ParentRoleID); + } + else if ((ro.BadPermissions & req) == req) + { + BadRoles.Add(ro.ParentRoleID); + } + } + + bool bad = false; + + foreach (Role Role in Server.roles.OrderBy(s => s.Index)) + { + if (BadRoles.Contains(Role.ID)) + { + bad = true; + } + + if (bad && GoodRoles.Contains(Role.ID)) + { + bad = false; + } + + if (!bad) + { + foreach (var m in await Role.GetMembers()) + { + var t = GoodPeople.Where(s => s.Id == m.Id); + if (t.Count() == 0) GoodPeople.Add(m); + } + } + } + + foreach (long m in GoodMembers) + { + var t = GoodPeople.Where(s => s.Id == m); + if (t.Count() == 0) GoodPeople.Add(await Server.GetUser(m, CancellationToken.None)); + } + + return GoodPeople.ToArray(); + } + public long TitleEncryptionKey { get; internal set; } public long DescriptionEncryptionKey { get; internal set; } public long[] EncryptionKeys { get; internal set; }