♻️ Re-built like activity sending

This commit is contained in:
2026-01-01 11:09:25 +08:00
parent 923ec0a157
commit a72dbcfc0c
2 changed files with 66 additions and 93 deletions

View File

@@ -370,29 +370,11 @@ public class ActivityPubDeliveryService(
} }
public async Task<bool> SendLikeActivityToLocalPostAsync( public async Task<bool> SendLikeActivityToLocalPostAsync(
Guid publisherId, SnFediverseActor actor,
Guid postId, Guid postId
Guid actorId
) )
{ {
var publisher = await db.Publishers var actorUrl = actor.Uri;
.Include(p => p.Members)
.FirstOrDefaultAsync(p => p.Id == publisherId);
if (publisher == null)
return false;
var publisherActor = await GetLocalActorAsync(publisherId);
if (publisherActor == null)
return false;
var actor = await db.FediverseActors
.FirstOrDefaultAsync(a => a.Id == actorId);
if (actor == null)
return false;
var actorUrl = publisherActor.Uri;
var postUrl = $"https://{Domain}/posts/{postId}"; var postUrl = $"https://{Domain}/posts/{postId}";
var activityId = $"{actorUrl}/likes/{Guid.NewGuid()}"; var activityId = $"{actorUrl}/likes/{Guid.NewGuid()}";
@@ -419,23 +401,11 @@ public class ActivityPubDeliveryService(
} }
public async Task<bool> SendUndoLikeActivityAsync( public async Task<bool> SendUndoLikeActivityAsync(
Guid publisherId, SnFediverseActor actor,
Guid postId, Guid postId
string likeActivityId
) )
{ {
var publisher = await db.Publishers var actorUrl = actor.Uri;
.Include(p => p.Members)
.FirstOrDefaultAsync(p => p.Id == publisherId);
if (publisher == null)
return false;
var localActor = await GetLocalActorAsync(publisherId);
if (localActor == null)
return false;
var actorUrl = localActor.Uri;
var postUrl = $"https://{Domain}/posts/{postId}"; var postUrl = $"https://{Domain}/posts/{postId}";
var activityId = $"{actorUrl}/undo/{Guid.NewGuid()}"; var activityId = $"{actorUrl}/undo/{Guid.NewGuid()}";
@@ -454,7 +424,7 @@ public class ActivityPubDeliveryService(
["cc"] = new[] { $"{actorUrl}/followers" } ["cc"] = new[] { $"{actorUrl}/followers" }
}; };
var followers = await GetRemoteFollowersAsync(localActor.Id); var followers = await GetRemoteFollowersAsync(actor.Id);
foreach (var follower in followers) foreach (var follower in followers)
{ {

View File

@@ -57,7 +57,8 @@ public partial class PostService(
} }
// Truncate forwarded post content with shorter embed length // Truncate forwarded post content with shorter embed length
if (item.ForwardedPost?.Content == null || Markdown.ToPlainText(item.ForwardedPost.Content).Length <= embedMaxLength) continue; if (item.ForwardedPost?.Content == null ||
Markdown.ToPlainText(item.ForwardedPost.Content).Length <= embedMaxLength) continue;
var forwardedPlainText = Markdown.ToPlainText(item.ForwardedPost.Content); var forwardedPlainText = Markdown.ToPlainText(item.ForwardedPost.Content);
item.ForwardedPost.Content = forwardedPlainText[..embedMaxLength]; item.ForwardedPost.Content = forwardedPlainText[..embedMaxLength];
item.ForwardedPost.IsTruncated = true; item.ForwardedPost.IsTruncated = true;
@@ -490,7 +491,8 @@ public partial class PostService(
} }
else else
{ {
if (post.PublisherId == null || !await ps.IsMemberWithRole(post.PublisherId.Value, accountId, Shared.Models.PublisherMemberRole.Editor)) if (post.PublisherId == null || !await ps.IsMemberWithRole(post.PublisherId.Value, accountId,
Shared.Models.PublisherMemberRole.Editor))
throw new InvalidOperationException("Only editors can pin replies."); throw new InvalidOperationException("Only editors can pin replies.");
post.PinMode = pinMode; post.PinMode = pinMode;
@@ -515,7 +517,8 @@ public partial class PostService(
} }
else else
{ {
if (post.PublisherId == null || !await ps.IsMemberWithRole(post.PublisherId.Value, accountId, Shared.Models.PublisherMemberRole.Editor)) if (post.PublisherId == null || !await ps.IsMemberWithRole(post.PublisherId.Value, accountId,
Shared.Models.PublisherMemberRole.Editor))
throw new InvalidOperationException("Only editors can unpin posts."); throw new InvalidOperationException("Only editors can unpin posts.");
} }
@@ -586,14 +589,14 @@ public partial class PostService(
// Send ActivityPub Like/Undo activities if post's publisher has actor // Send ActivityPub Like/Undo activities if post's publisher has actor
if (post.PublisherId.HasValue && reaction.AccountId.HasValue) if (post.PublisherId.HasValue && reaction.AccountId.HasValue)
{ {
var publisherActor = await apDelivery.GetLocalActorAsync(post.PublisherId.Value); var accountPublisher = await db.Publishers
.Where(p => p.Members.Any(m => m.AccountId == reaction.AccountId.Value))
.FirstOrDefaultAsync();
var accountActor = accountPublisher is null
? null
: await apDelivery.GetLocalActorAsync(accountPublisher.Id);
if (publisherActor != null && reaction.Attitude == Shared.Models.PostReactionAttitude.Positive) if (accountActor != null && reaction.Attitude == Shared.Models.PostReactionAttitude.Positive)
{
var likerActor = await db.FediverseActors
.FirstOrDefaultAsync(a => a.PublisherId.HasValue && a.PublisherId.Value == reaction.AccountId.Value);
if (likerActor != null)
{ {
if (!isRemoving) if (!isRemoving)
{ {
@@ -606,9 +609,8 @@ public partial class PostService(
var deliveryService = scope.ServiceProvider var deliveryService = scope.ServiceProvider
.GetRequiredService<ActivityPubDeliveryService>(); .GetRequiredService<ActivityPubDeliveryService>();
await deliveryService.SendLikeActivityToLocalPostAsync( await deliveryService.SendLikeActivityToLocalPostAsync(
post.PublisherId.Value, accountActor,
post.Id, post.Id
likerActor.Id
); );
} }
catch (Exception ex) catch (Exception ex)
@@ -628,9 +630,8 @@ public partial class PostService(
var deliveryService = scope.ServiceProvider var deliveryService = scope.ServiceProvider
.GetRequiredService<ActivityPubDeliveryService>(); .GetRequiredService<ActivityPubDeliveryService>();
await deliveryService.SendUndoLikeActivityAsync( await deliveryService.SendUndoLikeActivityAsync(
post.PublisherId.Value, accountActor,
post.Id, post.Id
string.Empty
); );
} }
catch (Exception ex) catch (Exception ex)
@@ -641,7 +642,6 @@ public partial class PostService(
} }
} }
} }
}
if (!isSelfReact) if (!isSelfReact)
_ = Task.Run(async () => _ = Task.Run(async () =>
@@ -1160,7 +1160,8 @@ public static class PostQueryExtensions
source = isListing switch source = isListing switch
{ {
true when currentUser is not null => source.Where(e => true when currentUser is not null => source.Where(e =>
e.Visibility != Shared.Models.PostVisibility.Unlisted || (e.PublisherId.HasValue && publishersId.Contains(e.PublisherId.Value))), e.Visibility != Shared.Models.PostVisibility.Unlisted ||
(e.PublisherId.HasValue && publishersId.Contains(e.PublisherId.Value))),
true => source.Where(e => e.Visibility != Shared.Models.PostVisibility.Unlisted), true => source.Where(e => e.Visibility != Shared.Models.PostVisibility.Unlisted),
_ => source _ => source
}; };
@@ -1171,8 +1172,10 @@ public static class PostQueryExtensions
.Where(e => e.Visibility == Shared.Models.PostVisibility.Public); .Where(e => e.Visibility == Shared.Models.PostVisibility.Public);
return source return source
.Where(e => (e.PublishedAt != null && now >= e.PublishedAt) || (e.PublisherId.HasValue && publishersId.Contains(e.PublisherId.Value))) .Where(e => (e.PublishedAt != null && now >= e.PublishedAt) ||
.Where(e => e.Visibility != Shared.Models.PostVisibility.Private || publishersId.Contains(e.PublisherId.Value)) (e.PublisherId.HasValue && publishersId.Contains(e.PublisherId.Value)))
.Where(e => e.Visibility != Shared.Models.PostVisibility.Private ||
publishersId.Contains(e.PublisherId.Value))
.Where(e => e.Visibility != Shared.Models.PostVisibility.Friends || .Where(e => e.Visibility != Shared.Models.PostVisibility.Friends ||
(e.Publisher.AccountId != null && userFriends.Contains(e.Publisher.AccountId.Value)) || (e.Publisher.AccountId != null && userFriends.Contains(e.Publisher.AccountId.Value)) ||
publishersId.Contains(e.PublisherId.Value)); publishersId.Contains(e.PublisherId.Value));