Update suggestions on domain block

This commit is contained in:
Mouse Reeve 2021-09-08 11:38:22 -07:00
parent 2e2ee72333
commit d3b3dd6d99
2 changed files with 29 additions and 2 deletions

View file

@ -28,7 +28,7 @@ class FederatedServer(BookWyrmModel):
def block(self): def block(self):
"""block a server""" """block a server"""
self.status = "blocked" self.status = "blocked"
self.save() self.save(update_fields="status")
# deactivate all associated users # deactivate all associated users
self.user_set.filter(is_active=True).update( self.user_set.filter(is_active=True).update(
@ -45,7 +45,7 @@ class FederatedServer(BookWyrmModel):
def unblock(self): def unblock(self):
"""unblock a server""" """unblock a server"""
self.status = "federated" self.status = "federated"
self.save() self.save(update_fields="status")
self.user_set.filter(deactivation_reason="domain_block").update( self.user_set.filter(deactivation_reason="domain_block").update(
is_active=True, deactivation_reason=None is_active=True, deactivation_reason=None

View file

@ -207,6 +207,19 @@ def update_user(sender, instance, created, update_fields=None, **kwargs):
remove_user_task.delay(instance.id) 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 # ------------------- TASKS
@ -235,3 +248,17 @@ def remove_suggestion_task(user_id, suggested_user_id):
"""remove a specific user from a specific user's suggestions""" """remove a specific user from a specific user's suggestions"""
suggested_user = models.User.objects.get(id=suggested_user_id) suggested_user = models.User.objects.get(id=suggested_user_id)
suggested_users.remove_suggestion(user_id, suggested_user) 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)