💄 Optimize the post service

This commit is contained in:
2026-01-08 23:12:01 +08:00
parent c16add5dfe
commit cb04e53b7e
5 changed files with 25 additions and 44 deletions

View File

@@ -57,6 +57,7 @@ public partial class PostService(
{
continue;
}
if (plainText.Length > maxLength)
{
item.Content = plainText.Substring(0, maxLength);
@@ -81,6 +82,7 @@ public partial class PostService(
{
continue;
}
if (plainText.Length > embedMaxLength)
{
item.RepliedPost.Content = plainText.Substring(0, embedMaxLength);
@@ -105,6 +107,7 @@ public partial class PostService(
{
continue;
}
if (plainText.Length > embedMaxLength)
{
item.ForwardedPost.Content = plainText.Substring(0, embedMaxLength);
@@ -124,8 +127,9 @@ public partial class PostService(
? string.Concat(post.Content.AsSpan(0, 97), "...")
: post.Content;
var title = post.Title ?? (post.Content?.Length >= 10 ? post.Content[..10] + "..." : post.Content);
content ??= localizer["PostOnlyMedia"];
title ??= localizer["PostOnlyMedia"];
if (string.IsNullOrWhiteSpace(content))
content = localizer["PostOnlyMedia"];
return (title, content);
}
@@ -219,13 +223,13 @@ public partial class PostService(
using var scope = factory.CreateScope();
var pub = scope.ServiceProvider.GetRequiredService<Publisher.PublisherService>();
var nty = scope.ServiceProvider.GetRequiredService<RingService.RingServiceClient>();
var accounts = scope.ServiceProvider.GetRequiredService<AccountService.AccountServiceClient>();
var notifyTargets = scope.ServiceProvider.GetRequiredService<AccountService.AccountServiceClient>();
try
{
var members = await pub.GetPublisherMembers(post.RepliedPost.PublisherId!.Value);
var queryRequest = new GetAccountBatchRequest();
queryRequest.Id.AddRange(members.Select(m => m.AccountId.ToString()));
var queryResponse = await accounts.GetAccountBatchAsync(queryRequest);
var queryResponse = await notifyTargets.GetAccountBatchAsync(queryRequest);
foreach (var member in queryResponse.Accounts)
{
if (member is null) continue;
@@ -238,10 +242,7 @@ public partial class PostService(
{
Topic = "post.replies",
Title = localizer["PostReplyTitle", sender!.Nick],
Body = string.IsNullOrWhiteSpace(post.Title)
? localizer["PostReplyBody", sender.Nick, ChopPostForNotification(post).content]
: localizer["PostReplyContentBody", sender.Nick, post.Title,
ChopPostForNotification(post).content],
Body = ChopPostForNotification(post).content,
IsSavable = true,
ActionUri = $"/posts/{post.Id}"
}
@@ -649,7 +650,8 @@ public partial class PostService(
: await objFactory.GetLocalActorAsync(accountPublisher.Id);
var publisherActor = await objFactory.GetLocalActorAsync(post.PublisherId.Value);
if (accountActor != null && publisherActor != null && reaction.Attitude == Shared.Models.PostReactionAttitude.Positive)
if (accountActor != null && publisherActor != null &&
reaction.Attitude == Shared.Models.PostReactionAttitude.Positive)
{
if (!isRemoving)
{
@@ -1239,4 +1241,4 @@ public static class PostQueryExtensions
(e.Publisher.AccountId != null && userFriends.Contains(e.Publisher.AccountId.Value)) ||
publishersId.Contains(e.PublisherId.Value));
}
}
}

View File

@@ -26,9 +26,9 @@ public class PublisherSubscriptionService(
public async Task<bool> SubscriptionExistsAsync(Guid accountId, Guid publisherId)
{
return await db.PublisherSubscriptions
.AnyAsync(ps => ps.AccountId == accountId &&
ps.PublisherId == publisherId &&
ps.Status == PublisherSubscriptionStatus.Active);
.AnyAsync(p => p.AccountId == accountId &&
p.PublisherId == publisherId &&
p.Status == PublisherSubscriptionStatus.Active);
}
/// <summary>
@@ -40,8 +40,8 @@ public class PublisherSubscriptionService(
public async Task<SnPublisherSubscription?> GetSubscriptionAsync(Guid accountId, Guid publisherId)
{
return await db.PublisherSubscriptions
.Include(ps => ps.Publisher)
.FirstOrDefaultAsync(ps => ps.AccountId == accountId && ps.PublisherId == publisherId);
.Include(p => p.Publisher)
.FirstOrDefaultAsync(p => p.AccountId == accountId && p.PublisherId == publisherId);
}
/// <summary>
@@ -51,6 +51,8 @@ public class PublisherSubscriptionService(
/// <returns>The number of subscribers notified</returns>
public async Task<int> NotifySubscriberPost(SnPost post)
{
if (!post.PublisherId.HasValue || post.Publisher is null)
return 0;
if (post.RepliedPostId is not null)
return 0;
if (post.Visibility != Shared.Models.PostVisibility.Public)
@@ -63,7 +65,7 @@ public class PublisherSubscriptionService(
var data = new Dictionary<string, object>
{
["post_id"] = post.Id,
["publisher_id"] = post.Publisher.Id.ToString()
["publisher_id"] = post.PublisherId.Value.ToString()
};
if (post.Attachments.Any(p => p.MimeType?.StartsWith("image/") ?? false))
@@ -71,7 +73,7 @@ public class PublisherSubscriptionService(
post.Attachments
.Where(p => p.MimeType?.StartsWith("image/") ?? false)
.Select(p => p.Id).First();
if (post.Publisher.Picture is not null) data["pfp"] = post.Publisher.Picture.Id;
if (post.Publisher?.Picture is not null) data["pfp"] = post.Publisher.Picture.Id;
// Gather subscribers
var subscribers = await db.PublisherSubscriptions
@@ -118,7 +120,7 @@ public class PublisherSubscriptionService(
var notification = new PushNotification
{
Topic = "posts.new",
Title = localizer["PostSubscriptionTitle", post.Publisher.Nick, title],
Title = localizer["PostSubscriptionTitle", post.Publisher!.Nick, title],
Body = message,
Meta = GrpcTypeHelper.ConvertObjectToByteString(data),
IsSavable = true,
@@ -147,8 +149,8 @@ public class PublisherSubscriptionService(
public async Task<List<SnPublisherSubscription>> GetAccountSubscriptionsAsync(Guid accountId)
{
return await db.PublisherSubscriptions
.Include(ps => ps.Publisher)
.Where(ps => ps.AccountId == accountId && ps.Status == PublisherSubscriptionStatus.Active)
.Include(p => p.Publisher)
.Where(p => p.AccountId == accountId && p.Status == PublisherSubscriptionStatus.Active)
.ToListAsync();
}
@@ -160,7 +162,8 @@ public class PublisherSubscriptionService(
public async Task<List<SnPublisherSubscription>> GetPublisherSubscribersAsync(Guid publisherId)
{
return await db.PublisherSubscriptions
.Where(ps => ps.PublisherId == publisherId && ps.Status == PublisherSubscriptionStatus.Active)
.Where(p => p.PublisherId == publisherId)
.Where(p => p.Status == PublisherSubscriptionStatus.Active)
.ToListAsync();
}