🔊 Proper error handling in activitypub inbox

This commit is contained in:
2025-12-31 22:42:57 +08:00
parent 4815d31b31
commit add9fa49e5

View File

@@ -71,6 +71,8 @@ public class ActivityPubActivityHandler(
logger.LogInformation("Signature verified successfully. Handling {Type} from {ActorUri}", logger.LogInformation("Signature verified successfully. Handling {Type} from {ActorUri}",
activityType, actorUri); activityType, actorUri);
try
{
switch (activityType) switch (activityType)
{ {
case "Follow": case "Follow":
@@ -97,6 +99,13 @@ public class ActivityPubActivityHandler(
return false; return false;
} }
} }
catch (InvalidOperationException err)
{
logger.LogError("Failed to handle activity: {Type}, due to {Message}. Full activity: {Activity}",
activityType, err.Message, JsonSerializer.Serialize(activity));
return false;
}
}
private async Task<bool> HandleFollowAsync(string actorUri, Dictionary<string, object> activity) private async Task<bool> HandleFollowAsync(string actorUri, Dictionary<string, object> activity)
{ {
@@ -252,8 +261,6 @@ public class ActivityPubActivityHandler(
private async Task<bool> HandleUndoAsync(string actorUri, Dictionary<string, object> activity) private async Task<bool> HandleUndoAsync(string actorUri, Dictionary<string, object> activity)
{ {
var objectValue = activity.GetValueOrDefault("object"); var objectValue = activity.GetValueOrDefault("object");
if (objectValue == null)
return false;
if (objectValue is not Dictionary<string, object> objectDict) return false; if (objectValue is not Dictionary<string, object> objectDict) return false;
var objectType = objectDict.GetValueOrDefault("type")?.ToString(); var objectType = objectDict.GetValueOrDefault("type")?.ToString();
@@ -262,7 +269,7 @@ public class ActivityPubActivityHandler(
"Follow" => await UndoFollowAsync(actorUri, objectDict.GetValueOrDefault("object")?.ToString()), "Follow" => await UndoFollowAsync(actorUri, objectDict.GetValueOrDefault("object")?.ToString()),
"Like" => await UndoLikeAsync(actorUri, objectDict.GetValueOrDefault("object")?.ToString()), "Like" => await UndoLikeAsync(actorUri, objectDict.GetValueOrDefault("object")?.ToString()),
"Announce" => await UndoAnnounceAsync(actorUri, objectDict.GetValueOrDefault("object")?.ToString()), "Announce" => await UndoAnnounceAsync(actorUri, objectDict.GetValueOrDefault("object")?.ToString()),
_ => false _ => throw new InvalidOperationException($"Unhandled undo operation for {objectType}")
}; };
} }