hermes-server/Requests/UpdateRedeemableAction.cs

59 lines
2.1 KiB
C#
Raw Normal View History

using System.Text.Json;
using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests
{
public class UpdateRedeemableAction : IRequest
{
public string Name => "update_redeemable_action";
public string[] RequiredKeys => ["name", "data", "type"];
private ILogger _logger;
public UpdateRedeemableAction(ILogger logger)
{
_logger = logger;
}
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{
string name = data["name"].ToString()!;
string d = data["data"].ToString()!;
string type = data["type"].ToString()!;
IDictionary<string, string> dict = new Dictionary<string, string>();
try
{
dict = JsonSerializer.Deserialize<IDictionary<string, string>>(d)!;
}
catch (Exception ex)
{
_logger.Error(ex, $"Failed to parse data on redeemable action while updating action [name: {name}][type: {type}][data: {d}]");
return Task.FromResult(RequestResult.Failed("Could not parse the data on this action."));
}
var action = new RedeemableAction()
{
UserId = channel.Id,
Name = name,
Data = dict,
Type = type,
};
bool result = channel.Actions.Modify(name, action =>
{
action.Type = type;
action.Data = dict;
});
if (result)
{
_logger.Information($"Added redeemable action to channel [name: {name}][type: {type}][channel: {channel.Id}]");
return Task.FromResult(RequestResult.Successful(action));
}
if (channel.Actions.Get(name) == null)
return Task.FromResult(RequestResult.Failed("Action does not exist."));
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
}
}
}