House rules: primary constructors, a cancellation token on every async call, and the rules fail the build
Build / Build (push) Successful in 30s
Build / Build (push) Successful in 30s
- Services, controllers and jobs take their dependencies through primary constructors and use them directly; where a method already named a local after a dependency, the dependency takes its type's name. - Every async method takes a CancellationToken last and passes it down to MongoDB, HTTP and the job queue. The setup claim is still given back when a request is cancelled. - The federation and media options are read through IOptionsMonitor. - The Mastodon entities keep sending "" for empty strings, and every model field where null means something says so. - The inbox's 503 with Retry-After, which other servers retry, is the one server error left, and says why. - Arasaka.HouseRules.Analyzers checks all of this in every build, here and in PrivaPub.ClientModels as decePubClient builds it. Checked against GoToSocial in the pasture: 64 passed, 0 failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
This commit is contained in:
1 parent
f0a14c737f
commit
0f73f69239
176 files changed
+2789
-3974
No files matched your search
@@ -23,39 +23,24 @@ using PostEntity = PrivaPub.Models.Post.Post;
|
||||
|
||||
namespace PrivaPub.Api.Mastodon.Controllers
|
||||
{
|
||||
public class AccountsController : MastodonController
|
||||
public class AccountsController(
|
||||
MastodonMapper mapper,
|
||||
DbEntities dbEntities,
|
||||
ILocalActorService localActors,
|
||||
IFollowService followService,
|
||||
IOutboxPublisher outbox,
|
||||
IRelationshipService relationshipService,
|
||||
IFeedFollows feedFollows) : MastodonController
|
||||
{
|
||||
readonly MastodonMapper _mapper;
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly IRemoteActorService _remoteActors;
|
||||
readonly IFollowService _follows;
|
||||
readonly IOutboxPublisher _outbox;
|
||||
readonly IRelationshipService _relationships;
|
||||
readonly IFeedFollows _feeds;
|
||||
|
||||
public AccountsController(MastodonMapper mapper, DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors,
|
||||
IFollowService follows, IOutboxPublisher outbox, IRelationshipService relationships, IFeedFollows feeds)
|
||||
{
|
||||
_feeds = feeds;
|
||||
_relationships = relationships;
|
||||
_mapper = mapper;
|
||||
_dbEntities = dbEntities;
|
||||
_localActors = localActors;
|
||||
_remoteActors = remoteActors;
|
||||
_follows = follows;
|
||||
_outbox = outbox;
|
||||
}
|
||||
|
||||
// Mastodon 4.4: a token with the profile scope alone may read it too
|
||||
[HttpGet("/api/v1/accounts/verify_credentials"), Scope("read:accounts|profile")]
|
||||
public async Task<IActionResult> VerifyCredentials(CancellationToken token) => Json(await _mapper.Local(Me, withSource: true, token));
|
||||
public async Task<IActionResult> VerifyCredentials(CancellationToken token) => Json(await mapper.Local(Me, withSource: true, token));
|
||||
|
||||
[HttpPatch("/api/v1/accounts/update_credentials"), Scope("write:accounts"), EnableRateLimiting(RateLimiting.Uploads), RequestSizeLimit(20 * 1024 * 1024),
|
||||
RequestFormLimits(MultipartBodyLengthLimit = 20 * 1024 * 1024)]
|
||||
public async Task<IActionResult> UpdateCredentials([FromServices] IMediaService media, CancellationToken token)
|
||||
{
|
||||
var avatar = await _dbEntities.Avatars.MatchID(Me.Id).ExecuteFirstAsync(token);
|
||||
var avatar = await dbEntities.Avatars.MatchID(Me.Id).ExecuteFirstAsync(token);
|
||||
// a new picture is a row of its own; the one it replaces goes to the trash once the profile is saved
|
||||
var replaced = new List<Models.Media.MediaAttachment>();
|
||||
if (Request.HasFormContentType)
|
||||
@@ -122,9 +107,9 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
foreach (var made in replaced)
|
||||
await media.Trash(m => m.ProfileOfAvatarId == avatar.ID && m.Kind == made.Kind && m.ID != made.ID, "replaced", token);
|
||||
|
||||
var actor = _localActors.FromAvatar(avatar);
|
||||
await _outbox.PublishProfile(actor, token);
|
||||
return Json(await _mapper.Local(actor, withSource: true, token));
|
||||
var actor = localActors.FromAvatar(avatar);
|
||||
await outbox.PublishProfile(actor, token);
|
||||
return Json(await mapper.Local(actor, withSource: true, token));
|
||||
}
|
||||
|
||||
// Mastodon's: the picture goes (to the trash), the profile shows the default again, and followers are told
|
||||
@@ -136,7 +121,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
|
||||
async Task<IActionResult> DeletePicture(IMediaService media, string kind, CancellationToken token)
|
||||
{
|
||||
var avatar = await _dbEntities.Avatars.MatchID(Me.Id).ExecuteFirstAsync(token);
|
||||
var avatar = await dbEntities.Avatars.MatchID(Me.Id).ExecuteFirstAsync(token);
|
||||
if (kind == "avatar")
|
||||
avatar.PictureURL = default;
|
||||
else
|
||||
@@ -144,9 +129,9 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
avatar.UpdatedAt = DateTime.UtcNow;
|
||||
await DB.Default.SaveAsync(avatar, token);
|
||||
await media.Trash(m => m.ProfileOfAvatarId == avatar.ID && m.Kind == kind, "removed", token);
|
||||
var actor = _localActors.FromAvatar(avatar);
|
||||
await _outbox.PublishProfile(actor, token);
|
||||
return Json(await _mapper.Local(actor, withSource: true, token));
|
||||
var actor = localActors.FromAvatar(avatar);
|
||||
await outbox.PublishProfile(actor, token);
|
||||
return Json(await mapper.Local(actor, withSource: true, token));
|
||||
}
|
||||
|
||||
[HttpGet("/api/v1/accounts/lookup"), Scope("read:accounts", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||
@@ -156,17 +141,17 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
var parts = acct?.Split('@');
|
||||
if (parts is not { Length: 1 or 2 } || string.IsNullOrEmpty(parts[0]))
|
||||
return NotFoundError();
|
||||
var localDomain = new Uri(_localActors.BaseAddress).Authority;
|
||||
var localDomain = new Uri(localActors.BaseAddress).Authority;
|
||||
if (parts.Length == 1 || parts[1].Equals(localDomain, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var local = await _localActors.FindByUserName(parts[0], token);
|
||||
return local is { IsFederated: true, IsCircle: false, IsServerActor: false, Suspended: false } ? Json(await _mapper.Local(local, false, token)) : NotFoundError();
|
||||
var local = await localActors.FindByUserName(parts[0], token);
|
||||
return local is { IsFederated: true, IsCircle: false, IsServerActor: false, Suspended: false } ? Json(await mapper.Local(local, false, token)) : NotFoundError();
|
||||
}
|
||||
var userName = parts[0];
|
||||
var domain = parts[1].ToLowerInvariant();
|
||||
var foreign = await _dbEntities.ForeignAvatars.Match(f => f.UserName == userName && f.Domain == domain && !f.DeletionAt.HasValue && !f.SuspendedAt.HasValue)
|
||||
var foreign = await dbEntities.ForeignAvatars.Match(f => f.UserName == userName && f.Domain == domain && !f.DeletionAt.HasValue && !f.SuspendedAt.HasValue)
|
||||
.ExecuteFirstAsync(token);
|
||||
return foreign == default ? NotFoundError() : Json(_mapper.Foreign(foreign));
|
||||
return foreign == default ? NotFoundError() : Json(mapper.Foreign(foreign));
|
||||
}
|
||||
|
||||
[HttpGet("/api/v1/accounts/relationships"), Scope("read:follows")]
|
||||
@@ -190,7 +175,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
{
|
||||
var accounts = new List<object>();
|
||||
foreach (var id in Params.List("id").Distinct().Take(40))
|
||||
if (await _mapper.Account(id, token) is { } account)
|
||||
if (await mapper.Account(id, token) is { } account)
|
||||
accounts.Add(account);
|
||||
return Json(accounts);
|
||||
}
|
||||
@@ -198,7 +183,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
[HttpGet("/api/v1/accounts/{id}"), Scope("read:accounts", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||
public async Task<IActionResult> Get(string id, CancellationToken token)
|
||||
{
|
||||
var account = await _mapper.Account(id, token);
|
||||
var account = await mapper.Account(id, token);
|
||||
return account == default ? NotFoundError() : Json(account);
|
||||
}
|
||||
|
||||
@@ -215,22 +200,22 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
{
|
||||
var featured = (await DB.Default.Find<RemoteFeatured>().Match(f => f.ActorURI == remote.ActorURI).Sort(f => f.Position, Order.Ascending)
|
||||
.Limit(Federation.Actors.FeaturedPosts.MaxFeatured).ExecuteAsync(token)).Select(f => f.PostId).ToList();
|
||||
var featuredPosts = await _dbEntities.Posts.Match(p => featured.Contains(p.ID) && !p.DeletedAt.HasValue).Match(VisibilityPolicy.IsPublic).ExecuteAsync(token);
|
||||
return Json(await _mapper.Statuses(featured.Select(id => featuredPosts.FirstOrDefault(p => p.ID == id)).Where(p => p != default).ToList(), MyId, token));
|
||||
var featuredPosts = await dbEntities.Posts.Match(p => featured.Contains(p.ID) && !p.DeletedAt.HasValue).Match(VisibilityPolicy.IsPublic).ExecuteAsync(token);
|
||||
return Json(await mapper.Statuses(featured.Select(id => featuredPosts.FirstOrDefault(p => p.ID == id)).Where(p => p != default).ToList(), MyId, token));
|
||||
}
|
||||
var pinIds = (await DB.Default.Find<Pin>().Match(p => p.AvatarId == local.Id).Sort(p => p.ID, Order.Descending).ExecuteAsync(token)).Select(p => p.PostId).ToList();
|
||||
var pinnedPosts = await _dbEntities.Posts.Match(p => pinIds.Contains(p.ID)).Match(VisibilityPolicy.IsPublic).ExecuteAsync(token);
|
||||
return Json(await _mapper.Statuses(pinIds.Select(id => pinnedPosts.FirstOrDefault(p => p.ID == id)).Where(p => p != default).ToList(), MyId, token));
|
||||
var pinnedPosts = await dbEntities.Posts.Match(p => pinIds.Contains(p.ID)).Match(VisibilityPolicy.IsPublic).ExecuteAsync(token);
|
||||
return Json(await mapper.Statuses(pinIds.Select(id => pinnedPosts.FirstOrDefault(p => p.ID == id)).Where(p => p != default).ToList(), MyId, token));
|
||||
}
|
||||
|
||||
var query = local is { Kind: LocalActorKind.Group }
|
||||
? _dbEntities.Posts.Match(p => p.GroupId == local.Id).Match(VisibilityPolicy.IsShown)
|
||||
? dbEntities.Posts.Match(p => p.GroupId == local.Id).Match(VisibilityPolicy.IsShown)
|
||||
: local != default
|
||||
? _dbEntities.Posts.Match(p => p.GroupUserId == local.Id && !p.IsFederatedCopy).Match(VisibilityPolicy.IsShown)
|
||||
: _dbEntities.Posts.Match(p => p.ActorURI == remote.ActorURI && p.IsFederatedCopy).Match(VisibilityPolicy.IsShown);
|
||||
? dbEntities.Posts.Match(p => p.GroupUserId == local.Id && !p.IsFederatedCopy).Match(VisibilityPolicy.IsShown)
|
||||
: dbEntities.Posts.Match(p => p.ActorURI == remote.ActorURI && p.IsFederatedCopy).Match(VisibilityPolicy.IsShown);
|
||||
var viewerFollows = MyId != default && (local != default
|
||||
? await _dbEntities.Followings.Match(f => f.AvatarId == MyId && f.TargetAccountId == local.Id && f.State == FollowState.Accepted).ExecuteAnyAsync(token)
|
||||
: await _dbEntities.Followings.Match(f => f.AvatarId == MyId && f.TargetActorURI == remote.ActorURI && f.State == FollowState.Accepted).ExecuteAnyAsync(token));
|
||||
? await dbEntities.Followings.Match(f => f.AvatarId == MyId && f.TargetAccountId == local.Id && f.State == FollowState.Accepted).ExecuteAnyAsync(token)
|
||||
: await dbEntities.Followings.Match(f => f.AvatarId == MyId && f.TargetActorURI == remote.ActorURI && f.State == FollowState.Accepted).ExecuteAnyAsync(token));
|
||||
if (MyId != id)
|
||||
query.Match(viewerFollows
|
||||
? p => p.Visibility == PostVisibility.Public || p.Visibility == PostVisibility.Unlisted || p.Visibility == PostVisibility.FollowersOnly
|
||||
@@ -250,7 +235,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
query.Match(p => p.Tags.Contains(tag.ToLowerInvariant()));
|
||||
|
||||
var posts = await Page.From(Params, Limit()).Fetch(query, p => p.ID, token);
|
||||
var statuses = await _mapper.Statuses(posts, MyId, token);
|
||||
var statuses = await mapper.Statuses(posts, MyId, token);
|
||||
Link($"/api/v1/accounts/{id}/statuses", posts.LastOrDefault()?.ID, posts.FirstOrDefault()?.ID);
|
||||
return Json(statuses);
|
||||
}
|
||||
@@ -262,7 +247,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
if (local == default || local.Id != MyId)
|
||||
return Json(Array.Empty<Account>());
|
||||
var followers = await Page.From(Params, Limit(40, 80))
|
||||
.Fetch(_dbEntities.Followers.Match(f => f.LocalActorId == local.Id && f.LocalActorKind == local.Kind && f.IsAccepted), f => f.ID, token);
|
||||
.Fetch(dbEntities.Followers.Match(f => f.LocalActorId == local.Id && f.LocalActorKind == local.Kind && f.IsAccepted), f => f.ID, token);
|
||||
Link($"/api/v1/accounts/{id}/followers", followers.LastOrDefault()?.ID, followers.FirstOrDefault()?.ID);
|
||||
return Json(await AccountsFor(followers.Select(f => f.ActorURI), token));
|
||||
}
|
||||
@@ -277,9 +262,9 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
if (local == default || local.Id != MyId)
|
||||
return Json(Array.Empty<Account>());
|
||||
var following = await Page.From(Params, Limit(40, 80))
|
||||
.Fetch(_dbEntities.Followings.Match(f => f.AvatarId == local.Id && f.State == FollowState.Accepted), f => f.ID, token);
|
||||
.Fetch(dbEntities.Followings.Match(f => f.AvatarId == local.Id && f.State == FollowState.Accepted), f => f.ID, token);
|
||||
Link($"/api/v1/accounts/{id}/following", following.LastOrDefault()?.ID, following.FirstOrDefault()?.ID);
|
||||
var accounts = await _mapper.Accounts(following.Select(f => f.TargetAccountId), token);
|
||||
var accounts = await mapper.Accounts(following.Select(f => f.TargetAccountId), token);
|
||||
return Json(following.Select(f => accounts.GetValueOrDefault(f.TargetAccountId)).Where(a => a != default).ToList());
|
||||
}
|
||||
|
||||
@@ -288,7 +273,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
public async Task<IActionResult> Feeds(CancellationToken token)
|
||||
{
|
||||
var read = await DB.Default.Find<FeedSubscription>().Match(s => s.AvatarId == MyId).Sort(s => s.ID, MongoDB.Entities.Order.Descending).ExecuteAsync(token);
|
||||
var accounts = await _mapper.Accounts(read.Select(s => s.FeedAccountId), token);
|
||||
var accounts = await mapper.Accounts(read.Select(s => s.FeedAccountId), token);
|
||||
return Json(read.Select(s => accounts.GetValueOrDefault(s.FeedAccountId)).Where(a => a != default).ToList());
|
||||
}
|
||||
|
||||
@@ -302,13 +287,13 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
// a feed is read through the server, whose reader follows its communities; the persona follows nothing
|
||||
if (remote is { AvatarType: AvatarType.Feed })
|
||||
{
|
||||
await _feeds.Subscribe(Me, remote, true, token);
|
||||
await feedFollows.Subscribe(Me, remote, true, token);
|
||||
return Json(await Relationship(id, token));
|
||||
}
|
||||
// notify: a `status` notification for each of its new posts; languages: only its posts in those reach the home (R8).
|
||||
// Either changes only when given, as on Mastodon
|
||||
var following = await _follows.FollowAs(Me, local?.Uri ?? remote.ActorURI, Params.Bool("reblogs") != false, token, Params.Bool("notify"),
|
||||
Params.Has("languages") ? Params.List("languages") : default);
|
||||
var following = await followService.FollowAs(Me, local?.Uri ?? remote.ActorURI, Params.Bool("reblogs") != false, Params.Bool("notify"),
|
||||
Params.Has("languages") ? Params.List("languages") : default, token);
|
||||
return following == default ? Error(StatusCodes.Status403Forbidden, "This action is not allowed") : Json(await Relationship(id, token));
|
||||
}
|
||||
|
||||
@@ -319,9 +304,9 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
if (local == default && remote == default)
|
||||
return NotFoundError();
|
||||
if (remote is { AvatarType: AvatarType.Feed })
|
||||
await _feeds.Subscribe(Me, remote, false, token);
|
||||
await feedFollows.Subscribe(Me, remote, false, token);
|
||||
else
|
||||
await _follows.UnfollowAs(Me, local?.Uri ?? remote.ActorURI, token);
|
||||
await followService.UnfollowAs(Me, local?.Uri ?? remote.ActorURI, token);
|
||||
return Json(await Relationship(id, token));
|
||||
}
|
||||
|
||||
@@ -332,7 +317,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
var uri = local?.Uri ?? remote?.ActorURI;
|
||||
if (uri == default)
|
||||
return NotFoundError();
|
||||
await _relationships.RemoveFollower(Me, uri, id, token);
|
||||
await relationshipService.RemoveFollower(Me, uri, id, token);
|
||||
return Json(await Relationship(id, token));
|
||||
}
|
||||
|
||||
@@ -342,7 +327,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
var (local, remote) = await Find(id, token);
|
||||
if (local == default && remote == default || local?.Id == MyId)
|
||||
return NotFoundError();
|
||||
await _relationships.Block(Me, local?.Uri ?? remote.ActorURI, id, token);
|
||||
await relationshipService.Block(Me, local?.Uri ?? remote.ActorURI, id, token);
|
||||
return Json(await Relationship(id, token));
|
||||
}
|
||||
|
||||
@@ -352,7 +337,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
var (local, remote) = await Find(id, token);
|
||||
if (local == default && remote == default)
|
||||
return NotFoundError();
|
||||
await _relationships.Unblock(Me, local?.Uri ?? remote.ActorURI, token);
|
||||
await relationshipService.Unblock(Me, local?.Uri ?? remote.ActorURI, token);
|
||||
return Json(await Relationship(id, token));
|
||||
}
|
||||
|
||||
@@ -363,7 +348,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
if (local == default && remote == default || local?.Id == MyId)
|
||||
return NotFoundError();
|
||||
var duration = Params.Int("duration") is { } seconds and > 0 ? TimeSpan.FromSeconds(seconds) : (TimeSpan?)null;
|
||||
await _relationships.Mute(Me, local?.Uri ?? remote.ActorURI, id, Params.Bool("notifications") != false, duration, token);
|
||||
await relationshipService.Mute(Me, local?.Uri ?? remote.ActorURI, id, Params.Bool("notifications") != false, duration, token);
|
||||
return Json(await Relationship(id, token));
|
||||
}
|
||||
|
||||
@@ -373,7 +358,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
var (local, remote) = await Find(id, token);
|
||||
if (local == default && remote == default)
|
||||
return NotFoundError();
|
||||
await _relationships.Unmute(Me, local?.Uri ?? remote.ActorURI, token);
|
||||
await relationshipService.Unmute(Me, local?.Uri ?? remote.ActorURI, token);
|
||||
return Json(await Relationship(id, token));
|
||||
}
|
||||
|
||||
@@ -381,7 +366,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
public async Task<IActionResult> Blocks(CancellationToken token)
|
||||
{
|
||||
var blocks = await Page.From(Params, Limit(40, 80)).Fetch(DB.Default.Find<Block>().Match(b => b.AvatarId == MyId), b => b.ID, token);
|
||||
var accounts = await _mapper.Accounts(blocks.Select(b => b.TargetAccountId), token);
|
||||
var accounts = await mapper.Accounts(blocks.Select(b => b.TargetAccountId), token);
|
||||
Link("/api/v1/blocks", blocks.LastOrDefault()?.ID, blocks.FirstOrDefault()?.ID);
|
||||
return Json(blocks.Select(b => accounts.GetValueOrDefault(b.TargetAccountId)).Where(a => a != default).ToList());
|
||||
}
|
||||
@@ -390,7 +375,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
public async Task<IActionResult> Mutes(CancellationToken token)
|
||||
{
|
||||
var mutes = await Page.From(Params, Limit(40, 80)).Fetch(DB.Default.Find<Mute>().Match(m => m.AvatarId == MyId), m => m.ID, token);
|
||||
var accounts = await _mapper.Accounts(mutes.Select(m => m.TargetAccountId), token);
|
||||
var accounts = await mapper.Accounts(mutes.Select(m => m.TargetAccountId), token);
|
||||
Link("/api/v1/mutes", mutes.LastOrDefault()?.ID, mutes.FirstOrDefault()?.ID);
|
||||
return Json(mutes.Select(m => accounts.GetValueOrDefault(m.TargetAccountId)).Where(a => a != default).ToList());
|
||||
}
|
||||
@@ -410,9 +395,9 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
return Error(StatusCodes.Status400BadRequest, "Param is missing or the value is empty: domain");
|
||||
if (Uri.CheckHostName(domain) != UriHostNameType.Dns)
|
||||
return Error(StatusCodes.Status422UnprocessableEntity, "Validation failed: Domain is invalid");
|
||||
var followings = await _dbEntities.Followings.Match(f => f.AvatarId == MyId && !f.TargetIsLocal && f.State == FollowState.Accepted)
|
||||
var followings = await dbEntities.Followings.Match(f => f.AvatarId == MyId && !f.TargetIsLocal && f.State == FollowState.Accepted)
|
||||
.ExecuteAsync(token);
|
||||
var followers = await _dbEntities.Followers.Match(f => f.LocalActorId == MyId && f.IsAccepted).ExecuteAsync(token);
|
||||
var followers = await dbEntities.Followers.Match(f => f.LocalActorId == MyId && f.IsAccepted).ExecuteAsync(token);
|
||||
var handles = await Hidden.HandleDomains(followings.Select(f => f.TargetActorURI).Concat(followers.Select(f => f.ActorURI)).ToList(), token);
|
||||
return Json(new
|
||||
{
|
||||
@@ -424,32 +409,32 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
[HttpPost("/api/v1/domain_blocks"), Scope("write:blocks")]
|
||||
public async Task<IActionResult> BlockDomain(CancellationToken token)
|
||||
{
|
||||
await _relationships.BlockDomain(Me, Params.Get("domain"), token);
|
||||
await relationshipService.BlockDomain(Me, Params.Get("domain"), token);
|
||||
return Json(new { });
|
||||
}
|
||||
|
||||
[HttpDelete("/api/v1/domain_blocks"), Scope("write:blocks")]
|
||||
public async Task<IActionResult> UnblockDomain(CancellationToken token)
|
||||
{
|
||||
await _relationships.UnblockDomain(Me, Params.Get("domain"), token);
|
||||
await relationshipService.UnblockDomain(Me, Params.Get("domain"), token);
|
||||
return Json(new { });
|
||||
}
|
||||
|
||||
[HttpGet("/api/v1/follow_requests"), Scope("read:follows")]
|
||||
public async Task<IActionResult> FollowRequests(CancellationToken token)
|
||||
{
|
||||
var requests = await _dbEntities.Followers.Match(f => f.LocalActorId == Me.Id && f.LocalActorKind == LocalActorKind.Person && !f.IsAccepted)
|
||||
var requests = await dbEntities.Followers.Match(f => f.LocalActorId == Me.Id && f.LocalActorKind == LocalActorKind.Person && !f.IsAccepted)
|
||||
.Sort(f => f.ID, Order.Descending).Limit(Limit(40, 80)).ExecuteAsync(token);
|
||||
return Json(await AccountsFor(requests.Select(r => r.ActorURI), token));
|
||||
}
|
||||
|
||||
[HttpPost("/api/v1/follow_requests/{id}/authorize"), Scope("write:follows")]
|
||||
public async Task<IActionResult> Authorize(string id, CancellationToken token) =>
|
||||
await _follows.Decide(Me, id, accept: true, token) ? Json(await Relationship(id, token)) : NotFoundError();
|
||||
await followService.Decide(Me, id, accept: true, token) ? Json(await Relationship(id, token)) : NotFoundError();
|
||||
|
||||
[HttpPost("/api/v1/follow_requests/{id}/reject"), Scope("write:follows")]
|
||||
public async Task<IActionResult> Reject(string id, CancellationToken token) =>
|
||||
await _follows.Decide(Me, id, accept: false, token) ? Json(await Relationship(id, token)) : NotFoundError();
|
||||
await followService.Decide(Me, id, accept: false, token) ? Json(await Relationship(id, token)) : NotFoundError();
|
||||
|
||||
// Familiar followers stay empty by design: which of the accounts the persona follows also follow another account would
|
||||
// tell who follows whom among the personas here (and their followers lists are never shown, hide_collections), so
|
||||
@@ -507,12 +492,12 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
|
||||
async Task<IActionResult> EndorsementsOf(string path, CancellationToken token)
|
||||
{
|
||||
var followed = (await _dbEntities.Followings.Match(f => f.AvatarId == MyId && f.State == FollowState.Accepted && f.FollowerKind == null)
|
||||
var followed = (await dbEntities.Followings.Match(f => f.AvatarId == MyId && f.State == FollowState.Accepted && f.FollowerKind == null)
|
||||
.Project(f => f.Include(x => x.TargetActorURI)).ExecuteAsync(token)).Select(f => f.TargetActorURI).ToList();
|
||||
var page = await Page.From(Params, Limit(40, 80))
|
||||
.Fetch(DB.Default.Find<Endorsement>().Match(e => e.AvatarId == MyId && followed.Contains(e.TargetActorURI)), e => e.ID, token);
|
||||
Link(path, page.LastOrDefault()?.ID, page.FirstOrDefault()?.ID);
|
||||
var accounts = await _mapper.Accounts(page.Select(e => e.TargetAccountId), token);
|
||||
var accounts = await mapper.Accounts(page.Select(e => e.TargetAccountId), token);
|
||||
return Json(page.Select(e => accounts.GetValueOrDefault(e.TargetAccountId)).Where(a => a != default).ToList());
|
||||
}
|
||||
|
||||
@@ -554,7 +539,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
const int MaxEndorsements = 500;
|
||||
|
||||
async Task<bool> Follows(string uri, CancellationToken token) =>
|
||||
await _dbEntities.Followings.Match(f => f.AvatarId == MyId && f.TargetActorURI == uri && f.State == FollowState.Accepted && f.FollowerKind == null)
|
||||
await dbEntities.Followings.Match(f => f.AvatarId == MyId && f.TargetActorURI == uri && f.State == FollowState.Accepted && f.FollowerKind == null)
|
||||
.ExecuteAnyAsync(token);
|
||||
|
||||
async Task<List<Account>> AccountsFor(IEnumerable<string> actorUris, CancellationToken token)
|
||||
@@ -562,37 +547,37 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
var accounts = new List<Account>();
|
||||
foreach (var uri in actorUris)
|
||||
{
|
||||
var local = await _localActors.FindByUri(uri, token);
|
||||
var local = await localActors.FindByUri(uri, token);
|
||||
if (local != default)
|
||||
{
|
||||
accounts.Add(await _mapper.Local(local, false, token));
|
||||
accounts.Add(await mapper.Local(local, false, token));
|
||||
continue;
|
||||
}
|
||||
var foreign = await _dbEntities.ForeignAvatars.Match(f => f.ActorURI == uri).ExecuteFirstAsync(token);
|
||||
var foreign = await dbEntities.ForeignAvatars.Match(f => f.ActorURI == uri).ExecuteFirstAsync(token);
|
||||
if (foreign != default)
|
||||
accounts.Add(_mapper.Foreign(foreign));
|
||||
accounts.Add(mapper.Foreign(foreign));
|
||||
}
|
||||
return accounts;
|
||||
}
|
||||
|
||||
async Task<(LocalActor Local, ForeignAvatar Remote)> Find(string id, CancellationToken token)
|
||||
{
|
||||
var avatar = await _dbEntities.Avatars.MatchID(id).ExecuteFirstAsync(token);
|
||||
var avatar = await dbEntities.Avatars.MatchID(id).ExecuteFirstAsync(token);
|
||||
if (avatar is { DeletionAt: null })
|
||||
return (_localActors.FromAvatar(avatar), default);
|
||||
var group = await _dbEntities.Groups.MatchID(id).ExecuteFirstAsync(token);
|
||||
if (group is { DeletionAt: null } && _localActors.FromGroup(group) is { IsFederated: true, IsCircle: false } community)
|
||||
return (localActors.FromAvatar(avatar), default);
|
||||
var group = await dbEntities.Groups.MatchID(id).ExecuteFirstAsync(token);
|
||||
if (group is { DeletionAt: null } && localActors.FromGroup(group) is { IsFederated: true, IsCircle: false } community)
|
||||
return (community, default);
|
||||
return (default, await _dbEntities.ForeignAvatars.MatchID(id).ExecuteFirstAsync(token));
|
||||
return (default, await dbEntities.ForeignAvatars.MatchID(id).ExecuteFirstAsync(token));
|
||||
}
|
||||
|
||||
async Task<Relationship> Relationship(string id, CancellationToken token)
|
||||
{
|
||||
var (local, remote) = await Find(id, token);
|
||||
var uri = local?.Uri ?? remote?.ActorURI;
|
||||
var following = uri == default ? default : await _dbEntities.Followings.Match(f => f.AvatarId == MyId && f.TargetActorURI == uri).ExecuteFirstAsync(token);
|
||||
var reading = remote is { AvatarType: AvatarType.Feed } && await _feeds.IsSubscribed(MyId, remote.ActorURI, token);
|
||||
var followedBy = uri == default ? default : await _dbEntities.Followers.Match(f => f.LocalActorId == MyId && f.ActorURI == uri).ExecuteFirstAsync(token);
|
||||
var following = uri == default ? default : await dbEntities.Followings.Match(f => f.AvatarId == MyId && f.TargetActorURI == uri).ExecuteFirstAsync(token);
|
||||
var reading = remote is { AvatarType: AvatarType.Feed } && await feedFollows.IsSubscribed(MyId, remote.ActorURI, token);
|
||||
var followedBy = uri == default ? default : await dbEntities.Followers.Match(f => f.LocalActorId == MyId && f.ActorURI == uri).ExecuteFirstAsync(token);
|
||||
var blocking = uri != default && await DB.Default.Find<Block>().Match(b => b.AvatarId == MyId && b.TargetActorURI == uri).ExecuteAnyAsync(token);
|
||||
var mute = uri == default ? default : await DB.Default.Find<Mute>().Match(m => m.AvatarId == MyId && m.TargetActorURI == uri).ExecuteFirstAsync(token);
|
||||
var blockedBy = local != default
|
||||
|
||||
Reference in new issue
Block a user