Consider group membership for list cache

This commit is contained in:
Mouse Reeve 2021-12-14 11:07:36 -08:00
parent 3358e45086
commit 0012f4464d
2 changed files with 45 additions and 12 deletions

View file

@ -64,18 +64,31 @@ class ListsStream(RedisStore):
Q(id__in=book_list.user.blocks.all()) | Q(blocks=book_list.user) Q(id__in=book_list.user.blocks.all()) | Q(blocks=book_list.user)
) )
# TODO: groups group = book_list.group
# only visible to the poster and mentioned users # only visible to the poster and mentioned users
if book_list.privacy == "direct": if book_list.privacy == "direct":
if group:
audience = audience.filter(
Q(id=book_list.user.id) # if the user is the post's author
| ~Q(groups=group.memberships) # if the user is in the group
)
else:
audience = audience.filter( audience = audience.filter(
Q(id=book_list.user.id) # if the user is the post's author Q(id=book_list.user.id) # if the user is the post's author
) )
# only visible to the poster's followers and tagged users # only visible to the poster's followers and tagged users
elif book_list.privacy == "followers": elif book_list.privacy == "followers":
if group:
audience = audience.filter( audience = audience.filter(
Q(id=book_list.user.id) # if the user is the post's author Q(id=book_list.user.id) # if the user is the list's owner
| Q(following=book_list.user) # if the user is following the author | Q(following=book_list.user) # if the user is following the pwmer
# if a user is in the group
| Q(memberships__group__id=book_list.group.id)
)
else:
audience = audience.filter(
Q(id=book_list.user.id) # if the user is the list's owner
| Q(following=book_list.user) # if the user is following the pwmer
) )
return audience.distinct() return audience.distinct()

View file

@ -92,7 +92,7 @@ class ListsStream(TestCase):
book_list = models.List.objects.create( book_list = models.List.objects.create(
user=self.local_user, user=self.local_user,
name="hi", name="hi",
privacy="direct", privacy="followers",
) )
users = self.stream.get_audience(book_list) users = self.stream.get_audience(book_list)
self.assertTrue(self.local_user in users) self.assertTrue(self.local_user in users)
@ -105,9 +105,29 @@ class ListsStream(TestCase):
book_list = models.List.objects.create( book_list = models.List.objects.create(
user=self.remote_user, user=self.remote_user,
name="hi", name="hi",
privacy="direct", privacy="followers",
)
users = self.stream.get_audience(book_list)
self.assertTrue(self.local_user in users)
self.assertFalse(self.another_user in users)
def test_get_audience_followers_with_group(self, *_):
"""get a list of users that should see a list"""
group = models.Group.objects.create(name="test group", user=self.remote_user)
models.GroupMember.objects.create(
group=group,
user=self.local_user,
)
book_list = models.List.objects.create(
user=self.remote_user, name="hi", privacy="followers", curation="group"
) )
users = self.stream.get_audience(book_list) users = self.stream.get_audience(book_list)
self.assertFalse(self.local_user in users) self.assertFalse(self.local_user in users)
book_list.group = group
book_list.save(broadcast=False)
users = self.stream.get_audience(book_list)
self.assertTrue(self.local_user in users)
self.assertFalse(self.another_user in users) self.assertFalse(self.another_user in users)
self.assertFalse(self.remote_user in users)