🚚 Rename activitypub processor to handler

This commit is contained in:
2025-12-31 18:33:49 +08:00
parent 3da6de1feb
commit 413ae80c96
3 changed files with 54 additions and 56 deletions

View File

@@ -11,15 +11,15 @@ using PostVisibility = DysonNetwork.Shared.Models.PostVisibility;
namespace DysonNetwork.Sphere.ActivityPub; namespace DysonNetwork.Sphere.ActivityPub;
public class ActivityPubActivityProcessor( public class ActivityPubActivityHandler(
AppDatabase db, AppDatabase db,
ActivityPubSignatureService signatureService, ActivityPubSignatureService signatureService,
ActivityPubDeliveryService deliveryService, ActivityPubDeliveryService deliveryService,
ActivityPubDiscoveryService discoveryService, ActivityPubDiscoveryService discoveryService,
ILogger<ActivityPubActivityProcessor> logger ILogger<ActivityPubActivityHandler> logger
) )
{ {
public async Task<bool> ProcessIncomingActivityAsync( public async Task<bool> HandleIncomingActivityAsync(
HttpContext context, HttpContext context,
string username, string username,
Dictionary<string, object> activity Dictionary<string, object> activity
@@ -45,29 +45,29 @@ public class ActivityPubActivityProcessor(
if (string.IsNullOrEmpty(actorUri)) if (string.IsNullOrEmpty(actorUri))
return false; return false;
logger.LogInformation("Signature verified successfully. Processing {Type} from {ActorUri}", logger.LogInformation("Signature verified successfully. Handling {Type} from {ActorUri}",
activityType, actorUri); activityType, actorUri);
switch (activityType) switch (activityType)
{ {
case "Follow": case "Follow":
return await ProcessFollowAsync(actorUri, activity); return await HandleFollowAsync(actorUri, activity);
case "Accept": case "Accept":
return await ProcessAcceptAsync(actorUri, activity); return await HandleAcceptAsync(actorUri, activity);
case "Reject": case "Reject":
return await ProcessRejectAsync(actorUri, activity); return await HandleRejectAsync(actorUri, activity);
case "Undo": case "Undo":
return await ProcessUndoAsync(actorUri, activity); return await HandleUndoAsync(actorUri, activity);
case "Create": case "Create":
return await ProcessCreateAsync(actorUri, activity); return await HandleCreateAsync(actorUri, activity);
case "Like": case "Like":
return await ProcessLikeAsync(actorUri, activity); return await HandleLikeAsync(actorUri, activity);
case "Announce": case "Announce":
return await ProcessAnnounceAsync(actorUri, activity); return await HandleAnnounceAsync(actorUri, activity);
case "Delete": case "Delete":
return await ProcessDeleteAsync(actorUri, activity); return await HandleDeleteAsync(actorUri, activity);
case "Update": case "Update":
return await ProcessUpdateAsync(actorUri, activity); return await HandleUpdateAsync(actorUri, activity);
default: default:
logger.LogWarning("Unsupported activity type: {Type}. Full activity: {Activity}", logger.LogWarning("Unsupported activity type: {Type}. Full activity: {Activity}",
activityType, JsonSerializer.Serialize(activity)); activityType, JsonSerializer.Serialize(activity));
@@ -75,12 +75,12 @@ public class ActivityPubActivityProcessor(
} }
} }
private async Task<bool> ProcessFollowAsync(string actorUri, Dictionary<string, object> activity) private async Task<bool> HandleFollowAsync(string actorUri, Dictionary<string, object> activity)
{ {
var objectUri = activity.GetValueOrDefault("object")?.ToString(); var objectUri = activity.GetValueOrDefault("object")?.ToString();
var activityId = activity.GetValueOrDefault("id")?.ToString(); var activityId = activity.GetValueOrDefault("id")?.ToString();
logger.LogInformation("Processing Follow. Actor: {ActorUri}, Target: {ObjectUri}, ActivityId: {Id}", logger.LogInformation("Handling Follow. Actor: {ActorUri}, Target: {ObjectUri}, ActivityId: {Id}",
actorUri, objectUri, activityId); actorUri, objectUri, activityId);
if (string.IsNullOrEmpty(objectUri)) if (string.IsNullOrEmpty(objectUri))
@@ -116,15 +116,13 @@ public class ActivityPubActivityProcessor(
r.ActorId == actor.Id && r.ActorId == actor.Id &&
r.TargetActorId == localActor.Id); r.TargetActorId == localActor.Id);
if (existingRelationship is { State: RelationshipState.Accepted }) switch (existingRelationship)
{ {
case { State: RelationshipState.Accepted }:
logger.LogInformation("Follow relationship already exists and is accepted. ActorId: {ActorId}, PublisherId: {PublisherId}", logger.LogInformation("Follow relationship already exists and is accepted. ActorId: {ActorId}, PublisherId: {PublisherId}",
actor.Id, targetPublisher.Id); actor.Id, targetPublisher.Id);
return true; return true;
} case null:
if (existingRelationship == null)
{
existingRelationship = new SnFediverseRelationship existingRelationship = new SnFediverseRelationship
{ {
ActorId = actor.Id, ActorId = actor.Id,
@@ -135,13 +133,13 @@ public class ActivityPubActivityProcessor(
db.FediverseRelationships.Add(existingRelationship); db.FediverseRelationships.Add(existingRelationship);
logger.LogInformation("Created new follow relationship. ActorId: {ActorId}, TargetActorId: {TargetActorId}", logger.LogInformation("Created new follow relationship. ActorId: {ActorId}, TargetActorId: {TargetActorId}",
actor.Id, localActor.Id); actor.Id, localActor.Id);
} break;
else default:
{
existingRelationship.State = RelationshipState.Accepted; existingRelationship.State = RelationshipState.Accepted;
existingRelationship.FollowedBackAt = SystemClock.Instance.GetCurrentInstant(); existingRelationship.FollowedBackAt = SystemClock.Instance.GetCurrentInstant();
logger.LogInformation("Updating existing relationship. CurrentState: {State}, NewState: Accepted", logger.LogInformation("Updating existing relationship. CurrentState: {State}, NewState: Accepted",
existingRelationship.State); existingRelationship.State);
break;
} }
await db.SaveChangesAsync(); await db.SaveChangesAsync();
@@ -152,12 +150,12 @@ public class ActivityPubActivityProcessor(
activityId ?? "" activityId ?? ""
); );
logger.LogInformation("Processed follow from {Actor} to {Target}. RelationshipState: Accepted", logger.LogInformation("Handleed follow from {Actor} to {Target}. RelationshipState: Accepted",
actorUri, objectUri); actorUri, objectUri);
return true; return true;
} }
private async Task<bool> ProcessAcceptAsync(string actorUri, Dictionary<string, object> activity) private async Task<bool> HandleAcceptAsync(string actorUri, Dictionary<string, object> activity)
{ {
var objectUri = activity.GetValueOrDefault("object")?.ToString(); var objectUri = activity.GetValueOrDefault("object")?.ToString();
if (string.IsNullOrEmpty(objectUri)) if (string.IsNullOrEmpty(objectUri))
@@ -197,11 +195,11 @@ public class ActivityPubActivityProcessor(
await db.SaveChangesAsync(); await db.SaveChangesAsync();
logger.LogInformation("Processed accept from {Actor}", actorUri); logger.LogInformation("Handleed accept from {Actor}", actorUri);
return true; return true;
} }
private async Task<bool> ProcessRejectAsync(string actorUri, Dictionary<string, object> activity) private async Task<bool> HandleRejectAsync(string actorUri, Dictionary<string, object> activity)
{ {
var objectUri = activity.GetValueOrDefault("object")?.ToString(); var objectUri = activity.GetValueOrDefault("object")?.ToString();
if (string.IsNullOrEmpty(objectUri)) if (string.IsNullOrEmpty(objectUri))
@@ -224,11 +222,11 @@ public class ActivityPubActivityProcessor(
await db.SaveChangesAsync(); await db.SaveChangesAsync();
logger.LogInformation("Processed reject from {Actor}", actorUri); logger.LogInformation("Handleed reject from {Actor}", actorUri);
return true; return true;
} }
private async Task<bool> ProcessUndoAsync(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) if (objectValue == null)
@@ -245,7 +243,7 @@ public class ActivityPubActivityProcessor(
}; };
} }
private async Task<bool> ProcessCreateAsync(string actorUri, Dictionary<string, object> activity) private async Task<bool> HandleCreateAsync(string actorUri, Dictionary<string, object> activity)
{ {
var objectValue = activity.GetValueOrDefault("object"); var objectValue = activity.GetValueOrDefault("object");
if (objectValue is not Dictionary<string, object> objectDict) if (objectValue is not Dictionary<string, object> objectDict)
@@ -301,7 +299,7 @@ public class ActivityPubActivityProcessor(
return true; return true;
} }
private async Task<bool> ProcessLikeAsync(string actorUri, Dictionary<string, object> activity) private async Task<bool> HandleLikeAsync(string actorUri, Dictionary<string, object> activity)
{ {
var objectUri = activity.GetValueOrDefault("object")?.ToString(); var objectUri = activity.GetValueOrDefault("object")?.ToString();
if (string.IsNullOrEmpty(objectUri)) if (string.IsNullOrEmpty(objectUri))
@@ -347,11 +345,11 @@ public class ActivityPubActivityProcessor(
await db.SaveChangesAsync(); await db.SaveChangesAsync();
logger.LogInformation("Processed like from {Actor}", actorUri); logger.LogInformation("Handleed like from {Actor}", actorUri);
return true; return true;
} }
private async Task<bool> ProcessAnnounceAsync(string actorUri, Dictionary<string, object> activity) private async Task<bool> HandleAnnounceAsync(string actorUri, Dictionary<string, object> activity)
{ {
var objectUri = activity.GetValueOrDefault("object")?.ToString(); var objectUri = activity.GetValueOrDefault("object")?.ToString();
if (string.IsNullOrEmpty(objectUri)) if (string.IsNullOrEmpty(objectUri))
@@ -367,11 +365,11 @@ public class ActivityPubActivityProcessor(
await db.SaveChangesAsync(); await db.SaveChangesAsync();
} }
logger.LogInformation("Processed announce from {Actor}", actorUri); logger.LogInformation("Handleed announce from {Actor}", actorUri);
return true; return true;
} }
private async Task<bool> ProcessDeleteAsync(string actorUri, Dictionary<string, object> activity) private async Task<bool> HandleDeleteAsync(string actorUri, Dictionary<string, object> activity)
{ {
var objectUri = activity.GetValueOrDefault("object")?.ToString(); var objectUri = activity.GetValueOrDefault("object")?.ToString();
if (string.IsNullOrEmpty(objectUri)) if (string.IsNullOrEmpty(objectUri))
@@ -390,7 +388,7 @@ public class ActivityPubActivityProcessor(
return true; return true;
} }
private async Task<bool> ProcessUpdateAsync(string actorUri, Dictionary<string, object> activity) private async Task<bool> HandleUpdateAsync(string actorUri, Dictionary<string, object> activity)
{ {
var objectUri = activity.GetValueOrDefault("object")?.ToString(); var objectUri = activity.GetValueOrDefault("object")?.ToString();
if (string.IsNullOrEmpty(objectUri)) if (string.IsNullOrEmpty(objectUri))

View File

@@ -14,7 +14,7 @@ public class ActivityPubController(
IConfiguration configuration, IConfiguration configuration,
ILogger<ActivityPubController> logger, ILogger<ActivityPubController> logger,
ActivityPubSignatureService signatureService, ActivityPubSignatureService signatureService,
ActivityPubActivityProcessor activityProcessor, ActivityPubActivityHandler activityHandler,
ActivityPubKeyService keyService ActivityPubKeyService keyService
) : ControllerBase ) : ControllerBase
{ {
@@ -106,7 +106,7 @@ public class ActivityPubController(
return Unauthorized(new { error = "Invalid signature" }); return Unauthorized(new { error = "Invalid signature" });
} }
var success = await activityProcessor.ProcessIncomingActivityAsync(HttpContext, username, activity); var success = await activityHandler.HandleIncomingActivityAsync(HttpContext, username, activity);
if (!success) if (!success)
{ {

View File

@@ -105,7 +105,7 @@ public static class ServiceCollectionExtensions
services.AddScoped<AutocompletionService>(); services.AddScoped<AutocompletionService>();
services.AddScoped<ActivityPubKeyService>(); services.AddScoped<ActivityPubKeyService>();
services.AddScoped<ActivityPubSignatureService>(); services.AddScoped<ActivityPubSignatureService>();
services.AddScoped<ActivityPubActivityProcessor>(); services.AddScoped<ActivityPubActivityHandler>();
services.AddScoped<ActivityPubDeliveryService>(); services.AddScoped<ActivityPubDeliveryService>();
services.AddScoped<ActivityPubDiscoveryService>(); services.AddScoped<ActivityPubDiscoveryService>();
services.AddScoped<ActivityPubFollowController>(); services.AddScoped<ActivityPubFollowController>();