From 228de1b83aaa0b0b30e2d4d6b104b55f8f923b9f Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Wed, 17 Aug 2022 21:34:04 +0200 Subject: [PATCH] Improve Accept/Reject for Follow --- app/boxes.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/app/boxes.py b/app/boxes.py index 7bd69a6..88d9912 100644 --- a/app/boxes.py +++ b/app/boxes.py @@ -1766,13 +1766,6 @@ async def save_to_inbox( ) else: if relates_to_outbox_object.ap_type == "Follow": - following = models.Following( - actor_id=actor.id, - outbox_object_id=relates_to_outbox_object.id, - ap_actor_id=actor.ap_id, - ) - db_session.add(following) - notif_type = ( models.NotificationType.FOLLOW_REQUEST_ACCEPTED if activity_ro.ap_type == "Accept" @@ -1784,9 +1777,29 @@ async def save_to_inbox( inbox_object_id=inbox_object.id, ) db_session.add(notif) + if activity_ro.ap_type == "Accept": + following = models.Following( + actor_id=actor.id, + outbox_object_id=relates_to_outbox_object.id, + ap_actor_id=actor.ap_id, + ) + db_session.add(following) + # Pre-fetch the latest activities await _prefetch_actor_outbox(db_session, actor) + elif activity_ro.ap_type == "Reject": + maybe_following = ( + await db_session.scalars( + select(models.Following).where( + models.Following.ap_actor_id == actor.ap_id, + ) + ) + ).one_or_none() + if maybe_following: + logger.info("Removing actor from following") + await db_session.delete(maybe_following) + else: logger.info( "Received an Accept for an unsupported activity: "