Add boost/like count to more timelines

This commit is contained in:
Andrew Godwin 2022-12-22 04:12:42 +00:00
parent 43372549c7
commit 02f942f1ad
4 changed files with 49 additions and 5 deletions

View File

@ -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],

View File

@ -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
),
),
)
)

View File

@ -1,9 +1,9 @@
{% if post.pk in interactions.boost %}
<a title="Unboost" class="active" hx-post="{{ post.urls.action_unboost }}" hx-swap="outerHTML">
<i class="fa-solid fa-retweet"></i> {{ event.boost_count }}
<i class="fa-solid fa-retweet"></i> {% if event.boost_count is not None %}{{ event.boost_count }}{% else %}{{ post.boost_count }}{% endif %}
</a>
{% else %}
<a title="Boost" hx-post="{{ post.urls.action_boost }}" hx-swap="outerHTML">
<i class="fa-solid fa-retweet"></i> {{ event.boost_count }}
<i class="fa-solid fa-retweet"></i> {% if event.boost_count is not None %}{{ event.boost_count }}{% else %}{{ post.boost_count }}{% endif %}
</a>
{% endif %}

View File

@ -1,9 +1,9 @@
{% if post.pk in interactions.like %}
<a title="Unlike" class="active" hx-post="{{ post.urls.action_unlike }}" hx-swap="outerHTML" role="menuitem">
<i class="fa-solid fa-star"></i> {{ event.like_count }}
<i class="fa-solid fa-star"></i> {% if event.like_count is not None %}{{ event.like_count }}{% else %}{{ post.like_count }}{% endif %}
</a>
{% else %}
<a title="Like" hx-post="{{ post.urls.action_like }}" hx-swap="outerHTML" role="menuitem">
<i class="fa-solid fa-star"></i> {{ event.like_count }}
<i class="fa-solid fa-star"></i> {% if event.like_count is not None %}{{ event.like_count }}{% else %}{{ post.like_count }}{% endif %}
</a>
{% endif %}