Added syncshell profile related items.
This commit is contained in:
@@ -18,11 +18,14 @@ public class LightlessProfileManager : MediatorSubscriberBase
|
||||
private const string _nsfw = "Profile not displayed - NSFW";
|
||||
private readonly ApiController _apiController;
|
||||
private readonly LightlessConfigService _lightlessConfigService;
|
||||
private readonly ConcurrentDictionary<UserData, LightlessProfileData> _lightlessProfiles = new(UserDataComparer.Instance);
|
||||
private readonly ConcurrentDictionary<UserData, LightlessUserProfileData> _lightlessUserProfiles = new(UserDataComparer.Instance);
|
||||
private readonly ConcurrentDictionary<GroupData, LightlessGroupProfileData> _lightlessGroupProfiles = new(GroupDataComparer.Instance);
|
||||
|
||||
private readonly LightlessProfileData _defaultProfileData = new(IsFlagged: false, IsNSFW: false, _lightlessLogo, string.Empty, _noDescription);
|
||||
private readonly LightlessProfileData _loadingProfileData = new(IsFlagged: false, IsNSFW: false, _lightlessLogoLoading, string.Empty, "Loading Data from server...");
|
||||
private readonly LightlessProfileData _nsfwProfileData = new(IsFlagged: false, IsNSFW: false, _lightlessLogoNsfw, string.Empty, _nsfw);
|
||||
private readonly LightlessUserProfileData _defaultProfileUserData = new(IsFlagged: false, IsNSFW: false, _lightlessLogo, string.Empty, _noDescription);
|
||||
private readonly LightlessUserProfileData _loadingProfileUserData = new(IsFlagged: false, IsNSFW: false, _lightlessLogoLoading, string.Empty, "Loading User Profile Data from server...");
|
||||
private readonly LightlessGroupProfileData _loadingProfileGroupData = new(_lightlessLogoLoading, "Loading Group Profile Data from server...", string.Empty);
|
||||
private readonly LightlessGroupProfileData _defaultProfileGroupData = new(_lightlessLogo, _noDescription, string.Empty);
|
||||
private readonly LightlessUserProfileData _nsfwProfileData = new(IsFlagged: false, IsNSFW: false, _lightlessLogoNsfw, string.Empty, _nsfw);
|
||||
|
||||
public LightlessProfileManager(ILogger<LightlessProfileManager> logger, LightlessConfigService lightlessConfigService,
|
||||
LightlessMediator mediator, ApiController apiController) : base(logger, mediator)
|
||||
@@ -30,22 +33,33 @@ public class LightlessProfileManager : MediatorSubscriberBase
|
||||
_lightlessConfigService = lightlessConfigService;
|
||||
_apiController = apiController;
|
||||
|
||||
Mediator.Subscribe<ClearProfileDataMessage>(this, (msg) =>
|
||||
Mediator.Subscribe<ClearProfileUserDataMessage>(this, (msg) =>
|
||||
{
|
||||
if (msg.UserData != null)
|
||||
_lightlessProfiles.Remove(msg.UserData, out _);
|
||||
_lightlessUserProfiles.Remove(msg.UserData, out _);
|
||||
else
|
||||
_lightlessProfiles.Clear();
|
||||
_lightlessUserProfiles.Clear();
|
||||
});
|
||||
Mediator.Subscribe<DisconnectedMessage>(this, (_) => _lightlessProfiles.Clear());
|
||||
Mediator.Subscribe<DisconnectedMessage>(this, (_) => _lightlessUserProfiles.Clear());
|
||||
}
|
||||
|
||||
public LightlessProfileData GetLightlessProfile(UserData data)
|
||||
public LightlessUserProfileData GetLightlessUserProfile(UserData data)
|
||||
{
|
||||
if (!_lightlessProfiles.TryGetValue(data, out var profile))
|
||||
if (!_lightlessUserProfiles.TryGetValue(data, out var profile))
|
||||
{
|
||||
_ = Task.Run(() => GetLightlessProfileFromService(data));
|
||||
return (_loadingProfileData);
|
||||
return (_loadingProfileUserData);
|
||||
}
|
||||
|
||||
return (profile);
|
||||
}
|
||||
|
||||
public LightlessGroupProfileData GetLightlessGroupProfile(GroupData data)
|
||||
{
|
||||
if (!_lightlessGroupProfiles.TryGetValue(data, out var profile))
|
||||
{
|
||||
_ = Task.Run(() => GetLightlessProfileFromService(data));
|
||||
return (_loadingProfileGroupData);
|
||||
}
|
||||
|
||||
return (profile);
|
||||
@@ -55,26 +69,47 @@ public class LightlessProfileManager : MediatorSubscriberBase
|
||||
{
|
||||
try
|
||||
{
|
||||
_lightlessProfiles[data] = _loadingProfileData;
|
||||
_lightlessUserProfiles[data] = _loadingProfileUserData;
|
||||
var profile = await _apiController.UserGetProfile(new API.Dto.User.UserDto(data)).ConfigureAwait(false);
|
||||
LightlessProfileData profileData = new(profile.Disabled, profile.IsNSFW ?? false,
|
||||
LightlessUserProfileData profileData = new(profile.Disabled, profile.IsNSFW ?? false,
|
||||
string.IsNullOrEmpty(profile.ProfilePictureBase64) ? _lightlessLogo : profile.ProfilePictureBase64,
|
||||
!string.IsNullOrEmpty(data.Alias) && !string.Equals(data.Alias, data.UID, StringComparison.Ordinal) ? _lightlessSupporter : string.Empty,
|
||||
string.IsNullOrEmpty(profile.Description) ? _noDescription : profile.Description);
|
||||
if (profileData.IsNSFW && !_lightlessConfigService.Current.ProfilesAllowNsfw && !string.Equals(_apiController.UID, data.UID, StringComparison.Ordinal))
|
||||
{
|
||||
_lightlessProfiles[data] = _nsfwProfileData;
|
||||
_lightlessUserProfiles[data] = _nsfwProfileData;
|
||||
}
|
||||
else
|
||||
{
|
||||
_lightlessProfiles[data] = profileData;
|
||||
_lightlessUserProfiles[data] = profileData;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// if fails save DefaultProfileData to dict
|
||||
Logger.LogWarning(ex, "Failed to get Profile from service for user {user}", data);
|
||||
_lightlessProfiles[data] = _defaultProfileData;
|
||||
_lightlessUserProfiles[data] = _defaultProfileUserData;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task GetLightlessProfileFromService(GroupData data)
|
||||
{
|
||||
try
|
||||
{
|
||||
_lightlessGroupProfiles[data] = _loadingProfileGroupData;
|
||||
var profile = await _apiController.GroupGetProfile(new API.Dto.Group.GroupDto(data)).ConfigureAwait(false);
|
||||
LightlessGroupProfileData profileData = new(
|
||||
string.IsNullOrEmpty(profile.PictureBase64) ? _lightlessLogo : profile.PictureBase64,
|
||||
!string.IsNullOrEmpty(data.Alias) && !string.Equals(data.Alias, data.GID, StringComparison.Ordinal) ? _lightlessSupporter : string.Empty,
|
||||
string.IsNullOrEmpty(profile.Description) ? _noDescription : profile.Description);
|
||||
|
||||
_lightlessGroupProfiles[data] = profileData;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// if fails save DefaultProfileData to dict
|
||||
Logger.LogWarning(ex, "Failed to get Profile from service for group {group}", data);
|
||||
_lightlessGroupProfiles[data] = _defaultProfileGroupData;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user