Change get_audience to return list of user IDs

This will make it simpler to implement various optimizations.
This commit is contained in:
Wesley Aptekar-Cassels 2023-03-09 00:28:11 -05:00
parent 41e14bdfaf
commit 23698dafe5
4 changed files with 41 additions and 37 deletions

View file

@ -37,12 +37,12 @@ class ActivityStream(RedisStore):
pipeline = self.add_object_to_related_stores(status, execute=False)
if increment_unread:
for user in self.get_audience(status):
for user_id in self.get_audience(status):
# add to the unread status count
pipeline.incr(self.unread_id(user.id))
pipeline.incr(self.unread_id(user_id))
# add to the unread status count for status type
pipeline.hincrby(
self.unread_by_status_type_id(user.id), get_status_type(status), 1
self.unread_by_status_type_id(user_id), get_status_type(status), 1
)
# and go!
@ -97,7 +97,7 @@ class ActivityStream(RedisStore):
"""go from zero to a timeline"""
self.populate_store(self.stream_id(user.id))
def get_audience(self, status): # pylint: disable=no-self-use
def _get_audience(self, status): # pylint: disable=no-self-use
"""given a status, what users should see it"""
# direct messages don't appeard in feeds, direct comments/reviews/etc do
if status.privacy == "direct" and status.status_type == "Note":
@ -136,8 +136,12 @@ class ActivityStream(RedisStore):
)
return audience.distinct()
def get_audience(self, status): # pylint: disable=no-self-use
"""given a status, what users should see it"""
return [user.id for user in self._get_audience(status)]
def get_stores_for_object(self, obj):
return [self.stream_id(u.id) for u in self.get_audience(obj)]
return [self.stream_id(user_id) for user_id in self.get_audience(obj)]
def get_statuses_for_user(self, user): # pylint: disable=no-self-use
"""given a user, what statuses should they see on this stream"""
@ -156,8 +160,8 @@ class HomeStream(ActivityStream):
key = "home"
def get_audience(self, status):
audience = super().get_audience(status)
def _get_audience(self, status):
audience = super()._get_audience(status)
if not audience:
return []
return audience.filter(
@ -183,11 +187,11 @@ class LocalStream(ActivityStream):
key = "local"
def get_audience(self, status):
def _get_audience(self, status):
# this stream wants no part in non-public statuses
if status.privacy != "public" or not status.user.local:
return []
return super().get_audience(status)
return super()._get_audience(status)
def get_statuses_for_user(self, user):
# all public statuses by a local user
@ -202,7 +206,7 @@ class BooksStream(ActivityStream):
key = "books"
def get_audience(self, status):
def _get_audience(self, status):
"""anyone with the mentioned book on their shelves"""
# only show public statuses on the books feed,
# and only statuses that mention books
@ -217,7 +221,7 @@ class BooksStream(ActivityStream):
else status.mention_books.first().parent_work
)
audience = super().get_audience(status)
audience = super()._get_audience(status)
if not audience:
return []
return audience.filter(shelfbook__book__parent_work=work).distinct()

View file

@ -118,9 +118,9 @@ class Activitystreams(TestCase):
)
users = self.test_stream.get_audience(status)
# remote users don't have feeds
self.assertFalse(self.remote_user in users)
self.assertTrue(self.local_user in users)
self.assertTrue(self.another_user in users)
self.assertFalse(self.remote_user.id in users)
self.assertTrue(self.local_user.id in users)
self.assertTrue(self.another_user.id in users)
def test_abstractstream_get_audience_direct(self, *_):
"""get a list of users that should see a status"""
@ -141,9 +141,9 @@ class Activitystreams(TestCase):
)
status.mention_users.add(self.local_user)
users = self.test_stream.get_audience(status)
self.assertTrue(self.local_user in users)
self.assertFalse(self.another_user in users)
self.assertFalse(self.remote_user in users)
self.assertTrue(self.local_user.id in users)
self.assertFalse(self.another_user.id in users)
self.assertFalse(self.remote_user.id in users)
def test_abstractstream_get_audience_followers_remote_user(self, *_):
"""get a list of users that should see a status"""
@ -153,7 +153,7 @@ class Activitystreams(TestCase):
privacy="followers",
)
users = self.test_stream.get_audience(status)
self.assertFalse(users.exists())
self.assertEqual(users, [])
def test_abstractstream_get_audience_followers_self(self, *_):
"""get a list of users that should see a status"""
@ -164,9 +164,9 @@ class Activitystreams(TestCase):
book=self.book,
)
users = self.test_stream.get_audience(status)
self.assertTrue(self.local_user in users)
self.assertFalse(self.another_user in users)
self.assertFalse(self.remote_user in users)
self.assertTrue(self.local_user.id in users)
self.assertFalse(self.another_user.id in users)
self.assertFalse(self.remote_user.id in users)
def test_abstractstream_get_audience_followers_with_mention(self, *_):
"""get a list of users that should see a status"""
@ -179,9 +179,9 @@ class Activitystreams(TestCase):
status.mention_users.add(self.local_user)
users = self.test_stream.get_audience(status)
self.assertTrue(self.local_user in users)
self.assertFalse(self.another_user in users)
self.assertFalse(self.remote_user in users)
self.assertTrue(self.local_user.id in users)
self.assertFalse(self.another_user.id in users)
self.assertFalse(self.remote_user.id in users)
def test_abstractstream_get_audience_followers_with_relationship(self, *_):
"""get a list of users that should see a status"""
@ -193,6 +193,6 @@ class Activitystreams(TestCase):
book=self.book,
)
users = self.test_stream.get_audience(status)
self.assertFalse(self.local_user in users)
self.assertFalse(self.another_user in users)
self.assertFalse(self.remote_user in users)
self.assertFalse(self.local_user.id in users)
self.assertFalse(self.another_user.id in users)
self.assertFalse(self.remote_user.id in users)

View file

@ -44,7 +44,7 @@ class Activitystreams(TestCase):
user=self.remote_user, content="hi", privacy="public"
)
users = activitystreams.HomeStream().get_audience(status)
self.assertFalse(users.exists())
self.assertEqual(users, [])
def test_homestream_get_audience_with_mentions(self, *_):
"""get a list of users that should see a status"""
@ -53,8 +53,8 @@ class Activitystreams(TestCase):
)
status.mention_users.add(self.local_user)
users = activitystreams.HomeStream().get_audience(status)
self.assertFalse(self.local_user in users)
self.assertFalse(self.another_user in users)
self.assertFalse(self.local_user.id in users)
self.assertFalse(self.another_user.id in users)
def test_homestream_get_audience_with_relationship(self, *_):
"""get a list of users that should see a status"""
@ -63,5 +63,5 @@ class Activitystreams(TestCase):
user=self.remote_user, content="hi", privacy="public"
)
users = activitystreams.HomeStream().get_audience(status)
self.assertTrue(self.local_user in users)
self.assertFalse(self.another_user in users)
self.assertTrue(self.local_user.id in users)
self.assertFalse(self.another_user.id in users)

View file

@ -54,8 +54,8 @@ class Activitystreams(TestCase):
user=self.local_user, content="hi", privacy="public"
)
users = activitystreams.LocalStream().get_audience(status)
self.assertTrue(self.local_user in users)
self.assertTrue(self.another_user in users)
self.assertTrue(self.local_user.id in users)
self.assertTrue(self.another_user.id in users)
def test_localstream_get_audience_unlisted(self, *_):
"""get a list of users that should see a status"""
@ -88,7 +88,7 @@ class Activitystreams(TestCase):
)
# yes book, yes audience
audience = activitystreams.BooksStream().get_audience(status)
self.assertTrue(self.local_user in audience)
self.assertTrue(self.local_user.id in audience)
def test_localstream_get_audience_books_book_field(self, *_):
"""get a list of users that should see a status"""
@ -102,7 +102,7 @@ class Activitystreams(TestCase):
)
# yes book, yes audience
audience = activitystreams.BooksStream().get_audience(status)
self.assertTrue(self.local_user in audience)
self.assertTrue(self.local_user.id in audience)
def test_localstream_get_audience_books_alternate_edition(self, *_):
"""get a list of users that should see a status"""
@ -119,7 +119,7 @@ class Activitystreams(TestCase):
)
# yes book, yes audience
audience = activitystreams.BooksStream().get_audience(status)
self.assertTrue(self.local_user in audience)
self.assertTrue(self.local_user.id in audience)
def test_localstream_get_audience_books_non_public(self, *_):
"""get a list of users that should see a status"""