2026-10-05 20:57:43 +02:00
|
|
|
using MongoDB.Driver;
|
2026-10-05 18:39:46 +02:00
|
|
|
using MongoDB.Entities;
|
|
|
|
|
|
2026-10-05 20:57:43 +02:00
|
|
|
using PrivaPub.Domain.Relationships;
|
|
|
|
|
using PrivaPub.Domain.Social;
|
2026-10-05 18:39:46 +02:00
|
|
|
using PrivaPub.Federation.Actors;
|
2026-10-05 20:57:43 +02:00
|
|
|
using PrivaPub.Models.Federation;
|
|
|
|
|
using PrivaPub.Models.Social;
|
2026-10-05 18:39:46 +02:00
|
|
|
using PrivaPub.Models.User;
|
2026-10-05 20:57:43 +02:00
|
|
|
using PrivaPub.StaticServices;
|
2026-10-05 18:39:46 +02:00
|
|
|
|
|
|
|
|
using System.Text.Json.Nodes;
|
|
|
|
|
|
|
|
|
|
using static PrivaPub.Federation.Objects.ActivityJson;
|
|
|
|
|
|
|
|
|
|
namespace PrivaPub.Federation.Inbox.Handlers
|
|
|
|
|
{
|
|
|
|
|
// An account moving to another (Mastodon's Move{object: itself, target: the new account}), believed as Mastodon
|
|
|
|
|
// believes it: the moving account sends it about itself, and the new account, read again from its own server, names
|
2026-10-05 20:57:43 +02:00
|
|
|
// it among its alsoKnownAs. The old account then shows where it went (`moved`), and, as Mastodon does it (owner
|
|
|
|
|
// decision 2026-10-05), the personas following it follow the new one instead, in the same lists, and those that
|
|
|
|
|
// muted or blocked it mute or block the new one too.
|
2026-11-10 01:24:43 +01:00
|
|
|
public class MoveHandler(
|
|
|
|
|
IRemoteActorService remoteActors,
|
|
|
|
|
DbEntities dbEntities,
|
|
|
|
|
ILocalActorService localActors,
|
|
|
|
|
IFollowService follows,
|
|
|
|
|
IRelationshipService relationships) : IActivityHandler
|
2026-10-05 18:39:46 +02:00
|
|
|
{
|
|
|
|
|
public string Type => "Move";
|
|
|
|
|
|
|
|
|
|
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var target = Id(activity["target"]);
|
|
|
|
|
if (Id(activity["object"]) != actor.ActorURI || string.IsNullOrEmpty(target) || target == actor.ActorURI)
|
|
|
|
|
{
|
|
|
|
|
Arrival.Drop("not-self");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-11-10 01:24:43 +01:00
|
|
|
var moved = await remoteActors.GetActor(target, refresh: true, token);
|
2026-10-05 18:39:46 +02:00
|
|
|
if (moved == default)
|
|
|
|
|
{
|
|
|
|
|
Arrival.Drop("target-unavailable");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!moved.AlsoKnownAs.Contains(actor.ActorURI))
|
|
|
|
|
{
|
|
|
|
|
Arrival.Drop("not-aliased");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
await DB.Default.Update<ForeignAvatar>().MatchID(actor.ID).Modify(a => a.MovedToURL, target).ExecuteAsync(token);
|
2026-10-05 20:57:43 +02:00
|
|
|
await MoveFollows(actor, moved, token);
|
|
|
|
|
await CarryOver(actor, moved, token);
|
2026-10-05 18:39:46 +02:00
|
|
|
Arrival.Accept("moved");
|
|
|
|
|
}
|
2026-10-05 20:57:43 +02:00
|
|
|
|
|
|
|
|
async Task MoveFollows(ForeignAvatar old, ForeignAvatar moved, CancellationToken token)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
foreach (var following in await dbEntities.Followings.Match(f => f.TargetActorURI == old.ActorURI).ExecuteAsync(token))
|
2026-10-05 20:57:43 +02:00
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
var persona = await localActors.FindById(LocalActorKind.Person, following.AvatarId, token);
|
2026-10-05 20:57:43 +02:00
|
|
|
if (persona == default)
|
|
|
|
|
continue;
|
|
|
|
|
var lists = await DB.Default.Find<PersonaListMember>().Match(m => m.AvatarId == persona.Id && m.AccountId == old.ID).ExecuteAsync(token);
|
2026-10-18 08:38:11 +02:00
|
|
|
var endorsed = await DB.Default.Find<Endorsement>().Match(e => e.AvatarId == persona.Id && e.TargetActorURI == old.ActorURI).ExecuteAnyAsync(token);
|
2026-11-10 01:24:43 +01:00
|
|
|
var followed = await follows.FollowAs(persona, moved.ActorURI, following.ShowReblogs, following.Notify, following.Languages ?? new List<string>(), token);
|
|
|
|
|
await follows.UnfollowAs(persona, old.ActorURI, token);
|
2026-10-05 20:57:43 +02:00
|
|
|
if (followed == default)
|
|
|
|
|
continue;
|
2026-10-18 08:38:11 +02:00
|
|
|
// the persona's endorsement and private note go with the follow, as Mastodon carries them over (R8)
|
|
|
|
|
if (endorsed)
|
|
|
|
|
await DB.Default.Update<Endorsement>().Match(e => e.AvatarId == persona.Id && e.TargetAccountId == moved.ID)
|
|
|
|
|
.Modify(e => e.TargetActorURI, moved.ActorURI).Modify(b => b.SetOnInsert(e => e.CreatedAt, DateTime.UtcNow))
|
|
|
|
|
.Option(o => o.IsUpsert = true).ExecuteAsync(token);
|
|
|
|
|
if (await DB.Default.Find<PersonaNote>().Match(n => n.AvatarId == persona.Id && n.TargetAccountId == old.ID).ExecuteFirstAsync(token) is { } note
|
|
|
|
|
&& !await DB.Default.Find<PersonaNote>().Match(n => n.AvatarId == persona.Id && n.TargetAccountId == moved.ID).ExecuteAnyAsync(token))
|
|
|
|
|
await DB.Default.SaveAsync(new PersonaNote { AvatarId = persona.Id, TargetAccountId = moved.ID, Text = note.Text }, token);
|
2026-10-05 20:57:43 +02:00
|
|
|
foreach (var member in lists)
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await DB.Default.SaveAsync(new PersonaListMember { ListId = member.ListId, AvatarId = persona.Id, AccountId = moved.ID }, token);
|
|
|
|
|
}
|
|
|
|
|
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async Task CarryOver(ForeignAvatar old, ForeignAvatar moved, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
foreach (var mute in await DB.Default.Find<Mute>().Match(m => m.TargetActorURI == old.ActorURI).ExecuteAsync(token))
|
2026-11-10 01:24:43 +01:00
|
|
|
if (await localActors.FindById(LocalActorKind.Person, mute.AvatarId, token) is { } persona
|
2026-10-05 20:57:43 +02:00
|
|
|
&& !await DB.Default.Find<Mute>().Match(m => m.AvatarId == persona.Id && m.TargetActorURI == moved.ActorURI).ExecuteAnyAsync(token))
|
2026-11-10 01:24:43 +01:00
|
|
|
await relationships.Mute(persona, moved.ActorURI, moved.ID, mute.HideNotifications,
|
2026-10-05 20:57:43 +02:00
|
|
|
mute.ExpiresAt is { } expires ? expires - DateTime.UtcNow : default(TimeSpan?), token);
|
|
|
|
|
foreach (var block in await DB.Default.Find<Block>().Match(b => b.TargetActorURI == old.ActorURI).ExecuteAsync(token))
|
2026-11-10 01:24:43 +01:00
|
|
|
if (await localActors.FindById(LocalActorKind.Person, block.AvatarId, token) is { } persona
|
2026-10-05 20:57:43 +02:00
|
|
|
&& !await DB.Default.Find<Block>().Match(b => b.AvatarId == persona.Id && b.TargetActorURI == moved.ActorURI).ExecuteAnyAsync(token))
|
2026-11-10 01:24:43 +01:00
|
|
|
await relationships.Block(persona, moved.ActorURI, moved.ID, token);
|
2026-10-05 20:57:43 +02:00
|
|
|
}
|
2026-10-05 18:39:46 +02:00
|
|
|
}
|
|
|
|
|
}
|