diff --git a/files/helpers/comments.py b/files/helpers/comments.py new file mode 100644 index 000000000..45013eacb --- /dev/null +++ b/files/helpers/comments.py @@ -0,0 +1,69 @@ +from files.classes import Comment, Notification, Subscription +from files.helpers.alerts import NOTIFY_USERS +from files.helpers.const import PUSHER_ID +from flask import g + +def comment_on_publish(comment:Comment): + """ + Run when comment becomes visible: immediately for non-filtered comments, + or on approval for previously filtered comments. + Should be used to update stateful counters, notifications, etc. that + reflect the comments users will actually see. + """ + # TODO: Get this out of the routes and into a model eventually... + author = comment.author + + # Shadowbanned users are invisible. This may lead to inconsistencies if + # a user comments while shadowed and is later unshadowed. (TODO?) + if author.shadowbanned: + return + + # Comment instances used for purposes other than actual comments (notifs, + # DMs) shouldn't be considered published. + if not comment.parent_submission: + return + + # Generate notifs for: mentions, post subscribers, parent post/comment + to_notify = NOTIFY_USERS(comment.body, comment.author) + + post_subscribers = g.db.query(Subscription.user_id).filter( + Subscription.submission_id == comment.parent_submission, + Subscription.user_id != comment.author_id, + ).all() + to_notify.update([x[0] for x in post_subscribers]) + + parent = comment.parent + if parent and parent.author_id != comment.author_id and not parent.author.is_blocking(author): + to_notify.add(parent.author_id) + + for uid in to_notify: + notif = Notification(comment_id=comment.id, user_id=uid) + g.db.add(notif) + + # Comment counter for parent submission + comment.post.comment_count += 1 + g.db.add(comment.post) + + # Comment counter for author's profile + comment.author.comment_count = g.db.query(Comment).filter( + Comment.author_id == comment.author_id, + Comment.parent_submission != None, + Comment.is_banned == False, + Comment.deleted_utc == 0, + ).count() + g.db.add(comment.author) + + # Generate push notifications if enabled. + if PUSHER_ID != 'blahblahblah' and comment.author_id != parent.author_id: + try: + gevent.spawn(pusher_thread, f'{request.host}{parent.author.id}', + comment, comment.author_name) + except: pass + +def comment_on_unpublish(comment:Comment): + """ + Run when a comment becomes invisible: when a moderator makes the comment non-visible + by changing the filter_state to "removed", or when the user deletes the comment. + Should be used to update stateful counters, notifications, etc. that + reflect the comments users will actually see. + """ diff --git a/files/routes/admin.py b/files/routes/admin.py index 4045d47af..51eb072be 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -13,7 +13,7 @@ from files.classes import * from flask import * from files.__main__ import app, cache, limiter from .front import frontlist -from files.routes.comments import comment_on_publish +from files.helpers.comments import comment_on_publish from datetime import datetime import requests from urllib.parse import quote, urlencode diff --git a/files/routes/comments.py b/files/routes/comments.py index 89c299558..72656a581 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -2,6 +2,7 @@ from files.helpers.wrappers import * from files.helpers.alerts import * from files.helpers.images import * from files.helpers.const import * +from files.helpers.comments import comment_on_publish from files.classes import * from pusher_push_notifications import PushNotifications from flask import * @@ -304,65 +305,6 @@ def api_comment(v): message = None return {"comment": render_template("comments.html", v=v, comments=[c], ajax=True, parent_level=level), "message": message} - -def comment_on_publish(comment:Comment): - """ - Run when comment becomes visible: immediately for non-filtered comments, - or on approval for previously filtered comments. - Should be used to update stateful counters, notifications, etc. that - reflect the comments users will actually see. - """ - # TODO: Get this out of the routes and into a model eventually... - author = comment.author - - # Shadowbanned users are invisible. This may lead to inconsistencies if - # a user comments while shadowed and is later unshadowed. (TODO?) - if author.shadowbanned: - return - - # Comment instances used for purposes other than actual comments (notifs, - # DMs) shouldn't be considered published. - if not comment.parent_submission: - return - - # Generate notifs for: mentions, post subscribers, parent post/comment - to_notify = NOTIFY_USERS(comment.body, comment.author) - - post_subscribers = g.db.query(Subscription.user_id).filter( - Subscription.submission_id == comment.parent_submission, - Subscription.user_id != comment.author_id, - ).all() - to_notify.update([x[0] for x in post_subscribers]) - - parent = comment.parent - if parent and parent.author_id != comment.author_id and not parent.author.is_blocking(author): - to_notify.add(parent.author_id) - - for uid in to_notify: - notif = Notification(comment_id=comment.id, user_id=uid) - g.db.add(notif) - - # Comment counter for parent submission - comment.post.comment_count += 1 - g.db.add(comment.post) - - # Comment counter for author's profile - comment.author.comment_count = g.db.query(Comment).filter( - Comment.author_id == comment.author_id, - Comment.parent_submission != None, - Comment.is_banned == False, - Comment.deleted_utc == 0, - ).count() - g.db.add(comment.author) - - # Generate push notifications if enabled. - if PUSHER_ID != 'blahblahblah' and comment.author_id != parent.author_id: - try: - gevent.spawn(pusher_thread, f'{request.host}{parent.author.id}', - comment, comment.author_name) - except: pass - - @app.post("/edit_comment/") @limiter.limit("1/second;30/minute;200/hour;1000/day") @auth_required