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
75 lines
1.8 KiB
C#
75 lines
1.8 KiB
C#
using MongoDB.Entities;
|
|
|
|
using PrivaPub.Models.Federation;
|
|
|
|
namespace PrivaPub.Federation.Moderation
|
|
{
|
|
public interface IDomainBlocks
|
|
{
|
|
DomainBlock Find(string host);
|
|
bool IsSuspended(string host);
|
|
Task Reload(CancellationToken token);
|
|
}
|
|
|
|
public class DomainBlocks(
|
|
ILogger<DomainBlocks> logger) : IDomainBlocks
|
|
{
|
|
static readonly TimeSpan Staleness = TimeSpan.FromMinutes(5);
|
|
|
|
IReadOnlyDictionary<string, DomainBlock> _blocks = new Dictionary<string, DomainBlock>();
|
|
DateTime _loadedAt = DateTime.MinValue;
|
|
int _reloading;
|
|
|
|
public static string Normalise(string domain) => domain?.Trim().Trim('.').ToLowerInvariant();
|
|
|
|
public DomainBlock Find(string host)
|
|
{
|
|
RefreshIfStale();
|
|
host = Normalise(host);
|
|
var blocks = _blocks;
|
|
while (!string.IsNullOrEmpty(host))
|
|
{
|
|
if (blocks.TryGetValue(host, out var block))
|
|
return block;
|
|
var dot = host.IndexOf('.');
|
|
host = dot < 0 ? default : host[(dot + 1)..];
|
|
}
|
|
return default;
|
|
}
|
|
|
|
public bool IsSuspended(string host) => Find(host)?.Severity == DomainBlockSeverity.Suspend;
|
|
|
|
public async Task Reload(CancellationToken token) =>
|
|
Load(await DB.Default.Find<DomainBlock>().ExecuteAsync(token));
|
|
|
|
public void Load(IEnumerable<DomainBlock> blocks)
|
|
{
|
|
_blocks = blocks.Where(b => !string.IsNullOrEmpty(b.Domain))
|
|
.GroupBy(b => Normalise(b.Domain))
|
|
.ToDictionary(g => g.Key, g => g.First());
|
|
_loadedAt = DateTime.UtcNow;
|
|
}
|
|
|
|
void RefreshIfStale()
|
|
{
|
|
if (DateTime.UtcNow - _loadedAt < Staleness || Interlocked.Exchange(ref _reloading, 1) == 1)
|
|
return;
|
|
_ = Task.Run(async () =>
|
|
{
|
|
try
|
|
{
|
|
await Reload(CancellationToken.None);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogWarning(ex, "Domain blocks could not be reloaded");
|
|
}
|
|
finally
|
|
{
|
|
Interlocked.Exchange(ref _reloading, 0);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|