warn if link already posted to other communties

This commit is contained in:
rimu 2024-08-15 19:39:33 +12:00
parent 1dd9461bfc
commit 8d60f8f4cd
4 changed files with 27 additions and 3 deletions

View file

@ -124,7 +124,10 @@ class CreateDiscussionForm(CreatePostForm):
class CreateLinkForm(CreatePostForm):
link_url = StringField(_l('URL'), validators=[DataRequired(), Regexp(r'^https?://', message='Submitted links need to start with "http://"" or "https://"')],
render_kw={'placeholder': 'https://...'})
render_kw={'placeholder': 'https://...',
'hx-get': '/community/check_url_already_posted',
'hx-params': '*',
'hx-target': '#urlUsed'})
def validate(self, extra_validators=None) -> bool:
domain = domain_from_url(self.link_url.data, create=False)

View file

@ -2,7 +2,9 @@ from collections import namedtuple
from io import BytesIO
from random import randint
from flask import redirect, url_for, flash, request, make_response, session, Markup, current_app, abort, g, json
import flask
from flask import redirect, url_for, flash, request, make_response, session, Markup, current_app, abort, g, json, \
jsonify
from flask_login import current_user, login_required
from flask_babel import _
from slugify import slugify
@ -36,7 +38,7 @@ from app.utils import get_setting, render_template, allowlist_html, markdown_to_
joined_communities, moderating_communities, blocked_domains, mimetype_from_url, blocked_instances, \
community_moderators, communities_banned_from, show_ban_message, recently_upvoted_posts, recently_downvoted_posts, \
blocked_users, post_ranking, languages_for_form, english_language_id, menu_topics, add_to_modlog, \
blocked_communities
blocked_communities, remove_tracking_from_link
from feedgen.feed import FeedGenerator
from datetime import timezone, timedelta
from copy import copy
@ -1804,6 +1806,17 @@ def lookup(community, domain):
return redirect('/')
@bp.route('/check_url_already_posted')
def check_url_already_posted():
url = request.args.get('link_url')
if url:
url = remove_tracking_from_link(url.strip())
communities = Community.query.filter_by(banned=False).join(Post).filter(Post.url == url).all()
return flask.render_template('community/check_url_posted.html', communities=communities)
else:
abort(404)
def upvote_own_post(post):
post.score = 1
post.up_votes = 1

View file

@ -0,0 +1,7 @@
{% if communities -%}
<p>{{ _('This link has already been posted to these communities:') }}<br>
{% for community in communities -%}
{{ community.display_name() }}<br>
{% endfor -%}
</p>
{% endif %}

View file

@ -15,6 +15,7 @@
{{ render_field(form.title) }}
{% if post_type == POST_TYPE_LINK %}
{{ render_field(form.link_url) }}
<div id="urlUsed"></div>
{% elif post_type == POST_TYPE_IMAGE %}
{{ render_field(form.image_file) }}
{{ render_field(form.image_alt_text) }}