From 58af64e2df1973a5eac457e20a6668c78b335735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=E2=96=88=E2=96=88=E2=96=88=E2=96=88=E2=96=88?= Date: Tue, 22 Jul 2025 22:52:22 -0400 Subject: [PATCH] bug: fixing profile photo not showing up when logging in There is an inherant race condition between the current profile image and the previous one if you're logging into the same user. Giving it a couple tries before giving up. --- .../ViewModels/Pages/AccountViewModel.cs | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/TwitchStreamingTools/ViewModels/Pages/AccountViewModel.cs b/src/TwitchStreamingTools/ViewModels/Pages/AccountViewModel.cs index 45a719a..7639bfc 100644 --- a/src/TwitchStreamingTools/ViewModels/Pages/AccountViewModel.cs +++ b/src/TwitchStreamingTools/ViewModels/Pages/AccountViewModel.cs @@ -13,6 +13,7 @@ using Newtonsoft.Json; +using Nullinside.Api.Common; using Nullinside.Api.Common.Extensions; using Nullinside.Api.Common.Twitch; @@ -264,36 +265,38 @@ private void ClearCredentials() { /// Downloads the user's profile image and adds it to the cache. /// /// The path to the saved file. - private async Task DownloadUserImage() { - // The user object from the API will tell us the download link on twitch for the image. - var api = new TwitchApiWrapper(); - if (string.IsNullOrWhiteSpace(api.OAuth?.AccessToken)) { - return null; - } + private async Task DownloadUserImage(CancellationToken token = new()) { + return await Retry.Execute(async () => { + // The user object from the API will tell us the download link on twitch for the image. + var api = new TwitchApiWrapper(); + if (string.IsNullOrWhiteSpace(api.OAuth?.AccessToken)) { + return null; + } - User? user = await api.GetUser().ConfigureAwait(false); - if (string.IsNullOrWhiteSpace(user?.ProfileImageUrl)) { - return null; - } + User? user = await api.GetUser(token).ConfigureAwait(false); + if (string.IsNullOrWhiteSpace(user?.ProfileImageUrl)) { + return null; + } - // Download the image via http. - using var http = new HttpClient(); - byte[] imageBytes = await http.GetByteArrayAsync(user.ProfileImageUrl).ConfigureAwait(false); + // Download the image via http. + using var http = new HttpClient(); + byte[] imageBytes = await http.GetByteArrayAsync(user.ProfileImageUrl, token).ConfigureAwait(false); - // If the directory doesn't exist, create it. - if (!Directory.Exists(PROFILE_IMAGE_FOLDER)) { - Directory.CreateDirectory(PROFILE_IMAGE_FOLDER); - } + // If the directory doesn't exist, create it. + if (!Directory.Exists(PROFILE_IMAGE_FOLDER)) { + Directory.CreateDirectory(PROFILE_IMAGE_FOLDER); + } - // I don't think twitch usernames can have non-filepath friendly characters but might as well sanitize it anyway. - string filename = SanitizeFilename(string.Format(PROFILE_IMAGE_FILENAME, user.Login)); - string imagePath = Path.Combine(PROFILE_IMAGE_FOLDER, filename); + // I don't think twitch usernames can have non-filepath friendly characters but might as well sanitize it anyway. + string filename = SanitizeFilename(string.Format(PROFILE_IMAGE_FILENAME, user.Login)); + string imagePath = Path.Combine(PROFILE_IMAGE_FOLDER, filename); - // Save to disk - await File.WriteAllBytesAsync(imagePath, imageBytes).ConfigureAwait(false); + // Save to disk + await File.WriteAllBytesAsync(imagePath, imageBytes, token).ConfigureAwait(false); - // Return path to file, even though everyone already knows it. - return imagePath; + // Return path to file, even though everyone already knows it. + return imagePath; + }, 10, token, TimeSpan.FromSeconds(1)).ConfigureAwait(false); } ///