Skip to content

Commit 58af64e

Browse files
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.
1 parent c9dc680 commit 58af64e

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

src/TwitchStreamingTools/ViewModels/Pages/AccountViewModel.cs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
using Newtonsoft.Json;
1515

16+
using Nullinside.Api.Common;
1617
using Nullinside.Api.Common.Extensions;
1718
using Nullinside.Api.Common.Twitch;
1819

@@ -264,36 +265,38 @@ private void ClearCredentials() {
264265
/// Downloads the user's profile image and adds it to the cache.
265266
/// </summary>
266267
/// <returns>The path to the saved file.</returns>
267-
private async Task<string?> DownloadUserImage() {
268-
// The user object from the API will tell us the download link on twitch for the image.
269-
var api = new TwitchApiWrapper();
270-
if (string.IsNullOrWhiteSpace(api.OAuth?.AccessToken)) {
271-
return null;
272-
}
268+
private async Task<string?> DownloadUserImage(CancellationToken token = new()) {
269+
return await Retry.Execute(async () => {
270+
// The user object from the API will tell us the download link on twitch for the image.
271+
var api = new TwitchApiWrapper();
272+
if (string.IsNullOrWhiteSpace(api.OAuth?.AccessToken)) {
273+
return null;
274+
}
273275

274-
User? user = await api.GetUser().ConfigureAwait(false);
275-
if (string.IsNullOrWhiteSpace(user?.ProfileImageUrl)) {
276-
return null;
277-
}
276+
User? user = await api.GetUser(token).ConfigureAwait(false);
277+
if (string.IsNullOrWhiteSpace(user?.ProfileImageUrl)) {
278+
return null;
279+
}
278280

279-
// Download the image via http.
280-
using var http = new HttpClient();
281-
byte[] imageBytes = await http.GetByteArrayAsync(user.ProfileImageUrl).ConfigureAwait(false);
281+
// Download the image via http.
282+
using var http = new HttpClient();
283+
byte[] imageBytes = await http.GetByteArrayAsync(user.ProfileImageUrl, token).ConfigureAwait(false);
282284

283-
// If the directory doesn't exist, create it.
284-
if (!Directory.Exists(PROFILE_IMAGE_FOLDER)) {
285-
Directory.CreateDirectory(PROFILE_IMAGE_FOLDER);
286-
}
285+
// If the directory doesn't exist, create it.
286+
if (!Directory.Exists(PROFILE_IMAGE_FOLDER)) {
287+
Directory.CreateDirectory(PROFILE_IMAGE_FOLDER);
288+
}
287289

288-
// I don't think twitch usernames can have non-filepath friendly characters but might as well sanitize it anyway.
289-
string filename = SanitizeFilename(string.Format(PROFILE_IMAGE_FILENAME, user.Login));
290-
string imagePath = Path.Combine(PROFILE_IMAGE_FOLDER, filename);
290+
// I don't think twitch usernames can have non-filepath friendly characters but might as well sanitize it anyway.
291+
string filename = SanitizeFilename(string.Format(PROFILE_IMAGE_FILENAME, user.Login));
292+
string imagePath = Path.Combine(PROFILE_IMAGE_FOLDER, filename);
291293

292-
// Save to disk
293-
await File.WriteAllBytesAsync(imagePath, imageBytes).ConfigureAwait(false);
294+
// Save to disk
295+
await File.WriteAllBytesAsync(imagePath, imageBytes, token).ConfigureAwait(false);
294296

295-
// Return path to file, even though everyone already knows it.
296-
return imagePath;
297+
// Return path to file, even though everyone already knows it.
298+
return imagePath;
299+
}, 10, token, TimeSpan.FromSeconds(1)).ConfigureAwait(false);
297300
}
298301

299302
/// <summary>

0 commit comments

Comments
 (0)