50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
using MongoDB.Entities;
|
|||
|
|
|
||
|
|
using PrivaPub.Federation.Actors;
|
||
|
|
using PrivaPub.Models.User;
|
||
|
|
|
||
|
|
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
|
||
|
|
// it among its alsoKnownAs. The old account then shows where it went (`moved`). The personas following it keep
|
||
|
|
// following it: following the new one is theirs to do.
|
||
|
|
public class MoveHandler : IActivityHandler
|
||
|
|
{
|
||
|
|
readonly IRemoteActorService _remoteActors;
|
||
|
|
|
||
|
|
public MoveHandler(IRemoteActorService remoteActors)
|
||
|
|
{
|
||
|
|
_remoteActors = remoteActors;
|
||
|
|
}
|
||
|
|
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
var moved = await _remoteActors.GetActor(target, refresh: true, token);
|
||
|
|
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);
|
||
|
|
Arrival.Accept("moved");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|