SocialPub/PrivaPub/Services/ClientToServer/Private/IPrivateAvatarUsersService.cs

116 lines
3.7 KiB
C#
Raw Permalink Normal View History

2023-02-19 00:43:43 +01:00
using Microsoft.Extensions.Localization;
using MongoDB.Entities;
using PrivaPub.ClientModels;
using PrivaPub.ClientModels.User.Avatar;
using PrivaPub.Models;
using PrivaPub.Models.User;
using PrivaPub.Resources;
using PrivaPub.StaticServices;
using System.Security.Cryptography;
namespace PrivaPub.Services.ClientToServer.Private
{
public interface IPrivateAvatarUsersService
{
Task<WebResult> UpdateAvatar(UpdateAvatarForm form);
Task<WebResult> InsertAvatar(InsertAvatarForm form);
}
public class PrivateAvatarUsersService : IPrivateAvatarUsersService
{
readonly AppConfigurationService _appConfiguration;
readonly DbEntities _dbEntities;
readonly IStringLocalizer<GenericRes> _localizer;
readonly ILogger<PrivateAvatarUsersService> _logger;
public PrivateAvatarUsersService(AppConfigurationService appConfiguration,
DbEntities dbEntities,
IStringLocalizer<GenericRes> localizer,
ILogger<PrivateAvatarUsersService> logger)
{
_appConfiguration = appConfiguration;
_dbEntities = dbEntities;
_localizer = localizer;
_logger = logger;
}
public async Task<WebResult> InsertAvatar(InsertAvatarForm form)
{
var result = new WebResult();
try
{
if (await _dbEntities.Avatars.Match(a => a.UserName == form.UserName).ExecuteAnyAsync())
return result.Invalidate(_localizer["The username '{0}' is already take.", form.UserName]);
if (!await IsValidRootUser(form.RootId))
return result.Invalidate(_localizer["You can't do this action because of your account status."]);
var newAvatar = new Avatar
{
Name = form.Name,
UserName = form.UserName,
Biography = form.Biography,
Fields = form.Fields,
PersonalNote = form.PersonalNote,
};
if (!form.Settings.IsDefault)
newAvatar.Settings = new()
{
IsDefault = false,
DarkThemeIndexColour = form.Settings.DarkThemeIndexColour,
IconsThemeIndexColour = form.Settings.IconsThemeIndexColour,
LanguageCode = form.Settings.LanguageCode,
LightThemeIndexColour = form.Settings.LightThemeIndexColour,
PreferSystemTheming = form.Settings.PreferSystemTheming,
ThemeIsDarkGray = form.Settings.ThemeIsDarkGray,
ThemeIsDarkMode = form.Settings.ThemeIsDarkMode,
ThemeIsLightGray = form.Settings.ThemeIsLightGray
};
if (_appConfiguration.AppConfiguration == default)
await _appConfiguration.Init();
newAvatar.Url = _appConfiguration.AppConfiguration.BackendBaseAddress + $"/peasants/{form.UserName}";
newAvatar.Domain = _appConfiguration.AppConfiguration.BackendBaseAddress;
newAvatar.InboxURL = _appConfiguration.AppConfiguration.BackendBaseAddress + $"/peasants/{form.UserName}/mouth";
newAvatar.OutboxURL = _appConfiguration.AppConfiguration.BackendBaseAddress + $"/peasants/{form.UserName}/anus";
var rsa = RSA.Create();
newAvatar.PrivateKey = rsa.ExportRSAPrivateKeyPem();
newAvatar.PublicKey = rsa.ExportRSAPublicKeyPem();
await newAvatar.SaveAsync();
result.Data = newAvatar;
return result;
}
catch (Exception ex)
{
_logger.LogError(ex, $"{nameof(PrivateAvatarUsersService)}.{nameof(InsertAvatar)}");
return result.Invalidate(_localizer["Error: {0}", ex.ToString()], exception: ex);
}
}
public async Task<WebResult> UpdateAvatar(UpdateAvatarForm form)
{
var result = new WebResult();
try
{
}
catch (Exception ex)
{
_logger.LogError(ex, $"{nameof(PrivateAvatarUsersService)}.{nameof(UpdateAvatar)}");
return result.Invalidate(_localizer["Error: {0}", ex.ToString()], exception: ex);
}
}
async ValueTask<bool> IsValidRootUser(string rootUserId) =>
await _dbEntities.RootUsers.MatchID(rootUserId)
.Match(ru => !ru.DeletedAt.HasValue)
.ExecuteAnyAsync();
}
}