67 lines
2.6 KiB
C#
67 lines
2.6 KiB
C#
using HermesSocketServer.Models;
|
|
using ILogger = Serilog.ILogger;
|
|
|
|
namespace HermesSocketServer.Requests
|
|
{
|
|
public class DeleteGroup : IRequest
|
|
{
|
|
public string Name => "delete_group";
|
|
public string[] RequiredKeys => ["id"];
|
|
private ILogger _logger;
|
|
|
|
public DeleteGroup(ILogger logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
|
|
{
|
|
var groupId = data["id"].ToString()!;
|
|
|
|
var result = channel.Groups.Remove(groupId);
|
|
if (result)
|
|
{
|
|
var permissions = channel.GroupPermissions.Get().Values
|
|
.Where(p => p.GroupId.ToString() == groupId);
|
|
|
|
Task? chattersSave = null;
|
|
if (channel.Groups.Chatters.TryGetValue(groupId, out var chatters))
|
|
{
|
|
var filteredChatters = chatters.Get().Values.Where(c => c.GroupId == groupId).ToArray();
|
|
if (filteredChatters.Any())
|
|
{
|
|
foreach (var chatter in filteredChatters)
|
|
{
|
|
var res = chatters.Remove(chatter.ChatterId.ToString(), fromCascade: true);
|
|
if (!res)
|
|
_logger.Warning($"Failed to delete group chatter by id [group chatter id: {chatter.ChatterId}]");
|
|
}
|
|
|
|
chattersSave = chatters.Save();
|
|
}
|
|
}
|
|
|
|
foreach (var permission in permissions)
|
|
{
|
|
var res = channel.GroupPermissions.Remove(permission.Id, fromCascade: true);
|
|
if (!res)
|
|
_logger.Warning($"Failed to delete group permission by id [group chatter id: {permission.Id}]");
|
|
}
|
|
|
|
if (chattersSave != null)
|
|
await Task.WhenAll(chattersSave, channel.GroupPermissions.Save());
|
|
else
|
|
await channel.GroupPermissions.Save();
|
|
|
|
if (!channel.Groups.Chatters.Remove(groupId))
|
|
_logger.Warning($"Failed to delete group chatters from inner store [group id: {groupId}]");
|
|
|
|
_logger.Information($"Deleted a group by id [group id: {groupId}]");
|
|
return RequestResult.Successful(null);
|
|
}
|
|
|
|
_logger.Warning($"Group Id does not exist [group id: {groupId}]");
|
|
return RequestResult.Failed("Something went wrong when updating the cache.");
|
|
}
|
|
}
|
|
} |