♻️ Refactor the follow activitypub process

This commit is contained in:
2025-12-31 22:52:31 +08:00
parent add9fa49e5
commit c11b30d0bb
2 changed files with 86 additions and 101 deletions

View File

@@ -39,6 +39,7 @@ public class ActivityPubActivityHandler(
{
return await db.Posts.FirstOrDefaultAsync(p => p.Id == id);
}
return null;
}
@@ -122,49 +123,33 @@ public class ActivityPubActivityHandler(
}
var actor = await GetOrCreateActorAsync(actorUri);
var targetUsername = ExtractUsernameFromUri(objectUri);
var targetPublisher = await db.Publishers
.FirstOrDefaultAsync(p => p.Name == targetUsername);
if (targetPublisher == null)
{
logger.LogWarning("Target publisher not found: {Uri}, ExtractedUsername: {Username}",
objectUri, targetUsername);
return false;
}
var localActor = await deliveryService.GetLocalActorAsync(targetPublisher.Id);
if (localActor == null)
{
logger.LogWarning("Target publisher has no enabled fediverse actor");
return false;
}
logger.LogInformation("Target publisher found: {PublisherName} (ID: {Id})",
targetPublisher.Name, targetPublisher.Id);
// This might be fail, but we assume it works great.
var targetActor = await GetOrCreateActorAsync(objectUri);
var existingRelationship = await db.FediverseRelationships
.FirstOrDefaultAsync(r =>
r.ActorId == actor.Id &&
r.TargetActorId == localActor.Id);
r.TargetActorId == targetActor.Id);
switch (existingRelationship)
{
case { State: RelationshipState.Accepted }:
logger.LogInformation("Follow relationship already exists and is accepted. ActorId: {ActorId}, PublisherId: {PublisherId}",
actor.Id, targetPublisher.Id);
logger.LogInformation(
"Follow relationship already exists and is accepted. ActorId: {ActorId}, TargetId: {TargetId}",
actor.Id, targetActor.Id);
return true;
case null:
existingRelationship = new SnFediverseRelationship
{
ActorId = actor.Id,
TargetActorId = localActor.Id,
TargetActorId = targetActor.Id,
State = RelationshipState.Accepted,
FollowedBackAt = SystemClock.Instance.GetCurrentInstant()
};
db.FediverseRelationships.Add(existingRelationship);
logger.LogInformation("Created new follow relationship. ActorId: {ActorId}, TargetActorId: {TargetActorId}",
actor.Id, localActor.Id);
logger.LogInformation(
"Created new follow relationship. ActorId: {ActorId}, TargetActorId: {TargetId}",
actor.Id, targetActor.Id);
break;
default:
existingRelationship.State = RelationshipState.Accepted;
@@ -177,7 +162,7 @@ public class ActivityPubActivityHandler(
await db.SaveChangesAsync();
await deliveryService.SendAcceptActivityAsync(
targetPublisher.Id,
targetActor,
actorUri,
activityId ?? ""
);
@@ -210,6 +195,7 @@ public class ActivityPubActivityHandler(
logger.LogWarning("Local actor not found for accept object: {ObjectUri}", objectUri);
return false;
}
relationship = new SnFediverseRelationship
{
ActorId = localActor.Id,
@@ -304,11 +290,15 @@ public class ActivityPubActivityHandler(
var content = new SnPost
{
FediverseUri = contentUri,
FediverseType = objectType == "Article" ? FediverseContentType.FediverseArticle : FediverseContentType.FediverseNote,
FediverseType = objectType == "Article"
? FediverseContentType.FediverseArticle
: FediverseContentType.FediverseNote,
Title = objectDict.GetValueOrDefault("name")?.ToString(),
Description = objectDict.GetValueOrDefault("summary")?.ToString(),
Content = objectDict.GetValueOrDefault("content")?.ToString(),
ContentType = objectDict.GetValueOrDefault("contentMap") != null ? PostContentType.Html : PostContentType.Markdown,
ContentType = objectDict.GetValueOrDefault("contentMap") != null
? PostContentType.Html
: PostContentType.Markdown,
PublishedAt = ParseInstant(objectDict.GetValueOrDefault("published")),
EditedAt = ParseInstant(objectDict.GetValueOrDefault("updated")),
ActorId = actor.Id,
@@ -500,8 +490,8 @@ public class ActivityPubActivityHandler(
var actor = await db.FediverseActors
.FirstOrDefaultAsync(a => a.Uri == actorUri);
if (actor == null)
{
if (actor != null) return actor;
var instance = await GetOrCreateInstanceAsync(actorUri);
actor = new SnFediverseActor
{
@@ -514,7 +504,6 @@ public class ActivityPubActivityHandler(
await db.SaveChangesAsync();
await discoveryService.FetchActorDataAsync(actor);
}
return actor;
}

View File

@@ -29,16 +29,12 @@ public class ActivityPubDeliveryService(
}
public async Task<bool> SendAcceptActivityAsync(
Guid publisherId,
SnFediverseActor actor,
string followerActorUri,
string followActivityId
)
{
var publisher = await db.Publishers.FindAsync(publisherId);
if (publisher == null)
return false;
var actorUrl = $"https://{Domain}/activitypub/actors/{publisher.Name}";
var actorUrl = actor.Uri;
var followerActor = await db.FediverseActors
.FirstOrDefaultAsync(a => a.Uri == followerActorUri);