From d0e996a89570b79ff97820e006a9b1ec4131c853 Mon Sep 17 00:00:00 2001 From: Gehongyan Date: Thu, 24 Oct 2024 16:20:41 +0800 Subject: [PATCH] room user roles --- .../Entities/Users/IRoomUser.cs | 68 +++++++++++++++++++ .../Entitites/Users/SocketRoomUser.cs | 44 +++++++++++- .../Entitites/Users/SocketUserHelper.cs | 42 ++++++++++++ 3 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 src/HeyBox.Net.WebSocket/Entitites/Users/SocketUserHelper.cs diff --git a/src/HeyBox.Net.Core/Entities/Users/IRoomUser.cs b/src/HeyBox.Net.Core/Entities/Users/IRoomUser.cs index 2423096..45f7121 100644 --- a/src/HeyBox.Net.Core/Entities/Users/IRoomUser.cs +++ b/src/HeyBox.Net.Core/Entities/Users/IRoomUser.cs @@ -35,4 +35,72 @@ public interface IRoomUser : IUser /// 获取此用户所属房间的 ID。 /// ulong RoomId { get; } + + #region Roles + + /// + /// 在该房间内授予此用户指定的角色。 + /// + /// 要在该房间内为此用户授予的角色的 ID。 + /// 发送请求时要使用的选项。 + /// 一个表示异步授予操作的任务。 + Task AddRoleAsync(ulong roleId, RequestOptions? options = null); + + /// + /// 在该房间内授予此用户指定的角色。 + /// + /// 要在该房间内为此用户授予的角色。 + /// 发送请求时要使用的选项。 + /// 一个表示异步授予操作的任务。 + Task AddRoleAsync(IRole role, RequestOptions? options = null); + + /// + /// 在该房间内授予此用户指定的一些角色。 + /// + /// 要在该房间内为此用户授予的所有角色的 ID。 + /// 发送请求时要使用的选项。 + /// 一个表示异步授予操作的任务。 + Task AddRolesAsync(IEnumerable roleIds, RequestOptions? options = null); + + /// + /// 在该房间内授予此用户指定的一些角色。 + /// + /// 要在该房间内为此用户授予的所有角色。 + /// 发送请求时要使用的选项。 + /// 一个表示异步授予操作的任务。 + Task AddRolesAsync(IEnumerable roles, RequestOptions? options = null); + + /// + /// 在该房间内撤销此用户指定的角色。 + /// + /// 要在该房间内为此用户撤销的角色的 ID。 + /// 发送请求时要使用的选项。 + /// 一个表示异步撤销操作的任务。 + Task RemoveRoleAsync(ulong roleId, RequestOptions? options = null); + + /// + /// 在该房间内撤销此用户指定的角色。 + /// + /// 要在该房间内为此用户撤销的角色。 + /// 发送请求时要使用的选项。 + /// 一个表示异步撤销操作的任务。 + Task RemoveRoleAsync(IRole role, RequestOptions? options = null); + + /// + /// 在该房间内撤销此用户指定的一些角色。 + /// + /// 要在该房间内为此用户撤销的所有角色的 ID。 + /// 发送请求时要使用的选项。 + /// 一个表示异步撤销操作的任务。 + Task RemoveRolesAsync(IEnumerable roleIds, RequestOptions? options = null); + + /// + /// 在该房间内撤销此用户指定的一些角色。 + /// + /// 要在该房间内为此用户撤销的所有角色。 + /// 发送请求时要使用的选项。 + /// 一个表示异步撤销操作的任务。 + Task RemoveRolesAsync(IEnumerable roles, RequestOptions? options = null); + + #endregion } diff --git a/src/HeyBox.Net.WebSocket/Entitites/Users/SocketRoomUser.cs b/src/HeyBox.Net.WebSocket/Entitites/Users/SocketRoomUser.cs index 62060d2..ee0f77b 100644 --- a/src/HeyBox.Net.WebSocket/Entitites/Users/SocketRoomUser.cs +++ b/src/HeyBox.Net.WebSocket/Entitites/Users/SocketRoomUser.cs @@ -66,7 +66,7 @@ public override int? Level /// /// 获取此用户在该房间内拥有的所有角色。 /// - public IReadOnlyCollection Roles => [..Room.Roles.Where(x => _roleIds.Contains(x.Id))]; + public IReadOnlyCollection Roles => [.._roleIds.Select(x => Room.GetRole(x)).OfType()]; /// internal SocketRoomUser(SocketRoom room, SocketGlobalUser globalUser) @@ -99,6 +99,48 @@ internal override void Update(ClientState state, API.RoomUser model) _roleIds = [..model.Roles]; } + internal void AddRole(ulong roleId) + { + _roleIds = [.._roleIds, roleId]; + } + + internal void RemoveRole(ulong roleId) + { + _roleIds = _roleIds.Remove(roleId); + } + + /// + public Task AddRoleAsync(ulong roleId, RequestOptions? options = null) => + AddRolesAsync([roleId], options); + + /// + public Task AddRoleAsync(IRole role, RequestOptions? options = null) => + AddRoleAsync(role.Id, options); + + /// + public Task AddRolesAsync(IEnumerable roleIds, RequestOptions? options = null) => + SocketUserHelper.AddRolesAsync(this, Client, roleIds, options); + + /// + public Task AddRolesAsync(IEnumerable roles, RequestOptions? options = null) => + AddRolesAsync(roles.Select(x => x.Id), options); + + /// + public Task RemoveRoleAsync(ulong roleId, RequestOptions? options = null) => + RemoveRolesAsync([roleId], options); + + /// + public Task RemoveRoleAsync(IRole role, RequestOptions? options = null) => + RemoveRoleAsync(role.Id, options); + + /// + public Task RemoveRolesAsync(IEnumerable roleIds, RequestOptions? options = null) => + SocketUserHelper.RemoveRolesAsync(this, Client, roleIds, options); + + /// + public Task RemoveRolesAsync(IEnumerable roles, RequestOptions? options = null) => + RemoveRolesAsync(roles.Select(x => x.Id)); + #region IRoomUser /// diff --git a/src/HeyBox.Net.WebSocket/Entitites/Users/SocketUserHelper.cs b/src/HeyBox.Net.WebSocket/Entitites/Users/SocketUserHelper.cs new file mode 100644 index 0000000..1cba3bf --- /dev/null +++ b/src/HeyBox.Net.WebSocket/Entitites/Users/SocketUserHelper.cs @@ -0,0 +1,42 @@ +using HeyBox.API.Rest; + +namespace HeyBox.WebSocket; + +internal static class SocketUserHelper +{ + public static async Task AddRolesAsync(SocketRoomUser user, BaseSocketClient client, + IEnumerable roleIds, RequestOptions? options) + { + IEnumerable args = roleIds + .Distinct() + .Select(x => new AddOrRemoveRoleParams + { + RoomId = user.Room.Id, + RoleId = x, + UserId = user.Id + }); + foreach (AddOrRemoveRoleParams arg in args) + { + await client.ApiClient.AddRoleAsync(arg, options).ConfigureAwait(false); + user.AddRole(arg.RoleId); + } + } + + public static async Task RemoveRolesAsync(SocketRoomUser user, BaseSocketClient client, + IEnumerable roleIds, RequestOptions? options) + { + IEnumerable args = roleIds + .Distinct() + .Select(x => new AddOrRemoveRoleParams + { + RoomId = user.Room.Id, + RoleId = x, + UserId = user.Id + }); + foreach (AddOrRemoveRoleParams arg in args) + { + await client.ApiClient.RemoveRoleAsync(arg, options).ConfigureAwait(false); + user.RemoveRole(arg.RoleId); + } + } +}