MarseyWorld/files/helpers/alerts.py

100 lines
2.9 KiB
Python
Raw Normal View History

2021-10-15 14:08:27 +00:00
from files.classes import *
from flask import g
from .sanitize import *
from .const import *
2022-01-04 12:30:08 +00:00
def create_comment(text_html, autojanny=False):
2021-12-20 20:34:22 +00:00
if autojanny: author_id = AUTOJANNY_ID
else: author_id = NOTIFICATIONS_ID
2021-10-15 14:08:27 +00:00
2021-12-20 20:34:22 +00:00
new_comment = Comment(author_id=author_id,
parent_submission=None,
2022-03-31 15:00:57 +00:00
body_html=text_html,
distinguish_level=6)
2021-12-20 20:34:22 +00:00
g.db.add(new_comment)
g.db.flush()
2022-04-04 01:41:20 +00:00
new_comment.top_comment_id = new_comment.id
2021-12-20 20:34:22 +00:00
return new_comment.id
def send_repeatable_notification(uid, text, autojanny=False):
2021-10-15 14:08:27 +00:00
2021-12-20 20:03:59 +00:00
if autojanny: author_id = AUTOJANNY_ID
else: author_id = NOTIFICATIONS_ID
2021-10-15 14:08:27 +00:00
2022-01-17 19:05:16 +00:00
text_html = sanitize(text)
2022-01-04 12:29:28 +00:00
2022-03-04 16:53:28 +00:00
existing_comment = g.db.query(Comment.id).filter_by(author_id=author_id, parent_submission=None, body_html=text_html).first()
2021-12-20 20:34:22 +00:00
if existing_comment:
cid = existing_comment[0]
2022-02-16 01:16:01 +00:00
existing_notif = g.db.query(Notification.user_id).filter_by(user_id=uid, comment_id=cid).one_or_none()
2022-01-04 12:30:08 +00:00
if existing_notif: cid = create_comment(text_html, autojanny)
else: cid = create_comment(text_html, autojanny)
2021-12-20 20:34:22 +00:00
notif = Notification(comment_id=cid, user_id=uid)
g.db.add(notif)
2021-10-15 14:08:27 +00:00
2021-12-20 20:03:59 +00:00
def send_notification(uid, text, autojanny=False):
2021-10-15 14:08:27 +00:00
2021-12-20 20:03:59 +00:00
cid = notif_comment(text, autojanny)
add_notif(cid, uid)
2021-10-15 14:08:27 +00:00
2021-12-20 20:03:59 +00:00
def notif_comment(text, autojanny=False):
2021-10-15 14:08:27 +00:00
2022-04-19 22:23:25 +00:00
if autojanny:
author_id = AUTOJANNY_ID
alert = True
else:
author_id = NOTIFICATIONS_ID
alert = False
text_html = sanitize(text, alert=alert)
2022-01-04 12:29:28 +00:00
2022-03-17 16:30:13 +00:00
existing = g.db.query(Comment.id).filter_by(author_id=author_id, parent_submission=None, body_html=text_html).one_or_none()
2021-12-20 20:03:59 +00:00
2021-12-20 20:34:22 +00:00
if existing: return existing[0]
2022-01-04 12:30:08 +00:00
else: return create_comment(text_html, autojanny)
2021-12-20 20:34:22 +00:00
2021-12-20 20:03:59 +00:00
2022-03-04 22:46:20 +00:00
def notif_comment2(p):
2022-03-21 20:56:43 +00:00
search_html = f'%</a> has mentioned you: <a href="/post/{p.id}">%'
2022-03-04 22:46:20 +00:00
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})"
2022-03-09 02:04:37 +00:00
if p.sub: text += f" in <a href='/h/{p.sub}'>/h/{p.sub}"
2022-03-04 22:46:20 +00:00
text_html = sanitize(text, alert=True)
return create_comment(text_html)
2021-12-20 20:03:59 +00:00
def add_notif(cid, uid):
2022-02-16 01:16:01 +00:00
existing = g.db.query(Notification.user_id).filter_by(comment_id=cid, user_id=uid).one_or_none()
2021-12-20 20:03:59 +00:00
if not existing:
notif = Notification(comment_id=cid, user_id=uid)
g.db.add(notif)
2021-10-15 14:08:27 +00:00
2021-12-20 20:34:22 +00:00
2021-12-29 06:43:20 +00:00
def NOTIFY_USERS(text, v):
2021-12-02 20:48:13 +00:00
notify_users = set()
2021-12-02 21:01:31 +00:00
for word, id in NOTIFIED_USERS.items():
2022-03-09 02:24:58 +00:00
if id == 0 or v.id == id: continue
if word in text.lower() and id not in notify_users: notify_users.add(id)
2021-12-29 06:43:20 +00:00
2022-02-28 23:30:44 +00:00
captured = []
2022-02-27 21:57:44 +00:00
for i in mention_regex.finditer(text):
2022-03-09 02:24:58 +00:00
if v.username.lower() == i.group(2).lower(): continue
2022-02-28 23:30:44 +00:00
if i.group(0) in captured: continue
captured.append(i.group(0))
2022-01-07 19:13:01 +00:00
user = get_user(i.group(2), graceful=True)
2022-03-09 02:24:58 +00:00
if user and v.id != user.id and not v.any_block_exists(user): notify_users.add(user.id)
2021-12-29 06:43:20 +00:00
2022-02-21 01:58:12 +00:00
return notify_users