🐛 Fix some issues in activitypub

This commit is contained in:
2026-01-01 01:45:35 +08:00
parent a795ff6db8
commit 247296476c
2 changed files with 48 additions and 16 deletions

View File

@@ -24,7 +24,9 @@ public class ActivityPubActivityHandler(
private async Task<SnPost?> 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<bool> HandleDeleteAsync(string actorUri, Dictionary<string, object> activity)
private async Task<bool> HandleDeleteAsync(Dictionary<string, object> 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<bool> HandleUpdateAsync(string actorUri, Dictionary<string, object> 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<Shared.Models.ContentMention>();
content.Attachments = ParseAttachments(objectDict.GetValueOrDefault("attachment")) ??
new List<Shared.Models.SnCloudFileReferenceObject>();
content.Metadata = BuildMetadataFromActivity(objectDict);
await db.SaveChangesAsync();
logger.LogInformation("Handled federated Update: {Uri}", objectUri);
return true;
}

View File

@@ -178,6 +178,7 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ATotp_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F48c9d2a1b3c84b32b36ebc6f20a927ea4600_003F7b_003Ff98e5727_003FTotp_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ATusDiskStore_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F8bb08a178b5b43c5bac20a5a54159a5b2a800_003Fe1_003Fefd9af34_003FTusDiskStore_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ATusDiskStore_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F8bb08a178b5b43c5bac20a5a54159a5b2a800_003F1c_003F21999acd_003FTusDiskStore_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AUriKind_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F7533cc3c5d824cc39f85ddc4e6a2be9241b10_003F08_003Ff9a88d70_003FUriKind_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AUri_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F5d2c480da9be415dab9be535bb6d08713cc00_003Fd0_003Fffc36a51_003FUri_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AValidationContext_00601_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F8bb08a178b5b43c5bac20a5a54159a5b2a800_003F6b_003F741ceebe_003FValidationContext_00601_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AWebSocketAcceptContext_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2025_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F16e10e91b7834a87b2f3f4f30bfada3ee000_003Fd0_003F44ef97dc_003FWebSocketAcceptContext_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>