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
|
|
|
const int MaxRevisions = 20;
|
|
|
|
|
|
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-01 18:35:20 +02:00
|
|
|
post.Poll = note.Poll ?? post.Poll;
|
2026-10-01 18:53:28 +02:00
|
|
|
post.QuotePolicy = note.QuotePolicy;
|
2026-10-01 18:35:20 +02:00
|
|
|
post.Video = note.Video ?? post.Video;
|
|
|
|
|
post.Audio = note.Audio ?? post.Audio;
|
|
|
|
|
post.Event = note.Event ?? post.Event;
|
|
|
|
|
if (!IsEdit(note, post))
|
|
|
|
|
{
|
2026-10-03 11:04:42 +02:00
|
|
|
Arrival.Accept("refresh");
|
2026-10-01 18:35:20 +02:00
|
|
|
await DB.Default.SaveAsync(post, token);
|
|
|
|
|
await _records.Revise(note, Id(activity), token);
|
2026-10-01 18:53:28 +02:00
|
|
|
await Requote(post, note, token);
|
2026-10-01 18:35:20 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-03 11:04:42 +02:00
|
|
|
Arrival.Accept("edit");
|
2026-10-01 11:17:14 +02:00
|
|
|
post.Revisions.Add(new PostRevision
|
|
|
|
|
{
|
|
|
|
|
Title = post.Title,
|
|
|
|
|
SpoilerText = post.SpoilerText,
|
|
|
|
|
ContentHtml = post.ContentHtml,
|
|
|
|
|
HasContentWarning = post.HasContentWarning,
|
|
|
|
|
EditedAt = post.EditedAt ?? post.CreationDate
|
|
|
|
|
});
|
|
|
|
|
if (post.Revisions.Count > MaxRevisions)
|
|
|
|
|
post.Revisions.RemoveRange(0, post.Revisions.Count - MaxRevisions);
|
|
|
|
|
|
|
|
|
|
var mentions = new List<PostMention>();
|
|
|
|
|
foreach (var mention in note.Mentions)
|
|
|
|
|
{
|
|
|
|
|
var local = await _localActors.FindByUri(mention.ActorURI, token);
|
|
|
|
|
mentions.Add(new PostMention { ActorURI = mention.ActorURI, Handle = mention.Handle, IsLocal = local != default, AccountId = local?.Id });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
post.Title = note.Title;
|
|
|
|
|
post.SpoilerText = note.SpoilerText;
|
2026-10-01 17:39:58 +02:00
|
|
|
post.Excerpt = note.Excerpt;
|
2026-10-01 17:53:59 +02:00
|
|
|
post.Source = note.Source;
|
2026-10-01 18:35:20 +02:00
|
|
|
post.Emojis = note.Emojis.ToList();
|
2026-10-01 17:53:59 +02:00
|
|
|
post.CoverURL = note.CoverURL;
|
|
|
|
|
post.Link = note.Link;
|
2026-10-01 11:17:14 +02:00
|
|
|
post.HasContentWarning = note.Sensitive;
|
|
|
|
|
post.Text = note.ContentHtml;
|
|
|
|
|
post.ContentHtml = note.ContentHtml;
|
|
|
|
|
post.Language = note.Language;
|
|
|
|
|
post.Tags = note.Tags.ToList();
|
|
|
|
|
post.Mentions = mentions;
|
|
|
|
|
post.Media = note.Attachments.ToList();
|
|
|
|
|
post.EditedAt = note.Updated ?? DateTime.UtcNow;
|
|
|
|
|
post.UpdateDate = DateTime.UtcNow;
|
|
|
|
|
await DB.Default.SaveAsync(post, token);
|
2026-10-01 17:49:22 +02:00
|
|
|
await _records.Revise(note, Id(activity), token);
|
2026-10-01 18:53:28 +02:00
|
|
|
await Requote(post, note, token);
|
2026-10-01 12:30:57 +02:00
|
|
|
if (!string.IsNullOrEmpty(post.GroupId) && await _localActors.FindById(Models.Federation.LocalActorKind.Group, post.GroupId, token) is { IsCircle: false } community)
|
|
|
|
|
await _groups.Announce(community, activity.AsObject(), post.ObjectURI, isNewPost: false, token);
|
2026-10-01 11:17:14 +02:00
|
|
|
}
|
2026-10-01 18:35:20 +02:00
|
|
|
|
|
|
|
|
static bool IsEdit(NoteDocument note, PostEntity post) =>
|
|
|
|
|
note.Updated is { } updated && updated > (post.EditedAt ?? post.CreationDate);
|
2026-10-01 18:53:28 +02:00
|
|
|
|
|
|
|
|
async Task Requote(PostEntity post, NoteDocument note, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
if (note.QuoteUri != post.QuoteURI || note.QuoteAuthorization != post.QuoteAuthorizationURI || post.QuoteState != QuoteState.Accepted)
|
|
|
|
|
await _quotes.Resolve(post, note, token);
|
|
|
|
|
}
|
2026-10-01 11:12:34 +02:00
|
|
|
}
|
|
|
|
|
}
|