net10, the refactor finished, and federation that works
The tree had not compiled since its first commit: Group, DmGroup and
IGroupUsersService were referenced and never written, an IDE rename had
turned the user-settings DTO into the ViewAvatarServer enum, and the settings
were saved as an entity they no longer were.
Built now:
- Group (an ActivityPub Group actor with its own keys, members, invitation
code and optional password) and DmGroup (a conversation), with
/clientapi/group/{list,insert,update,join,leave,approve}.
- Posts and DMs: /clientapi/post/{list,insert,delete}, /clientapi/dm/{list,insert};
DM recipients are local usernames or user@host handles resolved by WebFinger.
- Invitation sign-up and login against the group's invitation code, checking
the password before any account is created.
- Federation: WebFinger, NodeInfo 2.0, actors at /peasants/{name} (Person,
Group, and an Application instance actor) with SPKI keys, draft-cavage
RSA-SHA256 HTTP signatures both ways, an inbox handling Follow (+Accept),
Undo, Create, Delete and Update, an outbox, notes at /posts/{id}, and a
persisted, retried, signed delivery queue. A post to a group is announced
by the group to its followers (FEP-1b12).
- The unused, broken typed ActivityPub models are replaced by a renderer;
NSign's HMAC setup, which could not federate, is gone.
Upgrade: net10.0, MongoDB.Entities 25.1 (instance DB API, Standard GUIDs),
Swashbuckle 10 / OpenApi 2, Serilog.AspNetCore 10, MailKit 4.18,
PasswordGenerator 3. The JWT keys are 64 bytes (IdentityModel 8 refuses
shorter for HS512). Production runs its own mongod on 127.0.0.1:27022, as
Sintopia's apps do, and deploys to privapub.thepra.dev from the build runner.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012CzABvBkbcFqoHdmi8b9WB
This commit is contained in:
1 parent
56c5396106
commit
075c22228a
78 files changed
+3419
-1091
No files matched your search
@@ -1,37 +1,37 @@
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Microsoft.Extensions.Localization;
|
||||
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.ClientModels;
|
||||
using PrivaPub.ClientModels.User;
|
||||
using PrivaPub.ClientModels.User.Avatar;
|
||||
using PrivaPub.Models;
|
||||
using PrivaPub.Models.User;
|
||||
using PrivaPub.Resources;
|
||||
using PrivaPub.Services.Federation;
|
||||
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);
|
||||
Task<WebResult> GetRootAvatars(string rootUserId, CancellationToken token);
|
||||
}
|
||||
|
||||
public class PrivateAvatarUsersService : IPrivateAvatarUsersService
|
||||
{
|
||||
readonly AppConfigurationService _appConfiguration;
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly IStringLocalizer<GenericRes> _localizer;
|
||||
readonly ILogger<PrivateAvatarUsersService> _logger;
|
||||
|
||||
public PrivateAvatarUsersService(AppConfigurationService appConfiguration,
|
||||
public PrivateAvatarUsersService(ILocalActorService localActors,
|
||||
DbEntities dbEntities,
|
||||
IStringLocalizer<GenericRes> localizer,
|
||||
ILogger<PrivateAvatarUsersService> logger)
|
||||
{
|
||||
_appConfiguration = appConfiguration;
|
||||
_localActors = localActors;
|
||||
_dbEntities = dbEntities;
|
||||
_localizer = localizer;
|
||||
_logger = logger;
|
||||
@@ -42,54 +42,46 @@ namespace PrivaPub.Services.ClientToServer.Private
|
||||
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]);
|
||||
var userName = form.UserName?.Trim().ToLowerInvariant();
|
||||
if (string.IsNullOrEmpty(userName) || !userName.All(c => char.IsAsciiLetterOrDigit(c) || c == '_'))
|
||||
return result.Invalidate(_localizer["The username may contain only letters, digits and underscores."]);
|
||||
|
||||
if (await _localActors.IsUserNameTaken(userName, default))
|
||||
return result.Invalidate(_localizer["The username '{0}' is already take.", userName]);
|
||||
|
||||
if (!await IsValidRootUser(form.RootId))
|
||||
return result.Invalidate(_localizer["You can't do this action because of your account status."]);
|
||||
|
||||
var (privateKey, publicKey) = Keys.NewKeyPair();
|
||||
var newAvatar = new Avatar
|
||||
{
|
||||
Name = form.Name,
|
||||
UserName = form.UserName,
|
||||
UserName = userName,
|
||||
Biography = form.Biography,
|
||||
Fields = form.Fields,
|
||||
PersonalNote = form.PersonalNote,
|
||||
PrivateKey = privateKey,
|
||||
PublicKey = publicKey,
|
||||
Domain = _localActors.BaseAddress
|
||||
};
|
||||
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 (form.Settings is { IsDefault: false })
|
||||
newAvatar.Settings = ToSettings(form.Settings);
|
||||
|
||||
if (_appConfiguration.AppConfiguration == default)
|
||||
await _appConfiguration.Init();
|
||||
var actor = _localActors.FromAvatar(newAvatar);
|
||||
newAvatar.Url = actor.Uri;
|
||||
newAvatar.InboxURL = actor.Inbox;
|
||||
newAvatar.OutboxURL = actor.Outbox;
|
||||
|
||||
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;
|
||||
await DB.Default.SaveAsync(newAvatar);
|
||||
await DB.Default.SaveAsync(new RootToAvatar { RootId = form.RootId, AvatarId = newAvatar.ID });
|
||||
|
||||
result.Data = ToView(newAvatar);
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"{nameof(PrivateAvatarUsersService)}.{nameof(InsertAvatar)}");
|
||||
return result.Invalidate(_localizer["Error: {0}", ex.ToString()], exception: ex);
|
||||
return result.Invalidate(_localizer["Error: {0}", ex.Message], exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,18 +90,106 @@ namespace PrivaPub.Services.ClientToServer.Private
|
||||
var result = new WebResult();
|
||||
try
|
||||
{
|
||||
if (!await IsValidRootUser(form.RootId))
|
||||
return result.Invalidate(_localizer["You can't do this action because of your account status."]);
|
||||
|
||||
var owns = await _dbEntities.RootToAvatars
|
||||
.Match(ra => ra.RootId == form.RootId && ra.AvatarId == form.AvatarId)
|
||||
.ExecuteAnyAsync();
|
||||
if (!owns)
|
||||
return result.Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
|
||||
var avatar = await _dbEntities.Avatars.MatchID(form.AvatarId).ExecuteFirstAsync();
|
||||
if (avatar == default || avatar.DeletionAt.HasValue)
|
||||
return result.Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
|
||||
avatar.Name = form.Name;
|
||||
avatar.Biography = form.Biography;
|
||||
avatar.Fields = form.Fields ?? new();
|
||||
avatar.PersonalNote = form.PersonalNote;
|
||||
if (form.Settings != default)
|
||||
avatar.Settings = ToSettings(form.Settings);
|
||||
avatar.UpdatedAt = DateTime.UtcNow;
|
||||
await DB.Default.SaveAsync(avatar);
|
||||
|
||||
result.Data = ToView(avatar);
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"{nameof(PrivateAvatarUsersService)}.{nameof(UpdateAvatar)}");
|
||||
return result.Invalidate(_localizer["Error: {0}", ex.ToString()], exception: ex);
|
||||
return result.Invalidate(_localizer["Error: {0}", ex.Message], exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<WebResult> GetRootAvatars(string rootUserId, CancellationToken token)
|
||||
{
|
||||
var result = new WebResult();
|
||||
try
|
||||
{
|
||||
var avatarIds = (await _dbEntities.RootToAvatars.Match(ra => ra.RootId == rootUserId).ExecuteAsync(token))
|
||||
.Select(ra => ra.AvatarId)
|
||||
.ToList();
|
||||
var avatars = avatarIds.Count == 0
|
||||
? new List<Avatar>()
|
||||
: await _dbEntities.Avatars.Match(a => avatarIds.Contains(a.ID) && !a.DeletionAt.HasValue).ExecuteAsync(token);
|
||||
result.Data = avatars.Select(ToView).ToList();
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"{nameof(PrivateAvatarUsersService)}.{nameof(GetRootAvatars)}");
|
||||
return result.Invalidate(_localizer["Error: {0}", ex.Message], exception: ex);
|
||||
}
|
||||
}
|
||||
|
||||
async ValueTask<bool> IsValidRootUser(string rootUserId) =>
|
||||
await _dbEntities.RootUsers.MatchID(rootUserId)
|
||||
.Match(ru => !ru.DeletedAt.HasValue)
|
||||
.ExecuteAnyAsync();
|
||||
!string.IsNullOrEmpty(rootUserId)
|
||||
&& await _dbEntities.RootUsers.MatchID(rootUserId)
|
||||
.Match(ru => !ru.DeletedAt.HasValue && !ru.IsBanned)
|
||||
.ExecuteAnyAsync();
|
||||
|
||||
static AvatarSettings ToSettings(ViewAvatarSettings settings) => new()
|
||||
{
|
||||
IsDefault = settings.IsDefault,
|
||||
DarkThemeIndexColour = settings.DarkThemeIndexColour,
|
||||
IconsThemeIndexColour = settings.IconsThemeIndexColour,
|
||||
LanguageCode = settings.LanguageCode,
|
||||
LightThemeIndexColour = settings.LightThemeIndexColour,
|
||||
PreferSystemTheming = settings.PreferSystemTheming,
|
||||
ThemeIsDarkGray = settings.ThemeIsDarkGray,
|
||||
ThemeIsDarkMode = settings.ThemeIsDarkMode,
|
||||
ThemeIsLightGray = settings.ThemeIsLightGray
|
||||
};
|
||||
|
||||
ViewAvatar ToView(Avatar avatar) => new()
|
||||
{
|
||||
Id = avatar.ID,
|
||||
Url = avatar.Url,
|
||||
Handle = _localActors.FromAvatar(avatar).Handle,
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Microsoft.Extensions.Localization;
|
||||
|
||||
using PrivaPub.ClientModels;
|
||||
using PrivaPub.Models.User;
|
||||
@@ -151,7 +151,7 @@ namespace PrivaPub.Services.ClientToServer.Public
|
||||
{
|
||||
try
|
||||
{
|
||||
var rootUser = _dbEntities.RootUsers.MatchID(rootUserId).ExecuteFirstAsync(token);
|
||||
var rootUser = await _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);
|
||||
|
||||
Reference in new issue
Block a user