From 9940abfd81232dd4da3cd79eb72e45de11d43155 Mon Sep 17 00:00:00 2001 From: Hugh Rundle Date: Sat, 9 Oct 2021 22:11:46 +1100 Subject: [PATCH] refactor removing user from group This is in preparation for removing a user and their lists when the group owner blocks them. Remove the user via models.group Remove the lists via models.list --- bookwyrm/models/group.py | 9 +++++++++ bookwyrm/models/list.py | 20 +++++++++++++++++++- bookwyrm/views/group.py | 13 ++++++------- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/bookwyrm/models/group.py b/bookwyrm/models/group.py index 89fb3a0e..88320cf9 100644 --- a/bookwyrm/models/group.py +++ b/bookwyrm/models/group.py @@ -85,6 +85,15 @@ class GroupMember(models.Model): group=join_request.group, ) + @classmethod + def remove(cls, owner, user): + """remove a user from a group""" + + memberships = cls.objects.filter(group__user=owner, user=user).all() + for m in memberships: + # remove this user + m.delete() + class GroupMemberInvitation(models.Model): """adding a user to a group requires manual confirmation""" diff --git a/bookwyrm/models/list.py b/bookwyrm/models/list.py index b0222cef..295032f5 100644 --- a/bookwyrm/models/list.py +++ b/bookwyrm/models/list.py @@ -2,6 +2,7 @@ from django.apps import apps from django.db import models from django.db.models import Q +from django.db.models.fields import NullBooleanField from django.utils import timezone from bookwyrm import activitypub @@ -87,7 +88,24 @@ class List(OrderedCollectionMixin, BookWyrmModel): def direct_filter(cls, queryset, viewer): """Override filter for "direct" privacy level to allow group members to see the existence of group lists""" - return queryset.exclude(~Q(group__memberships__user=viewer), privacy="direct") + return queryset.exclude( + ~Q( # user not self and not in the group if this is a group list + Q(user=viewer) | Q(group__memberships__user=viewer) + ), + privacy="direct" + ) + + @classmethod + def remove_from_group(cls, owner, user): + """remove a list from a group""" + + memberships = GroupMember.objects.filter(group__user=owner, user=user).all() + for m in memberships: + # remove this user's group-curated lists from the group + cls.objects.filter(group=m.group, user=m.user).update( + group=None, curation="closed" + ) + class ListItem(CollectionItemMixin, BookWyrmModel): """ok""" diff --git a/bookwyrm/views/group.py b/bookwyrm/views/group.py index c66dcdd0..6ef21583 100644 --- a/bookwyrm/views/group.py +++ b/bookwyrm/views/group.py @@ -163,6 +163,10 @@ def remove_member(request): if not user: return HttpResponseBadRequest() + # you can't be removed from your own group + if request.POST["user"]== group.user: + return HttpResponseBadRequest() + is_member = models.GroupMember.objects.filter(group=group, user=user).exists() is_invited = models.GroupMemberInvitation.objects.filter( group=group, user=user @@ -182,13 +186,8 @@ def remove_member(request): if is_member: try: - membership = models.GroupMember.objects.get(group=group, user=user) - membership.delete() - - # remove this user's group-curated lists from the group - models.List.objects.filter(group=group, user=user).update( - group=None, curation="closed" - ) + models.List.remove_from_group(group.user, user) + models.GroupMember.remove(group.user, user) except IntegrityError: pass