Merge pull request 'Recognise fediverse community links' (#125) from freamon/pyfedi:test into main

Reviewed-on: https://codeberg.org/rimu/pyfedi/pulls/125
This commit is contained in:
rimu 2024-03-29 02:48:14 +00:00
commit d6d8a83ed5
5 changed files with 82 additions and 3 deletions

View file

@ -18,6 +18,8 @@ from sqlalchemy_searchable import make_searchable
from config import Config from config import Config
import re
def get_locale(): def get_locale():
try: try:
@ -103,6 +105,13 @@ def create_app(config_class=Config):
app.jinja_env.globals['get_resource_as_string'] = get_resource_as_string 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'<a href=https://' + app.config['SERVER_NAME'] + r'/community/lookup/'
return re.sub(pattern, server + r'\g<1>/\g<2>>' + r'!\g<1>@\g<2></a>', link)
app.jinja_env.filters['community_links'] = community_link_to_href
# send error reports via email # send error reports via email
if app.config['MAIL_SERVER'] and app.config['MAIL_ERRORS']: if app.config['MAIL_SERVER'] and app.config['MAIL_ERRORS']:
auth = None auth = None

View file

@ -1023,3 +1023,41 @@ def community_moderate_report_resolve(community_id, report_id):
return redirect(url_for('community.community_moderate', actor=community.link())) return redirect(url_for('community.community_moderate', actor=community.link()))
else: else:
return render_template('community/community_moderate_report_resolve.html', form=form) return render_template('community/community_moderate_report_resolve.html', form=form)
@bp.route('/lookup/<community>/<domain>')
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('/')

View file

@ -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 %}
<div class="row">
<div class="col mx-auto">
<div class="card mt-5">
<div class="card-body p-6">
<div class="card-title">{{ _('Found a community:') }}</div>
<div class="card-body">
<p>
<a href="/c/{{ new_community.link() }}"><img src="{{ new_community.icon_image()}}" class="community_icon rounded-circle" style="width: 30px; vertical-align: middle;" /></a>
<a href="/c/{{ new_community.link() }}">{{ new_community.title }}@{{ new_community.ap_domain }}</a>
</p>
<p> {% if subscribed %}
<a class="btn btn-primary mt-4" href="/community/{{ new_community.link() }}/unsubscribe">{{ _('Leave') }}</a>
{% else %}
<a class="btn btn-primary mt-4" href="/community/{{ new_community.link() }}/subscribe">{{ _('Join') }}</a>
{% endif %}
</p>
</div>
</div>
</div>
</div>
</div>
{% endif %}
{% endblock %}

View file

@ -42,7 +42,7 @@
{% endif %} {% endif %}
</div> </div>
<div class="post_body mt-2"> <div class="post_body mt-2">
{{ post.body_html|safe if post.body_html else '' }} {{ post.body_html|community_links|safe if post.body_html else '' }}
</div> </div>
</div> </div>
{% else %} {% else %}
@ -99,7 +99,7 @@
{% endif %} {% endif %}
{% endif %} {% endif %}
<div class="post_body"> <div class="post_body">
{{ post.body_html|safe if post.body_html else '' }} {{ post.body_html|community_links|safe if post.body_html else '' }}
</div> </div>
</div> </div>
{% endif %} {% endif %}

View file

@ -109,7 +109,7 @@
{% endif %} {% endif %}
</div> </div>
<div class="comment_body hidable {% if comment['comment'].reports and current_user.is_authenticated and post.community.is_moderator(current_user) %}reported{% endif %}"> <div class="comment_body hidable {% if comment['comment'].reports and current_user.is_authenticated and post.community.is_moderator(current_user) %}reported{% endif %}">
{{ comment['comment'].body_html | safe }} {{ comment['comment'].body_html | community_links | safe }}
</div>{% if not comment['comment'].author.indexable %}<!--googleon: all-->{% endif %} </div>{% if not comment['comment'].author.indexable %}<!--googleon: all-->{% endif %}
</div> </div>
<div class="comment_actions hidable"> <div class="comment_actions hidable">