Added database table data into configuration. Store saves is auto-handled. Added Action & Redemption stores.
This commit is contained in:
53
Requests/CreateRedeemableAction.cs
Normal file
53
Requests/CreateRedeemableAction.cs
Normal file
@ -0,0 +1,53 @@
|
||||
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", "data", "type"];
|
||||
private ILogger _logger;
|
||||
|
||||
public CreateRedeemableAction(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 creating 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.Set(name, action);
|
||||
if (result)
|
||||
{
|
||||
_logger.Information($"Added redeemable action to channel [name: {name}][type: {type}][channel: {channel.Id}]");
|
||||
return Task.FromResult(RequestResult.Successful(action));
|
||||
}
|
||||
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
|
||||
}
|
||||
}
|
||||
}
|
47
Requests/CreateRedemption.cs
Normal file
47
Requests/CreateRedemption.cs
Normal file
@ -0,0 +1,47 @@
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using HermesSocketServer.Models;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
{
|
||||
public class CreateRedemption : IRequest
|
||||
{
|
||||
public string Name => "create_redemption";
|
||||
public string[] RequiredKeys => ["redemption", "action", "order"];
|
||||
private ILogger _logger;
|
||||
|
||||
public CreateRedemption(ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
|
||||
{
|
||||
var id = Guid.NewGuid();
|
||||
string redemptionId = data["redemption"].ToString()!;
|
||||
string actionName = data["action"].ToString()!;
|
||||
if (channel.Actions.Get(actionName) == null)
|
||||
return Task.FromResult(RequestResult.Failed("Action Name must be an existing action."));
|
||||
if (!int.TryParse(data["order"].ToString()!, out var order))
|
||||
return Task.FromResult(RequestResult.Failed("Order must be an integer."));
|
||||
|
||||
var redemption = new Redemption()
|
||||
{
|
||||
Id = id.ToString(),
|
||||
UserId = channel.Id,
|
||||
RedemptionId = redemptionId,
|
||||
ActionName = actionName,
|
||||
Order = order,
|
||||
State = true,
|
||||
};
|
||||
|
||||
bool result = channel.Redemptions.Set(id.ToString(), redemption);
|
||||
if (result)
|
||||
{
|
||||
_logger.Information($"Added redemption to channel [id: {id}][redemption id: {redemptionId}][action: {actionName}][order: {order}][channel: {channel.Id}]");
|
||||
return Task.FromResult(RequestResult.Successful(redemption));
|
||||
}
|
||||
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using HermesSocketServer.Models;
|
||||
using HermesSocketServer.Store;
|
||||
using ILogger = Serilog.ILogger;
|
||||
@ -8,7 +9,7 @@ namespace HermesSocketServer.Requests
|
||||
{
|
||||
public string Name => "create_tts_voice";
|
||||
public string[] RequiredKeys => ["voice"];
|
||||
private IStore<string, Voice> _voices;
|
||||
private IStore<string, TTSVoice> _voices;
|
||||
private ILogger _logger;
|
||||
private Random _random;
|
||||
|
||||
@ -24,7 +25,7 @@ namespace HermesSocketServer.Requests
|
||||
string voice = data["voice"].ToString()!;
|
||||
string id = RandomString(25);
|
||||
|
||||
var result = _voices.Set(id, new Voice()
|
||||
var result = _voices.Set(id, new TTSVoice()
|
||||
{
|
||||
Id = id,
|
||||
Name = voice
|
||||
|
@ -25,7 +25,7 @@ namespace HermesSocketServer.Requests
|
||||
}
|
||||
|
||||
_logger.Warning($"Failed to find policy by id [id: {policyId}]");
|
||||
return Task.FromResult(RequestResult.Failed("Cannot find the policy by id."));
|
||||
return Task.FromResult(RequestResult.Failed("Policy ID does not exist."));
|
||||
}
|
||||
}
|
||||
}
|
32
Requests/DeleteRedeemableAction.cs
Normal file
32
Requests/DeleteRedeemableAction.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using HermesSocketServer.Models;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
{
|
||||
public class DeleteRedeemableAction : IRequest
|
||||
{
|
||||
public string Name => "delete_redeemable_action";
|
||||
public string[] RequiredKeys => ["name"];
|
||||
private ILogger _logger;
|
||||
|
||||
public DeleteRedeemableAction(ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
|
||||
{
|
||||
string name = data["name"].ToString()!;
|
||||
var result = channel.Actions.Remove(name);
|
||||
|
||||
if (result)
|
||||
{
|
||||
_logger.Information($"Deleted a redeemable action by name [name: {name}]");
|
||||
return Task.FromResult(RequestResult.Successful(null));
|
||||
}
|
||||
|
||||
_logger.Warning($"Action name does not exist [id: {name}]");
|
||||
return Task.FromResult(RequestResult.Failed("Action name does not exist."));
|
||||
}
|
||||
}
|
||||
}
|
32
Requests/DeleteRedemption.cs
Normal file
32
Requests/DeleteRedemption.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using HermesSocketServer.Models;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
{
|
||||
public class DeleteRedemption : IRequest
|
||||
{
|
||||
public string Name => "delete_redemption";
|
||||
public string[] RequiredKeys => ["id"];
|
||||
private ILogger _logger;
|
||||
|
||||
public DeleteRedemption(ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
|
||||
{
|
||||
string id = data["id"].ToString()!;
|
||||
var result = channel.Redemptions.Remove(id);
|
||||
|
||||
if (result)
|
||||
{
|
||||
_logger.Information($"Deleted a redemption by id [id: {id}]");
|
||||
return Task.FromResult(RequestResult.Successful(null));
|
||||
}
|
||||
|
||||
_logger.Warning($"Redemption ID does not exist [id: {id}]");
|
||||
return Task.FromResult(RequestResult.Failed("Redemption ID does not exist."));
|
||||
}
|
||||
}
|
||||
}
|
@ -17,10 +17,16 @@ namespace HermesSocketServer.Requests
|
||||
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
|
||||
{
|
||||
string filterId = data["id"].ToString()!;
|
||||
channel.Filters.Remove(filterId);
|
||||
var result = channel.Filters.Remove(filterId);
|
||||
|
||||
_logger.Information($"Deleted a TTS filter by id [tts filter id: {filterId}]");
|
||||
return Task.FromResult(RequestResult.Successful(null));
|
||||
if (result)
|
||||
{
|
||||
_logger.Information($"Deleted a TTS filter by id [tts filter id: {filterId}]");
|
||||
return Task.FromResult(RequestResult.Successful(null));
|
||||
}
|
||||
|
||||
_logger.Warning($"Filter ID does not exist [id: {filterId}]");
|
||||
return Task.FromResult(RequestResult.Failed("Filter ID does not exist."));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using HermesSocketServer.Models;
|
||||
using HermesSocketServer.Store;
|
||||
using ILogger = Serilog.ILogger;
|
||||
@ -8,7 +9,7 @@ namespace HermesSocketServer.Requests
|
||||
{
|
||||
public string Name => "delete_tts_voice";
|
||||
public string[] RequiredKeys => ["voice"];
|
||||
private IStore<string, Voice> _voices;
|
||||
private IStore<string, TTSVoice> _voices;
|
||||
private ILogger _logger;
|
||||
|
||||
public DeleteTTSVoice(VoiceStore voices, ILogger logger)
|
||||
@ -20,9 +21,16 @@ namespace HermesSocketServer.Requests
|
||||
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
|
||||
{
|
||||
string voiceId = data["voice"].ToString()!;
|
||||
_voices.Remove(voiceId);
|
||||
_logger.Information($"Deleted a voice by id [voice id: {voiceId}]");
|
||||
return Task.FromResult(RequestResult.Successful(null));
|
||||
var result = _voices.Remove(voiceId);
|
||||
|
||||
if (result)
|
||||
{
|
||||
_logger.Information($"Deleted a voice by id [voice id: {voiceId}]");
|
||||
return Task.FromResult(RequestResult.Successful(null));
|
||||
}
|
||||
|
||||
_logger.Warning($"Voice ID does not exist [id: {voiceId}]");
|
||||
return Task.FromResult(RequestResult.Failed("Voice ID does not exist."));
|
||||
}
|
||||
}
|
||||
}
|
@ -20,10 +20,10 @@ namespace HermesSocketServer.Requests
|
||||
|
||||
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
|
||||
{
|
||||
IEnumerable<VoiceDetails> voices = _voices.Get().Select(v => new VoiceDetails()
|
||||
IEnumerable<TTSVoice> voices = _voices.Get().Select(v => new TTSVoice()
|
||||
{
|
||||
Id = v.Value.Id,
|
||||
Name = v.Value.Name
|
||||
Name = v.Value.Name,
|
||||
});
|
||||
|
||||
_logger.Information($"Fetched all TTS voices for channel [channel: {channel.Id}]");
|
||||
|
59
Requests/UpdateRedeemableAction.cs
Normal file
59
Requests/UpdateRedeemableAction.cs
Normal file
@ -0,0 +1,59 @@
|
||||
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."));
|
||||
}
|
||||
}
|
||||
}
|
62
Requests/UpdateRedemption.cs
Normal file
62
Requests/UpdateRedemption.cs
Normal file
@ -0,0 +1,62 @@
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using HermesSocketServer.Models;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
{
|
||||
public class UpdateRedemption : IRequest
|
||||
{
|
||||
public string Name => "update_redemption";
|
||||
public string[] RequiredKeys => ["id", "redemption", "action", "order"];
|
||||
private ILogger _logger;
|
||||
|
||||
public UpdateRedemption(ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
|
||||
{
|
||||
var id = data["id"].ToString()!;
|
||||
string redemptionId = data["redemption"].ToString()!;
|
||||
string actionName = data["action"].ToString()!;
|
||||
if (channel.Actions.Get(actionName) == null)
|
||||
return Task.FromResult(RequestResult.Failed("Action Name must be an existing action."));
|
||||
if (!int.TryParse(data["order"].ToString()!, out var order))
|
||||
return Task.FromResult(RequestResult.Failed("Order must be an integer."));
|
||||
bool state = data["state"].ToString()?.ToLower() == "true";
|
||||
|
||||
var redemption = new Redemption()
|
||||
{
|
||||
Id = id,
|
||||
UserId = channel.Id,
|
||||
RedemptionId = redemptionId,
|
||||
ActionName = actionName,
|
||||
Order = order,
|
||||
State = true,
|
||||
};
|
||||
|
||||
bool result = channel.Redemptions.Modify(id.ToString(), r =>
|
||||
{
|
||||
if (r.UserId != channel.Id)
|
||||
return;
|
||||
|
||||
r.RedemptionId = redemptionId;
|
||||
r.ActionName = actionName;
|
||||
r.Order = order;
|
||||
r.State = state;
|
||||
});
|
||||
|
||||
var r = channel.Redemptions.Get(id);
|
||||
if (result)
|
||||
{
|
||||
_logger.Information($"Added redemption to channel [id: {id}][redemption id: {redemptionId}][action: {actionName}][order: {order}][channel: {channel.Id}]");
|
||||
return Task.FromResult(RequestResult.Successful(r));
|
||||
}
|
||||
|
||||
if (r == null || r.UserId != channel.Id)
|
||||
return Task.FromResult(RequestResult.Failed("Redemption does not exist."));
|
||||
return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache."));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using HermesSocketServer.Models;
|
||||
using HermesSocketServer.Store;
|
||||
using ILogger = Serilog.ILogger;
|
||||
@ -8,7 +9,7 @@ namespace HermesSocketServer.Requests
|
||||
{
|
||||
public string Name => "update_tts_voice";
|
||||
public string[] RequiredKeys => ["voice", "voiceId"];
|
||||
private IStore<string, Voice> _voices;
|
||||
private IStore<string, TTSVoice> _voices;
|
||||
private ILogger _logger;
|
||||
|
||||
public UpdateTTSVoice(VoiceStore voices, ILogger logger)
|
||||
@ -22,7 +23,7 @@ namespace HermesSocketServer.Requests
|
||||
string voiceName = data["voice"].ToString()!;
|
||||
string voiceId = data["voiceid"].ToString()!;
|
||||
|
||||
var result = _voices.Set(voiceId, new Voice()
|
||||
var result = _voices.Set(voiceId, new TTSVoice()
|
||||
{
|
||||
Id = voiceId,
|
||||
Name = voiceName
|
||||
|
Reference in New Issue
Block a user