From 02f942f1ad9b6ae58983bf5d47d56e86e2c5b051 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Thu, 22 Dec 2022 04:12:42 +0000 Subject: [PATCH] Add boost/like count to more timelines --- activities/models/post_interaction.py | 2 +- activities/services/timeline.py | 44 +++++++++++++++++++++++++++ templates/activities/_boost.html | 4 +-- templates/activities/_like.html | 4 +-- 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/activities/models/post_interaction.py b/activities/models/post_interaction.py index 299043d..c7a847e 100644 --- a/activities/models/post_interaction.py +++ b/activities/models/post_interaction.py @@ -160,7 +160,7 @@ class PostInteraction(StatorModel): Returns a dict of {interaction_type: set(post_ids)} for all the posts and the given identity, for use in templates. """ - # Bulk-fetch any interactions + # Bulk-fetch any of our own interactions ids_with_interaction_type = cls.objects.filter( identity=identity, post_id__in=[post.pk for post in posts], diff --git a/activities/services/timeline.py b/activities/services/timeline.py index c67a2fc..dcaa0a2 100644 --- a/activities/services/timeline.py +++ b/activities/services/timeline.py @@ -57,6 +57,16 @@ class TimelineService: .filter(author__restriction=Identity.Restriction.none) .select_related("author", "author__domain") .prefetch_related("attachments", "mentions", "emojis") + .annotate( + like_count=models.Count( + "interactions", + filter=models.Q(interactions__type=PostInteraction.Types.like), + ), + boost_count=models.Count( + "interactions", + filter=models.Q(interactions__type=PostInteraction.Types.boost), + ), + ) .order_by("-published") ) @@ -67,6 +77,16 @@ class TimelineService: .filter(author__restriction=Identity.Restriction.none) .select_related("author", "author__domain") .prefetch_related("attachments", "mentions", "emojis") + .annotate( + like_count=models.Count( + "interactions", + filter=models.Q(interactions__type=PostInteraction.Types.like), + ), + boost_count=models.Count( + "interactions", + filter=models.Q(interactions__type=PostInteraction.Types.boost), + ), + ) .order_by("-published") ) @@ -78,6 +98,16 @@ class TimelineService: .tagged_with(hashtag) .select_related("author", "author__domain") .prefetch_related("attachments", "mentions") + .annotate( + like_count=models.Count( + "interactions", + filter=models.Q(interactions__type=PostInteraction.Types.like), + ), + boost_count=models.Count( + "interactions", + filter=models.Q(interactions__type=PostInteraction.Types.boost), + ), + ) .order_by("-published") ) @@ -100,4 +130,18 @@ class TimelineService: "subject_post__mentions", "subject_post__attachments", ) + .annotate( + like_count=models.Count( + "subject_post__interactions", + filter=models.Q( + subject_post__interactions__type=PostInteraction.Types.like + ), + ), + boost_count=models.Count( + "subject_post__interactions", + filter=models.Q( + subject_post__interactions__type=PostInteraction.Types.boost + ), + ), + ) ) diff --git a/templates/activities/_boost.html b/templates/activities/_boost.html index 008690b..945683e 100644 --- a/templates/activities/_boost.html +++ b/templates/activities/_boost.html @@ -1,9 +1,9 @@ {% if post.pk in interactions.boost %} - {{ event.boost_count }} + {% if event.boost_count is not None %}{{ event.boost_count }}{% else %}{{ post.boost_count }}{% endif %} {% else %} - {{ event.boost_count }} + {% if event.boost_count is not None %}{{ event.boost_count }}{% else %}{{ post.boost_count }}{% endif %} {% endif %} diff --git a/templates/activities/_like.html b/templates/activities/_like.html index 64118d2..5877fff 100644 --- a/templates/activities/_like.html +++ b/templates/activities/_like.html @@ -1,9 +1,9 @@ {% if post.pk in interactions.like %} - {{ event.like_count }} + {% if event.like_count is not None %}{{ event.like_count }}{% else %}{{ post.like_count }}{% endif %} {% else %} - {{ event.like_count }} + {% if event.like_count is not None %}{{ event.like_count }}{% else %}{{ post.like_count }}{% endif %} {% endif %}