2021-10-15 14:08:27 +00:00
|
|
|
import mistletoe
|
|
|
|
|
|
|
|
from files.classes import *
|
|
|
|
from flask import g
|
|
|
|
from .markdown import *
|
|
|
|
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,
|
|
|
|
distinguish_level=6,
|
|
|
|
created_utc=0,
|
|
|
|
body_html=text_html)
|
|
|
|
g.db.add(new_comment)
|
|
|
|
g.db.flush()
|
|
|
|
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-04 12:29:28 +00:00
|
|
|
text_html = sanitize(Renderer2().render(mistletoe.Document(text)))
|
|
|
|
|
|
|
|
for i in re.finditer("<p>@((\w|-){1,25})", text_html):
|
2022-01-04 13:13:02 +00:00
|
|
|
u = get_user(i.group(1), graceful=True)
|
|
|
|
if u:
|
2022-01-04 13:18:37 +00:00
|
|
|
text_html = text_html.replace(f'<p>@{u.username}', f'<p><a href="/id/{u.id}"><img loading="lazy" src="/uid/{u.id}/pic" class="pp20">@{u.username}</a>')
|
2022-01-04 12:29:28 +00:00
|
|
|
|
|
|
|
existing_comment = g.db.query(Comment.id).filter_by(author_id=author_id, parent_submission=None, distinguish_level=6, body_html=text_html, created_utc=0).first()
|
2021-12-20 20:34:22 +00:00
|
|
|
|
|
|
|
if existing_comment:
|
|
|
|
cid = existing_comment[0]
|
2022-01-02 00:06:46 +00:00
|
|
|
existing_notif = g.db.query(Notification.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
|
|
|
|
2021-12-20 20:03:59 +00:00
|
|
|
if autojanny: author_id = AUTOJANNY_ID
|
|
|
|
else: author_id = NOTIFICATIONS_ID
|
2021-12-20 20:34:22 +00:00
|
|
|
|
2022-01-04 12:29:28 +00:00
|
|
|
text_html = sanitize(Renderer2().render(mistletoe.Document(text)))
|
|
|
|
|
|
|
|
for i in re.finditer("<p>@((\w|-){1,25})", text_html):
|
2022-01-04 13:13:02 +00:00
|
|
|
u = get_user(i.group(1), graceful=True)
|
|
|
|
if u:
|
2022-01-04 13:20:01 +00:00
|
|
|
text_html = text_html.replace(f'<p>@{u.username}', f'<p><a href="/id/{u.id}"><img loading="lazy" src="/uid/{u.id}/pic" class="pp20">@{u.username}</a>')
|
2022-01-04 12:29:28 +00:00
|
|
|
|
|
|
|
existing = g.db.query(Comment.id).filter_by(author_id=author_id, parent_submission=None, distinguish_level=6, body_html=text_html, created_utc=0).first()
|
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
|
|
|
|
|
|
|
def add_notif(cid, uid):
|
2022-01-02 00:06:46 +00:00
|
|
|
existing = g.db.query(Notification.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-10-15 14:08:27 +00:00
|
|
|
def send_admin(vid, text):
|
|
|
|
|
|
|
|
text_html = Renderer().render(mistletoe.Document(text))
|
|
|
|
|
|
|
|
text_html = sanitize(text_html, True)
|
|
|
|
|
|
|
|
new_comment = Comment(author_id=vid,
|
|
|
|
parent_submission=None,
|
|
|
|
level=1,
|
|
|
|
sentto=0,
|
|
|
|
body_html=text_html,
|
|
|
|
)
|
|
|
|
g.db.add(new_comment)
|
|
|
|
g.db.flush()
|
|
|
|
|
2022-01-06 16:46:09 +00:00
|
|
|
admins = g.db.query(User).filter(User.admin_level > 2).all()
|
2021-10-15 14:08:27 +00:00
|
|
|
for admin in admins:
|
|
|
|
notif = Notification(comment_id=new_comment.id, user_id=admin.id)
|
|
|
|
g.db.add(notif)
|
2021-12-02 20:48:13 +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():
|
2021-12-02 20:48:13 +00:00
|
|
|
if id == 0: continue
|
2021-12-29 06:43:20 +00:00
|
|
|
if word in text.lower() and id not in notify_users and v.id != id: notify_users.add(id)
|
|
|
|
|
|
|
|
soup = BeautifulSoup(text, features="html.parser")
|
2022-01-06 20:01:56 +00:00
|
|
|
for mention in soup.find_all("a", href=re.compile("^\/id\/([0-9]+)")):
|
2022-01-06 20:05:57 +00:00
|
|
|
id = int(mention["href"].split("/id/")[1])
|
2022-01-07 19:13:01 +00:00
|
|
|
if id != v.id: notify_users.add(id)
|
|
|
|
|
|
|
|
return notify_users
|
|
|
|
|
|
|
|
def NOTIFY_USERS2(text, v):
|
|
|
|
notify_users = set()
|
|
|
|
for word, id in NOTIFIED_USERS.items():
|
|
|
|
if id == 0: continue
|
|
|
|
if word in text.lower() and id not in notify_users and v.id != id: notify_users.add(id)
|
|
|
|
|
|
|
|
for i in re.finditer("(^|\s|\n)@((\w|-){1,25})", text):
|
|
|
|
user = get_user(i.group(2), graceful=True)
|
|
|
|
if user and not v.any_block_exists(user): notify_users.add(user.id)
|
2021-12-29 06:43:20 +00:00
|
|
|
|
2021-12-02 20:48:13 +00:00
|
|
|
return notify_users
|