♻️ Refactored publishers
This commit is contained in:
@ -8,6 +8,56 @@ namespace DysonNetwork.Sphere.Publisher;
|
||||
|
||||
public class PublisherService(AppDatabase db, FileService fs, FileReferenceService fileRefService, ICacheService cache)
|
||||
{
|
||||
private const string UserPublishersCacheKey = "accounts:{0}:publishers";
|
||||
|
||||
public async Task<List<Publisher>> GetUserPublishers(Guid userId)
|
||||
{
|
||||
var cacheKey = string.Format(UserPublishersCacheKey, userId);
|
||||
|
||||
// Try to get publishers from the cache first
|
||||
var publishers = await cache.GetAsync<List<Publisher>>(cacheKey);
|
||||
if (publishers is not null)
|
||||
return publishers;
|
||||
|
||||
// If not in cache, fetch from a database
|
||||
var publishersId = await db.PublisherMembers
|
||||
.Where(p => p.AccountId == userId)
|
||||
.Select(p => p.PublisherId)
|
||||
.ToListAsync();
|
||||
publishers = await db.Publishers
|
||||
.Where(p => publishersId.Contains(p.Id))
|
||||
.ToListAsync();
|
||||
|
||||
// Store in a cache for 5 minutes
|
||||
await cache.SetAsync(cacheKey, publishers, TimeSpan.FromMinutes(5));
|
||||
|
||||
return publishers;
|
||||
}
|
||||
|
||||
private const string PublisherMembersCacheKey = "publishers:{0}:members";
|
||||
|
||||
public async Task<List<PublisherMember>> GetPublisherMembers(Guid publisherId)
|
||||
{
|
||||
var cacheKey = string.Format(PublisherMembersCacheKey, publisherId);
|
||||
|
||||
// Try to get members from the cache first
|
||||
var members = await cache.GetAsync<List<PublisherMember>>(cacheKey);
|
||||
if (members is not null)
|
||||
return members;
|
||||
|
||||
// If not in cache, fetch from a database
|
||||
members = await db.PublisherMembers
|
||||
.Where(p => p.PublisherId == publisherId)
|
||||
.Include(p => p.Account)
|
||||
.ThenInclude(p => p.Profile)
|
||||
.ToListAsync();
|
||||
|
||||
// Store in cache for 5 minutes (consistent with other cache durations in the class)
|
||||
await cache.SetAsync(cacheKey, members, TimeSpan.FromMinutes(5));
|
||||
|
||||
return members;
|
||||
}
|
||||
|
||||
public async Task<Publisher> CreateIndividualPublisher(
|
||||
Account.Account account,
|
||||
string? name,
|
||||
|
@ -1,4 +1,5 @@
|
||||
using DysonNetwork.Sphere.Account;
|
||||
using DysonNetwork.Sphere.Post;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Localization;
|
||||
|
||||
@ -7,6 +8,7 @@ namespace DysonNetwork.Sphere.Publisher;
|
||||
public class PublisherSubscriptionService(
|
||||
AppDatabase db,
|
||||
NotificationService nty,
|
||||
PostService ps,
|
||||
IStringLocalizer<Notification> localizer)
|
||||
{
|
||||
/// <summary>
|
||||
@ -41,7 +43,7 @@ public class PublisherSubscriptionService(
|
||||
/// </summary>
|
||||
/// <param name="post">The new post</param>
|
||||
/// <returns>The number of subscribers notified</returns>
|
||||
public async Task<int> NotifySubscribersPostAsync(Post.Post post)
|
||||
public async Task<int> NotifySubscriberPost(Post.Post post)
|
||||
{
|
||||
var subscribers = await db.PublisherSubscriptions
|
||||
.Include(ps => ps.Account)
|
||||
@ -52,11 +54,7 @@ public class PublisherSubscriptionService(
|
||||
return 0;
|
||||
|
||||
// Create notification data
|
||||
var message = !string.IsNullOrEmpty(post.Description)
|
||||
? post.Description?.Length > 40 ? post.Description[..37] + "..." : post.Description
|
||||
: post.Content?.Length > 100
|
||||
? string.Concat(post.Content.AsSpan(0, 97), "...")
|
||||
: post.Content;
|
||||
var (title, message) = ps.ChopPostForNotification(post);
|
||||
|
||||
// Data to include with the notification
|
||||
var data = new Dictionary<string, object>
|
||||
@ -75,8 +73,8 @@ public class PublisherSubscriptionService(
|
||||
await nty.SendNotification(
|
||||
subscription.Account,
|
||||
"posts.new",
|
||||
localizer["New post from {0}", post.Publisher.Name],
|
||||
string.IsNullOrWhiteSpace(post.Title) ? null : post.Title,
|
||||
localizer["PostSubscriptionTitle", post.Publisher.Name, title],
|
||||
null,
|
||||
message,
|
||||
data
|
||||
);
|
||||
|
Reference in New Issue
Block a user