- Like: a Favourite per account and post (unique), the post's counter, a Favourite notification for a local author; only on posts the liker could see (public and unlisted, or followers-only and direct when they were addressed). - Announce: the original is always refetched from its own origin (or is ours), and must be public or unlisted. A boost of a local post counts and notifies; a boost by an account a persona follows is stored as a reblog row and fanned out to its followers' home timelines (as a reblog, where reblogs are wanted). A boost by a stranger of a stranger's post is ignored. FEP-1b12 group announces of activities wait for P4. - Undo of a Like or an Announce reverses each of them; an Undo naming only an id is matched against follows, likes and boosts in turn. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012CzABvBkbcFqoHdmi8b9WB
96 lines
3.6 KiB
C#
96 lines
3.6 KiB
C#
using MongoDB.Entities;
|
|
|
|
using PrivaPub.Federation.Actors;
|
|
using PrivaPub.Models.Federation;
|
|
using PrivaPub.Models.Social;
|
|
using PrivaPub.Models.User;
|
|
using PrivaPub.StaticServices;
|
|
|
|
using System.Text.Json.Nodes;
|
|
|
|
using static PrivaPub.Federation.Inbox.ForeignMembers;
|
|
using static PrivaPub.Federation.Objects.ActivityJson;
|
|
|
|
using PostEntity = PrivaPub.Models.Post.Post;
|
|
|
|
namespace PrivaPub.Federation.Inbox.Handlers
|
|
{
|
|
public class UndoHandler : IActivityHandler
|
|
{
|
|
readonly DbEntities _dbEntities;
|
|
readonly ILocalActorService _localActors;
|
|
|
|
public UndoHandler(DbEntities dbEntities, ILocalActorService localActors)
|
|
{
|
|
_dbEntities = dbEntities;
|
|
_localActors = localActors;
|
|
}
|
|
|
|
public string Type => "Undo";
|
|
|
|
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
|
|
{
|
|
var inner = activity["object"];
|
|
var innerType = inner is JsonObject ? Value(inner, "type") : default;
|
|
var innerId = Id(inner);
|
|
|
|
if (innerType is null or "Follow")
|
|
await UndoFollow(inner, innerId, actor, token);
|
|
if (innerType is null or "Like")
|
|
await UndoLike(inner, innerId, actor, token);
|
|
if (innerType is null or "Announce")
|
|
await UndoAnnounce(innerId, actor, token);
|
|
}
|
|
|
|
async Task UndoFollow(JsonNode inner, string innerId, ForeignAvatar actor, CancellationToken token)
|
|
{
|
|
var followers = await _dbEntities.Followers.Match(f => f.ActorURI == actor.ActorURI).ExecuteAsync(token);
|
|
var targetUri = inner is JsonObject ? Id(inner["object"]) : default;
|
|
var target = targetUri == default ? default : await _localActors.FindByUri(targetUri, token);
|
|
foreach (var follower in followers)
|
|
{
|
|
var matchesActivity = innerId != default && follower.FollowActivityURI == innerId;
|
|
var matchesTarget = target != default && target.Id == follower.LocalActorId && target.Kind == follower.LocalActorKind;
|
|
if (!matchesActivity && !matchesTarget)
|
|
continue;
|
|
|
|
await DB.Default.DeleteAsync<Follower>(follower.ID);
|
|
if (follower.LocalActorKind == LocalActorKind.Group)
|
|
await RemoveForeignMember(follower.LocalActorId, actor.ActorURI, token);
|
|
}
|
|
}
|
|
|
|
async Task UndoLike(JsonNode inner, string innerId, ForeignAvatar actor, CancellationToken token)
|
|
{
|
|
var favourite = innerId == default
|
|
? default
|
|
: await _dbEntities.Favourites.Match(f => f.ActivityURI == innerId && f.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
|
|
if (favourite == default && inner is JsonObject && Id(inner["object"]) is { } objectUri)
|
|
{
|
|
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token);
|
|
favourite = post == default
|
|
? default
|
|
: await _dbEntities.Favourites.Match(f => f.PostId == post.ID && f.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
|
|
}
|
|
if (favourite == default)
|
|
return;
|
|
await DB.Default.DeleteAsync<Favourite>(favourite.ID);
|
|
await DB.Default.Update<PostEntity>().MatchID(favourite.PostId).Modify(b => b.Inc(p => p.FavouritesCount, -1)).ExecuteAsync(token);
|
|
}
|
|
|
|
async Task UndoAnnounce(string innerId, ForeignAvatar actor, CancellationToken token)
|
|
{
|
|
if (innerId == default)
|
|
return;
|
|
var reblog = await _dbEntities.Posts
|
|
.Match(p => p.ObjectURI == innerId && p.ActorURI == actor.ActorURI && p.ReblogOfPostId != null)
|
|
.ExecuteFirstAsync(token);
|
|
if (reblog == default)
|
|
return;
|
|
await DB.Default.DeleteAsync<PostEntity>(reblog.ID);
|
|
await DB.Default.DeleteAsync<TimelineEntry>(e => e.PostId == reblog.ID);
|
|
await DB.Default.Update<PostEntity>().MatchID(reblog.ReblogOfPostId).Modify(b => b.Inc(p => p.ReblogsCount, -1)).ExecuteAsync(token);
|
|
}
|
|
}
|
|
}
|