♻️ Re-built like activity sending
This commit is contained in:
@@ -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)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -545,13 +548,13 @@ public partial class PostService(
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
var isExistingReaction = reaction.AccountId.HasValue &&
|
var isExistingReaction = reaction.AccountId.HasValue &&
|
||||||
await db.Set<SnPostReaction>()
|
await db.Set<SnPostReaction>()
|
||||||
.AnyAsync(r => r.PostId == post.Id && r.AccountId == reaction.AccountId.Value);
|
.AnyAsync(r => r.PostId == post.Id && r.AccountId == reaction.AccountId.Value);
|
||||||
|
|
||||||
if (isRemoving)
|
if (isRemoving)
|
||||||
await db.PostReactions
|
await db.PostReactions
|
||||||
.Where(r => r.PostId == post.Id && r.Symbol == reaction.Symbol &&
|
.Where(r => r.PostId == post.Id && r.Symbol == reaction.Symbol &&
|
||||||
reaction.AccountId.HasValue && r.AccountId == reaction.AccountId.Value)
|
reaction.AccountId.HasValue && r.AccountId == reaction.AccountId.Value)
|
||||||
.ExecuteDeleteAsync();
|
.ExecuteDeleteAsync();
|
||||||
else
|
else
|
||||||
db.PostReactions.Add(reaction);
|
db.PostReactions.Add(reaction);
|
||||||
@@ -586,59 +589,56 @@ 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
|
if (!isRemoving)
|
||||||
.FirstOrDefaultAsync(a => a.PublisherId.HasValue && a.PublisherId.Value == reaction.AccountId.Value);
|
|
||||||
|
|
||||||
if (likerActor != null)
|
|
||||||
{
|
{
|
||||||
if (!isRemoving)
|
// Sending Like - deliver to publisher's remote followers
|
||||||
|
_ = Task.Run(async () =>
|
||||||
{
|
{
|
||||||
// Sending Like - deliver to publisher's remote followers
|
try
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
{
|
||||||
try
|
using var scope = factory.CreateScope();
|
||||||
{
|
var deliveryService = scope.ServiceProvider
|
||||||
using var scope = factory.CreateScope();
|
.GetRequiredService<ActivityPubDeliveryService>();
|
||||||
var deliveryService = scope.ServiceProvider
|
await deliveryService.SendLikeActivityToLocalPostAsync(
|
||||||
.GetRequiredService<ActivityPubDeliveryService>();
|
accountActor,
|
||||||
await deliveryService.SendLikeActivityToLocalPostAsync(
|
post.Id
|
||||||
post.PublisherId.Value,
|
);
|
||||||
post.Id,
|
}
|
||||||
likerActor.Id
|
catch (Exception ex)
|
||||||
);
|
{
|
||||||
}
|
logger.LogError($"Error sending ActivityPub Like: {ex.Message}");
|
||||||
catch (Exception ex)
|
}
|
||||||
{
|
});
|
||||||
logger.LogError($"Error sending ActivityPub Like: {ex.Message}");
|
}
|
||||||
}
|
else
|
||||||
});
|
{
|
||||||
}
|
// Sending Undo Like - deliver to publisher's remote followers
|
||||||
else
|
_ = Task.Run(async () =>
|
||||||
{
|
{
|
||||||
// Sending Undo Like - deliver to publisher's remote followers
|
try
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
{
|
||||||
try
|
using var scope = factory.CreateScope();
|
||||||
{
|
var deliveryService = scope.ServiceProvider
|
||||||
using var scope = factory.CreateScope();
|
.GetRequiredService<ActivityPubDeliveryService>();
|
||||||
var deliveryService = scope.ServiceProvider
|
await deliveryService.SendUndoLikeActivityAsync(
|
||||||
.GetRequiredService<ActivityPubDeliveryService>();
|
accountActor,
|
||||||
await deliveryService.SendUndoLikeActivityAsync(
|
post.Id
|
||||||
post.PublisherId.Value,
|
);
|
||||||
post.Id,
|
}
|
||||||
string.Empty
|
catch (Exception ex)
|
||||||
);
|
{
|
||||||
}
|
logger.LogError($"Error sending ActivityPub Undo Like: {ex.Message}");
|
||||||
catch (Exception ex)
|
}
|
||||||
{
|
});
|
||||||
logger.LogError($"Error sending ActivityPub Undo Like: {ex.Message}");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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,10 +1172,12 @@ 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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user