♻️ Refactored fediverse relationships

This commit is contained in:
2025-12-31 18:29:35 +08:00
parent 2b6cf503a5
commit caf5468dad
9 changed files with 2437 additions and 42 deletions

View File

@@ -16,10 +16,7 @@ public class SnFediverseRelationship : ModelBase
public SnFediverseActor TargetActor { get; set; } = null!;
public RelationshipState State { get; set; } = RelationshipState.Pending;
public bool IsFollowing { get; set; } = false;
public bool IsFollowedBy { get; set; } = false;
public bool IsMuting { get; set; } = false;
public bool IsBlocking { get; set; } = false;

View File

@@ -129,13 +129,11 @@ public class ActivityPubActivityProcessor(
{
ActorId = actor.Id,
TargetActorId = localActor.Id,
State = RelationshipState.Pending,
IsFollowing = false,
IsFollowedBy = true
State = RelationshipState.Pending
};
db.FediverseRelationships.Add(existingRelationship);
logger.LogInformation("Created new follow relationship. ActorId: {ActorId}, TargetActorId: {TargetActorId}",
actor.Id, actor.Id);
logger.LogInformation("Created new follow relationship. ActorId: {ActorId}, TargetActorId: {TargetActorId}",
actor.Id, localActor.Id);
}
else
{
@@ -184,8 +182,6 @@ public class ActivityPubActivityProcessor(
ActorId = localActor.Id,
TargetActorId = actor.Id,
State = RelationshipState.Accepted,
IsFollowing = true,
IsFollowedBy = false,
FollowedAt = SystemClock.Instance.GetCurrentInstant()
};
db.FediverseRelationships.Add(relationship);
@@ -193,7 +189,6 @@ public class ActivityPubActivityProcessor(
else
{
relationship.State = RelationshipState.Accepted;
relationship.IsFollowing = true;
relationship.FollowedAt = SystemClock.Instance.GetCurrentInstant();
}
@@ -222,7 +217,6 @@ public class ActivityPubActivityProcessor(
}
relationship.State = RelationshipState.Rejected;
relationship.IsFollowing = false;
relationship.RejectReason = "Remote rejected follow";
await db.SaveChangesAsync();

View File

@@ -191,7 +191,7 @@ public class ActivityPubController(
var relationshipsQuery = db.FediverseRelationships
.Include(r => r.Actor)
.Where(r => r.Actor.PublisherId == publisher.Id && r.IsFollowedBy);
.Where(r => r.TargetActor.PublisherId == publisher.Id && r.State == RelationshipState.Accepted);
var totalItems = await relationshipsQuery.CountAsync();
@@ -257,7 +257,7 @@ public class ActivityPubController(
var relationshipsQuery = db.FediverseRelationships
.Include(r => r.TargetActor)
.Where(r => r.Actor.PublisherId == publisher.Id && r.IsFollowing);
.Where(r => r.Actor.PublisherId == publisher.Id && r.State == RelationshipState.Accepted);
var totalItems = await relationshipsQuery.CountAsync();

View File

@@ -99,15 +99,12 @@ public class ActivityPubDeliveryService(
{
ActorId = localActor.Id,
TargetActorId = targetActor.Id,
State = RelationshipState.Pending,
IsFollowing = true,
IsFollowedBy = false
State = RelationshipState.Pending
};
db.FediverseRelationships.Add(existingRelationship);
}
else
{
existingRelationship.IsFollowing = true;
existingRelationship.State = RelationshipState.Pending;
}
@@ -645,18 +642,23 @@ public class ActivityPubDeliveryService(
private async Task<List<SnFediverseActor>> GetRemoteFollowersAsync()
{
var localActorIds = await db.FediverseActors
.Where(a => a.PublisherId != null)
.Select(a => a.Id)
.ToListAsync();
return await db.FediverseRelationships
.Include(r => r.Actor)
.Where(r => r.IsFollowedBy)
.Where(r => r.State == RelationshipState.Accepted && localActorIds.Contains(r.TargetActorId))
.Select(r => r.Actor)
.ToListAsync();
}
private async Task<List<SnFediverseActor>> GetRemoteFollowersAsync(Guid actorId)
{
return await db.FediverseRelationships
.Include(r => r.Actor)
.Where(r => r.TargetActorId == actorId && r.IsFollowedBy)
.Where(r => r.TargetActorId == actorId && r.State == RelationshipState.Accepted)
.Select(r => r.Actor)
.ToListAsync();
}

View File

@@ -149,7 +149,6 @@ public class ActivityPubFollowController(
var totalCount = await db.FediverseRelationships
.CountAsync(r =>
r.Actor.PublisherId == publisher.Id &&
r.IsFollowing &&
r.State == RelationshipState.Accepted);
var actors = await db.FediverseRelationships
@@ -157,7 +156,6 @@ public class ActivityPubFollowController(
.ThenInclude(a => a.Instance)
.Where(r =>
r.Actor.PublisherId == publisher.Id &&
r.IsFollowing &&
r.State == RelationshipState.Accepted)
.OrderByDescending(r => r.FollowedAt)
.Skip(offset)
@@ -194,16 +192,14 @@ public class ActivityPubFollowController(
var totalCount = await db.FediverseRelationships
.CountAsync(r =>
r.Actor.PublisherId == publisher.Id &&
r.IsFollowedBy &&
r.TargetActor.PublisherId == publisher.Id &&
r.State == RelationshipState.Accepted);
var actors = await db.FediverseRelationships
.Include(r => r.Actor)
.ThenInclude(a => a.Instance)
.Where(r =>
r.Actor.PublisherId == publisher.Id &&
r.IsFollowedBy &&
r.TargetActor.PublisherId == publisher.Id &&
r.State == RelationshipState.Accepted)
.OrderByDescending(r => r.FollowedAt ?? r.CreatedAt)
.Skip(offset)
@@ -265,13 +261,11 @@ public class ActivityPubFollowController(
var followingCount = await db.FediverseRelationships
.CountAsync(r =>
r.Actor.PublisherId == publisher.Id &&
r.IsFollowing &&
r.State == RelationshipState.Accepted);
var followersCount = await db.FediverseRelationships
.CountAsync(r =>
r.Actor.PublisherId == publisher.Id &&
r.IsFollowedBy &&
r.TargetActor.PublisherId == publisher.Id &&
r.State == RelationshipState.Accepted);
var pendingCount = await db.FediverseRelationships
@@ -296,7 +290,7 @@ public class ActivityPubFollowController(
{
Actor = r.TargetActor,
State = r.State,
IsFollowing = r.IsFollowing,
IsFollowing = true,
FollowedAt = r.FollowedAt,
TargetActorUri = r.TargetActor.Uri,
Username = r.TargetActor.Username,
@@ -443,7 +437,6 @@ public class ActivityPubFollowController(
.Where(r =>
r.ActorId == userActor.Id &&
actorIds.Contains(r.TargetActorId) &&
r.IsFollowing &&
r.State == RelationshipState.Accepted)
.Select(r => r.TargetActorId)
.ToListAsync();

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,40 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace DysonNetwork.Sphere.Migrations
{
/// <inheritdoc />
public partial class RemoveFollowingBooleanInFediverse : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "is_followed_by",
table: "fediverse_relationships");
migrationBuilder.DropColumn(
name: "is_following",
table: "fediverse_relationships");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "is_followed_by",
table: "fediverse_relationships",
type: "boolean",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<bool>(
name: "is_following",
table: "fediverse_relationships",
type: "boolean",
nullable: false,
defaultValue: false);
}
}
}

View File

@@ -578,14 +578,6 @@ namespace DysonNetwork.Sphere.Migrations
.HasColumnType("boolean")
.HasColumnName("is_blocking");
b.Property<bool>("IsFollowedBy")
.HasColumnType("boolean")
.HasColumnName("is_followed_by");
b.Property<bool>("IsFollowing")
.HasColumnType("boolean")
.HasColumnName("is_following");
b.Property<bool>("IsMuting")
.HasColumnType("boolean")
.HasColumnName("is_muting");

View File

@@ -789,7 +789,7 @@ public class PublisherService(
.FirstOrDefaultAsync(a => a.PublisherId == publisherId);
var followerCount = await db.FediverseRelationships
.Where(r => r.Actor.PublisherId == publisherId && r.IsFollowedBy)
.Where(r => r.TargetActor.PublisherId == publisherId && r.State == RelationshipState.Accepted)
.CountAsync();
var publisher = await db.Publishers