refactors get_activity_feed function

is it better? idk
This commit is contained in:
Mouse Reeve 2021-01-04 19:05:21 -08:00
parent 8301bec4ff
commit 35696934da
2 changed files with 88 additions and 67 deletions

View file

@ -116,17 +116,20 @@ class Views(TestCase):
content='blah blah blah', user=rat, privacy='followers') content='blah blah blah', user=rat, privacy='followers')
rat_mention.mention_users.set([self.local_user]) rat_mention.mention_users.set([self.local_user])
statuses = views.get_activity_feed(self.local_user, 'home')
self.assertEqual(len(statuses), 2)
self.assertEqual(statuses[1], public_status)
self.assertEqual(statuses[0], rat_mention)
statuses = views.get_activity_feed( statuses = views.get_activity_feed(
self.local_user, 'home', model=models.Comment) self.local_user,
['public', 'unlisted', 'followers'],
following_only=True,
queryset=models.Comment.objects
)
self.assertEqual(len(statuses), 1) self.assertEqual(len(statuses), 1)
self.assertEqual(statuses[0], public_status) self.assertEqual(statuses[0], public_status)
statuses = views.get_activity_feed(self.local_user, 'local') statuses = views.get_activity_feed(
self.local_user,
['public', 'followers'],
local_only=True
)
self.assertEqual(len(statuses), 2) self.assertEqual(len(statuses), 2)
self.assertEqual(statuses[1], public_status) self.assertEqual(statuses[1], public_status)
self.assertEqual(statuses[0], rat_public) self.assertEqual(statuses[0], rat_public)
@ -135,19 +138,30 @@ class Views(TestCase):
self.assertEqual(len(statuses), 1) self.assertEqual(len(statuses), 1)
self.assertEqual(statuses[0], direct_status) self.assertEqual(statuses[0], direct_status)
statuses = views.get_activity_feed(self.local_user, 'federated') statuses = views.get_activity_feed(
self.local_user,
['public', 'followers'],
)
self.assertEqual(len(statuses), 3) self.assertEqual(len(statuses), 3)
self.assertEqual(statuses[2], public_status) self.assertEqual(statuses[2], public_status)
self.assertEqual(statuses[1], rat_public) self.assertEqual(statuses[1], rat_public)
self.assertEqual(statuses[0], remote_status) self.assertEqual(statuses[0], remote_status)
statuses = views.get_activity_feed(self.local_user, 'friends') statuses = views.get_activity_feed(
self.local_user,
['public', 'unlisted', 'followers'],
following_only=True
)
self.assertEqual(len(statuses), 2) self.assertEqual(len(statuses), 2)
self.assertEqual(statuses[1], public_status) self.assertEqual(statuses[1], public_status)
self.assertEqual(statuses[0], rat_mention) self.assertEqual(statuses[0], rat_mention)
rat.followers.add(self.local_user) rat.followers.add(self.local_user)
statuses = views.get_activity_feed(self.local_user, 'friends') statuses = views.get_activity_feed(
self.local_user,
['public', 'unlisted', 'followers'],
following_only=True
)
self.assertEqual(len(statuses), 5) self.assertEqual(len(statuses), 5)
self.assertEqual(statuses[4], public_status) self.assertEqual(statuses[4], public_status)
self.assertEqual(statuses[3], rat_public) self.assertEqual(statuses[3], rat_public)

View file

@ -83,7 +83,16 @@ def home_tab(request, tab):
suggested_books = get_suggested_books(request.user) suggested_books = get_suggested_books(request.user)
activities = get_activity_feed(request.user, tab) if tab == 'home':
activities = get_activity_feed(
request.user, ['public', 'unlisted', 'followers'],
following_only=True)
elif tab == 'local':
activities = get_activity_feed(
request.user, ['public', 'followers'], local_only=True)
else:
activities = get_activity_feed(
request.user, ['public', 'followers'])
paginated = Paginator(activities, PAGE_LENGTH) paginated = Paginator(activities, PAGE_LENGTH)
activity_page = paginated.page(page) activity_page = paginated.page(page)
@ -151,7 +160,7 @@ def discover_page(request):
book__in=book.parent_work.editions.all() book__in=book.parent_work.editions.all()
) )
reviews = get_activity_feed( reviews = get_activity_feed(
request.user, 'federated', model=reviews) request.user, ['public', 'unlisted'], queryset=reviews)
ratings[book.id] = reviews.aggregate(Avg('rating'))['rating__avg'] ratings[book.id] = reviews.aggregate(Avg('rating'))['rating__avg']
data = { data = {
'title': 'Discover', 'title': 'Discover',
@ -187,68 +196,58 @@ def direct_messages_page(request, page=1):
return TemplateResponse(request, 'direct_messages.html', data) return TemplateResponse(request, 'direct_messages.html', data)
def get_activity_feed(user, filter_level, model=models.Status): def get_activity_feed(
user, privacy, local_only=False, following_only=False,
queryset=models.Status.objects):
''' get a filtered queryset of statuses ''' ''' get a filtered queryset of statuses '''
if user.is_anonymous: privacy = privacy if isinstance(privacy, list) else [privacy]
user = None # if we're looking at Status, we need this. We don't if it's Comment
if hasattr(queryset, 'select_subclasses'):
queryset = queryset.select_subclasses()
if user: # exclude deleted
following = models.User.objects.filter( queryset = queryset.exclude(deleted=True).order_by('-published_date')
Q(followers=user) | Q(id=user.id)
# filter to only privided privacy levels
queryset = queryset.filter(privacy__in=privacy)
# only include statuses the user follows
if following_only:
queryset = queryset.exclude(
~Q(# remove everythign except
Q(user__in=user.following.all()) | # user follwoing
Q(user=user) |# is self
Q(mention_users=user)# mentions user
),
) )
else: # exclude followers-only statuses the user doesn't follow
following = [] elif 'followers' in privacy:
queryset = queryset.exclude(
activities = model ~Q(# user isn't following and it isn't their own status
if hasattr(model, 'objects'): Q(user__in=user.following.all()) | Q(user=user)
activities = model.objects ),
privacy='followers' # and the status is followers only
activities = activities.filter(
deleted=False,
).order_by(
'-published_date'
) )
if filter_level == 'direct': # exclude direct messages not intended for the user
return activities.filter( if 'direct' in privacy:
Q(user=user) | Q(mention_users=user), queryset = queryset.exclude(
privacy='direct' ~Q(
).distinct() Q(user=user) | Q(mention_users=user)
), privacy='direct'
# never show DMs in the regular feed
activities = activities.filter(~Q(privacy='direct'))
if hasattr(activities, 'select_subclasses'):
activities = activities.select_subclasses()
if filter_level in ['friends', 'home']:
# people you follow and direct mentions
activities = activities.filter(
Q(user__in=following, privacy__in=[
'public', 'unlisted', 'followers'
]) | Q(mention_users=user) | Q(user=user)
).distinct()
elif filter_level == 'self':
activities = activities.filter(user=user, privacy='public')
elif filter_level == 'local':
# everyone on this instance except unlisted
activities = activities.filter(
Q(user__in=following, privacy='followers') | Q(privacy='public'),
user__local=True
)
else:
# all activities from everyone you federate with
activities = activities.filter(
Q(user__in=following, privacy='followers') | Q(privacy='public')
) )
# filter for only local status
if local_only:
queryset = queryset.filter(user__local=True)
# remove statuses that have boosts in the same queryset
try: try:
activities = activities.filter(~Q(boosters__in=activities)) queryset = queryset.filter(~Q(boosters__in=queryset))
except ValueError: except ValueError:
pass pass
return activities return queryset
@require_GET @require_GET
@ -462,7 +461,11 @@ def user_page(request, username):
break break
# user's posts # user's posts
activities = get_activity_feed(user, 'self') activities = get_activity_feed(
user,
['public', 'unlisted', 'followers'],
queryset=models.Status.objects.filter(user=request.user)
)
paginated = Paginator(activities, PAGE_LENGTH) paginated = Paginator(activities, PAGE_LENGTH)
activity_page = paginated.page(page) activity_page = paginated.page(page)
@ -629,7 +632,11 @@ def book_page(request, book_id):
book__in=work.editions.all(), book__in=work.editions.all(),
) )
# all reviews for the book # all reviews for the book
reviews = get_activity_feed(request.user, 'federated', model=reviews) reviews = get_activity_feed(
request.user,
['public', 'unlisted', 'followers', 'direct'],
queryset=reviews
)
# the reviews to show # the reviews to show
paginated = Paginator(reviews.exclude( paginated = Paginator(reviews.exclude(