🐛 Trying to fix the wrong relationship fetch

This commit is contained in:
2025-12-23 23:56:24 +08:00
parent 3c023a71b1
commit 5ff1539f18
2 changed files with 21 additions and 13 deletions

View File

@@ -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();

View File

@@ -224,20 +224,27 @@ public class RelationshipService(
var cacheKey = $"{cachePrefix}{accountId}";
var relationships = await cache.GetAsync<List<Guid>>(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<Guid>();
relationships = await query.ToListAsync();
await cache.SetAsync(cacheKey, relationships, CacheExpiration);
return relationships;
}
private async Task PurgeRelationshipCache(Guid accountId, Guid relatedId, params RelationshipStatus[] statuses)