♻️ Better local actor
This commit is contained in:
@@ -94,6 +94,13 @@ public class ActivityPubActivityProcessor(
|
||||
objectUri, targetUsername);
|
||||
return false;
|
||||
}
|
||||
|
||||
var localActor = await deliveryService.GetOrCreateLocalActorAsync(targetPublisher);
|
||||
if (localActor == null)
|
||||
{
|
||||
logger.LogWarning("Target publisher has no actor...");
|
||||
return false;
|
||||
}
|
||||
|
||||
logger.LogInformation("Target publisher found: {PublisherName} (ID: {Id})",
|
||||
targetPublisher.Name, targetPublisher.Id);
|
||||
@@ -101,10 +108,9 @@ public class ActivityPubActivityProcessor(
|
||||
var existingRelationship = await db.FediverseRelationships
|
||||
.FirstOrDefaultAsync(r =>
|
||||
r.ActorId == actor.Id &&
|
||||
r.TargetActorId == actor.Id &&
|
||||
r.IsLocalActor);
|
||||
r.TargetActorId == actor.Id);
|
||||
|
||||
if (existingRelationship != null && existingRelationship.State == RelationshipState.Accepted)
|
||||
if (existingRelationship is { State: RelationshipState.Accepted })
|
||||
{
|
||||
logger.LogInformation("Follow relationship already exists and is accepted. ActorId: {ActorId}, PublisherId: {PublisherId}",
|
||||
actor.Id, targetPublisher.Id);
|
||||
@@ -116,9 +122,7 @@ public class ActivityPubActivityProcessor(
|
||||
existingRelationship = new SnFediverseRelationship
|
||||
{
|
||||
ActorId = actor.Id,
|
||||
TargetActorId = actor.Id,
|
||||
IsLocalActor = true,
|
||||
LocalPublisherId = targetPublisher.Id,
|
||||
TargetActorId = localActor.Id,
|
||||
State = RelationshipState.Pending,
|
||||
IsFollowing = false,
|
||||
IsFollowedBy = true
|
||||
@@ -158,7 +162,6 @@ public class ActivityPubActivityProcessor(
|
||||
.Include(r => r.Actor)
|
||||
.Include(r => r.TargetActor)
|
||||
.FirstOrDefaultAsync(r =>
|
||||
r.IsLocalActor &&
|
||||
r.TargetActorId == actor.Id &&
|
||||
r.State == RelationshipState.Pending);
|
||||
|
||||
@@ -188,7 +191,6 @@ public class ActivityPubActivityProcessor(
|
||||
|
||||
var relationship = await db.FediverseRelationships
|
||||
.FirstOrDefaultAsync(r =>
|
||||
r.IsLocalActor &&
|
||||
r.TargetActorId == actor.Id);
|
||||
|
||||
if (relationship == null)
|
||||
@@ -212,24 +214,21 @@ public class ActivityPubActivityProcessor(
|
||||
var objectValue = activity.GetValueOrDefault("object");
|
||||
if (objectValue == null)
|
||||
return false;
|
||||
|
||||
var objectDict = objectValue as Dictionary<string, object>;
|
||||
if (objectDict != null)
|
||||
|
||||
if (objectValue is not Dictionary<string, object> objectDict) return false;
|
||||
var objectType = objectDict.GetValueOrDefault("type")?.ToString();
|
||||
switch (objectType)
|
||||
{
|
||||
var objectType = objectDict.GetValueOrDefault("type")?.ToString();
|
||||
switch (objectType)
|
||||
{
|
||||
case "Follow":
|
||||
return await UndoFollowAsync(actorUri, objectDict.GetValueOrDefault("id")?.ToString());
|
||||
case "Like":
|
||||
return await UndoLikeAsync(actorUri, objectDict.GetValueOrDefault("id")?.ToString());
|
||||
case "Announce":
|
||||
return await UndoAnnounceAsync(actorUri, objectDict.GetValueOrDefault("id")?.ToString());
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
case "Follow":
|
||||
return await UndoFollowAsync(actorUri, objectDict.GetValueOrDefault("id")?.ToString());
|
||||
case "Like":
|
||||
return await UndoLikeAsync(actorUri, objectDict.GetValueOrDefault("id")?.ToString());
|
||||
case "Announce":
|
||||
return await UndoAnnounceAsync(actorUri, objectDict.GetValueOrDefault("id")?.ToString());
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -191,7 +191,7 @@ public class ActivityPubController(
|
||||
|
||||
var relationshipsQuery = db.FediverseRelationships
|
||||
.Include(r => r.Actor)
|
||||
.Where(r => r.LocalPublisherId == publisher.Id && r.IsFollowedBy);
|
||||
.Where(r => r.Actor.PublisherId == publisher.Id && r.IsFollowedBy);
|
||||
|
||||
var totalItems = await relationshipsQuery.CountAsync();
|
||||
|
||||
@@ -257,7 +257,7 @@ public class ActivityPubController(
|
||||
|
||||
var relationshipsQuery = db.FediverseRelationships
|
||||
.Include(r => r.TargetActor)
|
||||
.Where(r => r.LocalPublisherId == publisher.Id && r.IsFollowing);
|
||||
.Where(r => r.Actor.PublisherId == publisher.Id && r.IsFollowing);
|
||||
|
||||
var totalItems = await relationshipsQuery.CountAsync();
|
||||
|
||||
|
||||
@@ -87,16 +87,28 @@ public class ActivityPubDeliveryService(
|
||||
["object"] = targetActorUri
|
||||
};
|
||||
|
||||
await db.FediverseRelationships.AddAsync(new SnFediverseRelationship
|
||||
var existingRelationship = await db.FediverseRelationships
|
||||
.FirstOrDefaultAsync(r =>
|
||||
r.ActorId == localActor.Id &&
|
||||
r.TargetActorId == targetActor.Id);
|
||||
|
||||
if (existingRelationship == null)
|
||||
{
|
||||
IsLocalActor = true,
|
||||
LocalPublisherId = publisher.Id,
|
||||
ActorId = localActor.Id,
|
||||
TargetActorId = targetActor.Id,
|
||||
State = RelationshipState.Pending,
|
||||
IsFollowing = true,
|
||||
IsFollowedBy = false
|
||||
});
|
||||
existingRelationship = new SnFediverseRelationship
|
||||
{
|
||||
ActorId = localActor.Id,
|
||||
TargetActorId = targetActor.Id,
|
||||
State = RelationshipState.Pending,
|
||||
IsFollowing = true,
|
||||
IsFollowedBy = false
|
||||
};
|
||||
db.FediverseRelationships.Add(existingRelationship);
|
||||
}
|
||||
else
|
||||
{
|
||||
existingRelationship.IsFollowing = true;
|
||||
existingRelationship.State = RelationshipState.Pending;
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
@@ -139,7 +151,7 @@ public class ActivityPubDeliveryService(
|
||||
}
|
||||
};
|
||||
|
||||
var followers = await GetRemoteFollowersAsync(publisher.Id);
|
||||
var followers = await GetRemoteFollowersAsync();
|
||||
var successCount = 0;
|
||||
|
||||
foreach (var follower in followers)
|
||||
@@ -200,7 +212,7 @@ public class ActivityPubDeliveryService(
|
||||
return false;
|
||||
|
||||
var actorUrl = $"https://{Domain}/activitypub/actors/{publisher.Name}";
|
||||
var followers = await GetRemoteFollowersAsync(publisher.Id);
|
||||
var followers = await GetRemoteFollowersAsync();
|
||||
|
||||
var activity = new Dictionary<string, object>
|
||||
{
|
||||
@@ -297,19 +309,16 @@ public class ActivityPubDeliveryService(
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<List<SnFediverseActor>> GetRemoteFollowersAsync(Guid publisherId)
|
||||
private async Task<List<SnFediverseActor>> GetRemoteFollowersAsync()
|
||||
{
|
||||
return await db.FediverseRelationships
|
||||
.Include(r => r.TargetActor)
|
||||
.Where(r =>
|
||||
r.LocalPublisherId == publisherId &&
|
||||
r.IsFollowedBy &&
|
||||
r.IsLocalActor)
|
||||
.Where(r => r.IsFollowedBy)
|
||||
.Select(r => r.TargetActor)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
private async Task<SnFediverseActor?> GetOrCreateLocalActorAsync(SnPublisher publisher)
|
||||
public async Task<SnFediverseActor?> GetOrCreateLocalActorAsync(SnPublisher publisher)
|
||||
{
|
||||
var actorUrl = $"https://{Domain}/activitypub/actors/{publisher.Name}";
|
||||
|
||||
@@ -347,7 +356,8 @@ public class ActivityPubDeliveryService(
|
||||
FollowingUri = $"{actorUrl}/following",
|
||||
AvatarUrl = publisher.Picture != null ? $"{assetsBaseUrl}/{publisher.Picture.Id}" : null,
|
||||
HeaderUrl = publisher.Background != null ? $"{assetsBaseUrl}/{publisher.Background.Id}" : null,
|
||||
InstanceId = instance.Id
|
||||
InstanceId = instance.Id,
|
||||
PublisherId = publisher.Id,
|
||||
};
|
||||
|
||||
db.FediverseActors.Add(localActor);
|
||||
@@ -422,4 +432,4 @@ public class ActivityPubDeliveryService(
|
||||
{
|
||||
return actorUri.Split('/').Last();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,8 +110,7 @@ public class ActivityPubFollowController(
|
||||
.Include(r => r.TargetActor)
|
||||
.ThenInclude(a => a.Instance)
|
||||
.Where(r =>
|
||||
r.IsLocalActor &&
|
||||
r.LocalPublisherId == publisher.Id &&
|
||||
r.Actor.PublisherId == publisher.Id &&
|
||||
r.IsFollowing &&
|
||||
r.State == RelationshipState.Accepted)
|
||||
.OrderByDescending(r => r.FollowedAt)
|
||||
@@ -143,8 +142,7 @@ public class ActivityPubFollowController(
|
||||
.Include(r => r.Actor)
|
||||
.ThenInclude(a => a.Instance)
|
||||
.Where(r =>
|
||||
!r.IsLocalActor &&
|
||||
r.LocalPublisherId == publisher.Id &&
|
||||
r.Actor.PublisherId == publisher.Id &&
|
||||
r.IsFollowedBy &&
|
||||
r.State == RelationshipState.Accepted)
|
||||
.OrderByDescending(r => r.FollowedAt ?? r.CreatedAt)
|
||||
@@ -188,27 +186,24 @@ public class ActivityPubFollowController(
|
||||
|
||||
var followingCount = await db.FediverseRelationships
|
||||
.CountAsync(r =>
|
||||
r.IsLocalActor &&
|
||||
r.LocalPublisherId == publisher.Id &&
|
||||
r.Actor.PublisherId == publisher.Id &&
|
||||
r.IsFollowing &&
|
||||
r.State == RelationshipState.Accepted);
|
||||
|
||||
var followersCount = await db.FediverseRelationships
|
||||
.CountAsync(r =>
|
||||
!r.IsLocalActor &&
|
||||
r.LocalPublisherId == publisher.Id &&
|
||||
r.Actor.PublisherId == publisher.Id &&
|
||||
r.IsFollowedBy &&
|
||||
r.State == RelationshipState.Accepted);
|
||||
|
||||
var pendingCount = await db.FediverseRelationships
|
||||
.CountAsync(r =>
|
||||
r.IsLocalActor &&
|
||||
r.LocalPublisherId == publisher.Id &&
|
||||
r.Actor.PublisherId == publisher.Id &&
|
||||
r.State == RelationshipState.Pending);
|
||||
|
||||
var relationships = await db.FediverseRelationships
|
||||
.Include(r => r.TargetActor)
|
||||
.Where(r => r.IsLocalActor && r.LocalPublisherId == publisher.Id)
|
||||
.Where(r => r.Actor.PublisherId == publisher.Id)
|
||||
.OrderByDescending(r => r.FollowedAt ?? r.CreatedAt)
|
||||
.Take(20)
|
||||
.ToListAsync();
|
||||
|
||||
Reference in New Issue
Block a user