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 Returns a dict of {interaction_type: set(post_ids)} for all the posts
and the given identity, for use in templates. 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( ids_with_interaction_type = cls.objects.filter(
identity=identity, identity=identity,
post_id__in=[post.pk for post in posts], post_id__in=[post.pk for post in posts],

View file

@ -57,6 +57,16 @@ class TimelineService:
.filter(author__restriction=Identity.Restriction.none) .filter(author__restriction=Identity.Restriction.none)
.select_related("author", "author__domain") .select_related("author", "author__domain")
.prefetch_related("attachments", "mentions", "emojis") .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") .order_by("-published")
) )
@ -67,6 +77,16 @@ class TimelineService:
.filter(author__restriction=Identity.Restriction.none) .filter(author__restriction=Identity.Restriction.none)
.select_related("author", "author__domain") .select_related("author", "author__domain")
.prefetch_related("attachments", "mentions", "emojis") .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") .order_by("-published")
) )
@ -78,6 +98,16 @@ class TimelineService:
.tagged_with(hashtag) .tagged_with(hashtag)
.select_related("author", "author__domain") .select_related("author", "author__domain")
.prefetch_related("attachments", "mentions") .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") .order_by("-published")
) )
@ -100,4 +130,18 @@ class TimelineService:
"subject_post__mentions", "subject_post__mentions",
"subject_post__attachments", "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 %} {% if post.pk in interactions.boost %}
<a title="Unboost" class="active" hx-post="{{ post.urls.action_unboost }}" hx-swap="outerHTML"> <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> </a>
{% else %} {% else %}
<a title="Boost" hx-post="{{ post.urls.action_boost }}" hx-swap="outerHTML"> <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> </a>
{% endif %} {% endif %}

View file

@ -1,9 +1,9 @@
{% if post.pk in interactions.like %} {% if post.pk in interactions.like %}
<a title="Unlike" class="active" hx-post="{{ post.urls.action_unlike }}" hx-swap="outerHTML" role="menuitem"> <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> </a>
{% else %} {% else %}
<a title="Like" hx-post="{{ post.urls.action_like }}" hx-swap="outerHTML" role="menuitem"> <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> </a>
{% endif %} {% endif %}