♻️ 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(
Guid publisherId,
Guid postId,
Guid actorId
SnFediverseActor actor,
Guid postId
)
{
var publisher = await db.Publishers
.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 actorUrl = actor.Uri;
var postUrl = $"https://{Domain}/posts/{postId}";
var activityId = $"{actorUrl}/likes/{Guid.NewGuid()}";
@@ -419,23 +401,11 @@ public class ActivityPubDeliveryService(
}
public async Task<bool> SendUndoLikeActivityAsync(
Guid publisherId,
Guid postId,
string likeActivityId
SnFediverseActor actor,
Guid postId
)
{
var publisher = await db.Publishers
.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 actorUrl = actor.Uri;
var postUrl = $"https://{Domain}/posts/{postId}";
var activityId = $"{actorUrl}/undo/{Guid.NewGuid()}";
@@ -454,7 +424,7 @@ public class ActivityPubDeliveryService(
["cc"] = new[] { $"{actorUrl}/followers" }
};
var followers = await GetRemoteFollowersAsync(localActor.Id);
var followers = await GetRemoteFollowersAsync(actor.Id);
foreach (var follower in followers)
{

View File

@@ -57,7 +57,8 @@ public partial class PostService(
}
// 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);
item.ForwardedPost.Content = forwardedPlainText[..embedMaxLength];
item.ForwardedPost.IsTruncated = true;
@@ -490,7 +491,8 @@ public partial class PostService(
}
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.");
post.PinMode = pinMode;
@@ -515,7 +517,8 @@ public partial class PostService(
}
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.");
}
@@ -586,14 +589,14 @@ public partial class PostService(
// Send ActivityPub Like/Undo activities if post's publisher has actor
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)
{
var likerActor = await db.FediverseActors
.FirstOrDefaultAsync(a => a.PublisherId.HasValue && a.PublisherId.Value == reaction.AccountId.Value);
if (likerActor != null)
if (accountActor != null && reaction.Attitude == Shared.Models.PostReactionAttitude.Positive)
{
if (!isRemoving)
{
@@ -606,9 +609,8 @@ public partial class PostService(
var deliveryService = scope.ServiceProvider
.GetRequiredService<ActivityPubDeliveryService>();
await deliveryService.SendLikeActivityToLocalPostAsync(
post.PublisherId.Value,
post.Id,
likerActor.Id
accountActor,
post.Id
);
}
catch (Exception ex)
@@ -628,9 +630,8 @@ public partial class PostService(
var deliveryService = scope.ServiceProvider
.GetRequiredService<ActivityPubDeliveryService>();
await deliveryService.SendUndoLikeActivityAsync(
post.PublisherId.Value,
post.Id,
string.Empty
accountActor,
post.Id
);
}
catch (Exception ex)
@@ -641,7 +642,6 @@ public partial class PostService(
}
}
}
}
if (!isSelfReact)
_ = Task.Run(async () =>
@@ -1160,7 +1160,8 @@ public static class PostQueryExtensions
source = isListing switch
{
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),
_ => source
};
@@ -1171,8 +1172,10 @@ public static class PostQueryExtensions
.Where(e => e.Visibility == Shared.Models.PostVisibility.Public);
return source
.Where(e => (e.PublishedAt != null && now >= e.PublishedAt) || (e.PublisherId.HasValue && publishersId.Contains(e.PublisherId.Value)))
.Where(e => e.Visibility != Shared.Models.PostVisibility.Private || publishersId.Contains(e.PublisherId.Value))
.Where(e => (e.PublishedAt != null && now >= e.PublishedAt) ||
(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 ||
(e.Publisher.AccountId != null && userFriends.Contains(e.Publisher.AccountId.Value)) ||
publishersId.Contains(e.PublisherId.Value));