diff --git a/CLAUDE.md b/CLAUDE.md index b3009c5..5e645b1 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -848,7 +848,7 @@ client's role route never does. - **Usernames are `Constants.UserNameRegex`** (`^[a-z0-9_]+$`): the intersection of what Mastodon and Misskey accept. A name outside it creates a persona nobody on those servers can reach. - **An actor's `published` is `PublishedOn`**, a random whole day up to two weeks before creation, so personas made the - same day do not share a date. New persona and group ids carry that same day (`GenerateNewID`), because local clients + same day do not share a date. New persona and group ids carry that same day (`IOwnId.NewId`), because local clients see ids. Never emit `CreatedAt`. - **Per-avatar state stays per avatar:** blocks, mutes, notifications, follows, and since R8 featured tags, endorsements, private notes (`PersonaNote`, never `RootUserNote`) and dismissed suggestions. Nothing may relate sibling @@ -880,11 +880,18 @@ client's role route never does. ## Data -- MongoDB through **MongoDB.Entities 25.1**, instance API: `DB.Default.Find()`, `.SaveAsync(e)`, `.Update()`, +- MongoDB through **MongoDB.Entities 26**, instance API: `DB.Default.Find()`, `.SaveAsync(e)`, `.Update()`, `.DeleteAsync()`, `.CountAsync()`. `DbEntities` wraps the finds. -- **Pass no cancellation token to `DeleteAsync` outside a transaction;** v25 throws if you do. +- **Pass no cancellation token to `DeleteAsync` outside a transaction;** the library throws if you do (v25 and v26). - **Collection name = class name, so never rename an entity class.** -- `Entity.ID` is a 24-character lowercase hex string; `GenerateNewID()` returns `object`, so cast it. +- **IDs are stored as they always were.** Since v26 the library no longer decides it, so `EntityIds.Register()` + (`Infrastructure/Data/EntityIds.cs`) maps `Entity.ID` the way v25 did: an ObjectId-shaped id as a BSON ObjectId, + any other string as a string, both read back, nested entities too. + - A type that makes its own id implements `IOwnId.NewId()` (`Avatar`, `Group`); every other type gets a 24-character + lowercase hex string. + - `GenerateNewID()` is now the library's extension method, still returning `object`, so cast it. + - Checked on 10 November 2026 against a scratch mongod: records written with v25 read, filter, update and delete + under v26, new writes keep the same BSON types, and v25 reads them back. - New fields must be additive: a deploy rollback restores the binary, not the database. - **Every stored remote object has an `ObjectRecord`** (raw JSON up to 256 KB plus its hash; delivered or fetched; the activity, inbox, key and signed headers; received time; up to 10 later revisions). Write it right after the post's @@ -945,7 +952,8 @@ client's role route never does. (`ActivityPubRenderer.Timestamp`). GoToSocial (ULIDs) and Mastodon (Snowflakes) derive a remote status's id from `published` at millisecond resolution. With whole seconds, two of our posts from the same second sorted at random there. -- **Startup order:** `EntityMaps.Warm()` (every entity's class map, one at a time; two mapped at once throw "An item +- **Startup order:** `EntityIds.Register()` (first: if anything built `Entity`'s class map before it, start-up throws + rather than store ids differently), then `EntityMaps.Warm()` (every entity's class map, one at a time; two mapped at once throw "An item with the same key has already been added" and stay broken), then `MigrateAsync` (`Infrastructure/Data/Migrations`, `_NNN_` order, each runs once), then `Indexes.Create()`. A new entity needs nothing; a new unique index needs a dedupe migration before it. diff --git a/Directory.Build.props b/Directory.Build.props index 66abc89..6a0cabc 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -13,7 +13,7 @@ - + diff --git a/PrivaPub/Domain/Media/MediaService.cs b/PrivaPub/Domain/Media/MediaService.cs index e3621ce..a5cef9a 100644 --- a/PrivaPub/Domain/Media/MediaService.cs +++ b/PrivaPub/Domain/Media/MediaService.cs @@ -601,7 +601,7 @@ namespace PrivaPub.Domain.Media await _processing.WaitAsync(token); try { - var probe = await FFProbe.AnalyseAsync(input, default, token, "-protocol_whitelist file"); + var probe = await FFProbe.AnalyseAsync(input, customArguments: "-protocol_whitelist file", cancellationToken: token); if (probe.Duration > TimeSpan.FromSeconds(options.MaxSeconds)) return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: The file is too long"); var video = attachment.ContentType.StartsWith("video/", StringComparison.Ordinal) ? probe.PrimaryVideoStream : default; @@ -614,7 +614,7 @@ namespace PrivaPub.Domain.Media if (video == default) arguments = "-map 0:a:0 -vn -dn -sn -map_metadata -1 -map_chapters -1 -c copy" + (extension == "m4a" ? " -movflags +faststart" : string.Empty); else if (video.CodecName is "h264" or "vp8" or "vp9" or "av1" && (long)video.Width * video.Height <= options.MaxVideoPixels - && video.FrameRate <= options.MaxFrameRate + 0.5) + && video.RealFrameRate <= options.MaxFrameRate + 0.5) arguments = "-map 0:v:0 -map 0:a:0? -dn -sn -map_metadata -1 -map_chapters -1 -c copy" + (extension == "mp4" ? " -movflags +faststart" : string.Empty); else { @@ -631,13 +631,15 @@ namespace PrivaPub.Domain.Media .CancellableThrough(token) .ProcessAsynchronously(); - var made = await FFProbe.AnalyseAsync(output, default, token, "-protocol_whitelist file"); + var made = await FFProbe.AnalyseAsync(output, customArguments: "-protocol_whitelist file", cancellationToken: token); byte[] poster = default; if (video != default) { attachment.Width = made.PrimaryVideoStream?.Width; attachment.Height = made.PrimaryVideoStream?.Height; - await FFMpeg.SnapshotAsync(output, frame, captureTime: TimeSpan.FromSeconds(Math.Min(1, made.Duration.TotalSeconds / 2)), cancellationToken: token); + await FFMpeg.Snapshot(made, frame, captureTime: TimeSpan.FromSeconds(Math.Min(1, made.Duration.TotalSeconds / 2))) + .CancellableThrough(token) + .ProcessAsynchronously(); var still = ProcessImage(await File.ReadAllBytesAsync(frame, token), 640, options.PreviewSide); poster = still.Preview; attachment.Blurhash = still.Blurhash; diff --git a/PrivaPub/Infrastructure/Data/EntityIds.cs b/PrivaPub/Infrastructure/Data/EntityIds.cs new file mode 100644 index 0000000..32541d1 --- /dev/null +++ b/PrivaPub/Infrastructure/Data/EntityIds.cs @@ -0,0 +1,49 @@ +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +using MongoDB.Entities; + +namespace PrivaPub.Infrastructure.Data +{ + public interface IOwnId + { + string NewId(); + } + + public static class EntityIds + { + static readonly Lock Gate = new(); + + public static void Register() + { + lock (Gate) + { + if (BsonClassMap.IsClassMapRegistered(typeof(Entity))) + { + if (BsonClassMap.LookupClassMap(typeof(Entity)).IdMemberMap?.IdGenerator is Generator) + return; + + throw new InvalidOperationException( + "The BSON map of Entity was built before EntityIds.Register(), so IDs would not be stored as they always were. Register them before anything touches a stored record."); + } + + BsonClassMap.RegisterClassMap(map => + { + map.AutoMap(); + map.SetIgnoreExtraElements(true); + var id = map.MapIdProperty(entity => entity.ID); + ((IBsonMemberMapAttribute)new AsObjectIdAttribute()).Apply(id); + id.SetIdGenerator(new Generator()); + }); + } + } + + sealed class Generator : IIdGenerator + { + public object GenerateId(object container, object document) + => document is IOwnId own ? own.NewId() : ObjectId.GenerateNewId().ToString(); + + public bool IsEmpty(object id) + => string.IsNullOrEmpty(id as string); + } + } +} diff --git a/PrivaPub/Models/Group/Group.cs b/PrivaPub/Models/Group/Group.cs index 472ce76..0385c44 100644 --- a/PrivaPub/Models/Group/Group.cs +++ b/PrivaPub/Models/Group/Group.cs @@ -1,10 +1,11 @@ using MongoDB.Entities; +using PrivaPub.Infrastructure.Data; using PrivaPub.Infrastructure.Ids; namespace PrivaPub.Models.Group { - public class Group : Entity + public class Group : Entity, IOwnId { public string UserName { get; set; }//preferredUsername public string Name { get; set; }//name @@ -35,7 +36,7 @@ namespace PrivaPub.Models.Group public DateTime UpdatedAt { get; set; } = DateTime.UtcNow; public DateTime? DeletionAt { get; set; } - public override object GenerateNewID() => PrivacyIds.ForDay(PublishedOn); + public string NewId() => PrivacyIds.ForDay(PublishedOn); } public class GroupMember diff --git a/PrivaPub/Models/User/Avatar.cs b/PrivaPub/Models/User/Avatar.cs index 0316337..d5090b6 100644 --- a/PrivaPub/Models/User/Avatar.cs +++ b/PrivaPub/Models/User/Avatar.cs @@ -1,10 +1,11 @@ using MongoDB.Entities; +using PrivaPub.Infrastructure.Data; using PrivaPub.Infrastructure.Ids; namespace PrivaPub.Models.User { - public class Avatar : Entity + public class Avatar : Entity, IOwnId { public string Url { get; set; }//url public string Name { get; set; }//name @@ -40,7 +41,7 @@ namespace PrivaPub.Models.User public DateTime? BannedAt { get; set; } public DateTime? DeletionAt { get; set; } - public override object GenerateNewID() => PrivacyIds.ForDay(PublishedOn); + public string NewId() => PrivacyIds.ForDay(PublishedOn); } public class ForeignAvatar : Entity diff --git a/PrivaPub/PrivaPub.csproj b/PrivaPub/PrivaPub.csproj index 34cc923..70eed7b 100644 --- a/PrivaPub/PrivaPub.csproj +++ b/PrivaPub/PrivaPub.csproj @@ -8,22 +8,22 @@ - - - - + + + + - - + + - - + + - + - - + + diff --git a/PrivaPub/Program.cs b/PrivaPub/Program.cs index 5868fea..b2a93ff 100644 --- a/PrivaPub/Program.cs +++ b/PrivaPub/Program.cs @@ -90,6 +90,7 @@ try try { BsonSerializer.TryRegisterSerializer(new GuidSerializer(GuidRepresentation.Standard)); + EntityIds.Register(); var mongoSettings = builder.Configuration.GetSection(nameof(MongoSettings)).Get(); await DB.InitAsync(mongoSettings.Database, MongoClientSettings.FromConnectionString(mongoSettings.ConnectionString)); EntityMaps.Warm(); diff --git a/global.json b/global.json index d46d21e..735738d 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "10.0.100", + "version": "10.0.403", "rollForward": "latestFeature", "allowPrerelease": false }