hide low quality comments

This commit is contained in:
rimu 2023-10-23 22:54:11 +13:00
parent b630f76152
commit 58282b971b
9 changed files with 98 additions and 17 deletions

View file

@ -15,7 +15,7 @@ from app.models import User, Community, CommunityMember, CommunityJoinRequest, C
PostReplyVote, PostVote PostReplyVote, PostVote
from app.community import bp from app.community import bp
from app.utils import get_setting, render_template, allowlist_html, markdown_to_html, validation_required, \ from app.utils import get_setting, render_template, allowlist_html, markdown_to_html, validation_required, \
shorten_string, markdown_to_text shorten_string, markdown_to_text, domain_from_url
@bp.route('/add_local', methods=['GET', 'POST']) @bp.route('/add_local', methods=['GET', 'POST'])
@ -205,6 +205,9 @@ def add_post(actor):
post.title = form.link_title.data post.title = form.link_title.data
post.url = form.link_url.data post.url = form.link_url.data
post.type = POST_TYPE_LINK post.type = POST_TYPE_LINK
domain = domain_from_url(form.link_url.data, create=True)
domain.post_count += 1
post.domain = domain
elif form.type.data == 'image': elif form.type.data == 'image':
post.title = form.image_title.data post.title = form.image_title.data
post.type = POST_TYPE_IMAGE post.type = POST_TYPE_IMAGE

View file

@ -345,6 +345,7 @@ class Post(db.Model):
search_vector = db.Column(TSVectorType('title', 'body')) search_vector = db.Column(TSVectorType('title', 'body'))
image = db.relationship(File, foreign_keys=[image_id], cascade="all, delete") image = db.relationship(File, foreign_keys=[image_id], cascade="all, delete")
domain = db.relationship('Domain', foreign_keys=[domain_id])
@classmethod @classmethod
def get_by_ap_id(cls, ap_id): def get_by_ap_id(cls, ap_id):

View file

@ -12,6 +12,32 @@ window.addEventListener("load", function () {
setupHideButtons(); setupHideButtons();
}); });
function collapseReply(comment_id) {
const reply = document.getElementById('comment_' + comment_id);
let isHidden = false;
if(reply) {
const hidables = parentElement.querySelectorAll('.hidable');
hidables.forEach(hidable => {
hidable.style.display = isHidden ? 'block' : 'none';
});
const moreHidables = parentElement.parentElement.querySelectorAll('.hidable');
moreHidables.forEach(hidable => {
hidable.style.display = isHidden ? 'block' : 'none';
});
// Toggle the content of hideEl
if (isHidden) {
hideEl.innerHTML = "<a href='#'>[-] hide</a>";
} else {
hideEl.innerHTML = "<a href='#'>[+] show</a>";
}
isHidden = !isHidden; // Toggle the state
}
}
// every element with the 'confirm_first' class gets a popup confirmation dialog // every element with the 'confirm_first' class gets a popup confirmation dialog
function setupConfirmFirst() { function setupConfirmFirst() {
const show_first = document.querySelectorAll('.confirm_first'); const show_first = document.querySelectorAll('.confirm_first');
@ -123,6 +149,30 @@ function setupHideButtons() {
isHidden = !isHidden; // Toggle the state isHidden = !isHidden; // Toggle the state
}); });
}); });
if(toBeHidden) {
toBeHidden.forEach((arrayElement) => {
// Build the ID of the outer div
const divId = "comment_" + arrayElement;
// Access the outer div by its ID
const commentDiv = document.getElementById(divId);
if (commentDiv) {
// Access the inner div with class "hide_button" inside the outer div
const hideButton = commentDiv.getElementsByClassName("hide_button")[0];
if (hideButton) {
// Programmatically trigger a click event on the "hide_button" div
hideButton.click();
} else {
console.log(`"hide_button" not found in ${divId}`);
}
} else {
console.log(`Div with ID ${divId} not found`);
}
});
}
} }
function titleToURL(title) { function titleToURL(title) {

View file

@ -358,4 +358,12 @@ nav.navbar {
background: whitesmoke url(/static/images/collapsed.gif) no-repeat center left; background: whitesmoke url(/static/images/collapsed.gif) no-repeat center left;
} }
.domain_link {
color: #bbb;
font-size: 73%;
}
.domain_link a {
color: #777;
}
/*# sourceMappingURL=styles.css.map */ /*# sourceMappingURL=styles.css.map */

View file

@ -120,4 +120,12 @@ nav.navbar {
.coolfieldset.collapsed legend{ .coolfieldset.collapsed legend{
background: whitesmoke url(/static/images/collapsed.gif) no-repeat center left; background: whitesmoke url(/static/images/collapsed.gif) no-repeat center left;
}
.domain_link {
color: $grey;
font-size: 73%;
a {
color: $dark-grey;
}
} }

View file

@ -14,11 +14,9 @@
</div> </div>
<h1 class="mt-2">{{ post.title }}</h1> <h1 class="mt-2">{{ post.title }}</h1>
{% if post.url %} {% if post.url %}
<p><small><a href="{{ post.url }}" rel="nofollow ugc">{{ post.url|shorten_url }}</a> <p><small><a href="{{ post.url }}" rel="nofollow ugc">{{ post.url|shorten_url }}
{% if post.type == post_type_link %} <img src="/static/images/external_link_black.svg" class="external_link_icon" alt="External link" />
<span class="domain_link">(<a href="/d/{{ post.domain_id }}">domain</a>)</span> </a></small></p>
{% endif %}
</small></p>
{% endif %} {% endif %}
<p><small>submitted {{ moment(post.posted_at).fromNow() }} by {{ render_username(post.author) }}</small></p> <p><small>submitted {{ moment(post.posted_at).fromNow() }} by {{ render_username(post.author) }}</small></p>
</div> </div>
@ -48,9 +46,6 @@
{% if post.url %} {% if post.url %}
<p><small><a href="{{ post.url }}" rel="nofollow ugc">{{ post.url|shorten_url }} <p><small><a href="{{ post.url }}" rel="nofollow ugc">{{ post.url|shorten_url }}
<img src="/static/images/external_link_black.svg" class="external_link_icon" alt="External link" /></a> <img src="/static/images/external_link_black.svg" class="external_link_icon" alt="External link" /></a>
{% if post.type == post_type_link %}
<span class="domain_link">(<a href="/d/{{ post.domain_id }}">domain</a>)</span>
{% endif %}
</small></p> </small></p>
{% endif %} {% endif %}
<p class="small">submitted {{ moment(post.posted_at).fromNow() }} by <p class="small">submitted {{ moment(post.posted_at).fromNow() }} by

View file

@ -8,8 +8,11 @@
<div class="col{% if post.image_id %}-8{% endif %}"> <div class="col{% if post.image_id %}-8{% endif %}">
<h3> <h3>
<a href="{{ url_for('community.show_post', post_id=post.id) }}">{{ post.title }}</a> <a href="{{ url_for('community.show_post', post_id=post.id) }}">{{ post.title }}</a>
{% if post.type == post_type_link %} {% if post.type == post_type_link and post.domain_id %}
<span class="domain_link">(<a href="/d/{{ post.domain_id }}">domain</a>)</span> <a href="{{ post.url }}" rel="nofollow ugc">
<img src="/static/images/external_link_black.svg" class="external_link_icon" alt="External link" />
</a>
<span class="domain_link">(<a href="/d/{{ post.domain_id }}">{{ post.domain.name }}</a>)</span>
{% endif %} {% endif %}
</h3> </h3>
</div> </div>

View file

@ -2,6 +2,9 @@
{% from 'bootstrap/form.html' import render_form %} {% from 'bootstrap/form.html' import render_form %}
{% block app_content %} {% block app_content %}
<script type="text/javascript" nonce="{{ session['nonce'] }}">
var toBeHidden = Array(); // this list of comment IDs will be iterated over in setupHideButtons() and the 'hide' button clicked
</script>
<div class="row"> <div class="row">
<div class="col-12 col-md-8 position-relative"> <div class="col-12 col-md-8 position-relative">
{% include 'community/_post_full.html' %} {% include 'community/_post_full.html' %}
@ -35,19 +38,21 @@
{% include "community/_voting_buttons.html" %} {% include "community/_voting_buttons.html" %}
{% endwith %} {% endwith %}
</div> </div>
<div class="hide_button"><a href='#'>[-] hide</a></div> <div class="hide_button">{% if comment['comment'].score <= -10 %}<a href='#'>[+] show</a>{% else %}<a href='#'>[-] hide</a>{% endif %}
</div>
<div class="comment_author"> <div class="comment_author">
{% if comment['comment'].author.deleted %} {% if comment['comment'].author.deleted %}
<strong>[deleted]</strong> <strong>[deleted]</strong>
{% else %} {% else %}
{% if comment['comment'].author.avatar_id %} {% if comment['comment'].author.avatar_id and comment['comment'].score > -10 %}
<a href="/u/{{ comment['comment'].author.link() }}" title="{{ comment['comment'].author.ap_id }}"> <a href="/u/{{ comment['comment'].author.link() }}" title="{{ comment['comment'].author.ap_id }}">
<img src="{{ comment['comment'].author.avatar_image() }}" alt="Avatar" /></a> <img src="{{ comment['comment'].author.avatar_image() }}" alt="Avatar" /></a>
{% endif %} {% endif %}
<a href="/u/{{ comment['comment'].author.link() }}" title="{{ comment['comment'].author.link() }}"> <a href="/u/{{ comment['comment'].author.link() }}" title="{{ comment['comment'].author.link() }}">
<strong>{{ comment['comment'].author.user_name }}</strong></a> {% if comment['comment'].score > -10 %}<strong>{% endif %}
{{ comment['comment'].author.user_name }}{% if comment['comment'].score > -10 %}</strong>{% endif %}</a>
{% endif %} {% endif %}
{% if comment['comment'].author.id == post.author.id %}<span title="Submitter of original post" aria-label="submitter">[S]</span>{% endif %} {% if comment['comment'].author.id == post.author.id %}<span title="Submitter of original post" aria-label="submitter" class="small">[OP]</span>{% endif %}
<span class="text-muted small">{{ moment(comment['comment'].posted_at).fromNow(refresh=True) }}</span> <span class="text-muted small">{{ moment(comment['comment'].posted_at).fromNow(refresh=True) }}</span>
</div> </div>
<div class="comment_body hidable"> <div class="comment_body hidable">
@ -74,7 +79,11 @@
{% endif %} {% endif %}
{% endif %} {% endif %}
</div> </div>
{% if comment['comment'].score <= -10 %}
<script nonce="{{ session['nonce'] }}" type="text/javascript">
toBeHidden.push({{ comment['comment'].id }});
</script>
{% endif %}
{% endmacro %} {% endmacro %}
<div id="replies" class="comments"> <div id="replies" class="comments">

View file

@ -156,9 +156,13 @@ def markdown_to_text(markdown_text) -> str:
return markdown_text.replace("# ", '') return markdown_text.replace("# ", '')
def domain_from_url(url: str) -> Domain: def domain_from_url(url: str, create=False) -> Domain:
parsed_url = urlparse(url) parsed_url = urlparse(url)
domain = Domain.query.filter_by(name=parsed_url.hostname.lower()).first() domain = Domain.query.filter_by(name=parsed_url.hostname.lower()).first()
if create and domain is None:
domain = Domain(name=parsed_url.hostname.lower())
db.session.add(domain)
db.session.commit()
return domain return domain