SocialPub/PrivaPub/Services/ClientToServer/Public/IPublicAvatarUsersService.cs

175 lines
5.5 KiB
C#
Raw Permalink Normal View History

2023-02-19 00:43:43 +01:00
using Microsoft.Extensions.Localization;
using PrivaPub.ClientModels;
using PrivaPub.Models.User;
using PrivaPub.Resources;
using PrivaPub.StaticServices;
namespace PrivaPub.Services.ClientToServer.Public
{
public interface IPublicAvatarUsersService
{
Task<WebResult> GetLocalAvatar(UserPolicyType userPolicyType, string actor, CancellationToken token);
Task<IEnumerable<Avatar>> GetLocalAvatars(UserPolicyType userPolicyType, CancellationToken token);
Task<WebResult> GetForeignAvatar(UserPolicyType userPolicyType, string actor, CancellationToken token);
Task<IEnumerable<ForeignAvatar>> GetForeignAvatars(UserPolicyType userPolicyType, CancellationToken token);
Task<IEnumerable<Avatar>> GetRootAvatars(string rootUserId, CancellationToken token);
}
public class PublicAvatarUsersService : IPublicAvatarUsersService
{
readonly DbEntities _dbEntities;
readonly IStringLocalizer<GenericRes> _localizer;
readonly ILogger<PublicAvatarUsersService> _logger;
public PublicAvatarUsersService(DbEntities dbEntities,
IStringLocalizer<GenericRes> localizer,
ILogger<PublicAvatarUsersService> logger)
{
_dbEntities = dbEntities;
_localizer = localizer;
_logger = logger;
}
public async Task<WebResult> GetLocalAvatar(UserPolicyType userPolicyType, string actor, CancellationToken token)
{
var result = new WebResult();
try
{
var query = _dbEntities.Avatars;
switch (userPolicyType)
{
case UserPolicyType.IsUser:
query.Match(a => !a.DeletionAt.HasValue && a.UserName == actor);
break;
case UserPolicyType.IsModerator:
query.Match(a => !a.DeletionAt.HasValue && a.UserName == actor);
break;
case UserPolicyType.IsAdmin:
query.Match(a => a.UserName == actor);
break;
}
var avatar = await query.ExecuteFirstAsync(token);
if (avatar == default)
return result.Invalidate(_localizer["User '{0}' not found.", actor], StatusCodes.Status404NotFound);
result.Data = avatar;
return result;
}
catch (Exception ex)
{
_logger.LogError(ex, $"{nameof(PublicAvatarUsersService)}.{nameof(GetLocalAvatar)}");
return result.Invalidate(_localizer["Error: {0}", ex.ToString()], exception: ex);
}
}
public async Task<IEnumerable<Avatar>> GetLocalAvatars(UserPolicyType userPolicyType, CancellationToken token)
{
try
{
var query = _dbEntities.Avatars;
switch (userPolicyType)
{
case UserPolicyType.IsUser:
query.Match(a => !a.DeletionAt.HasValue);
break;
case UserPolicyType.IsModerator:
query.Match(a => !a.DeletionAt.HasValue);
break;
case UserPolicyType.IsAdmin:
break;
}
var avatars = await query.ExecuteAsync(token);
return avatars;
}
catch (Exception ex)
{
_logger.LogError(ex, $"{nameof(PublicAvatarUsersService)}.{nameof(GetLocalAvatars)}");
return Enumerable.Empty<Avatar>();
}
}
public async Task<WebResult> GetForeignAvatar(UserPolicyType userPolicyType, string actor, CancellationToken token)
{
var result = new WebResult();
try
{
var query = _dbEntities.ForeignAvatars;
switch (userPolicyType)
{
case UserPolicyType.IsUser:
query.Match(a => !a.DeletionAt.HasValue && a.UserName == actor);
break;
case UserPolicyType.IsModerator:
query.Match(a => !a.DeletionAt.HasValue && a.UserName == actor);
break;
case UserPolicyType.IsAdmin:
query.Match(a => a.UserName == actor);
break;
}
var avatar = await query.ExecuteFirstAsync(token);
if (avatar == default)
return result.Invalidate(_localizer["User '{0}' not found.", actor], StatusCodes.Status404NotFound);
result.Data = avatar;
return result;
}
catch (Exception ex)
{
_logger.LogError(ex, $"{nameof(PublicAvatarUsersService)}.{nameof(GetForeignAvatar)}");
return result.Invalidate(_localizer["Error: {0}", ex.ToString()], exception: ex);
}
}
public async Task<IEnumerable<ForeignAvatar>> GetForeignAvatars(UserPolicyType userPolicyType, CancellationToken token)
{
try
{
var query = _dbEntities.ForeignAvatars;
switch (userPolicyType)
{
case UserPolicyType.IsUser:
query.Match(a => !a.DeletionAt.HasValue);
break;
case UserPolicyType.IsModerator:
query.Match(a => !a.DeletionAt.HasValue);
break;
case UserPolicyType.IsAdmin:
break;
}
var foreignAvatars = await query.ExecuteAsync(token);
return foreignAvatars;
}
catch (Exception ex)
{
_logger.LogError(ex, $"{nameof(PublicAvatarUsersService)}.{nameof(GetForeignAvatars)}");
return Enumerable.Empty<ForeignAvatar>();
}
}
public async Task<IEnumerable<Avatar>> GetRootAvatars(string rootUserId, CancellationToken token)
{
try
{
var rootUser = _dbEntities.RootUsers.MatchID(rootUserId).ExecuteFirstAsync(token);
if (rootUser == default)
return Enumerable.Empty<Avatar>();
var rootToAvatars = await _dbEntities.RootToAvatars.Match(ra => ra.RootId == rootUserId).ExecuteAsync(token);
if (rootToAvatars.Count == 0)
return Enumerable.Empty<Avatar>();
var avatarIds = rootToAvatars.Select(ra => ra.AvatarId).ToArray();
var avatars = await _dbEntities.Avatars
.Match(a => avatarIds.Contains(a.ID))
.ProjectExcluding(a => a.Settings)
.ExecuteAsync(token);
return avatars;
}
catch (Exception ex)
{
_logger.LogError(ex, $"{nameof(PublicAvatarUsersService)}.{nameof(GetRootAvatars)}");
return Enumerable.Empty<Avatar>();
}
}
}
}