Added groups & permissions. Fixed 7tv reconnection. Added more subcommands for refresh.

This commit is contained in:
Tom
2024-07-12 17:36:09 +00:00
parent af3763a837
commit 9fb966474f
37 changed files with 806 additions and 159 deletions

View File

@ -4,10 +4,14 @@ using System.Text.Json;
using HermesSocketLibrary.Requests.Messages;
using TwitchChatTTS.Hermes;
using TwitchChatTTS.Twitch.Redemptions;
using TwitchChatTTS.Chat.Groups.Permissions;
using TwitchChatTTS.Chat.Groups;
public class HermesApiClient
{
private readonly WebClientWrap _web;
public const string BASE_URL = "tomtospeech.com";
public HermesApiClient(Configuration configuration)
{
@ -24,15 +28,14 @@ public class HermesApiClient
_web.AddHeader("x-api-key", configuration.Hermes.Token);
}
public async Task<TTSVersion> GetTTSVersion()
public async Task<TTSVersion?> GetLatestTTSVersion()
{
var version = await _web.GetJson<TTSVersion>("https://hermes.goblincaves.com/api/info/version");
return version;
return await _web.GetJson<TTSVersion>($"https://{BASE_URL}/api/info/version");
}
public async Task<Account> FetchHermesAccountDetails()
{
var account = await _web.GetJson<Account>("https://hermes.goblincaves.com/api/account");
var account = await _web.GetJson<Account>($"https://{BASE_URL}/api/account");
if (account == null || account.Id == null || account.Username == null)
throw new NullReferenceException("Invalid value found while fetching for hermes account data.");
return account;
@ -40,7 +43,7 @@ public class HermesApiClient
public async Task<TwitchBotToken> FetchTwitchBotToken()
{
var token = await _web.GetJson<TwitchBotToken>("https://hermes.goblincaves.com/api/token/bot");
var token = await _web.GetJson<TwitchBotToken>($"https://{BASE_URL}/api/token/bot");
if (token == null || token.ClientId == null || token.AccessToken == null || token.RefreshToken == null || token.ClientSecret == null)
throw new Exception("Failed to fetch Twitch API token from Hermes.");
@ -49,7 +52,7 @@ public class HermesApiClient
public async Task<IEnumerable<TTSUsernameFilter>> FetchTTSUsernameFilters()
{
var filters = await _web.GetJson<IEnumerable<TTSUsernameFilter>>("https://hermes.goblincaves.com/api/settings/tts/filter/users");
var filters = await _web.GetJson<IEnumerable<TTSUsernameFilter>>($"https://{BASE_URL}/api/settings/tts/filter/users");
if (filters == null)
throw new Exception("Failed to fetch TTS username filters from Hermes.");
@ -58,7 +61,7 @@ public class HermesApiClient
public async Task<string> FetchTTSDefaultVoice()
{
var data = await _web.GetJson<string>("https://hermes.goblincaves.com/api/settings/tts/default");
var data = await _web.GetJson<string>($"https://{BASE_URL}/api/settings/tts/default");
if (data == null)
throw new Exception("Failed to fetch TTS default voice from Hermes.");
@ -67,7 +70,7 @@ public class HermesApiClient
public async Task<IEnumerable<TTSChatterSelectedVoice>> FetchTTSChatterSelectedVoices()
{
var voices = await _web.GetJson<IEnumerable<TTSChatterSelectedVoice>>("https://hermes.goblincaves.com/api/settings/tts/selected");
var voices = await _web.GetJson<IEnumerable<TTSChatterSelectedVoice>>($"https://{BASE_URL}/api/settings/tts/selected");
if (voices == null)
throw new Exception("Failed to fetch TTS chatter selected voices from Hermes.");
@ -76,7 +79,7 @@ public class HermesApiClient
public async Task<IEnumerable<string>> FetchTTSEnabledVoices()
{
var voices = await _web.GetJson<IEnumerable<string>>("https://hermes.goblincaves.com/api/settings/tts");
var voices = await _web.GetJson<IEnumerable<string>>($"https://{BASE_URL}/api/settings/tts");
if (voices == null)
throw new Exception("Failed to fetch TTS enabled voices from Hermes.");
@ -85,7 +88,7 @@ public class HermesApiClient
public async Task<IEnumerable<TTSWordFilter>> FetchTTSWordFilters()
{
var filters = await _web.GetJson<IEnumerable<TTSWordFilter>>("https://hermes.goblincaves.com/api/settings/tts/filter/words");
var filters = await _web.GetJson<IEnumerable<TTSWordFilter>>($"https://{BASE_URL}/api/settings/tts/filter/words");
if (filters == null)
throw new Exception("Failed to fetch TTS word filters from Hermes.");
@ -94,20 +97,59 @@ public class HermesApiClient
public async Task<IEnumerable<Redemption>> FetchRedemptions()
{
var redemptions = await _web.GetJson<IEnumerable<Redemption>>("https://hermes.goblincaves.com/api/settings/redemptions", new JsonSerializerOptions()
var redemptions = await _web.GetJson<IEnumerable<Redemption>>($"https://{BASE_URL}/api/settings/redemptions", new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = false,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
if (redemptions == null)
throw new Exception("Failed to redemptions from Hermes.");
throw new Exception("Failed to fetch redemptions from Hermes.");
return redemptions;
}
public async Task<IEnumerable<Group>> FetchGroups()
{
var groups = await _web.GetJson<IEnumerable<Group>>($"https://{BASE_URL}/api/settings/groups", new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = false,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
if (groups == null)
throw new Exception("Failed to fetch groups from Hermes.");
return groups;
}
public async Task<IEnumerable<GroupChatter>> FetchGroupChatters()
{
var chatters = await _web.GetJson<IEnumerable<GroupChatter>>($"https://{BASE_URL}/api/settings/groups/users", new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = false,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
if (chatters == null)
throw new Exception("Failed to fetch groups from Hermes.");
return chatters;
}
public async Task<IEnumerable<GroupPermission>> FetchGroupPermissions()
{
var permissions = await _web.GetJson<IEnumerable<GroupPermission>>($"https://{BASE_URL}/api/settings/groups/permissions", new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = false,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
if (permissions == null)
throw new Exception("Failed to fetch group permissions from Hermes.");
return permissions;
}
public async Task<IEnumerable<RedeemableAction>> FetchRedeemableActions()
{
var actions = await _web.GetJson<IEnumerable<RedeemableAction>>("https://hermes.goblincaves.com/api/settings/redemptions/actions");
var actions = await _web.GetJson<IEnumerable<RedeemableAction>>($"https://{BASE_URL}/api/settings/redemptions/actions");
if (actions == null)
throw new Exception("Failed to fetch redeemable actions from Hermes.");

View File

@ -21,11 +21,9 @@ namespace TwitchChatTTS.Hermes.Socket.Handlers
return;
if (sender is not HermesSocketClient client)
{
return;
}
_logger.Verbose("Received heartbeat.");
_logger.Verbose("Received heartbeat from server.");
client.LastHeartbeatReceived = DateTime.UtcNow;

View File

@ -31,13 +31,10 @@ namespace TwitchChatTTS.Hermes.Socket.Handlers
_logger.Warning("Another client has connected to the same account.");
return;
}
else
{
client.UserId = message.UserId;
_logger.Information($"Logged in as {_user.TwitchUsername}.");
}
client.UserId = message.UserId;
_user.OwnerId = message.OwnerId;
_logger.Information($"Logged in as {_user.TwitchUsername} {(message.WebLogin ? "via web" : "via TTS app")}.");
await client.Send(3, new RequestMessage()
{

View File

@ -17,6 +17,8 @@ namespace TwitchChatTTS.Hermes.Socket
private System.Timers.Timer _heartbeatTimer;
private System.Timers.Timer _reconnectTimer;
public const string BASE_URL = "ws.tomtospeech.com";
public HermesSocketClient(
Configuration configuration,
[FromKeyedServices("hermes")] HandlerManager<WebSocketClient, IWebSocketHandler> handlerManager,
@ -84,7 +86,7 @@ namespace TwitchChatTTS.Hermes.Socket
{
try
{
await ConnectAsync($"wss://hermes-ws.goblincaves.com");
await ConnectAsync($"wss://{HermesSocketClient.BASE_URL}");
Connected = true;
}
catch (Exception)