diff --git a/bookwyrm/models/federated_server.py b/bookwyrm/models/federated_server.py index e297c46c..114f387b 100644 --- a/bookwyrm/models/federated_server.py +++ b/bookwyrm/models/federated_server.py @@ -28,7 +28,7 @@ class FederatedServer(BookWyrmModel): def block(self): """block a server""" self.status = "blocked" - self.save() + self.save(update_fields="status") # deactivate all associated users self.user_set.filter(is_active=True).update( @@ -45,7 +45,7 @@ class FederatedServer(BookWyrmModel): def unblock(self): """unblock a server""" self.status = "federated" - self.save() + self.save(update_fields="status") self.user_set.filter(deactivation_reason="domain_block").update( is_active=True, deactivation_reason=None diff --git a/bookwyrm/suggested_users.py b/bookwyrm/suggested_users.py index cbd33659..953945d1 100644 --- a/bookwyrm/suggested_users.py +++ b/bookwyrm/suggested_users.py @@ -207,6 +207,19 @@ def update_user(sender, instance, created, update_fields=None, **kwargs): remove_user_task.delay(instance.id) +@receiver(signals.post_save, sender=models.FederatedServer) +def domain_level_update(sender, instance, created, update_fields=None, **kwargs): + """remove users on a domain block""" + if not update_fields or "status" not in update_fields: + return + + userset = instance.user_set.values_list("id", flat=True) + if instance.status == "blocked": + bulk_remove_users_task.delay(userset) + return + bulk_add_users_task.delay(userset) + + # ------------------- TASKS @@ -235,3 +248,17 @@ def remove_suggestion_task(user_id, suggested_user_id): """remove a specific user from a specific user's suggestions""" suggested_user = models.User.objects.get(id=suggested_user_id) suggested_users.remove_suggestion(user_id, suggested_user) + + +@app.task(queue="low_priority") +def bulk_remove_users_task(user_ids): + """remove a bunch of users from recs""" + for user in models.User.objects.filter(id__in=user_ids): + suggested_users.remove_object_from_related_stores(user) + + +@app.task(queue="low_priority") +def bulk_add_users_task(user_ids): + """remove a bunch of users from recs""" + for user in models.User.objects.filter(id__in=user_ids): + suggested_users.rerank_obj(user, update_only=False)