diff --git a/app/__init__.py b/app/__init__.py index c566e8f4..87882c0e 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -18,6 +18,8 @@ from sqlalchemy_searchable import make_searchable from config import Config +import re + def get_locale(): try: @@ -103,6 +105,13 @@ def create_app(config_class=Config): app.jinja_env.globals['get_resource_as_string'] = get_resource_as_string + def community_link_to_href(link: str) -> str: + pattern = r"!([a-zA-Z0-9_.-]*)@([a-zA-Z0-9_.-]*)\b" + server = r'/\g<2>>' + r'!\g<1>@\g<2>', link) + + app.jinja_env.filters['community_links'] = community_link_to_href + # send error reports via email if app.config['MAIL_SERVER'] and app.config['MAIL_ERRORS']: auth = None diff --git a/app/community/routes.py b/app/community/routes.py index 6c32031b..11e27adb 100644 --- a/app/community/routes.py +++ b/app/community/routes.py @@ -1023,3 +1023,41 @@ def community_moderate_report_resolve(community_id, report_id): return redirect(url_for('community.community_moderate', actor=community.link())) else: return render_template('community/community_moderate_report_resolve.html', form=form) + + +@bp.route('/lookup//') +def lookup(community, domain): + if domain == current_app.config['SERVER_NAME']: + return redirect('/c/' + community) + + exists = Community.query.filter_by(name=community, ap_domain=domain).first() + if exists: + return redirect('/c/' + community + '@' + domain) + else: + address = '!' + community + '@' + domain + if current_user.is_authenticated: + new_community = None + + new_community = search_for_community(address) + if new_community is None: + if g.site.enable_nsfw: + flash(_('Community not found.'), 'warning') + else: + flash(_('Community not found. If you are searching for a nsfw community it is blocked by this instance.'), 'warning') + else: + if new_community.banned: + flash(_('That community is banned from %(site)s.', site=g.site.name), 'warning') + + return render_template('community/lookup_remote.html', + title=_('Search result for remote community'), new_community=new_community, + subscribed=community_membership(current_user, new_community) >= SUBSCRIPTION_MEMBER) + else: + # send them back where they came from + flash('Searching for remote communities requires login', 'error') + referrer = request.headers.get('Referer', None) + if referrer is not None: + return redirect(referrer) + else: + return redirect('/') + + diff --git a/app/templates/community/lookup_remote.html b/app/templates/community/lookup_remote.html new file mode 100644 index 00000000..074204da --- /dev/null +++ b/app/templates/community/lookup_remote.html @@ -0,0 +1,32 @@ +{% if theme() and file_exists('app/templates/themes/' + theme() + '/base.html') %} + {% extends 'themes/' + theme() + '/base.html' %} +{% else %} + {% extends "base.html" %} +{% endif %} %} +{% from 'bootstrap/form.html' import render_form %} + +{% block app_content %} + {% if new_community and not new_community.banned %} + + + + + {{ _('Found a community:') }} + + + + {{ new_community.title }}@{{ new_community.ap_domain }} + + {% if subscribed %} + {{ _('Leave') }} + {% else %} + {{ _('Join') }} + {% endif %} + + + + + + + {% endif %} +{% endblock %} diff --git a/app/templates/post/_post_full.html b/app/templates/post/_post_full.html index 18ccccd8..9d384edd 100644 --- a/app/templates/post/_post_full.html +++ b/app/templates/post/_post_full.html @@ -42,7 +42,7 @@ {% endif %} - {{ post.body_html|safe if post.body_html else '' }} + {{ post.body_html|community_links|safe if post.body_html else '' }} {% else %} @@ -99,7 +99,7 @@ {% endif %} {% endif %} - {{ post.body_html|safe if post.body_html else '' }} + {{ post.body_html|community_links|safe if post.body_html else '' }} {% endif %} diff --git a/app/templates/post/post.html b/app/templates/post/post.html index 7d7f7c6a..b47b130c 100644 --- a/app/templates/post/post.html +++ b/app/templates/post/post.html @@ -109,7 +109,7 @@ {% endif %} - {{ comment['comment'].body_html | safe }} + {{ comment['comment'].body_html | community_links | safe }} {% if not comment['comment'].author.indexable %}{% endif %}
+ + {{ new_community.title }}@{{ new_community.ap_domain }} +
{% if subscribed %} + {{ _('Leave') }} + {% else %} + {{ _('Join') }} + {% endif %} +