LANCommander/LANCommander.Launcher.Services/ProfileService.cs

132 lines
4.4 KiB
C#
Raw Permalink Normal View History

using LANCommander.Launcher.Data.Models;
using LANCommander.Launcher.Models;
2024-09-11 00:24:34 -05:00
using Microsoft.Extensions.Logging;
2024-08-01 18:08:55 -05:00
using System.Net.Mime;
2025-10-08 19:51:44 -05:00
using LANCommander.SDK.Extensions;
using LANCommander.SDK.Services;
using Microsoft.Extensions.DependencyInjection;
2024-06-03 19:08:50 -05:00
namespace LANCommander.Launcher.Services
2024-06-03 19:08:50 -05:00
{
public class ProfileService : BaseService
2024-06-03 19:08:50 -05:00
{
private readonly AuthenticationService _authenticationService;
private readonly UserService _userService;
private readonly IServiceScopeFactory _scopeFactory;
private readonly ProfileClient _profileClient;
2024-08-01 18:08:55 -05:00
2025-01-25 02:58:10 -06:00
public event EventHandler OnProfileDownloaded;
2024-09-11 00:24:34 -05:00
public ProfileService(
ProfileClient profileClient,
2024-09-11 00:24:34 -05:00
ILogger<ProfileService> logger,
IServiceScopeFactory scopeFactory,
AuthenticationService authenticationService,
UserService userService) : base(logger)
2024-09-11 00:24:34 -05:00
{
_profileClient = profileClient;
_scopeFactory = scopeFactory;
_authenticationService = authenticationService;
_userService = userService;
2024-06-03 19:08:50 -05:00
}
public async Task ChangeAlias(string newName)
{
2025-10-08 19:51:44 -05:00
using (var op = Logger.BeginOperation("Changing alias"))
{
var currentUserId = _authenticationService.GetUserId();
var currentUser = await _userService.GetAsync(currentUserId);
2025-10-08 19:51:44 -05:00
op.Enrich("UserId", currentUserId);
op.Enrich("CurrentAlias", currentUser.Alias);
op.Enrich("NewAlias", newName);
await _profileClient.ChangeAliasAsync(newName);
2024-06-03 19:08:50 -05:00
2025-10-08 19:51:44 -05:00
currentUser.Alias = newName;
await _userService.UpdateAsync(currentUser);
2025-10-08 19:51:44 -05:00
op.Complete();
}
}
public async Task DownloadProfileInfoAsync()
2024-09-02 17:52:18 -05:00
{
await using var scope = _scopeFactory.CreateAsyncScope();
2025-10-08 19:51:44 -05:00
using var op = Logger.BeginOperation("Downloading profile info");
var userService = scope.ServiceProvider.GetService<UserService>()!;
var mediaService = scope.ServiceProvider.GetService<MediaService>()!;
var remoteProfile = await _profileClient.GetAsync();
2025-10-08 19:51:44 -05:00
if (remoteProfile == null)
{
Logger.LogDebug("Could not find profile");
return;
}
op.Enrich("UserId", remoteProfile.Id);
var localUser = await userService
.GetAsync(remoteProfile.Id);
2024-08-01 18:08:55 -05:00
if (localUser == null)
{
localUser = new User()
2025-10-08 19:51:44 -05:00
{
Id = remoteProfile.Id,
UserName = remoteProfile.UserName,
Alias = remoteProfile.Alias,
};
2025-10-08 19:51:44 -05:00
localUser = await userService.AddAsync(localUser);
}
else
{
localUser.Alias = remoteProfile.Alias;
2025-10-08 19:51:44 -05:00
await userService.UpdateAsync(localUser);
}
2024-08-01 18:08:55 -05:00
if (localUser.Avatar == null)
{
try
2025-10-08 19:51:44 -05:00
{
Logger.LogDebug("Downloading avatar");
2025-10-08 19:51:44 -05:00
var tempAvatarPath = await _profileClient.DownloadAvatar();
2025-10-08 19:51:44 -05:00
if (!String.IsNullOrWhiteSpace(tempAvatarPath))
{
var media = new Media
{
FileId = Guid.NewGuid(),
Type = SDK.Enums.MediaType.Avatar,
MimeType = MediaTypeNames.Image.Png,
Crc32 = await MediaClient.CalculateChecksumAsync(tempAvatarPath),
UserId = remoteProfile.Id,
};
media = await mediaService.AddAsync(media);
var localPath = mediaService.GetImagePath(media);
if (File.Exists(tempAvatarPath))
File.Move(tempAvatarPath, localPath);
}
}
catch (Exception ex)
{
Logger?.LogError(ex, "Could not download avatar");
}
}
OnProfileDownloaded?.Invoke(this, EventArgs.Empty);
2024-06-03 19:08:50 -05:00
}
}
}