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
175 lines
5.5 KiB
C#
175 lines
5.5 KiB
C#
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 = 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);
|
|
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>();
|
|
}
|
|
}
|
|
}
|
|
}
|