diff --git a/app/admin/routes.py b/app/admin/routes.py index 6fbac168..fd1192f2 100644 --- a/app/admin/routes.py +++ b/app/admin/routes.py @@ -16,7 +16,8 @@ from app.admin.util import unsubscribe_from_everything_then_delete, unsubscribe_ from app.community.util import save_icon_file, save_banner_file from app.models import AllowedInstances, BannedInstances, ActivityPubLog, utcnow, Site, Community, CommunityMember, \ User, Instance, File, Report, Topic -from app.utils import render_template, permission_required, set_setting, get_setting, gibberish, markdown_to_html +from app.utils import render_template, permission_required, set_setting, get_setting, gibberish, markdown_to_html, \ + moderating_communities, joined_communities from app.admin import bp @@ -24,7 +25,8 @@ from app.admin import bp @login_required @permission_required('change instance settings') def admin_home(): - return render_template('admin/home.html', title=_('Admin')) + return render_template('admin/home.html', title=_('Admin'), moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id())) @bp.route('/site', methods=['GET', 'POST']) @@ -50,7 +52,10 @@ def admin_site(): form.description.data = site.description form.sidebar.data = site.sidebar form.legal_information.data = site.legal_information - return render_template('admin/site.html', title=_('Site profile'), form=form) + return render_template('admin/site.html', title=_('Site profile'), form=form, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/misc', methods=['GET', 'POST']) @@ -86,7 +91,10 @@ def admin_misc(): form.reports_email_admins.data = site.reports_email_admins form.registration_mode.data = site.registration_mode form.application_question.data = site.application_question - return render_template('admin/misc.html', title=_('Misc settings'), form=form) + return render_template('admin/misc.html', title=_('Misc settings'), form=form, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/federation', methods=['GET', 'POST']) @@ -123,7 +131,10 @@ def admin_federation(): instances = AllowedInstances.query.all() form.allowlist.data = '\n'.join([instance.domain for instance in instances]) - return render_template('admin/federation.html', title=_('Federation settings'), form=form) + return render_template('admin/federation.html', title=_('Federation settings'), form=form, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/activities', methods=['GET']) @@ -184,7 +195,8 @@ def admin_communities(): prev_url = url_for('admin.admin_communities', page=communities.prev_num) if communities.has_prev and page != 1 else None return render_template('admin/communities.html', title=_('Communities'), next_url=next_url, prev_url=prev_url, - communities=communities) + communities=communities, moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id())) def topics_for_form(): @@ -262,7 +274,10 @@ def admin_community_edit(community_id): form.low_quality.data = community.low_quality form.content_retention.data = community.content_retention form.topic.data = community.topic_id if community.topic_id else None - return render_template('admin/edit_community.html', title=_('Edit community'), form=form, community=community) + return render_template('admin/edit_community.html', title=_('Edit community'), form=form, community=community, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/community//delete', methods=['GET']) @@ -311,7 +326,10 @@ def unsubscribe_everyone_then_delete_task(community_id): @permission_required('administer all communities') def admin_topics(): topics = Topic.query.order_by(Topic.name).all() - return render_template('admin/topics.html', title=_('Topics'), topics=topics) + return render_template('admin/topics.html', title=_('Topics'), topics=topics, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/topic/add', methods=['GET', 'POST']) @@ -326,7 +344,10 @@ def admin_topic_add(): flash(_('Saved')) return redirect(url_for('admin.admin_topics')) - return render_template('admin/edit_topic.html', title=_('Add topic'), form=form) + return render_template('admin/edit_topic.html', title=_('Add topic'), form=form, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/topic//edit', methods=['GET', 'POST']) @login_required @@ -346,7 +367,10 @@ def admin_topic_edit(topic_id): return redirect(url_for('admin.admin_topics')) else: form.name.data = topic.name - return render_template('admin/edit_topic.html', title=_('Edit topic'), form=form, topic=topic) + return render_template('admin/edit_topic.html', title=_('Edit topic'), form=form, topic=topic, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/topic//delete', methods=['GET']) @@ -387,7 +411,10 @@ def admin_users(): prev_url = url_for('admin.admin_users', page=users.prev_num) if users.has_prev and page != 1 else None return render_template('admin/users.html', title=_('Users'), next_url=next_url, prev_url=prev_url, users=users, - local_remote=local_remote, search=search) + local_remote=local_remote, search=search, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/user//edit', methods=['GET', 'POST']) @@ -452,7 +479,10 @@ def admin_user_edit(user_id): form.indexable.data = user.indexable form.manually_approves_followers.data = user.ap_manually_approves_followers - return render_template('admin/edit_user.html', title=_('Edit user'), form=form, user=user) + return render_template('admin/edit_user.html', title=_('Edit user'), form=form, user=user, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/user//delete', methods=['GET']) @@ -496,4 +526,7 @@ def admin_reports(): prev_url = url_for('admin.admin_reports', page=reports.prev_num) if reports.has_prev and page != 1 else None return render_template('admin/reports.html', title=_('Reports'), next_url=next_url, prev_url=prev_url, reports=reports, - local_remote=local_remote, search=search) \ No newline at end of file + local_remote=local_remote, search=search, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) \ No newline at end of file diff --git a/app/community/routes.py b/app/community/routes.py index d06fb3ef..d6f42ece 100644 --- a/app/community/routes.py +++ b/app/community/routes.py @@ -17,7 +17,8 @@ from app.models import User, Community, CommunityMember, CommunityJoinRequest, C from app.community import bp from app.utils import get_setting, render_template, allowlist_html, markdown_to_html, validation_required, \ shorten_string, markdown_to_text, domain_from_url, validate_image, gibberish, community_membership, ap_datetime, \ - request_etag_matches, return_304, instance_banned, can_create, can_upvote, can_downvote, user_filters_posts + request_etag_matches, return_304, instance_banned, can_create, can_upvote, can_downvote, user_filters_posts, \ + joined_communities, moderating_communities from feedgen.feed import FeedGenerator from datetime import timezone, timedelta @@ -60,9 +61,12 @@ def add_local(): db.session.commit() flash(_('Your new community has been created.')) cache.delete_memoized(community_membership, current_user, community) + cache.delete_memoized(joined_communities, current_user.id) + cache.delete_memoized(moderating_communities, current_user.id) return redirect('/c/' + community.name) - return render_template('community/add_local.html', title=_('Create community'), form=form) + return render_template('community/add_local.html', title=_('Create community'), form=form, moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id())) @bp.route('/add_remote', methods=['GET', 'POST']) @@ -86,7 +90,8 @@ def add_remote(): return render_template('community/add_remote.html', title=_('Add remote community'), form=form, new_community=new_community, - subscribed=community_membership(current_user, new_community) >= SUBSCRIPTION_MEMBER) + subscribed=community_membership(current_user, new_community) >= SUBSCRIPTION_MEMBER, moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id())) # @bp.route('/c/', methods=['GET']) - defined in activitypub/routes.py, which calls this function for user requests. A bit weird. @@ -145,7 +150,8 @@ def show_community(community: Community): etag=f"{community.id}_{hash(community.last_active)}", next_url=next_url, prev_url=prev_url, low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1', rss_feed=f"https://{current_app.config['SERVER_NAME']}/community/{community.link()}/feed", rss_feed_name=f"{community.title} posts on PieFed", - content_filters=content_filters) + content_filters=content_filters, moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id())) # RSS feed of the community @@ -239,6 +245,7 @@ def subscribe(actor): flash('You joined ' + community.title) referrer = request.headers.get('Referer', None) cache.delete_memoized(community_membership, current_user, community) + cache.delete_memoized(joined_communities, current_user.id) if referrer is not None: return redirect(referrer) else: @@ -292,7 +299,7 @@ def unsubscribe(actor): flash('You have left ' + community.title) cache.delete_memoized(community_membership, current_user, community) - + cache.delete_memoized(joined_communities, current_user.id) else: # todo: community deletion flash('You need to make someone else the owner before unsubscribing.', 'warning') @@ -432,7 +439,10 @@ def add_post(actor): form.notify_author.data = True return render_template('community/add_post.html', title=_('Add post to community'), form=form, community=community, - images_disabled=images_disabled, markdown_editor=True, low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1') + images_disabled=images_disabled, markdown_editor=True, low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1', + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities = joined_communities(current_user.id) + ) @bp.route('/community//report', methods=['GET', 'POST']) @@ -484,7 +494,8 @@ def community_delete(community_id: int): return redirect('/communities') return render_template('community/community_delete.html', title=_('Delete community'), form=form, - community=community) + community=community, moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id())) else: abort(401) diff --git a/app/main/routes.py b/app/main/routes.py index f8616be0..f9a82e1d 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -15,7 +15,8 @@ from flask_babel import _, get_locale from sqlalchemy import select, desc, text 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 + ap_datetime, ip_address, retrieve_block_list, shorten_string, markdown_to_text, user_filters_home, \ + joined_communities, moderating_communities from app.models import Community, CommunityMember, Post, Site, User, utcnow, Domain, Topic @@ -61,7 +62,8 @@ def index(): etag=f"home_{hash(str(g.site.last_active))}", next_url=next_url, prev_url=prev_url, rss_feed=f"https://{current_app.config['SERVER_NAME']}/feed", rss_feed_name=f"Posts on " + g.site.name, title=f"{g.site.name} - {g.site.description}", description=shorten_string(markdown_to_text(g.site.sidebar), 150), - content_filters=content_filters) + content_filters=content_filters, moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id())) @bp.route('/new', methods=['HEAD', 'GET', 'POST']) @@ -100,7 +102,8 @@ def new_posts(): etag=f"home_{hash(str(g.site.last_active))}", next_url=next_url, prev_url=prev_url, rss_feed=f"https://{current_app.config['SERVER_NAME']}/feed", rss_feed_name=f"Posts on " + g.site.name, low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1', - content_filters=content_filters) + content_filters=content_filters, moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id())) @bp.route('/top', methods=['HEAD', 'GET', 'POST']) @@ -139,7 +142,18 @@ def top_posts(): etag=f"home_{hash(str(g.site.last_active))}", next_url=next_url, prev_url=prev_url, rss_feed=f"https://{current_app.config['SERVER_NAME']}/feed", rss_feed_name=f"Posts on " + g.site.name, low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1', - content_filters=content_filters) + content_filters=content_filters, moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id())) + + +@bp.route('/topics', methods=['GET']) +def list_topics(): + verification_warning() + topics = Topic.query.order_by(Topic.name).all() + + return render_template('list_topics.html', topics=topics, title=_('Browse by topic'), + low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1', moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id())) @bp.route('/communities', methods=['GET']) @@ -153,7 +167,7 @@ def list_communities(): pass else: flash('Sorry, no search function yet. Use the topic filter for now.', 'warning') - communities = Community.query.filter_by(banned=False) + # communities = Community.query.filter_by(banned=False) #query = search(select(Community), search_param, sort=True) # todo: exclude banned communities from search #communities = db.session.scalars(query).all() @@ -165,7 +179,8 @@ def list_communities(): SUBSCRIPTION_PENDING=SUBSCRIPTION_PENDING, SUBSCRIPTION_MEMBER=SUBSCRIPTION_MEMBER, SUBSCRIPTION_OWNER=SUBSCRIPTION_OWNER, SUBSCRIPTION_MODERATOR=SUBSCRIPTION_MODERATOR, topics=topics, topic_id=topic_id, sort_by=sort_by, - low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1') + low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1', moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id())) @bp.route('/communities/local', methods=['GET']) @@ -176,7 +191,8 @@ def list_local_communities(): return render_template('list_communities.html', communities=communities.order_by(sort_by).all(), title=_('Local communities'), sort_by=sort_by, SUBSCRIPTION_PENDING=SUBSCRIPTION_PENDING, SUBSCRIPTION_MEMBER=SUBSCRIPTION_MEMBER, SUBSCRIPTION_MODERATOR=SUBSCRIPTION_MODERATOR, - low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1') + low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1', moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id())) @bp.route('/communities/subscribed', methods=['GET']) @@ -190,7 +206,8 @@ def list_subscribed_communities(): return render_template('list_communities.html', communities=communities.order_by(sort_by).all(), title=_('Joined communities'), SUBSCRIPTION_PENDING=SUBSCRIPTION_PENDING, SUBSCRIPTION_MEMBER=SUBSCRIPTION_MEMBER, sort_by=sort_by, SUBSCRIPTION_MODERATOR=SUBSCRIPTION_MODERATOR, - low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1') + low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1', moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id())) @bp.route('/donate') @@ -217,7 +234,7 @@ def robots(): @bp.route('/test') def test(): - return 'done' + return str(current_user.get_id()) #ip = request.headers.get('X-Forwarded-For') or request.remote_addr #if ',' in ip: # Remove all but first ip addresses diff --git a/app/models.py b/app/models.py index f4f5e27e..7eeccdee 100644 --- a/app/models.py +++ b/app/models.py @@ -379,6 +379,12 @@ class User(UserMixin, db.Model): except Exception: return False + def get_id(self): + if self.is_authenticated: + return self.id + else: + return None + def display_name(self): if self.deleted is False: if self.title: diff --git a/app/post/routes.py b/app/post/routes.py index 11ed67ff..1aa5a8de 100644 --- a/app/post/routes.py +++ b/app/post/routes.py @@ -19,7 +19,7 @@ from app.post import bp from app.utils import get_setting, render_template, allowlist_html, markdown_to_html, validation_required, \ shorten_string, markdown_to_text, domain_from_url, validate_image, gibberish, ap_datetime, return_304, \ request_etag_matches, ip_address, user_ip_banned, instance_banned, can_downvote, can_upvote, post_ranking, \ - reply_already_exists, reply_is_just_link_to_gif_reaction, confidence + reply_already_exists, reply_is_just_link_to_gif_reaction, confidence, moderating_communities, joined_communities def show_post(post_id: int): @@ -362,7 +362,8 @@ def continue_discussion(post_id, comment_id): replies = get_comment_branch(post.id, comment.id, 'top') return render_template('post/continue_discussion.html', title=_('Discussing %(title)s', title=post.title), post=post, - is_moderator=is_moderator, comment=comment, replies=replies, markdown_editor=True) + is_moderator=is_moderator, comment=comment, replies=replies, markdown_editor=True, moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id())) @bp.route('/post//comment//reply', methods=['GET', 'POST']) @@ -522,20 +523,26 @@ def add_reply(post_id: int, comment_id: int): else: form.notify_author.data = True return render_template('post/add_reply.html', title=_('Discussing %(title)s', title=post.title), post=post, - is_moderator=is_moderator, form=form, comment=in_reply_to, markdown_editor=True) + is_moderator=is_moderator, form=form, comment=in_reply_to, markdown_editor=True, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities = joined_communities(current_user.id)) @bp.route('/post//options', methods=['GET']) def post_options(post_id: int): post = Post.query.get_or_404(post_id) - return render_template('post/post_options.html', post=post) + return render_template('post/post_options.html', post=post, moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id())) @bp.route('/post//comment//options', methods=['GET']) def post_reply_options(post_id: int, comment_id: int): post = Post.query.get_or_404(post_id) post_reply = PostReply.query.get_or_404(comment_id) - return render_template('post/post_reply_options.html', post=post, post_reply=post_reply) + return render_template('post/post_reply_options.html', post=post, post_reply=post_reply, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/post//edit', methods=['GET', 'POST']) @@ -577,7 +584,10 @@ def post_edit(post_id: int): form.image_body.data = post.body form.notify_author.data = post.notify_author return render_template('post/post_edit.html', title=_('Edit post'), form=form, post=post, - images_disabled=images_disabled, markdown_editor=True) + images_disabled=images_disabled, markdown_editor=True, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) else: abort(401) @@ -654,7 +664,10 @@ def post_report(post_id: int): elif request.method == 'GET': form.report_remote.data = True - return render_template('post/post_report.html', title=_('Report post'), form=form, post=post) + return render_template('post/post_report.html', title=_('Report post'), form=form, post=post, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/post//block_user', methods=['GET', 'POST']) @@ -709,7 +722,10 @@ def post_mea_culpa(post_id: int): db.session.commit() return redirect(url_for('activitypub.post_ap', post_id=post.id)) - return render_template('post/post_mea_culpa.html', title=_('I changed my mind'), form=form, post=post) + return render_template('post/post_mea_culpa.html', title=_('I changed my mind'), form=form, post=post, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/post//comment//report', methods=['GET', 'POST']) @@ -749,7 +765,10 @@ def post_reply_report(post_id: int, comment_id: int): elif request.method == 'GET': form.report_remote.data = True - return render_template('post/post_reply_report.html', title=_('Report comment'), form=form, post=post, post_reply=post_reply) + return render_template('post/post_reply_report.html', title=_('Report comment'), form=form, post=post, post_reply=post_reply, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/post//comment//block_user', methods=['GET', 'POST']) @@ -807,7 +826,8 @@ def post_reply_edit(post_id: int, comment_id: int): form.body.data = post_reply.body form.notify_author.data = not post_reply.notify_author return render_template('post/post_reply_edit.html', title=_('Edit comment'), form=form, post=post, post_reply=post_reply, - comment=comment, markdown_editor=True) + comment=comment, markdown_editor=True, moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id())) else: abort(401) diff --git a/app/templates/base.html b/app/templates/base.html index 79975c77..eac40738 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -98,8 +98,34 @@ {% else %} - - + + {% if user_access('change instance settings', current_user.id) %} @@ -155,7 +181,7 @@ {% endif %} {% endblock %} {% if not low_bandwidth %} - {{ bootstrap.load_js() }} + {{ str(bootstrap.load_js()).replace(' diff --git a/app/templates/list_topics.html b/app/templates/list_topics.html new file mode 100644 index 00000000..50ed9183 --- /dev/null +++ b/app/templates/list_topics.html @@ -0,0 +1,21 @@ +{% extends "base.html" %} +{% from 'bootstrap5/form.html' import render_form %} + +{% block app_content %} +{% if len(topics) > 0 %} +

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

+
+ + + {% for topic in topics %} + + + + {% endfor %} + +
{{ topic.name }}
+
+{% else %} +

{{ _('There are no communities yet.') }}

+{% endif %} +{% endblock %} diff --git a/app/user/routes.py b/app/user/routes.py index 6e1c5a44..125ba6d5 100644 --- a/app/user/routes.py +++ b/app/user/routes.py @@ -16,7 +16,7 @@ from app.user import bp from app.user.forms import ProfileForm, SettingsForm, DeleteAccountForm, ReportUserForm, FilterEditForm from app.utils import get_setting, render_template, markdown_to_html, user_access, markdown_to_text, shorten_string, \ is_image_url, ensure_directory_exists, gibberish, file_get_contents, community_membership, user_filters_home, \ - user_filters_posts, user_filters_replies + user_filters_posts, user_filters_replies, moderating_communities, joined_communities from sqlalchemy import desc, or_, text import os @@ -25,7 +25,8 @@ import os @login_required def show_people(): people = User.query.filter_by(ap_id=None, deleted=False, banned=False).all() - return render_template('user/people.html', people=people) + return render_template('user/people.html', people=people, moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id())) def show_profile(user): @@ -72,7 +73,10 @@ def show_profile(user): user_name=user.user_name), description=description, subscribed=subscribed, upvoted=upvoted, post_next_url=post_next_url, post_prev_url=post_prev_url, - replies_next_url=replies_next_url, replies_prev_url=replies_prev_url) + replies_next_url=replies_next_url, replies_prev_url=replies_prev_url, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/u//profile', methods=['GET', 'POST']) @@ -134,7 +138,10 @@ def edit_profile(actor): form.matrix_user_id.data = current_user.matrix_user_id form.password_field.data = '' - return render_template('user/edit_profile.html', title=_('Edit profile'), form=form, user=current_user) + return render_template('user/edit_profile.html', title=_('Edit profile'), form=form, user=current_user, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/user/settings', methods=['GET', 'POST']) @@ -181,7 +188,10 @@ def change_settings(): form.searchable.data = current_user.searchable form.indexable.data = current_user.indexable - return render_template('user/edit_settings.html', title=_('Edit profile'), form=form, user=current_user) + return render_template('user/edit_settings.html', title=_('Edit profile'), form=form, user=current_user, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/u//ban', methods=['GET']) @@ -325,7 +335,10 @@ def report_profile(actor): elif request.method == 'GET': form.report_remote.data = True - return render_template('user/user_report.html', title=_('Report user'), form=form, user=user) + return render_template('user/user_report.html', title=_('Report user'), form=form, user=user, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/u//delete', methods=['GET']) @@ -386,7 +399,10 @@ def delete_account(): elif request.method == 'GET': ... - return render_template('user/delete_account.html', title=_('Delete my account'), form=form, user=current_user) + return render_template('user/delete_account.html', title=_('Delete my account'), form=form, user=current_user, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @celery.task @@ -462,7 +478,10 @@ def notifications(): notification_list = Notification.query.filter_by(user_id=current_user.id).order_by(desc(Notification.created_at)).all() - return render_template('user/notifications.html', title=_('Notifications'), notifications=notification_list, user=current_user) + return render_template('user/notifications.html', title=_('Notifications'), notifications=notification_list, user=current_user, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/notification//goto', methods=['GET', 'POST']) @@ -598,7 +617,10 @@ def user_settings_filters_add(): flash(_('Your changes have been saved.'), 'success') return redirect(url_for('user.user_settings_filters')) - return render_template('user/edit_filters.html', title=_('Add filter'), form=form, user=current_user) + return render_template('user/edit_filters.html', title=_('Add filter'), form=form, user=current_user, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/user/settings/filters//edit', methods=['GET', 'POST']) @@ -635,7 +657,10 @@ def user_settings_filters_edit(filter_id): form.keywords.data = content_filter.keywords form.expire_after.data = content_filter.expire_after - return render_template('user/edit_filters.html', title=_('Edit filter'), form=form, content_filter=content_filter, user=current_user) + return render_template('user/edit_filters.html', title=_('Edit filter'), form=form, content_filter=content_filter, user=current_user, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()) + ) @bp.route('/user/settings/filters//delete', methods=['GET', 'POST']) diff --git a/app/utils.py b/app/utils.py index a6cda23d..1e0f5816 100644 --- a/app/utils.py +++ b/app/utils.py @@ -23,7 +23,7 @@ from app import db, cache import re from app.models import Settings, Domain, Instance, BannedInstances, User, Community, DomainBlock, ActivityPubLog, IpBan, \ - Site, Post, PostReply, utcnow, Filter + Site, Post, PostReply, utcnow, Filter, CommunityMember # Flask's render_template function, with support for themes added @@ -551,6 +551,27 @@ def user_filters_replies(user_id): return result +@cache.memoize(timeout=300) +def moderating_communities(user_id): + if user_id is None: + return [] + return Community.query.join(CommunityMember, Community.id == CommunityMember.community_id).\ + filter(Community.banned == False).\ + filter(or_(CommunityMember.is_moderator == True, CommunityMember.is_owner == True)).\ + filter(CommunityMember.user_id == user_id).order_by(Community.title).all() + + +@cache.memoize(timeout=300) +def joined_communities(user_id): + if user_id is None: + return [] + return Community.query.join(CommunityMember, Community.id == CommunityMember.community_id).\ + filter(Community.banned == False). \ + filter(CommunityMember.is_moderator == False, CommunityMember.is_owner == False). \ + filter(CommunityMember.user_id == user_id).order_by(Community.title).all() + + + # 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)