LANCommander/LANCommander.Launcher.Services/ProfileService.cs

122 lines
3.9 KiB
C#
Raw Permalink Normal View History

using JetBrains.Annotations;
using LANCommander.Launcher.Data.Models;
using LANCommander.Launcher.Models;
2024-09-11 00:24:34 -05:00
using Microsoft.Extensions.Logging;
2024-06-03 19:08:50 -05:00
using System;
using System.Collections.Generic;
using System.Linq;
2024-08-01 18:08:55 -05:00
using System.Net.Mime;
2024-06-03 19:08:50 -05:00
using System.Text;
using System.Threading.Tasks;
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;
2024-08-01 18:08:55 -05:00
private readonly MediaService MediaService;
private readonly UserService UserService;
2024-08-01 18:08:55 -05:00
private Settings Settings;
2024-06-03 19:08:50 -05:00
2025-01-25 02:58:10 -06:00
public event EventHandler OnProfileDownloaded;
2024-09-11 00:24:34 -05:00
public ProfileService(
SDK.Client client,
ILogger<ProfileService> logger,
AuthenticationService authenticationService,
MediaService mediaService,
UserService userService) : base(client, logger)
2024-09-11 00:24:34 -05:00
{
AuthenticationService = authenticationService;
2024-08-01 18:08:55 -05:00
MediaService = mediaService;
UserService = userService;
Settings = SettingService.GetSettings();
AuthenticationService.OnLogin += async (sender, args) => await DownloadProfileInfoAsync();
AuthenticationService.OnRegister += async (sender, args) => await DownloadProfileInfoAsync();
2024-06-03 19:08:50 -05:00
}
public async Task ChangeAlias(string newName)
{
var currentUserId = AuthenticationService.GetUserId();
var currentUser = await UserService.GetAsync(currentUserId);
await Client.Profile.ChangeAliasAsync(newName);
2024-06-03 19:08:50 -05:00
currentUser.Alias = newName;
await UserService.UpdateAsync(currentUser);
}
public async Task DownloadProfileInfoAsync()
2024-09-02 17:52:18 -05:00
{
var remoteProfile = await Client.Profile.GetAsync();
2025-03-17 12:29:35 -05:00
if (remoteProfile == null)
return;
var localUser = await UserService
.GetAsync(remoteProfile.Id);
2024-09-02 17:52:18 -05:00
if (localUser == null)
2024-08-01 18:08:55 -05:00
{
localUser = new User()
{
Id = remoteProfile.Id,
UserName = remoteProfile.UserName,
Alias = remoteProfile.Alias,
};
localUser = await UserService.AddAsync(localUser);
}
else
{
localUser.Alias = remoteProfile.Alias;
await UserService.UpdateAsync(localUser);
}
2024-08-01 18:08:55 -05:00
if (localUser.Avatar == null)
{
try
{
var tempAvatarPath = await Client.Profile.DownloadAvatar();
2024-08-01 18:08:55 -05:00
if (!String.IsNullOrWhiteSpace(tempAvatarPath))
{
var media = new Media
{
FileId = Guid.NewGuid(),
Type = SDK.Enums.MediaType.Avatar,
MimeType = MediaTypeNames.Image.Png,
Crc32 = SDK.Services.MediaService.CalculateChecksum(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");
}
}
2024-06-03 19:08:50 -05:00
SettingService.SaveSettings(Settings);
OnProfileDownloaded?.Invoke(this, EventArgs.Empty);
2024-06-03 19:08:50 -05:00
}
public bool IsAuthenticated()
{
return !String.IsNullOrWhiteSpace(Settings.Authentication.AccessToken);
2024-06-03 19:08:50 -05:00
}
}
}