2026-10-04 03:56:18 +02:00
|
|
|
using MongoDB.Entities;
|
|
|
|
|
|
2026-10-01 12:30:57 +02:00
|
|
|
using PrivaPub.Federation.Actors;
|
|
|
|
|
using PrivaPub.Federation.Rendering;
|
2026-10-04 03:56:18 +02:00
|
|
|
using PrivaPub.Models.Federation;
|
2026-10-01 12:30:57 +02:00
|
|
|
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Json.Nodes;
|
|
|
|
|
|
|
|
|
|
namespace PrivaPub.Federation.Outbox
|
|
|
|
|
{
|
|
|
|
|
public interface IGroupDistributor
|
|
|
|
|
{
|
|
|
|
|
Task Announce(LocalActor group, JsonObject activity, string objectUri, bool isNewPost, CancellationToken token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class GroupDistributor : IGroupDistributor
|
|
|
|
|
{
|
2026-10-04 03:56:18 +02:00
|
|
|
// so the announce's id resolves (PeasantsController.Grunt); a repeat of the same announce keeps the first
|
|
|
|
|
static async Task Keep(LocalActor group, JsonObject announce, string objectUri, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var body = (JsonObject)announce.DeepClone();
|
|
|
|
|
body.Remove("@context");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await DB.Default.SaveAsync(new GroupAnnouncement
|
|
|
|
|
{
|
|
|
|
|
GroupId = group.Id,
|
|
|
|
|
ActivityURI = body["id"]!.GetValue<string>(),
|
|
|
|
|
ObjectURI = objectUri,
|
|
|
|
|
Body = body.ToJsonString()
|
|
|
|
|
}, token);
|
|
|
|
|
}
|
|
|
|
|
catch (MongoDB.Driver.MongoWriteException ex) when (ex.WriteError?.Category == MongoDB.Driver.ServerErrorCategory.DuplicateKey)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-01 12:30:57 +02:00
|
|
|
readonly IDeliveryService _delivery;
|
|
|
|
|
|
|
|
|
|
public GroupDistributor(IDeliveryService delivery)
|
|
|
|
|
{
|
|
|
|
|
_delivery = delivery;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Announce(LocalActor group, JsonObject activity, string objectUri, bool isNewPost, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
if (group is not { IsCircle: false, Kind: Models.Federation.LocalActorKind.Group })
|
|
|
|
|
return;
|
|
|
|
|
var embedded = (JsonObject)activity.DeepClone();
|
|
|
|
|
embedded.Remove("@context");
|
|
|
|
|
embedded["audience"] ??= group.Uri;
|
|
|
|
|
var key = Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(activity["id"]?.GetValue<string>() ?? embedded.ToJsonString())))[..24];
|
|
|
|
|
var announce = new JsonObject
|
|
|
|
|
{
|
|
|
|
|
["@context"] = ActivityPubRenderer.Context(),
|
|
|
|
|
["id"] = group.ActivityUri($"announce-{key}"),
|
|
|
|
|
["type"] = "Announce",
|
|
|
|
|
["actor"] = group.Uri,
|
|
|
|
|
["to"] = new JsonArray(ActivityPubRenderer.Public),
|
|
|
|
|
["cc"] = new JsonArray(group.Followers),
|
|
|
|
|
["audience"] = group.Uri,
|
|
|
|
|
["object"] = embedded
|
|
|
|
|
};
|
2026-10-03 14:24:15 +02:00
|
|
|
// Every follower, the author's own server included: Lemmy 1.0 keeps its user's post pending until the community
|
|
|
|
|
// announces it back, and clears that before it answers the echo 400 ("Object is not remote").
|
2026-10-01 12:30:57 +02:00
|
|
|
await _delivery.EnqueueToFollowers(group, announce, token);
|
2026-10-04 03:56:18 +02:00
|
|
|
await Keep(group, announce, objectUri, token);
|
2026-10-01 12:30:57 +02:00
|
|
|
if (isNewPost && !string.IsNullOrEmpty(objectUri))
|
2026-10-04 03:56:18 +02:00
|
|
|
{
|
|
|
|
|
var boost = ActivityPubRenderer.Announce(group, objectUri, $"announce-object-{key}");
|
|
|
|
|
await _delivery.EnqueueToFollowers(group, boost, token);
|
|
|
|
|
await Keep(group, boost, objectUri, token);
|
|
|
|
|
}
|
2026-10-01 12:30:57 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|