2026-10-01 08:05:56 +02:00
|
|
|
using Microsoft.Extensions.Localization;
|
2023-02-19 00:43:43 +01:00
|
|
|
|
|
|
|
|
using MongoDB.Entities;
|
|
|
|
|
|
|
|
|
|
using PrivaPub.ClientModels;
|
2026-10-01 08:05:56 +02:00
|
|
|
using PrivaPub.ClientModels.User;
|
2023-02-19 00:43:43 +01:00
|
|
|
using PrivaPub.ClientModels.User.Avatar;
|
2026-10-01 11:01:08 +02:00
|
|
|
using PrivaPub.Models.Federation;
|
2023-02-19 00:43:43 +01:00
|
|
|
using PrivaPub.Models.User;
|
|
|
|
|
using PrivaPub.Resources;
|
|
|
|
|
using PrivaPub.StaticServices;
|
2026-10-01 10:39:41 +02:00
|
|
|
using PrivaPub.Federation.Actors;
|
2026-10-01 11:34:35 +02:00
|
|
|
using PrivaPub.Federation.Outbox;
|
2023-02-19 00:43:43 +01:00
|
|
|
|
|
|
|
|
namespace PrivaPub.Services.ClientToServer.Private
|
|
|
|
|
{
|
|
|
|
|
public interface IPrivateAvatarUsersService
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
Task<WebResult> UpdateAvatar(UpdateAvatarForm form, CancellationToken token = default);
|
|
|
|
|
Task<WebResult> InsertAvatar(InsertAvatarForm form, CancellationToken token = default);
|
2026-10-01 08:05:56 +02:00
|
|
|
Task<WebResult> GetRootAvatars(string rootUserId, CancellationToken token);
|
2023-02-19 00:43:43 +01:00
|
|
|
}
|
|
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
public class PrivateAvatarUsersService(
|
|
|
|
|
ILocalActorService localActors,
|
|
|
|
|
IOutboxPublisher outbox,
|
|
|
|
|
DbEntities dbEntities,
|
|
|
|
|
IStringLocalizer<GenericRes> localizer,
|
|
|
|
|
ILogger<PrivateAvatarUsersService> logger) : IPrivateAvatarUsersService
|
2023-02-19 00:43:43 +01:00
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
public async Task<WebResult> InsertAvatar(InsertAvatarForm form, CancellationToken token = default)
|
2023-02-19 00:43:43 +01:00
|
|
|
{
|
|
|
|
|
var result = new WebResult();
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-10-01 08:05:56 +02:00
|
|
|
var userName = form.UserName?.Trim().ToLowerInvariant();
|
|
|
|
|
if (string.IsNullOrEmpty(userName) || !userName.All(c => char.IsAsciiLetterOrDigit(c) || c == '_'))
|
2026-11-10 08:11:11 +01:00
|
|
|
return result.Invalidate(26001.ToCodedError(localizer["The username may contain only letters, digits and underscores."]));
|
2026-10-01 08:05:56 +02:00
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
if (await localActors.IsUserNameTaken(userName, default))
|
2026-11-10 08:11:11 +01:00
|
|
|
return result.Invalidate(26002.ToCodedError(localizer["The username '{0}' is already taken.", userName]));
|
2023-02-19 00:43:43 +01:00
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
if (!await IsValidRootUser(form.RootId, token))
|
2026-11-10 08:11:11 +01:00
|
|
|
return result.Invalidate(26003.ToCodedError(localizer["You can't do this action because of your account status."]));
|
2023-02-19 00:43:43 +01:00
|
|
|
|
2026-10-01 08:05:56 +02:00
|
|
|
var (privateKey, publicKey) = Keys.NewKeyPair();
|
2023-02-19 00:43:43 +01:00
|
|
|
var newAvatar = new Avatar
|
|
|
|
|
{
|
|
|
|
|
Name = form.Name,
|
2026-10-01 08:05:56 +02:00
|
|
|
UserName = userName,
|
2023-02-19 00:43:43 +01:00
|
|
|
Biography = form.Biography,
|
|
|
|
|
Fields = form.Fields,
|
|
|
|
|
PersonalNote = form.PersonalNote,
|
2026-10-01 08:05:56 +02:00
|
|
|
PrivateKey = privateKey,
|
|
|
|
|
PublicKey = publicKey,
|
2026-10-06 09:01:21 +02:00
|
|
|
SigningKey = Federation.Signing.IntegrityProofs.NewSeed(),
|
2026-11-10 01:24:43 +01:00
|
|
|
Domain = localActors.BaseAddress
|
2023-02-19 00:43:43 +01:00
|
|
|
};
|
2026-10-17 05:57:25 +02:00
|
|
|
newAvatar.Emojis = PrivaPub.Domain.Content.LocalEmojis.OfProfile(newAvatar);
|
2026-10-01 11:01:08 +02:00
|
|
|
newAvatar.ID = (string)newAvatar.GenerateNewID();
|
2026-11-10 01:24:43 +01:00
|
|
|
if (!await localActors.TryReserveUserName(userName, LocalActorKind.Person, newAvatar.ID, default))
|
2026-11-10 08:11:11 +01:00
|
|
|
return result.Invalidate(26004.ToCodedError(localizer["The username '{0}' is already taken.", userName]));
|
2026-10-01 08:05:56 +02:00
|
|
|
if (form.Settings is { IsDefault: false })
|
2026-10-04 02:37:38 +02:00
|
|
|
Apply(newAvatar.Settings, form.Settings);
|
2026-10-01 08:05:56 +02:00
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
var actor = localActors.FromAvatar(newAvatar);
|
2026-10-01 08:05:56 +02:00
|
|
|
newAvatar.Url = actor.Uri;
|
|
|
|
|
newAvatar.InboxURL = actor.Inbox;
|
|
|
|
|
newAvatar.OutboxURL = actor.Outbox;
|
2023-02-19 00:43:43 +01:00
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
await DB.Default.SaveAsync(newAvatar, token);
|
|
|
|
|
await DB.Default.SaveAsync(new RootToAvatar { RootId = form.RootId, AvatarId = newAvatar.ID }, token);
|
2026-10-01 08:05:56 +02:00
|
|
|
|
|
|
|
|
result.Data = ToView(newAvatar);
|
2023-02-19 00:43:43 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(PrivateAvatarUsersService)}.{nameof(InsertAvatar)}");
|
2026-11-10 08:11:11 +01:00
|
|
|
return result.Invalidate(26005.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
2023-02-19 00:43:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
public async Task<WebResult> UpdateAvatar(UpdateAvatarForm form, CancellationToken token = default)
|
2023-02-19 00:43:43 +01:00
|
|
|
{
|
|
|
|
|
var result = new WebResult();
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
if (!await IsValidRootUser(form.RootId, token))
|
2026-11-10 08:11:11 +01:00
|
|
|
return result.Invalidate(26006.ToCodedError(localizer["You can't do this action because of your account status."]));
|
2023-02-19 00:43:43 +01:00
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
var owns = await dbEntities.RootToAvatars
|
2026-10-01 08:05:56 +02:00
|
|
|
.Match(ra => ra.RootId == form.RootId && ra.AvatarId == form.AvatarId)
|
2026-11-10 01:24:43 +01:00
|
|
|
.ExecuteAnyAsync(token);
|
2026-10-01 08:05:56 +02:00
|
|
|
if (!owns)
|
2026-11-10 08:11:11 +01:00
|
|
|
return result.Invalidate(26007.ToCodedError(localizer["Avatar not found."]), StatusCodes.Status404NotFound);
|
2026-10-01 08:05:56 +02:00
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
var avatar = await dbEntities.Avatars.MatchID(form.AvatarId).ExecuteFirstAsync(token);
|
2026-10-01 08:05:56 +02:00
|
|
|
if (avatar == default || avatar.DeletionAt.HasValue)
|
2026-11-10 08:11:11 +01:00
|
|
|
return result.Invalidate(26008.ToCodedError(localizer["Avatar not found."]), StatusCodes.Status404NotFound);
|
2026-10-17 05:23:45 +02:00
|
|
|
// a suspended persona's actor stays as it was until the suspension is lifted (R6)
|
|
|
|
|
if (avatar.SuspendedAt.HasValue)
|
2026-11-10 08:11:11 +01:00
|
|
|
return result.Invalidate(26009.ToCodedError(localizer["This persona is suspended."]), StatusCodes.Status403Forbidden);
|
2026-10-01 08:05:56 +02:00
|
|
|
|
|
|
|
|
avatar.Name = form.Name;
|
|
|
|
|
avatar.Biography = form.Biography;
|
|
|
|
|
avatar.Fields = form.Fields ?? new();
|
|
|
|
|
avatar.PersonalNote = form.PersonalNote;
|
2026-10-17 05:57:25 +02:00
|
|
|
avatar.Emojis = PrivaPub.Domain.Content.LocalEmojis.OfProfile(avatar);//the server's custom emoji its profile uses (R7)
|
2026-10-01 08:05:56 +02:00
|
|
|
if (form.Settings != default)
|
2026-10-04 02:37:38 +02:00
|
|
|
Apply(avatar.Settings ??= new(), form.Settings);
|
2026-10-01 08:05:56 +02:00
|
|
|
avatar.UpdatedAt = DateTime.UtcNow;
|
2026-11-10 01:24:43 +01:00
|
|
|
await DB.Default.SaveAsync(avatar, token);
|
|
|
|
|
await outbox.PublishProfile(localActors.FromAvatar(avatar), default);
|
2026-10-01 08:05:56 +02:00
|
|
|
|
|
|
|
|
result.Data = ToView(avatar);
|
|
|
|
|
return result;
|
2023-02-19 00:43:43 +01:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(PrivateAvatarUsersService)}.{nameof(UpdateAvatar)}");
|
2026-11-10 08:11:11 +01:00
|
|
|
return result.Invalidate(26010.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
2026-10-01 08:05:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<WebResult> GetRootAvatars(string rootUserId, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var result = new WebResult();
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
var avatarIds = (await dbEntities.RootToAvatars.Match(ra => ra.RootId == rootUserId).ExecuteAsync(token))
|
2026-10-01 08:05:56 +02:00
|
|
|
.Select(ra => ra.AvatarId)
|
|
|
|
|
.ToList();
|
|
|
|
|
var avatars = avatarIds.Count == 0
|
|
|
|
|
? new List<Avatar>()
|
2026-11-10 01:24:43 +01:00
|
|
|
: await dbEntities.Avatars.Match(a => avatarIds.Contains(a.ID) && !a.DeletionAt.HasValue).ExecuteAsync(token);
|
2026-10-01 08:05:56 +02:00
|
|
|
result.Data = avatars.Select(ToView).ToList();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(PrivateAvatarUsersService)}.{nameof(GetRootAvatars)}");
|
2026-11-10 08:11:11 +01:00
|
|
|
return result.Invalidate(26011.ToCodedError(localizer["Something went wrong."]), exception: ex);
|
2023-02-19 00:43:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
async ValueTask<bool> IsValidRootUser(string rootUserId, CancellationToken token = default) =>
|
2026-10-01 08:05:56 +02:00
|
|
|
!string.IsNullOrEmpty(rootUserId)
|
2026-11-10 01:24:43 +01:00
|
|
|
&& await dbEntities.RootUsers.MatchID(rootUserId)
|
2026-10-01 08:05:56 +02:00
|
|
|
.Match(ru => !ru.DeletedAt.HasValue && !ru.IsBanned)
|
2026-11-10 01:24:43 +01:00
|
|
|
.ExecuteAnyAsync(token);
|
2026-10-01 08:05:56 +02:00
|
|
|
|
2026-10-04 02:37:38 +02:00
|
|
|
// The client API carries only the theme; the rest (discoverable, locked, quote policy…) is set through the Mastodon API
|
|
|
|
|
// and must survive an edit made here.
|
|
|
|
|
static void Apply(AvatarSettings target, ViewAvatarSettings settings)
|
2026-10-01 08:05:56 +02:00
|
|
|
{
|
2026-10-04 02:37:38 +02:00
|
|
|
target.IsDefault = settings.IsDefault;
|
|
|
|
|
target.DarkThemeIndexColour = settings.DarkThemeIndexColour;
|
|
|
|
|
target.IconsThemeIndexColour = settings.IconsThemeIndexColour;
|
|
|
|
|
target.LanguageCode = settings.LanguageCode;
|
|
|
|
|
target.LightThemeIndexColour = settings.LightThemeIndexColour;
|
|
|
|
|
target.PreferSystemTheming = settings.PreferSystemTheming;
|
|
|
|
|
target.ThemeIsDarkGray = settings.ThemeIsDarkGray;
|
|
|
|
|
target.ThemeIsDarkMode = settings.ThemeIsDarkMode;
|
|
|
|
|
target.ThemeIsLightGray = settings.ThemeIsLightGray;
|
|
|
|
|
}
|
2026-10-01 08:05:56 +02:00
|
|
|
|
|
|
|
|
ViewAvatar ToView(Avatar avatar) => new()
|
|
|
|
|
{
|
|
|
|
|
Id = avatar.ID,
|
|
|
|
|
Url = avatar.Url,
|
2026-11-10 01:24:43 +01:00
|
|
|
Handle = localActors.FromAvatar(avatar).Handle,
|
2026-10-01 08:05:56 +02:00
|
|
|
Name = avatar.Name,
|
|
|
|
|
UserName = avatar.UserName,
|
|
|
|
|
Biography = avatar.Biography,
|
|
|
|
|
Fields = avatar.Fields,
|
|
|
|
|
PictureURL = avatar.PictureURL,
|
|
|
|
|
ThumbnailURL = avatar.ThumbnailURL,
|
|
|
|
|
SharedPersonalContacts = avatar.SharedPersonalContacts,
|
|
|
|
|
AccountState = (ViewAvatarState)(int)avatar.AccountState,
|
|
|
|
|
ServerType = ViewAvatarServer.PrivaPub,
|
|
|
|
|
Settings = new()
|
|
|
|
|
{
|
|
|
|
|
IsDefault = avatar.Settings.IsDefault,
|
|
|
|
|
LanguageCode = avatar.Settings.LanguageCode,
|
|
|
|
|
IconsThemeIndexColour = avatar.Settings.IconsThemeIndexColour,
|
|
|
|
|
LightThemeIndexColour = avatar.Settings.LightThemeIndexColour,
|
|
|
|
|
DarkThemeIndexColour = avatar.Settings.DarkThemeIndexColour,
|
|
|
|
|
PreferSystemTheming = avatar.Settings.PreferSystemTheming,
|
|
|
|
|
ThemeIsDarkMode = avatar.Settings.ThemeIsDarkMode,
|
|
|
|
|
ThemeIsDarkGray = avatar.Settings.ThemeIsDarkGray,
|
|
|
|
|
ThemeIsLightGray = avatar.Settings.ThemeIsLightGray
|
|
|
|
|
},
|
|
|
|
|
UpdatedAt = avatar.UpdatedAt,
|
|
|
|
|
CreatedAt = avatar.CreatedAt
|
|
|
|
|
};
|
2023-02-19 00:43:43 +01:00
|
|
|
}
|
|
|
|
|
}
|