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