popular tags

This commit is contained in:
Your Name 2024-06-03 07:27:44 -04:00 committed by Henri Dickson
parent c9466ac770
commit 5907027bab
9 changed files with 227 additions and 78 deletions

View file

@ -17,6 +17,7 @@ from journal.models import (
TagManager,
q_item_in_category,
)
from takahe.utils import Takahe
from users.models import APIdentity
MAX_ITEMS_PER_PERIOD = 12
@ -138,10 +139,14 @@ class DiscoverGenerator(BaseJob):
.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
)
cache.set(cache_key, gallery_list, timeout=None)
cache.set("trends_links", trends, timeout=None)
cache.set("featured_collections", collection_ids, timeout=None)
cache.set("popular_tags", tags, timeout=None)
cache.set("popular_tags", list(tags), timeout=None)
cache.set("popular_posts", list(post_ids), timeout=None)
logger.info(
f"Discover data updated, trends: {len(trends)}, collections: {len(collection_ids)}, tags: {len(tags)}."
f"Discover data updated, trends: {len(trends)}, collections: {len(collection_ids)}, tags: {len(tags)}, posts: {len(post_ids)}."
)

View file

@ -22,6 +22,7 @@ from journal.models import (
q_piece_in_home_feed_of_user,
q_piece_visible_to_user,
)
from takahe.utils import Takahe
from .forms import *
from .models import *
@ -260,9 +261,11 @@ def discover(request):
return redirect(reverse("users:register"))
layout = request.user.preference.discover_layout
identity = request.user.identity
popular_posts = Takahe.get_posts(cache.get("popular_posts", []))
else:
identity = None
layout = []
popular_posts = []
collection_ids = cache.get("featured_collections", [])
if collection_ids:
@ -282,6 +285,7 @@ def discover(request):
"gallery_list": gallery_list,
"featured_collections": featured_collections,
"popular_tags": popular_tags,
"popular_posts": popular_posts,
"layout": layout,
},
)

View file

@ -1,5 +1,6 @@
{% load static %}
{% load i18n %}
{% load humanize %}
{% load mastodon %}
{% load thumb %}
{% load collection %}
@ -40,6 +41,55 @@
</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%">
{% for post in popular_posts %}
<section class="activity">
<div>
<span class="action">
{# <span class="timestamp">{{ post.created|date }}</span> #}
{% include "action_reply_post.html" %}
{% include "action_like_post.html" %}
{% include "action_boost_post.html" %}
{% include "action_open_post.html" %}
</span>
<!--span class="avatar" style="margin:0.5em;">
<img src="{{ post.author.icon_uri }}" alt="cover" />
</span -->
<span style="">
<a href="{{ post.author.url }}"
class="nickname"
title="@{{ post.author.handle }}">{{ post.author.name }}</a>
</span>
<div>{{ post.summary|default:'' }}</div>
<div {% if post.summary or post.sensitive %}class="spoiler" _="on click toggle .revealed on me"{% endif %}>
<div>
{% for attachment in post.attachments.all %}
{% if attachment.is_image %}
<img src="{{ attachment.full_url.relative }}"
alt="attachment.file_display_name"
style="max-height:6em;
max-width:50%">
{% endif %}
{% endfor %}
</div>
{{ post.content|bleach:"a,p,span,br,div,img"|default:"" }}
</div>
</div>
<div id="replies_{{ post.pk }}"></div>
</section>
{% empty %}
<div class="empty">{% trans "nothing so far." %}</div>
{% endfor %}
</div>
</details>
</article>
</section>
{% endif %}
{% if show_profile %}
<section class="profile">
<article>

View file

@ -4,7 +4,7 @@
{% load thumb %}
{% load collection %}
{% load bleach_tags %}
<div class="grid__aside">
<aside class="grid__aside sidebar">
<section>
{% if site_intro %}
<div hx-get="{{ site_intro }}" hx-trigger="load" hx-swap="outerHTML">
@ -34,4 +34,42 @@
</article>
{% endif %}
</section>
</div>
<section class="announcement">
<article>
<details {% if announcements %}open{% endif %}>
<summary>{% trans "Announcements" %}</summary>
{% for ann in announcements %}<div>{{ ann.html }}</div>{% endfor %}
</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>
<script>
$(function () {
function _sidebar_auto_collapse(mm){
if (mm.matches) {
$('.auto-collapse').removeAttr('open')
} else {
$('.auto-collapse').attr('open', '')
}
}
var mm = window.matchMedia("(max-width: 768px)")
mm.addListener(_sidebar_auto_collapse);
_sidebar_auto_collapse(mm);
});
</script>

View file

@ -140,7 +140,7 @@ class TagManager:
.order_by("-total")
.values_list("title", flat=True)[:limit]
)
return list(titles)
return titles
def get_item_tags(self, item: Item):
return sorted(

View file

@ -43,7 +43,7 @@
<div id="replies_{{ post.pk }}"></div>
</div>
{% empty %}
<div class="empty">{% trans 'nothing so far.' %}</div>
<div class="empty">{% trans 'no replies so far.' %}</div>
{% endfor %}
{% if post %}
<form class="reply"

View file

@ -6,7 +6,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-06-02 15:07-0400\n"
"POT-Creation-Date: 2024-06-03 07:26-0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -667,7 +667,7 @@ msgstr "不再提示"
#: catalog/templates/_item_comments_by_episode.html:78
#: catalog/templates/_item_reviews.html:43
#: catalog/templates/podcast_episode_data.html:41
#: common/templates/_sidebar.html:202
#: common/templates/_sidebar.html:267
msgid "show more"
msgstr "显示更多"
@ -1067,23 +1067,29 @@ msgstr "本操作不可撤销。确认合并吗?"
msgid "Discover"
msgstr "发现"
#: catalog/templates/discover.html:73 journal/templates/profile.html:195
#: catalog/templates/discover.html:84
#: journal/templates/user_collection_list.html:17
#: journal/templates/user_collection_list.html:31
msgid "Collections"
msgstr "收藏单"
#: catalog/templates/discover.html:102 journal/templates/profile.html:195
msgid "edit layout"
msgstr "编辑布局"
#: catalog/templates/discover.html:76 journal/templates/profile.html:198
#: catalog/templates/discover.html:105 journal/templates/profile.html:198
msgid "save"
msgstr "保存"
#: catalog/templates/discover.html:87 journal/templates/profile.html:209
#: catalog/templates/discover.html:116 journal/templates/profile.html:209
msgid "cancel"
msgstr "取消"
#: catalog/templates/discover.html:93 journal/templates/profile.html:215
#: catalog/templates/discover.html:122 journal/templates/profile.html:215
msgid "show"
msgstr "显示"
#: catalog/templates/discover.html:94 journal/templates/profile.html:216
#: catalog/templates/discover.html:123 journal/templates/profile.html:216
msgid "hide"
msgstr "隐藏"
@ -1159,10 +1165,12 @@ msgstr "简介"
#: catalog/templates/item_base.html:236
#: catalog/templates/item_mark_list.html:54
#: catalog/templates/item_review_list.html:50 common/templates/_sidebar.html:97
#: common/templates/_sidebar.html:197 journal/templates/collection_items.html:8
#: journal/templates/profile.html:109 journal/templates/profile.html:151
#: journal/templates/profile.html:187 journal/templates/replies.html:46
#: catalog/templates/item_review_list.html:50 common/templates/_sidebar.html:37
#: common/templates/_sidebar.html:83 common/templates/_sidebar.html:162
#: common/templates/_sidebar.html:262
#: common/templates/_sidebar_anonymous.html:55
#: journal/templates/collection_items.html:8 journal/templates/profile.html:109
#: journal/templates/profile.html:151 journal/templates/profile.html:187
#: journal/templates/user_collection_list.html:51
#: journal/templates/user_item_list_base.html:25
#: journal/templates/user_tag_list.html:29 social/templates/events.html:46
@ -1346,11 +1354,11 @@ msgstr "{show_title} 第{season_number}季"
msgid "{season_title} E{episode_number}"
msgstr "{season_title} 第{episode_number}集"
#: catalog/views.py:48 catalog/views.py:71
#: catalog/views.py:50 catalog/views.py:73
msgid "Item not found"
msgstr "条目不存在"
#: catalog/views.py:52 catalog/views.py:79
#: catalog/views.py:54 catalog/views.py:81
msgid "Item no longer exists"
msgstr "条目已不存在"
@ -1432,7 +1440,8 @@ msgstr "站点规则"
msgid "Terms"
msgstr "服务协议"
#: common/templates/_footer.html:8 users/templates/users/announcements.html:10
#: common/templates/_footer.html:8 common/templates/_sidebar_anonymous.html:40
#: users/templates/users/announcements.html:10
#: users/templates/users/announcements.html:40
msgid "Announcements"
msgstr "公告栏"
@ -1513,39 +1522,48 @@ msgstr "注册或登录"
msgid "Set a username."
msgstr "设置用户名"
#: common/templates/_sidebar.html:13
#: common/templates/_sidebar.html:14
msgid "Unread Announcements"
msgstr "未读公告"
#: common/templates/_sidebar.html:19
#: common/templates/_sidebar.html:20
msgid "mark all as read"
msgstr "全部标为已读"
#: common/templates/_sidebar.html:52
#: common/templates/_sidebar.html:30
#: common/templates/_sidebar_anonymous.html:48
msgid "Popular Tags"
msgstr "热门标签"
#: common/templates/_sidebar.html:48
msgid "Popular Posts"
msgstr "热门帖文"
#: common/templates/_sidebar.html:117
msgid "approving followers manually"
msgstr "已开启关注审核"
#: common/templates/_sidebar.html:82
#: common/templates/_sidebar.html:147
msgid "Current Targets"
msgstr "当前目标"
#: common/templates/_sidebar.html:95
#: common/templates/_sidebar.html:160
msgid "Set a collection as target, its progress will show up here."
msgstr "将自己或他人的收藏单设为目标,这里就会显示进度"
#: common/templates/_sidebar.html:108
#: common/templates/_sidebar.html:173
msgid "Recent podcast episodes"
msgstr "近期播客节目"
#: common/templates/_sidebar.html:143
#: common/templates/_sidebar.html:208
msgid "Currently reading"
msgstr "正在阅读"
#: common/templates/_sidebar.html:166
#: common/templates/_sidebar.html:231
msgid "Currently watching"
msgstr "正在追看"
#: common/templates/_sidebar.html:189
#: common/templates/_sidebar.html:254
msgid "Common Tags"
msgstr "常用标签"
@ -2478,6 +2496,10 @@ msgstr "收藏单"
msgid "liked collection"
msgstr "喜欢的收藏单"
#: journal/templates/replies.html:46
msgid "no replies so far."
msgstr "暂无回应。"
#: journal/templates/replies.html:53
msgid "add a reply"
msgstr "添加回应"
@ -2515,11 +2537,6 @@ msgstr "删除这个标签"
msgid "Liked Collections"
msgstr "喜欢的收藏单"
#: journal/templates/user_collection_list.html:17
#: journal/templates/user_collection_list.html:31
msgid "Collections"
msgstr "收藏单"
#: journal/templates/user_tag_list.html:12
#: journal/templates/user_tagmember_list.html:4
#: journal/templates/user_tagmember_list.html:13
@ -2671,7 +2688,7 @@ msgstr "标签已更新"
msgid "Summary posted to timeline."
msgstr "总结已发布到时间轴"
#: mastodon/api.py:523 takahe/utils.py:514
#: mastodon/api.py:523 takahe/utils.py:521
#, python-brace-format
msgid "regarding {item_title}, may contain spoiler or triggering content"
msgstr "关于 {item_title},可能包含剧透或敏感内容"
@ -2745,7 +2762,7 @@ msgstr "播放"
msgid "comment"
msgstr "写短评"
#: social/templates/activity/create_collection.html:17 takahe/utils.py:554
#: social/templates/activity/create_collection.html:17 takahe/utils.py:561
msgid "created collection"
msgstr "创建了收藏单"

View file

@ -6,7 +6,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-06-02 15:07-0400\n"
"POT-Creation-Date: 2024-06-03 07:26-0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -667,7 +667,7 @@ msgstr "不再提示"
#: catalog/templates/_item_comments_by_episode.html:78
#: catalog/templates/_item_reviews.html:43
#: catalog/templates/podcast_episode_data.html:41
#: common/templates/_sidebar.html:202
#: common/templates/_sidebar.html:267
msgid "show more"
msgstr "顯示更多"
@ -1067,23 +1067,29 @@ msgstr "本操作不可撤銷。確認合併嗎?"
msgid "Discover"
msgstr "發現"
#: catalog/templates/discover.html:73 journal/templates/profile.html:195
#: catalog/templates/discover.html:84
#: journal/templates/user_collection_list.html:17
#: journal/templates/user_collection_list.html:31
msgid "Collections"
msgstr "收藏單"
#: catalog/templates/discover.html:102 journal/templates/profile.html:195
msgid "edit layout"
msgstr "編輯佈局"
#: catalog/templates/discover.html:76 journal/templates/profile.html:198
#: catalog/templates/discover.html:105 journal/templates/profile.html:198
msgid "save"
msgstr "保存"
#: catalog/templates/discover.html:87 journal/templates/profile.html:209
#: catalog/templates/discover.html:116 journal/templates/profile.html:209
msgid "cancel"
msgstr "取消"
#: catalog/templates/discover.html:93 journal/templates/profile.html:215
#: catalog/templates/discover.html:122 journal/templates/profile.html:215
msgid "show"
msgstr "顯示"
#: catalog/templates/discover.html:94 journal/templates/profile.html:216
#: catalog/templates/discover.html:123 journal/templates/profile.html:216
msgid "hide"
msgstr "隱藏"
@ -1159,10 +1165,12 @@ msgstr "簡介"
#: catalog/templates/item_base.html:236
#: catalog/templates/item_mark_list.html:54
#: catalog/templates/item_review_list.html:50 common/templates/_sidebar.html:97
#: common/templates/_sidebar.html:197 journal/templates/collection_items.html:8
#: journal/templates/profile.html:109 journal/templates/profile.html:151
#: journal/templates/profile.html:187 journal/templates/replies.html:46
#: catalog/templates/item_review_list.html:50 common/templates/_sidebar.html:37
#: common/templates/_sidebar.html:83 common/templates/_sidebar.html:162
#: common/templates/_sidebar.html:262
#: common/templates/_sidebar_anonymous.html:55
#: journal/templates/collection_items.html:8 journal/templates/profile.html:109
#: journal/templates/profile.html:151 journal/templates/profile.html:187
#: journal/templates/user_collection_list.html:51
#: journal/templates/user_item_list_base.html:25
#: journal/templates/user_tag_list.html:29 social/templates/events.html:46
@ -1346,11 +1354,11 @@ msgstr "{show_title} 第{season_number}季"
msgid "{season_title} E{episode_number}"
msgstr "{season_title} 第{episode_number}集"
#: catalog/views.py:48 catalog/views.py:71
#: catalog/views.py:50 catalog/views.py:73
msgid "Item not found"
msgstr "條目不存在"
#: catalog/views.py:52 catalog/views.py:79
#: catalog/views.py:54 catalog/views.py:81
msgid "Item no longer exists"
msgstr "條目已不存在"
@ -1432,7 +1440,8 @@ msgstr "站點規則"
msgid "Terms"
msgstr "服務協議"
#: common/templates/_footer.html:8 users/templates/users/announcements.html:10
#: common/templates/_footer.html:8 common/templates/_sidebar_anonymous.html:40
#: users/templates/users/announcements.html:10
#: users/templates/users/announcements.html:40
msgid "Announcements"
msgstr "公告欄"
@ -1513,39 +1522,48 @@ msgstr "註冊或登錄"
msgid "Set a username."
msgstr "設置用戶名"
#: common/templates/_sidebar.html:13
#: common/templates/_sidebar.html:14
msgid "Unread Announcements"
msgstr "未讀公告"
#: common/templates/_sidebar.html:19
#: common/templates/_sidebar.html:20
msgid "mark all as read"
msgstr "全部標爲已讀"
#: common/templates/_sidebar.html:52
#: common/templates/_sidebar.html:30
#: common/templates/_sidebar_anonymous.html:48
msgid "Popular Tags"
msgstr "熱門標籤"
#: common/templates/_sidebar.html:48
msgid "Popular Posts"
msgstr "熱門帖文"
#: common/templates/_sidebar.html:117
msgid "approving followers manually"
msgstr "已開啓關注審覈"
#: common/templates/_sidebar.html:82
#: common/templates/_sidebar.html:147
msgid "Current Targets"
msgstr "當前目標"
#: common/templates/_sidebar.html:95
#: common/templates/_sidebar.html:160
msgid "Set a collection as target, its progress will show up here."
msgstr "將自己或他人的收藏單設爲目標,這裏就會顯示進度"
#: common/templates/_sidebar.html:108
#: common/templates/_sidebar.html:173
msgid "Recent podcast episodes"
msgstr "近期播客節目"
#: common/templates/_sidebar.html:143
#: common/templates/_sidebar.html:208
msgid "Currently reading"
msgstr "正在閱讀"
#: common/templates/_sidebar.html:166
#: common/templates/_sidebar.html:231
msgid "Currently watching"
msgstr "正在追看"
#: common/templates/_sidebar.html:189
#: common/templates/_sidebar.html:254
msgid "Common Tags"
msgstr "常用標籤"
@ -2478,6 +2496,12 @@ msgstr "收藏單"
msgid "liked collection"
msgstr "喜歡的收藏單"
#: journal/templates/replies.html:46
#, fuzzy
#| msgid "nothing so far."
msgid "no replies so far."
msgstr "暫無回應。"
#: journal/templates/replies.html:53
msgid "add a reply"
msgstr "添加回應"
@ -2515,11 +2539,6 @@ msgstr "刪除這個標籤"
msgid "Liked Collections"
msgstr "喜歡的收藏單"
#: journal/templates/user_collection_list.html:17
#: journal/templates/user_collection_list.html:31
msgid "Collections"
msgstr "收藏單"
#: journal/templates/user_tag_list.html:12
#: journal/templates/user_tagmember_list.html:4
#: journal/templates/user_tagmember_list.html:13
@ -2671,7 +2690,7 @@ msgstr "標籤已更新"
msgid "Summary posted to timeline."
msgstr "總結已發佈到時間軸"
#: mastodon/api.py:523 takahe/utils.py:514
#: mastodon/api.py:523 takahe/utils.py:521
#, python-brace-format
msgid "regarding {item_title}, may contain spoiler or triggering content"
msgstr "關於 {item_title},可能包含劇透或敏感內容"
@ -2745,7 +2764,7 @@ msgstr "播放"
msgid "comment"
msgstr "寫短評"
#: social/templates/activity/create_collection.html:17 takahe/utils.py:554
#: social/templates/activity/create_collection.html:17 takahe/utils.py:561
msgid "created collection"
msgstr "創建了收藏單"

View file

@ -1,10 +1,13 @@
import io
from datetime import timedelta
from typing import TYPE_CHECKING
import blurhash
from django.conf import settings
from django.core.cache import cache
from django.core.files.images import ImageFile
from django.db.models import Count
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from PIL import Image
@ -494,7 +497,11 @@ class Takahe:
@staticmethod
def get_posts(post_pks: list[int]):
return Post.objects.filter(pk__in=post_pks)
return (
Post.objects.filter(pk__in=post_pks)
.exclude(state__in=["deleted", "deleted_fanned_out"])
.prefetch_related("author", "attachments")
)
@staticmethod
def get_post_url(post_pk: int) -> str | None:
@ -912,18 +919,13 @@ class Takahe:
else None
)
user = identity.users.all().first() if identity else None
if not user:
return Announcement.objects.none()
now = timezone.now()
return (
Announcement.objects.filter(
models.Q(start__lte=now) | models.Q(start__isnull=True),
models.Q(end__gte=now) | models.Q(end__isnull=True),
published=True,
)
.order_by("-start", "-created")
.exclude(seen=user)
)
qs = Announcement.objects.filter(
models.Q(start__lte=now) | models.Q(start__isnull=True),
models.Q(end__gte=now) | models.Q(end__isnull=True),
published=True,
).order_by("-start", "-created")
return qs.exclude(seen=user) if user else qs
@staticmethod
def mark_announcements_seen(u: "NeoUser"):
@ -969,3 +971,17 @@ class Takahe:
.exclude(subject_identity_id=identity_id)
.order_by("-created")
)
@staticmethod
def get_popular_posts(days: int = 14, limit: int = 20):
since = timezone.now().date() - timedelta(days=days)
domains = Takahe.get_neodb_peers() + [settings.SITE_DOMAIN]
return (
Post.objects.exclude(state__in=["deleted", "deleted_fanned_out"])
.filter(
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]
)