[themotte/rDrama#451] Move comment_on_publish() to files/helpers/comments.py
This commit is contained in:
parent
9525396131
commit
28f4807a9b
3 changed files with 71 additions and 60 deletions
69
files/helpers/comments.py
Normal file
69
files/helpers/comments.py
Normal file
|
@ -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.
|
||||||
|
"""
|
|
@ -13,7 +13,7 @@ from files.classes import *
|
||||||
from flask import *
|
from flask import *
|
||||||
from files.__main__ import app, cache, limiter
|
from files.__main__ import app, cache, limiter
|
||||||
from .front import frontlist
|
from .front import frontlist
|
||||||
from files.routes.comments import comment_on_publish
|
from files.helpers.comments import comment_on_publish
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import requests
|
import requests
|
||||||
from urllib.parse import quote, urlencode
|
from urllib.parse import quote, urlencode
|
||||||
|
|
|
@ -2,6 +2,7 @@ from files.helpers.wrappers import *
|
||||||
from files.helpers.alerts import *
|
from files.helpers.alerts import *
|
||||||
from files.helpers.images import *
|
from files.helpers.images import *
|
||||||
from files.helpers.const import *
|
from files.helpers.const import *
|
||||||
|
from files.helpers.comments import comment_on_publish
|
||||||
from files.classes import *
|
from files.classes import *
|
||||||
from pusher_push_notifications import PushNotifications
|
from pusher_push_notifications import PushNotifications
|
||||||
from flask import *
|
from flask import *
|
||||||
|
@ -304,65 +305,6 @@ def api_comment(v):
|
||||||
message = None
|
message = None
|
||||||
return {"comment": render_template("comments.html", v=v, comments=[c], ajax=True, parent_level=level), "message": message}
|
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>")
|
@app.post("/edit_comment/<cid>")
|
||||||
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
@limiter.limit("1/second;30/minute;200/hour;1000/day")
|
||||||
@auth_required
|
@auth_required
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue