tweak discover

This commit is contained in:
Your Name 2024-06-03 18:09:47 -04:00 committed by Henri Dickson
parent a1d8175311
commit 059c95f2c3
6 changed files with 57 additions and 40 deletions

View file

@ -8,6 +8,7 @@ from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from loguru import logger
from boofilsic.settings import MIN_MARKS_FOR_DISCOVER
from catalog.models import *
from common.models import BaseJob, JobManager
from journal.models import (
@ -138,10 +139,10 @@ class DiscoverGenerator(BaseJob):
.order_by("-edited_time")
.values_list("pk", flat=True)[:40]
)
tags = TagManager.popular_tags(days=14, limit=20)
post_ids = Takahe.get_popular_posts(days=14, limit=20).values_list(
"pk", flat=True
)
tags = TagManager.popular_tags(days=14)[:40]
post_ids = Takahe.get_popular_posts(
30, settings.MIN_MARKS_FOR_DISCOVER
).values_list("pk", flat=True)[:20]
cache.set(cache_key, gallery_list, timeout=None)
cache.set("trends_links", trends, timeout=None)
cache.set("featured_collections", collection_ids, timeout=None)

View file

@ -147,7 +147,48 @@
{% endif %}
</div>
{% if request.user.is_authenticated %}
{% include "_sidebar.html" with identity=request.user.identity %}
<aside class="grid__aside sidebar">
{% if request.user.unread_announcements %}
<section class="announcement">
<article>
<details open>
<summary>{% trans "Unread Announcements" %}</summary>
{% for ann in request.user.unread_announcements %}<div>{{ ann.html }}</div>{% endfor %}
<form action="{% url 'users:mark_announcements_read' %}" method="post">
{% csrf_token %}
<input type="submit"
class="secondary outline"
value="{% trans 'mark all as read' %}">
</form>
</details>
</article>
</section>
{% endif %}
<section>
<article>
<details class="auto-collapse" open>
<summary>{% trans 'Popular Posts' %}</summary>
<div style="font-size:80%">{% include "posts.html" with posts=popular_posts %}</div>
</details>
</article>
</section>
<section>
<article>
<details class="auto-collapse" open>
<summary>{% trans 'Popular Tags' %}</summary>
<div class="tag-list">
{% for t in popular_tags %}
<span>
<a href="{% url 'catalog:search' %}?tag={{ t|urlencode }}">{{ t }}</a>
</span>
{% empty %}
<div class="empty">{% trans "nothing so far." %}</div>
{% endfor %}
</div>
</details>
</article>
</section>
</aside>
{% else %}
{% include "_sidebar_anonymous.html" %}
{% endif %}

View file

@ -263,7 +263,10 @@ def discover(request):
layout = request.user.preference.discover_layout
identity = request.user.identity
announcements = []
popular_posts = Takahe.get_posts(cache.get("popular_posts", []))
post_ids = cache.get("popular_posts", [])
i = rot * len(post_ids) // 10
post_ids = post_ids[i:] + post_ids[:i]
popular_posts = Takahe.get_posts(post_ids)
else:
identity = None
layout = []

View file

@ -23,34 +23,6 @@
</article>
</section>
{% endif %}
{% if popular_tags is not None %}
<section>
<article>
<details class="auto-collapse" open>
<summary>{% trans 'Popular Tags' %}</summary>
<div class="tag-list">
{% for t in popular_tags %}
<span>
<a href="{% url 'catalog:search' %}?tag={{ t|urlencode }}">{{ t }}</a>
</span>
{% empty %}
<div class="empty">{% trans "nothing so far." %}</div>
{% endfor %}
</div>
</details>
</article>
</section>
{% endif %}
{% if popular_posts is not None %}
<section>
<article>
<details class="auto-collapse" open>
<summary>{% trans 'Popular Posts' %}</summary>
<div style="font-size:80%">{% include "posts.html" with posts=popular_posts %}</div>
</details>
</article>
</section>
{% endif %}
{% if show_profile %}
<section class="profile">
<article>

View file

@ -129,7 +129,7 @@ class TagManager:
return TagManager.all_tags_by_owner(self.owner, public_only=True)
@staticmethod
def popular_tags(days: int = 30, limit: int = 20):
def popular_tags(days: int = 30):
t = timezone.now() - timedelta(days=days)
titles = (
TagMember.objects.filter(created_time__gt=t)
@ -138,7 +138,7 @@ class TagManager:
.values("title")
.annotate(total=Count("parent_id", distinct=True))
.order_by("-total")
.values_list("title", flat=True)[:limit]
.values_list("title", flat=True)
)
return titles

View file

@ -973,8 +973,8 @@ class Takahe:
)
@staticmethod
def get_popular_posts(days: int = 14, limit: int = 20):
since = timezone.now().date() - timedelta(days=days)
def get_popular_posts(days: int = 30, min_interaction: int = 1):
since = timezone.now() - timedelta(days=days)
domains = Takahe.get_neodb_peers() + [settings.SITE_DOMAIN]
return (
Post.objects.exclude(state__in=["deleted", "deleted_fanned_out"])
@ -982,8 +982,8 @@ class Takahe:
author__domain__in=domains, visibility__in=[0, 4], published__gte=since
)
.annotate(num_interactions=Count("interactions"))
.filter(num_interactions__gt=0)
.order_by("-num_interactions", "-published")[:limit]
.filter(num_interactions__gte=min_interaction)
.order_by("-num_interactions", "-published")
)
@staticmethod