Changed sender parameter to channel.

This commit is contained in:
Tom 2024-12-27 23:31:36 +00:00
parent 06bfe110bb
commit 8277ea0154
27 changed files with 113 additions and 134 deletions

View File

@ -1,5 +1,4 @@
using HermesSocketServer.Models; using HermesSocketServer.Models;
using HermesSocketServer.Services;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
@ -8,16 +7,14 @@ namespace HermesSocketServer.Requests
{ {
public string Name => "create_policy"; public string Name => "create_policy";
public string[] RequiredKeys => ["groupId", "path", "count", "span"]; public string[] RequiredKeys => ["groupId", "path", "count", "span"];
private ChannelManager _channels;
private ILogger _logger; private ILogger _logger;
public CreatePolicy(ChannelManager channels, ILogger logger) public CreatePolicy(ILogger logger)
{ {
_channels = channels;
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var id = Guid.NewGuid(); var id = Guid.NewGuid();
string groupId = data["groupId"].ToString()!; string groupId = data["groupId"].ToString()!;
@ -28,19 +25,17 @@ namespace HermesSocketServer.Requests
var policy = new PolicyMessage() var policy = new PolicyMessage()
{ {
Id = id, Id = id,
UserId = sender, UserId = channel.Id,
GroupId = Guid.Parse(groupId), GroupId = Guid.Parse(groupId),
Path = path, Path = path,
Usage = count, Usage = count,
Span = span, Span = span,
}; };
var channel = _channels.Get(sender);
bool result = channel.Policies.Set(id.ToString(), policy); bool result = channel.Policies.Set(id.ToString(), policy);
if (result) if (result)
{ {
_logger.Information($"Added policy to channel [policy id: {id}][group id: {groupId}][path: {path}][count: {count}][span: {span}][channel: {sender}]"); _logger.Information($"Added policy to channel [policy id: {id}][group id: {groupId}][path: {path}][count: {count}][span: {span}][channel: {channel.Id}]");
return RequestResult.Successful(policy); return RequestResult.Successful(policy);
} }
return RequestResult.Failed("Something went wrong when updating the cache."); return RequestResult.Failed("Something went wrong when updating the cache.");

View File

@ -1,5 +1,5 @@
using HermesSocketLibrary.Requests.Messages; using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Services; using HermesSocketServer.Models;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
@ -8,16 +8,14 @@ namespace HermesSocketServer.Requests
{ {
public string Name => "create_tts_filter"; public string Name => "create_tts_filter";
public string[] RequiredKeys => ["search", "replace"]; public string[] RequiredKeys => ["search", "replace"];
private ChannelManager _channels;
private ILogger _logger; private ILogger _logger;
public CreateTTSFilter(ChannelManager channels, ILogger logger) public CreateTTSFilter(ILogger logger)
{ {
_channels = channels;
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var id = Guid.NewGuid(); var id = Guid.NewGuid();
string search = data["search"].ToString()!; string search = data["search"].ToString()!;
@ -26,17 +24,15 @@ namespace HermesSocketServer.Requests
var filter = new TTSWordFilter() var filter = new TTSWordFilter()
{ {
Id = id.ToString(), Id = id.ToString(),
UserId = sender, UserId = channel.Id,
Search = search, Search = search,
Replace = replace, Replace = replace,
}; };
var channel = _channels.Get(sender);
bool result = channel.Filters.Set(id.ToString(), filter); bool result = channel.Filters.Set(id.ToString(), filter);
if (result) if (result)
{ {
_logger.Information($"Added filter to channel [filter id: {id}][search: {search}][replace: {replace}][channel: {sender}]"); _logger.Information($"Added filter to channel [filter id: {id}][search: {search}][replace: {replace}][channel: {channel.Id}]");
return RequestResult.Successful(filter); return RequestResult.Successful(filter);
} }
return RequestResult.Failed("Something went wrong when updating the cache."); return RequestResult.Failed("Something went wrong when updating the cache.");

View File

@ -1,6 +1,5 @@
using HermesSocketLibrary.db; using HermesSocketLibrary.db;
using HermesSocketServer.Models; using HermesSocketServer.Models;
using HermesSocketServer.Services;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
@ -9,37 +8,32 @@ namespace HermesSocketServer.Requests
{ {
public string Name => "create_tts_user"; public string Name => "create_tts_user";
public string[] RequiredKeys => ["chatter", "voice"]; public string[] RequiredKeys => ["chatter", "voice"];
private ChannelManager _channels;
private Database _database; private Database _database;
private readonly ServerConfiguration _configuration; private readonly ServerConfiguration _configuration;
private ILogger _logger; private ILogger _logger;
public CreateTTSUser(ChannelManager channels, Database database, ServerConfiguration configuration, ILogger logger) public CreateTTSUser(Database database, ServerConfiguration configuration, ILogger logger)
{ {
_database = database; _database = database;
_channels = channels;
_configuration = configuration; _configuration = configuration;
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
if (data == null)
return RequestResult.Failed("Data received from client is null.");
if (!long.TryParse(data["chatter"].ToString(), out long chatterId)) if (!long.TryParse(data["chatter"].ToString(), out long chatterId))
return RequestResult.Failed("Invalid Twitch user id"); return RequestResult.Failed("Invalid Twitch user id");
data["user"] = sender; data["user"] = channel.Id;
data["voice"] = data["voice"].ToString(); data["voice"] = data["voice"].ToString()!;
var check = await _database.ExecuteScalar("SELECT state FROM \"TtsVoiceState\" WHERE \"userId\" = @user AND \"ttsVoiceId\" = @voice", data) ?? false; var check = await _database.ExecuteScalar("SELECT state FROM \"TtsVoiceState\" WHERE \"userId\" = @user AND \"ttsVoiceId\" = @voice", data) ?? false;
if ((check is not bool state || !state) && chatterId != _configuration.Tts.OwnerId) if ((check is not bool state || !state) && chatterId != _configuration.Tts.OwnerId)
return RequestResult.Failed("Voice is disabled on this channel."); return RequestResult.Failed("Voice is disabled on this channel.");
var channel = _channels.Get(sender);
bool result = channel.Chatters.Set(chatterId.ToString(), new ChatterVoice() bool result = channel.Chatters.Set(chatterId.ToString(), new ChatterVoice()
{ {
UserId = sender, UserId = channel.Id,
ChatterId = chatterId, ChatterId = chatterId,
VoiceId = data["voice"].ToString()! VoiceId = data["voice"].ToString()!
}); });

View File

@ -19,20 +19,19 @@ namespace HermesSocketServer.Requests
_random = new Random(); _random = new Random();
} }
public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data)
{ {
data["voice"] = data["voice"].ToString()!; string voice = data["voice"].ToString()!;
string id = RandomString(25); string id = RandomString(25);
var result = _voices.Set(id, new Voice() var result = _voices.Set(id, new Voice()
{ {
Id = id, Id = id,
Name = data["voice"].ToString() Name = voice
}); });
if (result) { if (result) {
_logger.Information($"Added a new voice [voice: {data["voice"]}][voice id: {id}]"); _logger.Information($"Added a new voice [voice: {voice}][voice id: {id}]");
return RequestResult.Successful(id); return RequestResult.Successful(id);
} }
return RequestResult.Failed("Something went wrong when updating the cache."); return RequestResult.Failed("Something went wrong when updating the cache.");

View File

@ -1,4 +1,4 @@
using HermesSocketServer.Services; using HermesSocketServer.Models;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
@ -7,20 +7,16 @@ namespace HermesSocketServer.Requests
{ {
public string Name => "delete_policy"; public string Name => "delete_policy";
public string[] RequiredKeys => ["id"]; public string[] RequiredKeys => ["id"];
private ChannelManager _channels;
private ILogger _logger; private ILogger _logger;
public DeletePolicy(ChannelManager channels, ILogger logger) public DeletePolicy(ILogger logger)
{ {
_channels = channels;
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var channel = _channels.Get(sender); string policyId = data["id"].ToString()!;
string policyId = data!["id"].ToString()!;
var policy = channel.Policies.Get(policyId); var policy = channel.Policies.Get(policyId);
if (policy != null) { if (policy != null) {
channel.Policies.Remove(policyId); channel.Policies.Remove(policyId);

View File

@ -1,4 +1,4 @@
using HermesSocketServer.Services; using HermesSocketServer.Models;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
@ -7,21 +7,19 @@ namespace HermesSocketServer.Requests
{ {
public string Name => "delete_tts_filter"; public string Name => "delete_tts_filter";
public string[] RequiredKeys => ["id"]; public string[] RequiredKeys => ["id"];
private ChannelManager _channels;
private ILogger _logger; private ILogger _logger;
public DeleteTTSFilter(ChannelManager channels, ILogger logger) public DeleteTTSFilter(ILogger logger)
{ {
_channels = channels;
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var channel = _channels.Get(sender); string filterId = data["id"].ToString()!;
channel.Filters.Remove(data!["id"].ToString()); channel.Filters.Remove(filterId);
_logger.Information($"Deleted a TTS filter by id [tts filter id: {data["id"]}]"); _logger.Information($"Deleted a TTS filter by id [tts filter id: {filterId}]");
return RequestResult.Successful(null); return RequestResult.Successful(null);
} }
} }

View File

@ -17,10 +17,11 @@ namespace HermesSocketServer.Requests
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
_voices.Remove(data!["voice"].ToString()); string voiceId = data["voice"].ToString()!;
_logger.Information($"Deleted a voice by id [voice id: {data["voice"]}]"); _voices.Remove(voiceId);
_logger.Information($"Deleted a voice by id [voice id: {voiceId}]");
return RequestResult.Successful(null); return RequestResult.Successful(null);
} }
} }

View File

@ -1,4 +1,4 @@
using HermesSocketLibrary.db; using HermesSocketServer.Models;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
@ -7,21 +7,17 @@ namespace HermesSocketServer.Requests
{ {
public string Name => "get_chatter_ids"; public string Name => "get_chatter_ids";
public string[] RequiredKeys => []; public string[] RequiredKeys => [];
private Database _database;
private ILogger _logger; private ILogger _logger;
public GetChatterIds(Database database, ILogger logger) public GetChatterIds(ILogger logger)
{ {
_database = database;
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
IList<long> ids = new List<long>(); IEnumerable<long> ids = channel.Chatters.Get().Values.Select(c => c.ChatterId);
string sql = $"SELECT id FROM \"Chatter\""; _logger.Information($"Fetched all chatters for channel [channel: {channel.Id}]");
await _database.Execute(sql, (IDictionary<string, object>?) null, (r) => ids.Add(r.GetInt64(0)));
_logger.Information($"Fetched all chatters for channel [channel: {sender}]");
return RequestResult.Successful(ids, notifyClientsOnAccount: false); return RequestResult.Successful(ids, notifyClientsOnAccount: false);
} }
} }

View File

@ -1,5 +1,7 @@
using HermesSocketLibrary.db; using HermesSocketLibrary.db;
using HermesSocketLibrary.Socket.Data; using HermesSocketLibrary.Socket.Data;
using HermesSocketServer.Models;
using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
{ {
@ -8,17 +10,17 @@ namespace HermesSocketServer.Requests
public string Name => "get_connections"; public string Name => "get_connections";
public string[] RequiredKeys => []; public string[] RequiredKeys => [];
private Database _database; private Database _database;
private Serilog.ILogger _logger; private ILogger _logger;
public GetConnections(Database database, Serilog.ILogger logger) public GetConnections(Database database, ILogger logger)
{ {
_database = database; _database = database;
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var temp = new Dictionary<string, object>() { { "user", sender } }; var temp = new Dictionary<string, object>() { { "user", channel.Id } };
var connections = new List<Connection>(); var connections = new List<Connection>();
string sql = "select \"name\", \"type\", \"clientId\", \"accessToken\", \"grantType\", \"scope\", \"expiresAt\", \"default\" from \"Connection\" where \"userId\" = @user"; string sql = "select \"name\", \"type\", \"clientId\", \"accessToken\", \"grantType\", \"scope\", \"expiresAt\", \"default\" from \"Connection\" where \"userId\" = @user";

View File

@ -1,3 +1,4 @@
using HermesSocketServer.Models;
using HermesSocketServer.Store; using HermesSocketServer.Store;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
@ -18,9 +19,9 @@ namespace HermesSocketServer.Requests
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var user = _users.Get(sender); var user = _users.Get(channel.Id);
if (user == null) if (user == null)
return RequestResult.Failed("Unable to find user data.", notifyClientsOnAccount: false); return RequestResult.Failed("Unable to find user data.", notifyClientsOnAccount: false);

View File

@ -1,5 +1,6 @@
using HermesSocketLibrary.db; using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests.Messages; using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
@ -17,7 +18,7 @@ namespace HermesSocketServer.Requests
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
IList<EmoteInfo> emotes = new List<EmoteInfo>(); IList<EmoteInfo> emotes = new List<EmoteInfo>();
string sql = $"SELECT id, name FROM \"Emote\""; string sql = $"SELECT id, name FROM \"Emote\"";
@ -26,7 +27,7 @@ namespace HermesSocketServer.Requests
Id = r.GetString(0), Id = r.GetString(0),
Name = r.GetString(1) Name = r.GetString(1)
})); }));
_logger.Information($"Fetched all emotes for channel [channel: {sender}]"); _logger.Information($"Fetched all emotes for channel [channel: {channel.Id}]");
return RequestResult.Successful(emotes, notifyClientsOnAccount: false); return RequestResult.Successful(emotes, notifyClientsOnAccount: false);
} }
} }

View File

@ -1,4 +1,5 @@
using HermesSocketLibrary.db; using HermesSocketLibrary.db;
using HermesSocketServer.Models;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
@ -16,16 +17,16 @@ namespace HermesSocketServer.Requests
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var temp = new Dictionary<string, object>() { { "user", sender } }; var temp = new Dictionary<string, object>() { { "user", channel.Id } };
var voices = new List<string>(); var voices = new List<string>();
string sql = $"SELECT v.name FROM \"TtsVoiceState\" s " string sql = $"SELECT v.name FROM \"TtsVoiceState\" s "
+ "INNER JOIN \"TtsVoice\" v ON s.\"ttsVoiceId\" = v.id " + "INNER JOIN \"TtsVoice\" v ON s.\"ttsVoiceId\" = v.id "
+ "WHERE \"userId\" = @user AND state = true"; + "WHERE \"userId\" = @user AND state = true";
await _database.Execute(sql, temp, (r) => voices.Add(r.GetString(0))); await _database.Execute(sql, temp, (r) => voices.Add(r.GetString(0)));
_logger.Information($"Fetched all enabled TTS voice for channel [channel: {sender}]"); _logger.Information($"Fetched all enabled TTS voice for channel [channel: {channel.Id}]");
return RequestResult.Successful(voices, notifyClientsOnAccount: false); return RequestResult.Successful(voices, notifyClientsOnAccount: false);
} }
} }

View File

@ -1,5 +1,6 @@
using HermesSocketLibrary.db; using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests.Messages; using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
@ -17,9 +18,9 @@ namespace HermesSocketServer.Requests
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var temp = new Dictionary<string, object>() { { "user", sender } }; var temp = new Dictionary<string, object>() { { "user", channel.Id } };
var groups = new List<Group>(); var groups = new List<Group>();
string sql = $"SELECT id, name, priority FROM \"Group\" WHERE \"userId\" = @user"; string sql = $"SELECT id, name, priority FROM \"Group\" WHERE \"userId\" = @user";
@ -47,7 +48,7 @@ namespace HermesSocketServer.Requests
Path = r.GetString(2), Path = r.GetString(2),
Allow = r.GetBoolean(3) Allow = r.GetBoolean(3)
})); }));
_logger.Information($"Fetched all redemptions for channel [channel: {sender}]"); _logger.Information($"Fetched all redemptions for channel [channel: {channel.Id}]");
var info = new GroupInfo() var info = new GroupInfo()
{ {

View File

@ -1,4 +1,4 @@
using HermesSocketServer.Services; using HermesSocketServer.Models;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
@ -7,21 +7,18 @@ namespace HermesSocketServer.Requests
{ {
public string Name => "get_policies"; public string Name => "get_policies";
public string[] RequiredKeys => []; public string[] RequiredKeys => [];
private ChannelManager _channels;
private ILogger _logger; private ILogger _logger;
public GetPolicies(ChannelManager channels, ILogger logger) public GetPolicies(ILogger logger)
{ {
_channels = channels;
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var channel = _channels.Get(sender);
var results = channel.Policies.Get().Values; var results = channel.Policies.Get().Values;
_logger.Information($"Fetched policies for channel [policy size: {results.Count}][channel: {sender}]"); _logger.Information($"Fetched policies for channel [policy size: {results.Count}][channel: {channel.Id}]");
return RequestResult.Successful(results, notifyClientsOnAccount: false); return RequestResult.Successful(results, notifyClientsOnAccount: false);
} }
} }

View File

@ -1,6 +1,7 @@
using System.Text.Json; using System.Text.Json;
using HermesSocketLibrary.db; using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests.Messages; using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
@ -20,9 +21,9 @@ namespace HermesSocketServer.Requests
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var temp = new Dictionary<string, object>() { { "user", sender } }; var temp = new Dictionary<string, object>() { { "user", channel.Id } };
var redemptions = new List<RedeemableAction>(); var redemptions = new List<RedeemableAction>();
string sql = $"SELECT name, type, data FROM \"Action\" WHERE \"userId\" = @user"; string sql = $"SELECT name, type, data FROM \"Action\" WHERE \"userId\" = @user";
@ -32,7 +33,7 @@ namespace HermesSocketServer.Requests
Type = r.GetString(1), Type = r.GetString(1),
Data = JsonSerializer.Deserialize<IDictionary<string, string>>(r.GetString(2), _options)! Data = JsonSerializer.Deserialize<IDictionary<string, string>>(r.GetString(2), _options)!
})); }));
_logger.Information($"Fetched all chatters' selected tts voice for channel [channel: {sender}]"); _logger.Information($"Fetched all chatters' selected tts voice for channel [channel: {channel.Id}]");
return RequestResult.Successful(redemptions, notifyClientsOnAccount: false); return RequestResult.Successful(redemptions, notifyClientsOnAccount: false);
} }
} }

View File

@ -1,5 +1,6 @@
using HermesSocketLibrary.db; using HermesSocketLibrary.db;
using HermesSocketLibrary.Requests.Messages; using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
@ -17,9 +18,9 @@ namespace HermesSocketServer.Requests
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var temp = new Dictionary<string, object>() { { "user", sender } }; var temp = new Dictionary<string, object>() { { "user", channel.Id } };
var redemptions = new List<Redemption>(); var redemptions = new List<Redemption>();
string sql = $"SELECT id, \"redemptionId\", \"actionName\", \"order\", state FROM \"Redemption\" WHERE \"userId\" = @user"; string sql = $"SELECT id, \"redemptionId\", \"actionName\", \"order\", state FROM \"Redemption\" WHERE \"userId\" = @user";
@ -31,7 +32,7 @@ namespace HermesSocketServer.Requests
Order = r.GetInt32(3), Order = r.GetInt32(3),
State = r.GetBoolean(4) State = r.GetBoolean(4)
})); }));
_logger.Information($"Fetched all redemptions for channel [channel: {sender}]"); _logger.Information($"Fetched all redemptions for channel [channel: {channel.Id}]");
return RequestResult.Successful(redemptions, notifyClientsOnAccount: false); return RequestResult.Successful(redemptions, notifyClientsOnAccount: false);
} }
} }

View File

@ -1,4 +1,4 @@
using HermesSocketServer.Services; using HermesSocketServer.Models;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
@ -7,20 +7,17 @@ namespace HermesSocketServer.Requests
{ {
public string Name => "get_tts_users"; public string Name => "get_tts_users";
public string[] RequiredKeys => []; public string[] RequiredKeys => [];
private ChannelManager _channels;
private ILogger _logger; private ILogger _logger;
public GetTTSUsers(ChannelManager channels, ILogger logger) public GetTTSUsers(ILogger logger)
{ {
_channels = channels;
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var channel = _channels.Get(sender);
var results = channel.Chatters.Get().ToDictionary(p => p.Key, p => p.Value.VoiceId); var results = channel.Chatters.Get().ToDictionary(p => p.Key, p => p.Value.VoiceId);
_logger.Information($"Fetched all chatters' selected tts voice for channel [channel: {sender}]"); _logger.Information($"Fetched all chatters' selected tts voice for channel [channel: {channel.Id}]");
return RequestResult.Successful(results, notifyClientsOnAccount: false); return RequestResult.Successful(results, notifyClientsOnAccount: false);
} }
} }

View File

@ -1,4 +1,5 @@
using HermesSocketLibrary.Requests.Messages; using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models;
using HermesSocketServer.Store; using HermesSocketServer.Store;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
@ -17,7 +18,7 @@ namespace HermesSocketServer.Requests
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
IEnumerable<VoiceDetails> voices = _voices.Get().Select(v => new VoiceDetails() IEnumerable<VoiceDetails> voices = _voices.Get().Select(v => new VoiceDetails()
{ {
@ -25,7 +26,7 @@ namespace HermesSocketServer.Requests
Name = v.Value.Name Name = v.Value.Name
}); });
_logger.Information($"Fetched all TTS voices for channel [channel: {sender}]"); _logger.Information($"Fetched all TTS voices for channel [channel: {channel.Id}]");
return RequestResult.Successful(voices, notifyClientsOnAccount: false); return RequestResult.Successful(voices, notifyClientsOnAccount: false);
} }
} }

View File

@ -1,4 +1,5 @@
using HermesSocketLibrary.Requests.Messages; using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models;
using HermesSocketServer.Services; using HermesSocketServer.Services;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
@ -17,12 +18,11 @@ namespace HermesSocketServer.Requests
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var channel = _channels.Get(sender);
IEnumerable<TTSWordFilter> filters = channel.Filters.Get().Values; IEnumerable<TTSWordFilter> filters = channel.Filters.Get().Values;
_logger.Information($"Fetched all word filters for channel [channel: {sender}]"); _logger.Information($"Fetched all word filters for channel [channel: {channel.Id}]");
return RequestResult.Successful(filters, notifyClientsOnAccount: false); return RequestResult.Successful(filters, notifyClientsOnAccount: false);
} }
} }

View File

@ -1,3 +1,5 @@
using HermesSocketServer.Models;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
{ {
public interface IRequest public interface IRequest
@ -5,6 +7,6 @@ namespace HermesSocketServer.Requests
string Name { get; } string Name { get; }
string[] RequiredKeys { get; } string[] RequiredKeys { get; }
Task<RequestResult> Grant(string sender, IDictionary<string, object>? data); Task<RequestResult> Grant(Channel channel, IDictionary<string, object>? data);
} }
} }

View File

@ -58,7 +58,7 @@ namespace HermesSocketServer.Requests
try try
{ {
return await request.Grant(sender, message.Data); return await request.Grant(channel, message.Data);
} }
catch (Exception e) catch (Exception e)
{ {

View File

@ -1,3 +1,4 @@
using HermesSocketServer.Models;
using HermesSocketServer.Store; using HermesSocketServer.Store;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
@ -16,16 +17,16 @@ namespace HermesSocketServer.Requests
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
data["user"] = data["user"].ToString(); string user = data["user"].ToString()!;
data["voice"] = data["voice"].ToString(); string voice = data["voice"].ToString()!;
var success = _users.Modify(data["user"].ToString(), (user) => user.DefaultVoice = data["voice"].ToString()!); var success = _users.Modify(user, (user) => user.DefaultVoice = voice);
if (!success) if (!success)
return RequestResult.Failed("Unable to find user data.", notifyClientsOnAccount: false); return RequestResult.Failed("Unable to find user data.");
_logger.Information($"Updated default TTS voice for channel [channel: {sender}][voice: {data["voice"]}]"); _logger.Information($"Updated default TTS voice for channel [channel: {channel.Id}][voice: {voice}]");
return RequestResult.Successful(null); return RequestResult.Successful(null);
} }
} }

View File

@ -17,7 +17,7 @@ namespace HermesSocketServer.Requests
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var id = Guid.Parse(data["id"].ToString()!); var id = Guid.Parse(data["id"].ToString()!);
string groupId = data["groupId"].ToString()!; string groupId = data["groupId"].ToString()!;
@ -25,11 +25,10 @@ namespace HermesSocketServer.Requests
int count = int.Parse(data["count"].ToString()!); int count = int.Parse(data["count"].ToString()!);
int span = int.Parse(data["span"].ToString()!); int span = int.Parse(data["span"].ToString()!);
var channel = _channels.Get(sender)!;
bool result = channel.Policies.Set(id.ToString(), new PolicyMessage() bool result = channel.Policies.Set(id.ToString(), new PolicyMessage()
{ {
Id = id, Id = id,
UserId = sender, UserId = channel.Id,
GroupId = Guid.Parse(groupId), GroupId = Guid.Parse(groupId),
Path = path, Path = path,
Usage = count, Usage = count,
@ -39,7 +38,7 @@ namespace HermesSocketServer.Requests
if (result) if (result)
{ {
var policy = channel.Policies.Get(id.ToString()); var policy = channel.Policies.Get(id.ToString());
_logger.Information($"Updated policy to channel [policy id: {id}][group id: {groupId}][path: {path}][count: {count}][span: {span}][channel: {sender}]"); _logger.Information($"Updated policy to channel [policy id: {id}][group id: {groupId}][path: {path}][count: {count}][span: {span}][channel: {channel.Id}]");
return RequestResult.Successful(policy); return RequestResult.Successful(policy);
} }
return RequestResult.Failed("Something went wrong when updating the cache."); return RequestResult.Failed("Something went wrong when updating the cache.");

View File

@ -1,4 +1,5 @@
using HermesSocketLibrary.Requests.Messages; using HermesSocketLibrary.Requests.Messages;
using HermesSocketServer.Models;
using HermesSocketServer.Services; using HermesSocketServer.Services;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
@ -17,7 +18,7 @@ namespace HermesSocketServer.Requests
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
var id = data["id"].ToString()!; var id = data["id"].ToString()!;
string search = data["search"].ToString()!; string search = data["search"].ToString()!;
@ -26,17 +27,16 @@ namespace HermesSocketServer.Requests
var filter = new TTSWordFilter() var filter = new TTSWordFilter()
{ {
Id = id, Id = id,
UserId = sender, UserId = channel.Id,
Search = search, Search = search,
Replace = replace, Replace = replace,
}; };
var channel = _channels.Get(sender);
bool result = channel.Filters.Set(id, filter); bool result = channel.Filters.Set(id, filter);
if (result) if (result)
{ {
_logger.Information($"Updated filter to channel [filter id: {id}][search: {search}][replace: {replace}][channel: {sender}]"); _logger.Information($"Updated filter to channel [filter id: {id}][search: {search}][replace: {replace}][channel: {channel.Id}]");
return RequestResult.Successful(filter); return RequestResult.Successful(filter);
} }
return RequestResult.Failed("Something went wrong when updating the cache."); return RequestResult.Failed("Something went wrong when updating the cache.");

View File

@ -14,36 +14,34 @@ namespace HermesSocketServer.Requests
private readonly ServerConfiguration _configuration; private readonly ServerConfiguration _configuration;
private ILogger _logger; private ILogger _logger;
public UpdateTTSUser(ChannelManager channels, Database database, ServerConfiguration configuration, ILogger logger) public UpdateTTSUser(Database database, ServerConfiguration configuration, ILogger logger)
{ {
_database = database; _database = database;
_channels = channels;
_configuration = configuration; _configuration = configuration;
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
if (long.TryParse(data["chatter"].ToString(), out long chatterId)) if (long.TryParse(data["chatter"].ToString(), out long chatterId))
data["chatter"] = chatterId; data["chatter"] = chatterId;
data["voice"] = data["voice"].ToString(); data["voice"] = data["voice"].ToString()!;
data["user"] = sender; data["user"] = channel.Id;
var check = await _database.ExecuteScalar("SELECT state FROM \"TtsVoiceState\" WHERE \"userId\" = @user AND \"ttsVoiceId\" = @voice", data) ?? false; var check = await _database.ExecuteScalar("SELECT state FROM \"TtsVoiceState\" WHERE \"userId\" = @user AND \"ttsVoiceId\" = @voice", data) ?? false;
if ((check is not bool state || !state) && chatterId != _configuration.Tts.OwnerId) if ((check is not bool state || !state) && chatterId != _configuration.Tts.OwnerId)
return RequestResult.Failed("Voice is either non-existent or disabled on this channel."); return RequestResult.Failed("Voice is either non-existent or disabled on this channel.");
var channel = _channels.Get(sender);
var result = channel.Chatters.Set(chatterId.ToString(), new ChatterVoice() var result = channel.Chatters.Set(chatterId.ToString(), new ChatterVoice()
{ {
UserId = sender, UserId = channel.Id,
ChatterId = chatterId, ChatterId = chatterId,
VoiceId = data["voice"].ToString()! VoiceId = data["voice"].ToString()!
}); });
if (result) if (result)
{ {
_logger.Information($"Updated chatter's [chatter: {data["chatter"]}] selected tts voice [voice: {data["voice"]}] in channel [channel: {sender}]"); _logger.Information($"Updated chatter's [chatter: {data["chatter"]}] selected tts voice [voice: {data["voice"]}] in channel [channel: {channel.Id}]");
return RequestResult.Successful(null); return RequestResult.Successful(null);
} }
return RequestResult.Failed("Soemthing went wrong when updating the cache."); return RequestResult.Failed("Soemthing went wrong when updating the cache.");

View File

@ -17,19 +17,19 @@ namespace HermesSocketServer.Requests
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
data["voice"] = data["voice"].ToString(); string voiceName = data["voice"].ToString()!;
data["voiceid"] = data["voiceid"].ToString(); string voiceId = data["voiceid"].ToString()!;
var result = _voices.Set(data["voiceid"].ToString(), new Voice() var result = _voices.Set(voiceId, new Voice()
{ {
Id = data["voiceid"].ToString()!, Id = voiceId,
Name = data["voice"].ToString()! Name = voiceName
}); });
if (result) if (result)
{ {
_logger.Information($"Updated voice's [voice id: {data["voiceid"]}] name [new name: {data["voice"]}]"); _logger.Information($"Updated voice's [voice id: {voiceId}] name [new name: {voiceName}]");
return RequestResult.Successful(null); return RequestResult.Successful(null);
} }
return RequestResult.Failed("Something went wrong when updating the cache."); return RequestResult.Failed("Something went wrong when updating the cache.");

View File

@ -1,4 +1,5 @@
using HermesSocketLibrary.db; using HermesSocketLibrary.db;
using HermesSocketServer.Models;
using ILogger = Serilog.ILogger; using ILogger = Serilog.ILogger;
namespace HermesSocketServer.Requests namespace HermesSocketServer.Requests
@ -16,11 +17,11 @@ namespace HermesSocketServer.Requests
_logger = logger; _logger = logger;
} }
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data) public async Task<RequestResult> Grant(Channel channel, IDictionary<string, object> data)
{ {
data["voice"] = data["voice"].ToString(); data["voice"] = data["voice"].ToString();
data["state"] = data["state"].ToString() == "True"; data["state"] = data["state"].ToString() == "True";
data["user"] = sender; data["user"] = channel.Id;
string sql = "INSERT INTO \"TtsVoiceState\" (\"userId\", \"ttsVoiceId\", state) VALUES (@user, @voice, @state) ON CONFLICT (\"userId\", \"ttsVoiceId\") DO UPDATE SET state = @state"; string sql = "INSERT INTO \"TtsVoiceState\" (\"userId\", \"ttsVoiceId\", state) VALUES (@user, @voice, @state) ON CONFLICT (\"userId\", \"ttsVoiceId\") DO UPDATE SET state = @state";
var result = await _database.Execute(sql, data); var result = await _database.Execute(sql, data);