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
@@ -15,26 +15,14 @@ using PostEntity = PrivaPub.Models.Post.Post;
|
||||
|
||||
namespace PrivaPub.Api.Mastodon.Controllers
|
||||
{
|
||||
public partial class SearchController : MastodonController
|
||||
public partial class SearchController(
|
||||
MastodonMapper mapper,
|
||||
DbEntities dbEntities,
|
||||
ILocalActorService localActors,
|
||||
IRemoteActorService remoteActors,
|
||||
IRemotePosts remotePosts,
|
||||
AccountSearch accountSearch) : MastodonController
|
||||
{
|
||||
readonly MastodonMapper _mapper;
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly IRemoteActorService _remoteActors;
|
||||
readonly IRemotePosts _remotePosts;
|
||||
readonly AccountSearch _search;
|
||||
|
||||
public SearchController(MastodonMapper mapper, DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors,
|
||||
IRemotePosts remotePosts, AccountSearch search)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_dbEntities = dbEntities;
|
||||
_localActors = localActors;
|
||||
_remoteActors = remoteActors;
|
||||
_remotePosts = remotePosts;
|
||||
_search = search;
|
||||
}
|
||||
|
||||
// Anyone may search, as on Mastodon; only resolving (which fetches from other servers) and paging need a sign-in.
|
||||
[HttpGet("/api/v2/search"), Scope("read:search", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||
public async Task<IActionResult> Search(CancellationToken token)
|
||||
@@ -51,33 +39,33 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
|
||||
if (q.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var post = await _dbEntities.Posts.Match(p => (p.ObjectURI == q || p.Url == q) && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||
var post = await dbEntities.Posts.Match(p => (p.ObjectURI == q || p.Url == q) && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||
if (post == default && resolve && type is null or "statuses")
|
||||
post = await _remotePosts.StoreContext(q, 0, token);
|
||||
post = await remotePosts.StoreContext(q, 0, token);
|
||||
if (post != default && type is null or "statuses" && await VisibilityPolicy.CanSee(post, MyId, token))
|
||||
if (await _mapper.Status(post, MyId, token) is { } status)
|
||||
if (await mapper.Status(post, MyId, token) is { } status)
|
||||
results.Statuses.Add(status);
|
||||
if (results.Statuses.Count == 0 && type is null or "accounts")
|
||||
{
|
||||
var local = await _localActors.FindByUri(q, token);
|
||||
var foreign = local == default ? (resolve ? await _remoteActors.GetActor(q, refresh: false, token) : default) : default;
|
||||
var local = await localActors.FindByUri(q, token);
|
||||
var foreign = local == default ? (resolve ? await remoteActors.GetActor(q, refresh: false, token) : default) : default;
|
||||
if (local is { IsFederated: true, IsCircle: false })
|
||||
results.Accounts.Add(await _mapper.Local(local, false, token));
|
||||
results.Accounts.Add(await mapper.Local(local, false, token));
|
||||
else if (foreign != default)
|
||||
results.Accounts.Add(_mapper.Foreign(foreign));
|
||||
results.Accounts.Add(mapper.Foreign(foreign));
|
||||
}
|
||||
return Json(results);
|
||||
}
|
||||
|
||||
if (type is null or "accounts")
|
||||
{
|
||||
results.Accounts = await _search.Find(q, resolve, Limit(), Math.Max(0, offset), token);
|
||||
results.Accounts = await accountSearch.Find(q, resolve, Limit(), Math.Max(0, offset), token);
|
||||
}
|
||||
// an account's handle asks for the account, never for posts that happen to share its words
|
||||
if (type is null or "statuses" && MyId != default && !q.StartsWith('#') && !Handle().IsMatch(q))
|
||||
results.Statuses = await Words(q, Math.Max(0, offset), Limit(), token);
|
||||
if (type is null or "hashtags" && TagsController.Normalise(q) is var tag && TagsController.IsHashtag(tag))
|
||||
results.Hashtags.Add(new { name = tag, url = $"{_localActors.BaseAddress}/tags/{Uri.EscapeDataString(tag)}", history = await TagsController.History(_dbEntities, tag, token) });
|
||||
results.Hashtags.Add(new { name = tag, url = $"{localActors.BaseAddress}/tags/{Uri.EscapeDataString(tag)}", history = await TagsController.History(dbEntities, tag, token) });
|
||||
return Json(results);
|
||||
}
|
||||
}
|
||||
@@ -93,27 +81,27 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
// named in, and public posts of authors who let themselves be indexed; newest first, what the persona may see only
|
||||
async Task<List<Status>> Words(string q, int offset, int limit, CancellationToken token)
|
||||
{
|
||||
var candidates = await _dbEntities.Posts.Match(f => f.Text(q)).Match(p => p.ReblogOfPostId == null).Match(VisibilityPolicy.IsShown)
|
||||
var candidates = await dbEntities.Posts.Match(f => f.Text(q)).Match(p => p.ReblogOfPostId == null).Match(VisibilityPolicy.IsShown)
|
||||
.Sort(p => p.ID, MongoDB.Entities.Order.Descending).Limit(WordCandidates).ExecuteAsync(token);
|
||||
if (candidates.Count == 0)
|
||||
return new List<Status>();
|
||||
var ids = candidates.Select(p => p.ID).ToList();
|
||||
var mine = new HashSet<string>();
|
||||
mine.UnionWith((await _dbEntities.Favourites.Match(f => f.AccountId == MyId && ids.Contains(f.PostId)).ExecuteAsync(token)).Select(f => f.PostId));
|
||||
mine.UnionWith((await dbEntities.Favourites.Match(f => f.AccountId == MyId && ids.Contains(f.PostId)).ExecuteAsync(token)).Select(f => f.PostId));
|
||||
mine.UnionWith((await MongoDB.Entities.DB.Default.Find<Models.Social.Bookmark>().Match(b => b.AvatarId == MyId && ids.Contains(b.PostId)).ExecuteAsync(token))
|
||||
.Select(b => b.PostId));
|
||||
mine.UnionWith((await _dbEntities.Posts.Match(p => p.AuthorAccountId == MyId && ids.Contains(p.ReblogOfPostId) && !p.DeletedAt.HasValue).ExecuteAsync(token))
|
||||
mine.UnionWith((await dbEntities.Posts.Match(p => p.AuthorAccountId == MyId && ids.Contains(p.ReblogOfPostId) && !p.DeletedAt.HasValue).ExecuteAsync(token))
|
||||
.Select(p => p.ReblogOfPostId));
|
||||
var authors = candidates.Select(p => p.AuthorAccountId ?? p.GroupUserId).Where(a => a != default).Distinct().ToList();
|
||||
var indexable = (await _dbEntities.Avatars.Match(a => authors.Contains(a.ID) && a.Settings.IsIndexable).ExecuteAsync(token)).Select(a => a.ID).ToHashSet();
|
||||
var indexable = (await dbEntities.Avatars.Match(a => authors.Contains(a.ID) && a.Settings.IsIndexable).ExecuteAsync(token)).Select(a => a.ID).ToHashSet();
|
||||
// a remote author's searchableBy (FEP-268d) outranks its indexable, and a post's own outranks both
|
||||
var remoteAuthors = (await _dbEntities.ForeignAvatars.Match(f => authors.Contains(f.ID)).ExecuteAsync(token)).ToDictionary(f => f.ID);
|
||||
var remoteAuthors = (await dbEntities.ForeignAvatars.Match(f => authors.Contains(f.ID)).ExecuteAsync(token)).ToDictionary(f => f.ID);
|
||||
var stating = remoteAuthors.Values.Where(f => f.SearchableBy != default).Select(f => f.ActorURI)
|
||||
.Concat(candidates.Where(p => p.SearchableBy != default).Select(p => p.ActorURI))
|
||||
.Where(uri => uri != default).Distinct().ToList();
|
||||
var followed = stating.Count == 0
|
||||
? new HashSet<string>()
|
||||
: (await _dbEntities.Followings.Match(f => f.AvatarId == MyId && stating.Contains(f.TargetActorURI) && f.State == Models.Social.FollowState.Accepted)
|
||||
: (await dbEntities.Followings.Match(f => f.AvatarId == MyId && stating.Contains(f.TargetActorURI) && f.State == Models.Social.FollowState.Accepted)
|
||||
.ExecuteAsync(token)).Select(f => f.TargetActorURI).ToHashSet();
|
||||
bool Searchable(PostEntity post, string author)
|
||||
{
|
||||
@@ -134,7 +122,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
if (found.Count >= offset + limit)
|
||||
break;
|
||||
}
|
||||
return await _mapper.Statuses(found.Skip(offset).Take(limit).ToList(), MyId, token);
|
||||
return await mapper.Statuses(found.Skip(offset).Take(limit).ToList(), MyId, token);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user