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