From ff5465c4f8ca92070becd30938ec9d4fa9137f64 Mon Sep 17 00:00:00 2001 From: Deimos Date: Wed, 24 Jul 2019 14:13:58 -0600 Subject: [PATCH] Add warning/confirmation for reposting link topics If a user tries to post a link topic that's been posted before, this will add a warning below the form with info about the previous posts and ask them to confirm that they want to re-post. Currently, this doesn't have any sort of time/group/etc. restrictions and will just find previous topics from all time. The method for doing this is a big ugly to be able to handle both the no-JS and with-JS posting methods, but it's not too bad. --- tildes/scss/modules/_toast.scss | 6 ++ .../static/js/behaviors/autocomplete-input.js | 1 + .../templates/includes/new_topic_form.jinja2 | 79 +++++++++++++++++++ tildes/tildes/templates/new_topic.jinja2 | 47 +---------- tildes/tildes/views/topic.py | 48 +++++++++-- 5 files changed, 130 insertions(+), 51 deletions(-) create mode 100644 tildes/tildes/templates/includes/new_topic_form.jinja2 diff --git a/tildes/scss/modules/_toast.scss b/tildes/scss/modules/_toast.scss index 2a7aaea..49119ed 100644 --- a/tildes/scss/modules/_toast.scss +++ b/tildes/scss/modules/_toast.scss @@ -2,8 +2,14 @@ // SPDX-License-Identifier: AGPL-3.0-or-later .toast { + @extend %text-container; + margin: 1rem 0; font-weight: bold; + + ul { + margin-bottom: 1rem; + } } .toast-minor { diff --git a/tildes/static/js/behaviors/autocomplete-input.js b/tildes/static/js/behaviors/autocomplete-input.js index 910094d..340e349 100644 --- a/tildes/static/js/behaviors/autocomplete-input.js +++ b/tildes/static/js/behaviors/autocomplete-input.js @@ -70,6 +70,7 @@ $.onmount("[data-js-autocomplete-input]", function() { for (var i = 0; i < dataPieces.length; i++) { if (dataPieces[i].indexOf("tags=") === 0) { dataPieces[i] += $autocompleteInput.val(); + $autocompleteInput.val(""); break; } } diff --git a/tildes/tildes/templates/includes/new_topic_form.jinja2 b/tildes/tildes/templates/includes/new_topic_form.jinja2 new file mode 100644 index 0000000..d139bdc --- /dev/null +++ b/tildes/tildes/templates/includes/new_topic_form.jinja2 @@ -0,0 +1,79 @@ +{# Copyright (c) 2019 Tildes contributors #} +{# SPDX-License-Identifier: AGPL-3.0-or-later #} + +{% from "macros/datetime.jinja2" import adaptive_date_responsive %} +{% from 'macros/forms.jinja2' import markdown_textarea, topic_tagging %} + +
+ + +
+

Tildes prioritizes high-quality content and discussions

+

Please post topics that are interesting, informative, or have the potential to start a good discussion.

+

Please avoid posting topics that are primarily for entertainment or that don't have discussion value.

+
+ +
+ + +
+ +
+ Enter a link, text, or both: +
+ + +

If you enter a link, your post will be a link topic (whether you also include text or not).

+
+ +
+ {{ markdown_textarea(text=markdown) }} +

If you enter only text (and no link), your post will be a text topic.

+

If you also enter a link, this text will be posted as the first comment and can be used to add more information or give your thoughts on the linked content. Adding text when posting a link is not required, but it can help get the discussion started.

+

+
+
+ + {{ topic_tagging(autocomplete_options=group.common_topic_tags, value=tags) }} + +
+ + {% if previous_topics %} +
Link has been posted before, see below
+ {% endif %} +
+ + {% if previous_topics %} +
+

This link has been posted previously:

+ +
    + {% for previous_topic in previous_topics %} +
  • {{ adaptive_date_responsive(previous_topic.created_time) }} in ~{{ previous_topic.group }}: {{ previous_topic.title }}
  • + {% endfor %} +
+ +

If you want to post it again anyway, check the box below and click the "Post topic" button above again.

+ +
+ +
+
+ {% endif %} +
+ diff --git a/tildes/tildes/templates/new_topic.jinja2 b/tildes/tildes/templates/new_topic.jinja2 index e220d20..0807a51 100644 --- a/tildes/tildes/templates/new_topic.jinja2 +++ b/tildes/tildes/templates/new_topic.jinja2 @@ -3,8 +3,6 @@ {% extends 'base_no_sidebar.jinja2' %} -{% from 'macros/forms.jinja2' import markdown_textarea, topic_tagging %} - {% block title %}New topic{% endblock %} {% block header_context_link %} @@ -14,48 +12,5 @@ {% block main_heading %}Post a new topic in ~{{ group.path }}{% endblock %} {% block content %} -
- - -
-

Tildes prioritizes high-quality content and discussions

-

Please post topics that are interesting, informative, or have the potential to start a good discussion.

-

Please avoid posting topics that are primarily for entertainment or that don't have discussion value.

-
- -
- - -
- -
- Enter a link, text, or both: -
- - -

If you enter a link, your post will be a link topic (whether you also include text or not).

-
- -
- {{ markdown_textarea() }} -

If you enter only text (and no link), your post will be a text topic.

-

If you also enter a link, this text will be posted as the first comment and can be used to add more information or give your thoughts on the linked content. Adding text when posting a link is not required, but it can help get the discussion started.

-

-
-
- - {{ topic_tagging(autocomplete_options=group.common_topic_tags) }} - -
- -
-
+ {% include "includes/new_topic_form.jinja2" %} {% endblock %} diff --git a/tildes/tildes/views/topic.py b/tildes/tildes/views/topic.py index a3e1ad4..346b1cc 100644 --- a/tildes/tildes/views/topic.py +++ b/tildes/tildes/views/topic.py @@ -4,12 +4,14 @@ """Views related to posting/viewing topics and comments on them.""" from collections import namedtuple -from typing import Any, Optional +from typing import Any, Optional, Union from marshmallow import missing, ValidationError -from marshmallow.fields import String +from marshmallow.fields import Boolean, String from pyramid.httpexceptions import HTTPFound +from pyramid.renderers import render_to_response from pyramid.request import Request +from pyramid.response import Response from pyramid.view import view_config from sqlalchemy import cast from sqlalchemy.sql.expression import any_, desc @@ -43,14 +45,50 @@ DefaultSettings = namedtuple("DefaultSettings", ["order", "period"]) @view_config(route_name="group_topics", request_method="POST", permission="post_topic") @use_kwargs(TopicSchema(only=("title", "markdown", "link"))) -@use_kwargs({"tags": String(missing="")}) +@use_kwargs({"tags": String(missing=""), "confirm_repost": Boolean(missing=False)}) def post_group_topics( - request: Request, title: str, markdown: str, link: str, tags: str -) -> HTTPFound: + request: Request, + title: str, + markdown: str, + link: str, + tags: str, + confirm_repost: bool, +) -> Union[HTTPFound, Response]: """Post a new topic to a group.""" + # pylint: disable=too-many-arguments group = request.context if link: + # check to see if this link has been posted before + previous_topics = ( + request.db_session.query(Topic) + .filter(Topic.link == link) + .order_by(desc(Topic.created_time)) + .limit(5) + .all() + ) + + if previous_topics and not confirm_repost: + # Render partial form for Intercooler.js request, whole page for normal POST + # (I don't like this much, there must be a better way to handle this) + if "X-IC-Request" in request.headers: + template = "tildes:templates/includes/new_topic_form.jinja2" + else: + template = "tildes:templates/new_topic.jinja2" + + return render_to_response( + template, + { + "group": group, + "title": title, + "link": link, + "markdown": markdown, + "tags": tags, + "previous_topics": previous_topics, + }, + request=request, + ) + new_topic = Topic.create_link_topic( group=group, author=request.user, title=title, link=link )