2026-10-01 11:12:34 +02:00
|
|
|
using MongoDB.Entities;
|
|
|
|
|
|
|
|
|
|
using PrivaPub.Federation.Actors;
|
2026-10-01 18:53:28 +02:00
|
|
|
using PrivaPub.Domain.Statuses;
|
2026-10-01 11:12:34 +02:00
|
|
|
using PrivaPub.Federation.Objects;
|
2026-10-01 12:30:57 +02:00
|
|
|
using PrivaPub.Federation.Outbox;
|
2026-10-01 11:12:34 +02:00
|
|
|
using PrivaPub.Models.Post;
|
|
|
|
|
using PrivaPub.Models.User;
|
|
|
|
|
using PrivaPub.StaticServices;
|
|
|
|
|
|
|
|
|
|
using System.Text.Json.Nodes;
|
|
|
|
|
|
|
|
|
|
using static PrivaPub.Federation.Objects.ActivityJson;
|
|
|
|
|
|
|
|
|
|
using PostEntity = PrivaPub.Models.Post.Post;
|
|
|
|
|
|
|
|
|
|
namespace PrivaPub.Federation.Inbox.Handlers
|
|
|
|
|
{
|
|
|
|
|
public class UpdateHandler : IActivityHandler
|
|
|
|
|
{
|
2026-10-01 11:17:14 +02:00
|
|
|
|
2026-10-01 11:12:34 +02:00
|
|
|
readonly DbEntities _dbEntities;
|
|
|
|
|
readonly ILocalActorService _localActors;
|
|
|
|
|
readonly IRemoteActorService _remoteActors;
|
2026-10-01 12:30:57 +02:00
|
|
|
readonly IGroupDistributor _groups;
|
2026-10-01 17:49:22 +02:00
|
|
|
readonly IObjectRecords _records;
|
2026-10-01 18:53:28 +02:00
|
|
|
readonly IQuoteService _quotes;
|
2026-10-01 11:12:34 +02:00
|
|
|
|
2026-10-01 17:49:22 +02:00
|
|
|
public UpdateHandler(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IGroupDistributor groups,
|
2026-10-01 18:53:28 +02:00
|
|
|
IObjectRecords records, IQuoteService quotes)
|
2026-10-01 11:12:34 +02:00
|
|
|
{
|
2026-10-01 18:53:28 +02:00
|
|
|
_quotes = quotes;
|
2026-10-01 17:49:22 +02:00
|
|
|
_records = records;
|
2026-10-01 12:30:57 +02:00
|
|
|
_groups = groups;
|
2026-10-01 11:12:34 +02:00
|
|
|
_dbEntities = dbEntities;
|
|
|
|
|
_localActors = localActors;
|
|
|
|
|
_remoteActors = remoteActors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Type => "Update";
|
|
|
|
|
|
|
|
|
|
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
|
|
|
|
|
{
|
2026-10-01 11:17:14 +02:00
|
|
|
var inner = activity["object"];
|
2026-10-01 11:12:34 +02:00
|
|
|
if (Id(inner) == actor.ActorURI)
|
|
|
|
|
{
|
|
|
|
|
await _remoteActors.GetActor(actor.ActorURI, refresh: true, token);
|
2026-10-03 11:04:42 +02:00
|
|
|
Arrival.Accept("actor-refresh");
|
|
|
|
|
Arrival.About(actor.AvatarType.ToString());
|
2026-10-01 11:12:34 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2026-10-01 11:17:14 +02:00
|
|
|
if (inner is not JsonObject || !Origin.Same(Id(inner), actor.ActorURI))
|
2026-10-03 11:04:42 +02:00
|
|
|
{
|
|
|
|
|
Arrival.Drop("cross-origin");
|
2026-10-01 11:17:14 +02:00
|
|
|
return;
|
2026-10-03 11:04:42 +02:00
|
|
|
}
|
2026-10-01 11:17:14 +02:00
|
|
|
var note = NoteParser.Parse(inner);
|
|
|
|
|
if (note == default || note.AttributedTo != actor.ActorURI)
|
2026-10-03 11:04:42 +02:00
|
|
|
{
|
|
|
|
|
Arrival.Drop(note == default ? "unparseable" : "misattributed");
|
2026-10-01 11:12:34 +02:00
|
|
|
return;
|
2026-10-03 11:04:42 +02:00
|
|
|
}
|
2026-10-01 11:12:34 +02:00
|
|
|
|
2026-10-01 11:17:14 +02:00
|
|
|
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == note.Id && p.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
|
|
|
|
|
if (post == default || post.DeletedAt.HasValue)
|
2026-10-03 11:04:42 +02:00
|
|
|
{
|
|
|
|
|
Arrival.Drop("unknown-object");
|
2026-10-01 11:17:14 +02:00
|
|
|
return;
|
2026-10-03 11:04:42 +02:00
|
|
|
}
|
|
|
|
|
Arrival.About(note.Type, post.Visibility, post.CreationDate);
|
2026-10-01 11:17:14 +02:00
|
|
|
|
2026-10-03 11:15:21 +02:00
|
|
|
var edited = await RemoteEdits.Apply(post, note, Id(activity), _localActors, _records, _quotes, token);
|
|
|
|
|
Arrival.Accept(edited ? "edit" : "refresh");
|
|
|
|
|
if (edited && !string.IsNullOrEmpty(post.GroupId) && await _localActors.FindById(Models.Federation.LocalActorKind.Group, post.GroupId, token) is { IsCircle: false } community)
|
2026-10-01 12:30:57 +02:00
|
|
|
await _groups.Announce(community, activity.AsObject(), post.ObjectURI, isNewPost: false, token);
|
2026-10-01 11:17:14 +02:00
|
|
|
}
|
2026-10-01 11:12:34 +02:00
|
|
|
}
|
|
|
|
|
}
|