[themotte/rDrama#451] Move comment_on_publish() to files/helpers/comments.py

This commit is contained in:
faul_sname 2023-01-03 01:11:50 -08:00
parent 9525396131
commit 28f4807a9b
3 changed files with 71 additions and 60 deletions

View file

@ -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/<cid>")
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required