From 5ff1539f185f796b3204c15a24999ed265bcde9b Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 23 Dec 2025 23:56:24 +0800 Subject: [PATCH] :bug: Trying to fix the wrong relationship fetch --- .../Account/RelationshipController.cs | 5 ++-- .../Account/RelationshipService.cs | 29 ++++++++++++------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/DysonNetwork.Pass/Account/RelationshipController.cs b/DysonNetwork.Pass/Account/RelationshipController.cs index f13d931..94ce205 100644 --- a/DysonNetwork.Pass/Account/RelationshipController.cs +++ b/DysonNetwork.Pass/Account/RelationshipController.cs @@ -35,8 +35,9 @@ public class RelationshipController(AppDatabase db, RelationshipService rels) : .Where(r => r.AccountId == userId) .ToDictionaryAsync(r => r.RelatedId); foreach (var relationship in relationships) - if (statuses.TryGetValue(relationship.RelatedId, out var status)) - relationship.Status = status.Status; + relationship.Status = statuses.TryGetValue(relationship.AccountId, out var status) + ? status.Status + : RelationshipStatus.Pending; Response.Headers["X-Total"] = totalCount.ToString(); diff --git a/DysonNetwork.Pass/Account/RelationshipService.cs b/DysonNetwork.Pass/Account/RelationshipService.cs index f0ae32b..248f9aa 100644 --- a/DysonNetwork.Pass/Account/RelationshipService.cs +++ b/DysonNetwork.Pass/Account/RelationshipService.cs @@ -224,20 +224,27 @@ public class RelationshipService( var cacheKey = $"{cachePrefix}{accountId}"; var relationships = await cache.GetAsync>(cacheKey); - if (relationships == null) - { - var now = Instant.FromDateTimeUtc(DateTime.UtcNow); - relationships = await db.AccountRelationships - .Where(r => r.RelatedId == accountId) - .Where(r => r.Status == status) - .Where(r => r.ExpiredAt == null || r.ExpiredAt > now) - .Select(r => r.AccountId) - .ToListAsync(); + if (relationships != null) return relationships; + var now = Instant.FromDateTimeUtc(DateTime.UtcNow); + var query = db.AccountRelationships + .Where(r => r.RelatedId == accountId) + .Where(r => r.Status == status) + .Where(r => r.ExpiredAt == null || r.ExpiredAt > now) + .Select(r => r.AccountId); - await cache.SetAsync(cacheKey, relationships, CacheExpiration); + if (status == RelationshipStatus.Friends) + { + var usersBlockedByMe = db.AccountRelationships + .Where(r => r.AccountId == accountId && r.Status == RelationshipStatus.Blocked) + .Select(r => r.RelatedId); + query = query.Except(usersBlockedByMe); } - return relationships ?? new List(); + relationships = await query.ToListAsync(); + + await cache.SetAsync(cacheKey, relationships, CacheExpiration); + + return relationships; } private async Task PurgeRelationshipCache(Guid accountId, Guid relatedId, params RelationshipStatus[] statuses)