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 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 updating 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.Modify(name, action); if (result) { _logger.Information($"Updated redeemable action on channel [name: {name}][type: {type}][has message: {hasMessage}][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.")); } } }