[backend/federation] Handle note delete activities (ISH-12)

This commit is contained in:
Laura Hausmann 2024-02-09 11:37:38 +01:00
parent 2c46e55a6a
commit eb7f160b23
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 33 additions and 2 deletions

View file

@ -33,12 +33,19 @@ public class ActivityHandlerService(
switch (activity) {
case ASCreate: {
//TODO: implement the rest
//TODO: should we handle other types of creates?
if (activity.Object is not ASNote note)
throw GracefulException.UnprocessableEntity("Create activity object is invalid");
await noteSvc.ProcessNoteAsync(note, activity.Actor);
return;
}
case ASDelete: {
//TODO: handle user deletes
if (activity.Object is not ASNote note)
throw GracefulException.UnprocessableEntity("Delete activity object is invalid");
await noteSvc.DeleteNoteAsync(note, activity.Actor);
return;
}
case ASFollow: {
if (activity.Object is not { } obj)
throw GracefulException.UnprocessableEntity("Follow activity object is invalid");
@ -64,7 +71,7 @@ public class ActivityHandlerService(
return;
}
case ASUndo: {
//TODO: implement the rest
//TODO: what other types of undo objects are there?
if (activity.Object is not ASActivity { Type: ASActivity.Types.Follow, Object: not null } undoActivity)
throw new NotImplementedException("Unsupported undo operation");
await UnfollowAsync(undoActivity.Object, activity.Actor);

View file

@ -61,6 +61,30 @@ public class NoteService(
return note;
}
public async Task DeleteNoteAsync(ASNote note, ASActor actor) {
// ReSharper disable once EntityFramework.NPlusOne.IncompleteDataQuery (it doesn't know about IncludeCommonProperties())
var dbNote = await db.Notes.IncludeCommonProperties().FirstOrDefaultAsync(p => p.Uri == note.Id);
if (dbNote == null) {
logger.LogDebug("Note '{id}' isn't known, skipping", note.Id);
return;
}
var user = await userResolver.ResolveAsync(actor.Id);
// ReSharper disable once EntityFramework.NPlusOne.IncompleteDataUsage (same reason as above)
if (dbNote.User != user) {
logger.LogDebug("Note '{id}' isn't owned by actor requesting its deletion, skipping", note.Id);
return;
}
logger.LogDebug("Deleting note '{id}' owned by {userId}", note.Id, user.Id);
user.NotesCount--;
db.Remove(dbNote);
await db.SaveChangesAsync();
}
public async Task<Note> ProcessNoteAsync(ASNote note, ASActor actor) {
var dbHit = await db.Notes.IncludeCommonProperties().FirstOrDefaultAsync(p => p.Uri == note.Id);