🐛 Trying to fix the wrong relationship fetch
This commit is contained in:
@@ -35,8 +35,9 @@ public class RelationshipController(AppDatabase db, RelationshipService rels) :
|
|||||||
.Where(r => r.AccountId == userId)
|
.Where(r => r.AccountId == userId)
|
||||||
.ToDictionaryAsync(r => r.RelatedId);
|
.ToDictionaryAsync(r => r.RelatedId);
|
||||||
foreach (var relationship in relationships)
|
foreach (var relationship in relationships)
|
||||||
if (statuses.TryGetValue(relationship.RelatedId, out var status))
|
relationship.Status = statuses.TryGetValue(relationship.AccountId, out var status)
|
||||||
relationship.Status = status.Status;
|
? status.Status
|
||||||
|
: RelationshipStatus.Pending;
|
||||||
|
|
||||||
Response.Headers["X-Total"] = totalCount.ToString();
|
Response.Headers["X-Total"] = totalCount.ToString();
|
||||||
|
|
||||||
|
|||||||
@@ -224,20 +224,27 @@ public class RelationshipService(
|
|||||||
var cacheKey = $"{cachePrefix}{accountId}";
|
var cacheKey = $"{cachePrefix}{accountId}";
|
||||||
var relationships = await cache.GetAsync<List<Guid>>(cacheKey);
|
var relationships = await cache.GetAsync<List<Guid>>(cacheKey);
|
||||||
|
|
||||||
if (relationships == null)
|
if (relationships != null) return relationships;
|
||||||
{
|
var now = Instant.FromDateTimeUtc(DateTime.UtcNow);
|
||||||
var now = Instant.FromDateTimeUtc(DateTime.UtcNow);
|
var query = db.AccountRelationships
|
||||||
relationships = await db.AccountRelationships
|
.Where(r => r.RelatedId == accountId)
|
||||||
.Where(r => r.RelatedId == accountId)
|
.Where(r => r.Status == status)
|
||||||
.Where(r => r.Status == status)
|
.Where(r => r.ExpiredAt == null || r.ExpiredAt > now)
|
||||||
.Where(r => r.ExpiredAt == null || r.ExpiredAt > now)
|
.Select(r => r.AccountId);
|
||||||
.Select(r => r.AccountId)
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
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)
|
private async Task PurgeRelationshipCache(Guid accountId, Guid relatedId, params RelationshipStatus[] statuses)
|
||||||
|
|||||||
Reference in New Issue
Block a user