topic tree fixes #145

This commit is contained in:
rimu 2024-04-08 19:48:25 +12:00
parent 4295dff760
commit b2e678926d
5 changed files with 41 additions and 24 deletions

View file

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

View file

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

View file

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

View file

@ -8,14 +8,30 @@
{% block app_content %}
{% if len(topics) > 0 %}
{% macro render_topic(topic, depth) %}
<tr>
<td nowrap="nowrap">{{ '--' * depth }} {{ topic['topic'].name }}</td>
<td>{{ topic['topic'].num_communities }}</td>
<td><a href="{{ url_for('admin.admin_topic_edit', topic_id=topic['topic'].id) }}">Edit</a> |
{% if topic['topic'].num_communities == 0 %}
<a href="{{ url_for('admin.admin_topic_delete', topic_id=topic['topic'].id) }}" class="confirm_first">Delete</a>
{% else %}
Delete
{% endif %}
</td>
</tr>
{% if topic['children'] %}
{% for topic in topic['children'] %}
{{ render_topic(topic, depth + 1)|safe }}
{% endfor %}
{% endif %}
{% endmacro %}
<h1>{{ _('Choose a topic') }}</h1>
<div class="table-responsive-md mt-4">
<table class="communities_table table table-striped table-hover w-100">
<tbody>
{% for topic in topics %}
<tr>
<th class="pl-2"><a href="/topic/{{ topic.machine_name }}">{{ topic.name }}</a></th>
</tr>
{{ render_topic(topic, 0)|safe }}
{% endfor %}
</tbody>
</table>

View file

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