From 247296476c2955ede481b02bb5fb155d93007052 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 1 Jan 2026 01:45:35 +0800 Subject: [PATCH] :bug: Fix some issues in activitypub --- .../ActivityPub/ActivityPubActivityHandler.cs | 63 ++++++++++++++----- DysonNetwork.sln.DotSettings.user | 1 + 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/DysonNetwork.Sphere/ActivityPub/ActivityPubActivityHandler.cs b/DysonNetwork.Sphere/ActivityPub/ActivityPubActivityHandler.cs index 2a5ac7d..41dfa9d 100644 --- a/DysonNetwork.Sphere/ActivityPub/ActivityPubActivityHandler.cs +++ b/DysonNetwork.Sphere/ActivityPub/ActivityPubActivityHandler.cs @@ -24,7 +24,9 @@ public class ActivityPubActivityHandler( private async Task GetPostByUriAsync(string objectUri) { - var uri = new Uri(objectUri); + if (!Uri.TryCreate(objectUri, UriKind.Absolute, out var uri)) + return await db.Posts.FirstOrDefaultAsync(c => c.FediverseUri == objectUri); + var domain = uri.Host; // Remote post @@ -91,7 +93,7 @@ public class ActivityPubActivityHandler( case "Announce": return await HandleAnnounceAsync(actorUri, activity); case "Delete": - return await HandleDeleteAsync(actorUri, activity); + return await HandleDeleteAsync(activity); case "Update": return await HandleUpdateAsync(actorUri, activity); default: @@ -381,7 +383,6 @@ public class ActivityPubActivityHandler( if (string.IsNullOrEmpty(objectUri)) return false; - var actor = await GetOrCreateActorAsync(actorUri); var content = await GetPostByUriAsync(objectUri); if (content != null) @@ -394,38 +395,68 @@ public class ActivityPubActivityHandler( return true; } - private async Task HandleDeleteAsync(string actorUri, Dictionary activity) + private async Task HandleDeleteAsync(Dictionary activity) { var objectUri = activity.GetValueOrDefault("object")?.ToString(); if (string.IsNullOrEmpty(objectUri)) return false; var content = await GetPostByUriAsync(objectUri); - if (content == null) return true; - content.DeletedAt = SystemClock.Instance.GetCurrentInstant(); - await db.SaveChangesAsync(); - logger.LogInformation("Deleted federated content: {Uri}", objectUri); + db.Remove(content); + await db.SaveChangesAsync(); + + logger.LogInformation("Handled federated Delete (tombstoned): {Uri}", objectUri); return true; } private async Task HandleUpdateAsync(string actorUri, Dictionary activity) { - var objectUri = activity.GetValueOrDefault("object")?.ToString(); - if (string.IsNullOrEmpty(objectUri)) + var objectValue = activity.GetValueOrDefault("object"); + var objectDict = ConvertToDictionary(objectValue); + + if (objectDict == null) return false; - var content = await GetPostByUriAsync(objectUri); + var objectUri = GetStringValue(objectDict, "id"); + if (string.IsNullOrEmpty(objectUri)) + return false; + + var actor = await GetOrCreateActorAsync(actorUri); - if (content != null) + var content = await GetPostByUriAsync(objectUri); + if (content == null) { - content.EditedAt = SystemClock.Instance.GetCurrentInstant(); - content.UpdatedAt = SystemClock.Instance.GetCurrentInstant(); - await db.SaveChangesAsync(); - logger.LogInformation("Updated federated content: {Uri}", objectUri); + content = new SnPost + { + FediverseUri = objectUri, + FediverseType = FediverseContentType.FediverseNote, + Type = PostType.Moment, + Visibility = PostVisibility.Public, + ActorId = actor.Id + }; + db.Posts.Add(content); } + content.Title = GetStringValue(objectDict, "name"); + content.Description = GetStringValue(objectDict, "summary"); + content.Content = GetStringValue(objectDict, "content"); + content.ContentType = PostContentType.Html; + + content.PublishedAt = ParseInstant(objectDict.GetValueOrDefault("published")) ?? content.PublishedAt; + content.EditedAt = ParseInstant(objectDict.GetValueOrDefault("updated")) ?? + SystemClock.Instance.GetCurrentInstant(); + + content.Mentions = ParseMentions(objectDict.GetValueOrDefault("tag")) ?? + new List(); + content.Attachments = ParseAttachments(objectDict.GetValueOrDefault("attachment")) ?? + new List(); + content.Metadata = BuildMetadataFromActivity(objectDict); + + await db.SaveChangesAsync(); + + logger.LogInformation("Handled federated Update: {Uri}", objectUri); return true; } diff --git a/DysonNetwork.sln.DotSettings.user b/DysonNetwork.sln.DotSettings.user index e609556..1f553ef 100644 --- a/DysonNetwork.sln.DotSettings.user +++ b/DysonNetwork.sln.DotSettings.user @@ -178,6 +178,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded