🐛 Fix publisher subscription cache and status validation
This commit is contained in:
parent
d1fb0b9b55
commit
f50894a3d1
@ -54,23 +54,14 @@ public class ActivityService(AppDatabase db, PublisherService pub, RelationshipS
|
||||
var userPublishers = await pub.GetUserPublishers(currentUser.Id);
|
||||
|
||||
// Get publishers based on filter
|
||||
List<Publisher.Publisher>? filteredPublishers = null;
|
||||
switch (filter)
|
||||
var filteredPublishers = filter switch
|
||||
{
|
||||
case "subscriptions":
|
||||
filteredPublishers = await pub.GetSubscribedPublishers(currentUser.Id);
|
||||
break;
|
||||
case "friends":
|
||||
{
|
||||
filteredPublishers = (await pub.GetUserPublishersBatch(userFriends))
|
||||
.SelectMany(x => x.Value)
|
||||
.DistinctBy(x => x.Id)
|
||||
.ToList();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
"subscriptions" => await pub.GetSubscribedPublishers(currentUser.Id),
|
||||
"friends" => (await pub.GetUserPublishersBatch(userFriends)).SelectMany(x => x.Value)
|
||||
.DistinctBy(x => x.Id)
|
||||
.ToList(),
|
||||
_ => null
|
||||
};
|
||||
|
||||
var filteredPublishersId = filteredPublishers?.Select(e => e.Id).ToList();
|
||||
|
||||
|
@ -9,7 +9,6 @@ namespace DysonNetwork.Sphere.Publisher;
|
||||
public class PublisherService(AppDatabase db, FileReferenceService fileRefService, ICacheService cache)
|
||||
{
|
||||
private const string UserPublishersCacheKey = "accounts:{0}:publishers";
|
||||
private const string UserPublishersBatchCacheKey = "accounts:batch:{0}:publishers";
|
||||
|
||||
public async Task<List<Publisher>> GetUserPublishers(Guid userId)
|
||||
{
|
||||
@ -89,7 +88,7 @@ public class PublisherService(AppDatabase db, FileReferenceService fileRefServic
|
||||
|
||||
|
||||
|
||||
private const string SubscribedPublishersCacheKey = "accounts:{0}:subscribed-publishers";
|
||||
public const string SubscribedPublishersCacheKey = "accounts:{0}:subscribed-publishers";
|
||||
|
||||
public async Task<List<Publisher>> GetSubscribedPublishers(Guid userId)
|
||||
{
|
||||
@ -103,6 +102,7 @@ public class PublisherService(AppDatabase db, FileReferenceService fileRefServic
|
||||
// If not in cache, fetch from a database
|
||||
var publishersId = await db.PublisherSubscriptions
|
||||
.Where(p => p.AccountId == userId)
|
||||
.Where(p => p.Status == SubscriptionStatus.Active)
|
||||
.Select(p => p.PublisherId)
|
||||
.ToListAsync();
|
||||
publishers = await db.Publishers
|
||||
|
@ -1,5 +1,6 @@
|
||||
using DysonNetwork.Sphere.Account;
|
||||
using DysonNetwork.Sphere.Post;
|
||||
using DysonNetwork.Sphere.Storage;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Localization;
|
||||
|
||||
@ -9,7 +10,9 @@ public class PublisherSubscriptionService(
|
||||
AppDatabase db,
|
||||
NotificationService nty,
|
||||
PostService ps,
|
||||
IStringLocalizer<Notification> localizer)
|
||||
IStringLocalizer<Notification> localizer,
|
||||
ICacheService cache
|
||||
)
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks if a subscription exists between the account and publisher
|
||||
@ -158,6 +161,8 @@ public class PublisherSubscriptionService(
|
||||
|
||||
db.PublisherSubscriptions.Add(subscription);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
await cache.RemoveAsync(string.Format(PublisherService.SubscribedPublishersCacheKey, accountId));
|
||||
|
||||
return subscription;
|
||||
}
|
||||
@ -176,6 +181,9 @@ public class PublisherSubscriptionService(
|
||||
|
||||
subscription.Status = SubscriptionStatus.Cancelled;
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
await cache.RemoveAsync(string.Format(PublisherService.SubscribedPublishersCacheKey, accountId));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user