💄 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

@@ -105,18 +105,6 @@ namespace DysonNetwork.Sphere.Resources.Localization {
} }
} }
internal static string PostReplyBody {
get {
return ResourceManager.GetString("PostReplyBody", resourceCulture);
}
}
internal static string PostReplyContentBody {
get {
return ResourceManager.GetString("PostReplyContentBody", resourceCulture);
}
}
internal static string PostOnlyMedia { internal static string PostOnlyMedia {
get { get {
return ResourceManager.GetString("PostOnlyMedia", resourceCulture); return ResourceManager.GetString("PostOnlyMedia", resourceCulture);

View File

@@ -53,12 +53,6 @@
<data name="PostReplyTitle" xml:space="preserve"> <data name="PostReplyTitle" xml:space="preserve">
<value>{0} replied your post</value> <value>{0} replied your post</value>
</data> </data>
<data name="PostReplyBody">
<value>{0} replied: {1}</value>
</data>
<data name="PostReplyContentBody">
<value>{0} replied post {1}: {2}</value>
</data>
<data name="PostOnlyMedia" xml:space="preserve"> <data name="PostOnlyMedia" xml:space="preserve">
<value>shared media</value> <value>shared media</value>
</data> </data>

View File

@@ -45,12 +45,6 @@
<data name="PostReplyTitle" xml:space="preserve"> <data name="PostReplyTitle" xml:space="preserve">
<value>{0} 回复了你的帖子</value> <value>{0} 回复了你的帖子</value>
</data> </data>
<data name="PostReplyBody">
<value>{0}{1}</value>
</data>
<data name="PostReplyContentBody">
<value>{0} 回复了帖子 {1}: {2}</value>
</data>
<data name="PostOnlyMedia" xml:space="preserve"> <data name="PostOnlyMedia" xml:space="preserve">
<value>分享媒体</value> <value>分享媒体</value>
</data> </data>

View File

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

View File

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