Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,19 @@ public override async Task<bool> ValidateTokenAsync(string accessToken, Cancella
var response = await s_httpClient.SendAsync(request, cancellationToken);
if (response.IsSuccessStatusCode)
{
using var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken);
await using var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken);
var validateResponse = await JsonSerializer.DeserializeAsync<ValidateResponse>(responseStream, cancellationToken: cancellationToken);
if (validateResponse?.ExpiresIn > 0)
{
logger.LogInformation("Successfully validated Twitch OAuth token");
return true;
}

logger.LogWarning("Twitch OAuth token expired");
}
else
{
logger.LogError("Unsuccessful API status code {Code} when checking Twitch OAuth token validity.", response.StatusCode);
}
}
catch (Exception ex)
Expand Down
5 changes: 5 additions & 0 deletions src/TrackerCouncil.Smz3.Tracking/Tracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ public override async Task ConnectToChatAsync(string? userName, string? oauthTok
{
try
{
_logger.LogInformation("Connecting to Twitch Chat");
await _chatClient.ConnectAsync(userName, oauthToken, channel ?? userName, id ?? "");
}
catch (AggregateException e)
Expand All @@ -361,6 +362,10 @@ public override async Task ConnectToChatAsync(string? userName, string? oauthTok
Say(x => x.Chat.WhenDisconnected);
}
}
else if (!string.IsNullOrEmpty(userName))
{
_logger.LogWarning("Twitch username specified, but no auth token found");
}
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/TrackerCouncil.Smz3.UI/Services/MainWindowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public async Task ValidateTwitchToken()

if (!isTokenValid)
{
logger.LogInformation("Clearing Twitch oauth token due to it being invalid");
_options.GeneralOptions.TwitchOAuthToken = string.Empty;
Dispatcher.UIThread.Invoke(() =>
{
Expand Down
81 changes: 59 additions & 22 deletions src/TrackerCouncil.Smz3.UI/Services/OptionsWindowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,25 @@ public void Close()

public void SaveViewModel()
{
var options = optionsFactory.Create();
_model.UpdateOptions(options.GeneralOptions);
logger.LogInformation("Saving option window settings");

if (!Directory.Exists(options.GeneralOptions.RomOutputPath))
try
{
Directory.CreateDirectory(options.GeneralOptions.RomOutputPath);
var options = optionsFactory.Create();
_model.UpdateOptions(options.GeneralOptions);

if (!Directory.Exists(options.GeneralOptions.RomOutputPath))
{
Directory.CreateDirectory(options.GeneralOptions.RomOutputPath);
}

options.Save();
}
catch (Exception e)
{
logger.LogError(e, "Error saving options window settings");
}

options.Save();
}

private async Task ValidateTwitchOAuthToken()
Expand Down Expand Up @@ -150,13 +160,16 @@ await Task.Run(async () =>
{
try
{
logger.LogInformation("Attempting to log into twitch");

var token = await chatAuthenticationService.GetTokenInteractivelyAsync(default);

if(token == null)
if (string.IsNullOrEmpty(token))
{
logger.LogError("Token returned by chat authentication service null");
TwitchError?.Invoke(this, new TwitchErrorEventHandler("An unexpected error occurred while trying to log you in with Twitch. " +
"Please try again or report this issue with the log file."));
TwitchError?.Invoke(this, new TwitchErrorEventHandler(
"An unexpected error occurred while trying to log you in with Twitch. " +
"Please try again or report this issue with the log file."));
return;
}

Expand All @@ -165,21 +178,25 @@ await Task.Run(async () =>
if (userData == null)
{
logger.LogError("User Data returned by chat authentication service null");
TwitchError?.Invoke(this, new TwitchErrorEventHandler("An unexpected error occurred while trying to log you in with Twitch. " +
"Please try again or report this issue with the log file."));
TwitchError?.Invoke(this, new TwitchErrorEventHandler(
"An unexpected error occurred while trying to log you in with Twitch. " +
"Please try again or report this issue with the log file."));
return;
}

_model.TwitchIntegration.TwitchUserName = userData.Name;
_model.TwitchIntegration.TwitchOAuthToken = token;
_model.TwitchIntegration.TwitchChannel = userData.Name;
_model.TwitchIntegration.TwitchId = userData.Id;
SaveTwitchLogin();
logger.LogInformation("Updated Twitch token successfully");
}
catch (Exception ex)
{
logger.LogError(ex, "An unknown error occurred while logging in with Twitch");
TwitchError?.Invoke(this, new TwitchErrorEventHandler("An unexpected error occurred while trying to log you in with Twitch. " +
"Please try again or report this issue with the log file."));
TwitchError?.Invoke(this, new TwitchErrorEventHandler(
"An unexpected error occurred while trying to log you in with Twitch. " +
"Please try again or report this issue with the log file."));
}
});
}
Expand All @@ -193,21 +210,41 @@ await Task.Run(async () =>

private async void TwitchLogout()
{
if (string.IsNullOrEmpty(_model.TwitchIntegration.TwitchOAuthToken))
return;
try
{
if (string.IsNullOrEmpty(_model.TwitchIntegration.TwitchOAuthToken))
return;

var revoked = await chatAuthenticationService.RevokeTokenAsync(_model.TwitchIntegration.TwitchOAuthToken, default);

var revoked = await chatAuthenticationService.RevokeTokenAsync(_model.TwitchIntegration.TwitchOAuthToken, default);
if (revoked)
{
_model.TwitchIntegration.TwitchUserName = "";
_model.TwitchIntegration.TwitchOAuthToken = "";
_model.TwitchIntegration.TwitchChannel = "";
_model.TwitchIntegration.TwitchId = "";
}

if (revoked)
_model.TwitchIntegration.TwitchStatusText = revoked ? "Logged out." : "Something went wrong.";
_model.TwitchIntegration.IsLoggedIn = false;
SaveTwitchLogin();
}
catch (Exception e)
{
_model.TwitchIntegration.TwitchUserName = "";
_model.TwitchIntegration.TwitchOAuthToken = "";
_model.TwitchIntegration.TwitchChannel = "";
_model.TwitchIntegration.TwitchId = "";
logger.LogError(e, "Error logging out from Twitch");
}

_model.TwitchIntegration.TwitchStatusText = revoked ? "Logged out." : "Something went wrong.";
_model.TwitchIntegration.IsLoggedIn = false;
}

private void SaveTwitchLogin()
{
logger.LogInformation("Saving Twitch login settings. Valid OAuthToken? {Value}", !string.IsNullOrEmpty(_model.TwitchIntegration.TwitchOAuthToken));
var options = optionsFactory.Create();
options.GeneralOptions.TwitchUserName = _model.TwitchIntegration.TwitchUserName;
options.GeneralOptions.TwitchOAuthToken = _model.TwitchIntegration.TwitchOAuthToken;
options.GeneralOptions.TwitchChannel = _model.TwitchIntegration.TwitchChannel;
options.GeneralOptions.TwitchId = _model.TwitchIntegration.TwitchId;
options.Save();
}

private async Task UpdateConfigsAsync()
Expand Down
Loading