
* remove holes * don't import class that doesn't exist * remove subs in template code * remove subs_created * fix sticky bug * uh... i think this will fix it???? * fix stupid nearly ungrokkable nesting error * ... * .... * .... * *sigh* * .............................i think i got it. * Revert ".............................i think i got it." This reverts commit419a545875
. * Revert "*sigh*" This reverts commitfcfc9d7995
. * Revert "...." This reverts commite200c8f6f0
. * Revert "...." This reverts commit681db8cb02
. * Revert "..." This reverts commitc54372b9ff
. * Revert "fix stupid nearly ungrokkable nesting error" This reverts commite202fd774c
. * Revert "uh... i think this will fix it????" This reverts commite4d9366093
. * readd missing endif * fix email templates. * fix frontpage listing * some minor fixes wrt saving * fix some listing errors * Remove more references to holes system A couple of these came from the merge of #554. A few others were just left in the templates and presumably hard to catch on the first removal pass. * remove unnecessary lazy * Add migration Tested. Very sensitive to order. I wound up borrowing the statement order we used for deleting sub rows on upstream (manually, of course). --------- Co-authored-by: TLSM <duolsm@outlook.com>
117 lines
3.4 KiB
Python
117 lines
3.4 KiB
Python
from files.classes import *
|
|
from flask import g
|
|
|
|
from .sanitize import *
|
|
from .config.const import *
|
|
|
|
def create_comment(text_html, autojanny=False):
|
|
if autojanny: author_id = AUTOJANNY_ID
|
|
else: author_id = NOTIFICATIONS_ID
|
|
|
|
new_comment = Comment(author_id=author_id,
|
|
parent_submission=None,
|
|
body_html=text_html,
|
|
distinguish_level=6)
|
|
g.db.add(new_comment)
|
|
g.db.flush()
|
|
|
|
new_comment.top_comment_id = new_comment.id
|
|
|
|
return new_comment.id
|
|
|
|
def send_repeatable_notification(uid, text, autojanny=False):
|
|
if autojanny: author_id = AUTOJANNY_ID
|
|
else: author_id = NOTIFICATIONS_ID
|
|
|
|
text_html = sanitize(text)
|
|
|
|
existing_comment = g.db.query(Comment.id).filter_by(author_id=author_id, parent_submission=None, body_html=text_html).first()
|
|
|
|
if existing_comment:
|
|
cid = existing_comment[0]
|
|
existing_notif = g.db.query(Notification.user_id).filter_by(user_id=uid, comment_id=cid).one_or_none()
|
|
if existing_notif: cid = create_comment(text_html, autojanny)
|
|
else: cid = create_comment(text_html, autojanny)
|
|
|
|
notif = Notification(comment_id=cid, user_id=uid)
|
|
g.db.add(notif)
|
|
|
|
|
|
def send_notification(uid, text, autojanny=False):
|
|
cid = notif_comment(text, autojanny)
|
|
add_notif(cid, uid)
|
|
|
|
|
|
def notif_comment(text, autojanny=False):
|
|
if autojanny:
|
|
author_id = AUTOJANNY_ID
|
|
alert = True
|
|
else:
|
|
author_id = NOTIFICATIONS_ID
|
|
alert = False
|
|
|
|
text_html = sanitize(text, alert=alert)
|
|
|
|
existing = g.db.query(Comment.id).filter_by(author_id=author_id, parent_submission=None, body_html=text_html).one_or_none()
|
|
|
|
if existing: return existing[0]
|
|
else: return create_comment(text_html, autojanny)
|
|
|
|
|
|
def notif_comment2(p):
|
|
search_html = f'%</a> has mentioned you: <a href="/post/{p.id}">%'
|
|
|
|
existing = g.db.query(Comment.id).filter(Comment.author_id == NOTIFICATIONS_ID, Comment.parent_submission == None, Comment.body_html.like(search_html)).first()
|
|
|
|
if existing: return existing[0]
|
|
else:
|
|
text = f"@{p.author.username} has mentioned you: [{p.title}](/post/{p.id})"
|
|
text_html = sanitize(text, alert=True)
|
|
return create_comment(text_html)
|
|
|
|
|
|
def add_notif(cid, uid):
|
|
existing = g.db.query(Notification.user_id).filter_by(comment_id=cid, user_id=uid).one_or_none()
|
|
if not existing:
|
|
notif = Notification(comment_id=cid, user_id=uid)
|
|
g.db.add(notif)
|
|
|
|
|
|
def NOTIFY_USERS(text, v) -> set[int]:
|
|
notify_users = set()
|
|
for word, id in NOTIFIED_USERS.items():
|
|
if id == 0 or v.id == id: continue
|
|
if word in text.lower() and id not in notify_users:
|
|
notify_users.add(id)
|
|
|
|
captured = []
|
|
for i in mention_regex.finditer(text):
|
|
if v.username.lower() == i.group(2).lower(): continue
|
|
|
|
if i.group(0) in captured: continue
|
|
captured.append(i.group(0))
|
|
|
|
user = get_user(i.group(2), graceful=True)
|
|
if user and v.id != user.id and not v.any_block_exists(user):
|
|
notify_users.add(user.id)
|
|
|
|
return notify_users
|
|
|
|
def notify_submission_publish(target: Submission):
|
|
# Username mentions in title & body
|
|
text: str = f'{target.title} {target.body}'
|
|
notify_users = NOTIFY_USERS(text, target.author)
|
|
if notify_users:
|
|
comment_id = notif_comment2(target)
|
|
for user_id in notify_users:
|
|
add_notif(comment_id, user_id)
|
|
|
|
# Submission author followers
|
|
if target.author.followers:
|
|
message: str = (
|
|
f"@{target.author.username} has made a new post: "
|
|
f"[{target.title}]({target.shortlink})"
|
|
)
|
|
cid = notif_comment(message, autojanny=True)
|
|
for follow in target.author.followers:
|
|
add_notif(cid, follow.user_id)
|