From b2e678926d98612a7df5fcc53844bfd3a02c2427 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Mon, 8 Apr 2024 19:48:25 +1200 Subject: [PATCH] topic tree fixes #145 --- app/admin/routes.py | 5 +++-- app/admin/util.py | 17 +---------------- app/main/routes.py | 4 ++-- app/templates/list_topics.html | 22 +++++++++++++++++++--- app/utils.py | 17 ++++++++++++++++- 5 files changed, 41 insertions(+), 24 deletions(-) diff --git a/app/admin/routes.py b/app/admin/routes.py index 8a6b5fb8..f1f197dc 100644 --- a/app/admin/routes.py +++ b/app/admin/routes.py @@ -14,13 +14,14 @@ from app.activitypub.util import default_context, instance_allowed, instance_blo from app.admin.forms import FederationForm, SiteMiscForm, SiteProfileForm, EditCommunityForm, EditUserForm, \ EditTopicForm, SendNewsletterForm, AddUserForm from app.admin.util import unsubscribe_from_everything_then_delete, unsubscribe_from_community, send_newsletter, \ - topic_tree, topics_for_form + topics_for_form from app.community.util import save_icon_file, save_banner_file from app.constants import REPORT_STATE_NEW, REPORT_STATE_ESCALATED from app.models import AllowedInstances, BannedInstances, ActivityPubLog, utcnow, Site, Community, CommunityMember, \ User, Instance, File, Report, Topic, UserRegistration, Role, Post from app.utils import render_template, permission_required, set_setting, get_setting, gibberish, markdown_to_html, \ - moderating_communities, joined_communities, finalize_user_setup, theme_list, blocked_phrases, blocked_referrers + moderating_communities, joined_communities, finalize_user_setup, theme_list, blocked_phrases, blocked_referrers, \ + topic_tree from app.admin import bp diff --git a/app/admin/util.py b/app/admin/util.py index b1669ac1..70462b23 100644 --- a/app/admin/util.py +++ b/app/admin/util.py @@ -9,7 +9,7 @@ from app import db, cache, celery from app.activitypub.signature import post_request from app.activitypub.util import default_context from app.models import User, Community, Instance, Site, ActivityPubLog, CommunityMember, Topic -from app.utils import gibberish +from app.utils import gibberish, topic_tree def unsubscribe_from_everything_then_delete(user_id): @@ -106,21 +106,6 @@ def send_newsletter(form): break -# replies to a post, in a tree, sorted by a variety of methods -def topic_tree() -> List: - topics = Topic.query.order_by(Topic.name) - - topics_dict = {topic.id: {'topic': topic, 'children': []} for topic in topics.all()} - - for topic in topics: - if topic.parent_id is not None: - parent_comment = topics_dict.get(topic.parent_id) - if parent_comment: - parent_comment['children'].append(topics_dict[topic.id]) - - return [topic for topic in topics_dict.values() if topic['topic'].parent_id is None] - - def topics_for_form(current_topic: int) -> List[Tuple[int, str]]: result = [(0, _('None'))] topics = topic_tree() diff --git a/app/main/routes.py b/app/main/routes.py index dfaf1317..9df40e6c 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -25,7 +25,7 @@ from sqlalchemy_searchable import search from app.utils import render_template, get_setting, gibberish, request_etag_matches, return_304, blocked_domains, \ ap_datetime, ip_address, retrieve_block_list, shorten_string, markdown_to_text, user_filters_home, \ joined_communities, moderating_communities, parse_page, theme_list, get_request, markdown_to_html, allowlist_html, \ - blocked_instances, communities_banned_from + blocked_instances, communities_banned_from, topic_tree from app.models import Community, CommunityMember, Post, Site, User, utcnow, Domain, Topic, File, Instance, \ InstanceRole, Notification from PIL import Image @@ -158,7 +158,7 @@ def home_page(type, sort): @bp.route('/topics', methods=['GET']) def list_topics(): verification_warning() - topics = Topic.query.filter_by(parent_id=None).order_by(Topic.name).all() + topics = topic_tree() return render_template('list_topics.html', topics=topics, title=_('Browse by topic'), low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1', diff --git a/app/templates/list_topics.html b/app/templates/list_topics.html index f25a0b9b..d6ec82ca 100644 --- a/app/templates/list_topics.html +++ b/app/templates/list_topics.html @@ -8,14 +8,30 @@ {% block app_content %} {% if len(topics) > 0 %} + {% macro render_topic(topic, depth) %} + + {{ '--' * depth }} {{ topic['topic'].name }} + {{ topic['topic'].num_communities }} + Edit | + {% if topic['topic'].num_communities == 0 %} + Delete + {% else %} + Delete + {% endif %} + + + {% if topic['children'] %} + {% for topic in topic['children'] %} + {{ render_topic(topic, depth + 1)|safe }} + {% endfor %} + {% endif %} + {% endmacro %}

{{ _('Choose a topic') }}

{% for topic in topics %} - - - + {{ render_topic(topic, 0)|safe }} {% endfor %}
{{ topic.name }}
diff --git a/app/utils.py b/app/utils.py index a88cda93..e5ebe888 100644 --- a/app/utils.py +++ b/app/utils.py @@ -28,7 +28,7 @@ import re from app.email import send_welcome_email from app.models import Settings, Domain, Instance, BannedInstances, User, Community, DomainBlock, ActivityPubLog, IpBan, \ - Site, Post, PostReply, utcnow, Filter, CommunityMember, InstanceBlock, CommunityBan + Site, Post, PostReply, utcnow, Filter, CommunityMember, InstanceBlock, CommunityBan, Topic # Flask's render_template function, with support for themes added @@ -679,6 +679,21 @@ def finalize_user_setup(user, application_required=False): send_welcome_email(user, application_required) +# topics, in a tree +def topic_tree() -> List: + topics = Topic.query.order_by(Topic.name) + + topics_dict = {topic.id: {'topic': topic, 'children': []} for topic in topics.all()} + + for topic in topics: + if topic.parent_id is not None: + parent_comment = topics_dict.get(topic.parent_id) + if parent_comment: + parent_comment['children'].append(topics_dict[topic.id]) + + return [topic for topic in topics_dict.values() if topic['topic'].parent_id is None] + + # All the following post/comment ranking math is explained at https://medium.com/hacking-and-gonzo/how-reddit-ranking-algorithms-work-ef111e33d0d9 epoch = datetime(1970, 1, 1)