From 850e159a58a5ee34b6bdae978c1a9a3c86ecc051 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Sun, 12 May 2024 14:16:12 +1200 Subject: [PATCH] add follow on mastodon button #2 --- app/post/forms.py | 2 +- app/static/js/scripts.js | 14 +++++ app/templates/user/mastodon_redirect.html | 64 +++++++++++++++++++++++ app/templates/user/show_profile.html | 8 ++- app/user/forms.py | 14 +++-- app/user/routes.py | 21 +++++++- 6 files changed, 115 insertions(+), 8 deletions(-) create mode 100644 app/templates/user/mastodon_redirect.html diff --git a/app/post/forms.py b/app/post/forms.py index 23a51c01..bdc24272 100644 --- a/app/post/forms.py +++ b/app/post/forms.py @@ -8,7 +8,7 @@ from app.utils import MultiCheckboxField class NewReplyForm(FlaskForm): - body = TextAreaField(_l('Body'), render_kw={'placeholder': 'What are your thoughts?', 'rows': 5}, validators={DataRequired(), Length(min=1, max=5000)}) + body = TextAreaField(_l('Body'), render_kw={'placeholder': 'What are your thoughts?', 'rows': 5}, validators=[DataRequired(), Length(min=1, max=5000)]) notify_author = BooleanField(_l('Notify about replies')) language_id = SelectField(_l('Language'), validators=[DataRequired()], coerce=int, render_kw={'class': 'form-select language-float-right'}) submit = SubmitField(_l('Comment')) diff --git a/app/static/js/scripts.js b/app/static/js/scripts.js index 40758ff2..286daebe 100644 --- a/app/static/js/scripts.js +++ b/app/static/js/scripts.js @@ -632,6 +632,20 @@ function setupMarkdownEditorEnabler() { }); } +function getCookie(name) { + var cookies = document.cookie.split(';'); + + for (var i = 0; i < cookies.length; i++) { + var cookie = cookies[i].trim(); + + if (cookie.indexOf(name + '=') === 0) { + return cookie.substring(name.length + 1); + } + } + + return null; +} + /* register a service worker */ if ('serviceWorker' in navigator) { window.addEventListener('load', function() { diff --git a/app/templates/user/mastodon_redirect.html b/app/templates/user/mastodon_redirect.html new file mode 100644 index 00000000..5b6d5d10 --- /dev/null +++ b/app/templates/user/mastodon_redirect.html @@ -0,0 +1,64 @@ +{% 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 %} +{% set active_child = 'settings' %} + +{% block app_content %} + +
+
+ +

{{ _('Follow on Mastodon') }}

+

{{ _('We are about to send you to your mastodon instance where you will then need to click the "Follow" button.') }}

+ {{ render_form(form) }} + + {% if send_to %} +
+ + {% endif %} +
+
+{% endblock %} \ No newline at end of file diff --git a/app/templates/user/show_profile.html b/app/templates/user/show_profile.html index 3f9babd8..4c71dc14 100644 --- a/app/templates/user/show_profile.html +++ b/app/templates/user/show_profile.html @@ -64,8 +64,8 @@ {% endif %} {% endif %} +
{% if current_user.is_authenticated and current_user != user %} -
{{ _('Send message') }} {% if user.matrix_user_id %} {{ _('Send message using Matrix') }} @@ -76,8 +76,12 @@ {{ _('Block') }} {% endif %} {{ _('Report') }} -
{% endif %} + {% if user.is_local() %} + {{ _('Follow on Mastodon') }} + {% endif %} + +

{{ _('Joined') }}: {{ moment(user.created).fromNow(refresh=True) }}
{% if user.bot %} {{ _('Bot Account') }}
diff --git a/app/user/forms.py b/app/user/forms.py index 03669cdd..cb513ac0 100644 --- a/app/user/forms.py +++ b/app/user/forms.py @@ -88,14 +88,20 @@ class ReportUserForm(FlaskForm): class FilterEditForm(FlaskForm): - title = StringField(_l('Name'), validators={DataRequired(), Length(min=3, max=50)}) + title = StringField(_l('Name'), validators=[DataRequired(), Length(min=3, max=50)]) filter_home = BooleanField(_l('Home feed'), default=True) filter_posts = BooleanField(_l('Posts in communities')) filter_replies = BooleanField(_l('Comments on posts')) hide_type_choices = [(0, _l('Make semi-transparent')), (1, _l('Hide completely'))] hide_type = RadioField(_l('Action to take'), choices=hide_type_choices, default=1, coerce=int) keywords = TextAreaField(_l('Keywords that trigger this filter'), - render_kw={'placeholder': 'One keyword or phrase per line', 'rows': 3}, - validators={DataRequired(), Length(min=3, max=500)}) - expire_after = DateField(_l('Expire after'), validators={Optional()}) + render_kw={'placeholder': 'One keyword or phrase per line', 'rows': 3}, + validators=[DataRequired(), Length(min=3, max=500)]) + expire_after = DateField(_l('Expire after'), validators=[Optional()]) submit = SubmitField(_l('Save')) + + +class FollowOnMastodonForm(FlaskForm): + instance_url = StringField(_l('Your mastodon instance:'), validators=[DataRequired(), Length(min=3, max=50)], + render_kw={'placeholder': 'e.g. mastodon.social'}) + submit = SubmitField(_l('View profile in Mastodon')) diff --git a/app/user/routes.py b/app/user/routes.py index 12dceb26..843d7736 100644 --- a/app/user/routes.py +++ b/app/user/routes.py @@ -14,7 +14,8 @@ from app.models import Post, Community, CommunityMember, User, PostReply, PostVo Instance, Report, UserBlock, CommunityBan, CommunityJoinRequest, CommunityBlock, Filter, Domain, DomainBlock, \ InstanceBlock, NotificationSubscription from app.user import bp -from app.user.forms import ProfileForm, SettingsForm, DeleteAccountForm, ReportUserForm, FilterEditForm +from app.user.forms import ProfileForm, SettingsForm, DeleteAccountForm, ReportUserForm, FilterEditForm, \ + FollowOnMastodonForm from app.user.utils import purge_user_then_delete, unsubscribe_from_community 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, \ @@ -804,3 +805,21 @@ def user_email_notifs_unsubscribe(user_id, token): user.email_unread = False db.session.commit() return render_template('user/email_notifs_unsubscribed.html') + + +@bp.route('/u//mastodon_redirect', methods=['GET', 'POST']) +def mastodon_redirect(actor): + actor = actor.strip() + user = User.query.filter_by(user_name=actor, deleted=False).first() + if user and user.is_local(): + form = FollowOnMastodonForm() + if form.validate_on_submit(): + resp = make_response(redirect(f'https://{form.instance_url.data}/@{user.user_name}@{current_app.config["SERVER_NAME"]}')) + resp.set_cookie('mastodon_instance_url', form.instance_url.data, expires=datetime(year=2099, month=12, day=30)) + return resp + else: + send_to = '' + if request.cookies.get('mastodon_instance_url'): + send_to = request.cookies.get('mastodon_instance_url') + form.instance_url.data = send_to + return render_template('user/mastodon_redirect.html', form=form, user=user, send_to=send_to, current_app=current_app)