🐛 Fix some issues in AP

This commit is contained in:
2025-12-31 01:31:06 +08:00
parent 71031e2222
commit eb8d126261
2 changed files with 46 additions and 19 deletions

View File

@@ -112,9 +112,9 @@ public class ActivityPubActivityProcessor(
targetPublisher.Name, targetPublisher.Id);
var existingRelationship = await db.FediverseRelationships
.FirstOrDefaultAsync(r =>
r.ActorId == actor.Id &&
r.TargetActorId == actor.Id);
.FirstOrDefaultAsync(r =>
r.ActorId == actor.Id &&
r.TargetActorId == localActor.Id);
if (existingRelationship is { State: RelationshipState.Accepted })
{
@@ -167,20 +167,36 @@ public class ActivityPubActivityProcessor(
var relationship = await db.FediverseRelationships
.Include(r => r.Actor)
.Include(r => r.TargetActor)
.FirstOrDefaultAsync(r =>
r.TargetActorId == actor.Id &&
r.State == RelationshipState.Pending);
.FirstOrDefaultAsync(r =>
r.TargetActorId == actor.Id);
if (relationship == null)
{
logger.LogWarning("No pending relationship found for accept");
return false;
// Assume objectUri is the local actor URI that was followed
var localActor = await db.FediverseActors.FirstOrDefaultAsync(a => a.Uri == objectUri);
if (localActor == null)
{
logger.LogWarning("Local actor not found for accept object: {ObjectUri}", objectUri);
return false;
}
relationship = new SnFediverseRelationship
{
ActorId = localActor.Id,
TargetActorId = actor.Id,
State = RelationshipState.Accepted,
IsFollowing = true,
IsFollowedBy = false,
FollowedAt = SystemClock.Instance.GetCurrentInstant()
};
db.FediverseRelationships.Add(relationship);
}
relationship.State = RelationshipState.Accepted;
relationship.IsFollowing = true;
relationship.FollowedAt = SystemClock.Instance.GetCurrentInstant();
else
{
relationship.State = RelationshipState.Accepted;
relationship.IsFollowing = true;
relationship.FollowedAt = SystemClock.Instance.GetCurrentInstant();
}
await db.SaveChangesAsync();
logger.LogInformation("Processed accept from {Actor}", actorUri);

View File

@@ -115,7 +115,7 @@ public class ActivityPubDeliveryService(
return await SendActivityToInboxAsync(activity, targetActor.InboxUri, actorUrl);
}
public async Task<bool> SendUnfollowActivityAsync(
Guid publisherId,
string targetActorUri
@@ -124,11 +124,11 @@ public class ActivityPubDeliveryService(
var publisher = await db.Publishers.FindAsync(publisherId);
if (publisher == null)
return false;
var actorUrl = $"https://{Domain}/activitypub/actors/{publisher.Name}";
var targetActor = await GetOrFetchActorAsync(targetActorUri);
var localActor = await GetLocalActorAsync(publisher.Id);
if (targetActor?.InboxUri == null || localActor == null)
{
logger.LogWarning("Target actor or inbox not found: {Uri}", targetActorUri);
@@ -148,8 +148,19 @@ public class ActivityPubDeliveryService(
}
};
var relationship = await db.FediverseRelationships
.FirstOrDefaultAsync(r =>
r.ActorId == localActor.Id &&
r.TargetActorId == targetActor.Id);
if (relationship == null) return false;
var success = await SendActivityToInboxAsync(activity, targetActor.InboxUri, actorUrl);
if (!success) return success;
return await SendActivityToInboxAsync(activity, targetActor.InboxUri, actorUrl);
db.Remove(relationship);
await db.SaveChangesAsync();
return success;
}
public async Task<bool> SendCreateActivityAsync(SnPost post)
@@ -764,4 +775,4 @@ public class ActivityPubDeliveryService(
{
return actorUri.Split('/').Last();
}
}
}