diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 2a9d99c7..87f0fadf 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -1426,6 +1426,8 @@ def create_post_reply(activity_log: ActivityPubLog, community: Community, in_rep post_reply.ranking = confidence(post_reply.up_votes, post_reply.down_votes) db.session.commit() + user.recalculate_avg_comment_length() + # send notification to the post/comment being replied to if parent_comment_id: notify_about_post_reply(parent_comment, post_reply) diff --git a/app/models.py b/app/models.py index fa79283b..f663942c 100644 --- a/app/models.py +++ b/app/models.py @@ -3,9 +3,10 @@ from time import time from typing import List, Union import requests +from bs4 import BeautifulSoup from flask import current_app, escape, url_for, render_template_string from flask_login import UserMixin, current_user -from sqlalchemy import or_, text +from sqlalchemy import or_, text, desc from werkzeug.security import generate_password_hash, check_password_hash from flask_babel import _, lazy_gettext as _l from sqlalchemy.orm import backref @@ -615,6 +616,8 @@ class User(UserMixin, db.Model): markdown_editor = db.Column(db.Boolean, default=False) interface_language = db.Column(db.String(10)) # a locale that the translation system understands e.g. 'en' or 'en-us'. If empty, use browser default language_id = db.Column(db.Integer, db.ForeignKey('language.id')) # the default choice in the language dropdown when composing posts & comments + average_comment_length = db.Column(db.Integer) + comment_length_warning = db.Column(db.Integer, default=15) avatar = db.relationship('File', lazy='joined', foreign_keys=[avatar_id], single_parent=True, cascade="all, delete-orphan") cover = db.relationship('File', lazy='joined', foreign_keys=[cover_id], single_parent=True, cascade="all, delete-orphan") @@ -808,6 +811,19 @@ class User(UserMixin, db.Model): else: self.attitude = (total_upvotes - total_downvotes) / (total_upvotes + total_downvotes) + def recalculate_avg_comment_length(self): + replies = db.session.execute(text('SELECT body, body_html FROM "post_reply" WHERE user_id = :user_id ORDER BY created_at DESC LIMIT 30'), + {'user_id': self.id}).all() + lengths = [] + for reply in replies: + if reply.body.strip() != '': + lengths.append(len(reply.body.strip())) + else: + soup = BeautifulSoup(reply.body_html, 'html.parser') + lengths.append(len(soup.get_text())) + if len(lengths) > 5: + self.average_comment_length = math.ceil(sum(lengths) / len(lengths)) + def subscribed(self, community_id: int) -> int: if community_id is None: return False diff --git a/app/post/routes.py b/app/post/routes.py index 8f9c3fb1..5ea82b2f 100644 --- a/app/post/routes.py +++ b/app/post/routes.py @@ -111,6 +111,8 @@ def show_post(post_id: int): db.session.add(reply) db.session.commit() + current_user.recalculate_avg_comment_length() + notify_about_post_reply(None, reply) # Subscribe to own comment @@ -671,6 +673,8 @@ def add_reply(post_id: int, comment_id: int): db.session.add(reply) db.session.commit() + current_user.recalculate_avg_comment_length() + # Notify subscribers notify_about_post_reply(in_reply_to, reply) diff --git a/app/static/scss/_colours.scss b/app/static/scss/_colours.scss index 83a6b470..eb62b6e5 100644 --- a/app/static/scss/_colours.scss +++ b/app/static/scss/_colours.scss @@ -14,4 +14,8 @@ $super-dark-grey: #424549; .orangered { color: orangered; -} \ No newline at end of file +} + +.purple { + color: purple; +} diff --git a/app/static/structure.css b/app/static/structure.css index 83b02d79..be9600f3 100644 --- a/app/static/structure.css +++ b/app/static/structure.css @@ -8,6 +8,10 @@ nav, etc which are used site-wide */ color: orangered; } +.purple { + color: purple; +} + .pl-0 { padding-left: 0 !important; } diff --git a/app/static/styles.css b/app/static/styles.css index 40988b41..71aabe5b 100644 --- a/app/static/styles.css +++ b/app/static/styles.css @@ -7,6 +7,10 @@ color: orangered; } +.purple { + color: purple; +} + .pl-0 { padding-left: 0 !important; } diff --git a/app/templates/post/_post_full.html b/app/templates/post/_post_full.html index aaf4cc27..ae8cb32d 100644 --- a/app/templates/post/_post_full.html +++ b/app/templates/post/_post_full.html @@ -138,7 +138,9 @@ {% if post.type == POST_TYPE_POLL %}
- {% if poll_results %} + {% if poll_results and poll_total_votes == 0 %} +

{{ _('The poll has finished, yet no votes were cast.') }}

+ {% elif poll_results and poll_total_votes %}