Added stores for connections. Added requests for groups, group chatters, group permissions & connections. Using TTS Voice State store.
This commit is contained in:
67
Requests/DeleteGroup.cs
Normal file
67
Requests/DeleteGroup.cs
Normal file
@ -0,0 +1,67 @@
|
||||
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 == 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());
|
||||
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);
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user