rDrama/files/helpers/alerts.py

138 lines
3.1 KiB
Python
Raw Normal View History

2021-07-21 01:12:26 +00:00
import mistletoe
2021-08-04 15:35:10 +00:00
from files.classes import *
2021-07-21 01:12:26 +00:00
from flask import g
from .markdown import *
from .sanitize import *
2021-08-21 11:06:28 +00:00
from .const import *
2021-07-21 01:12:26 +00:00
2021-09-17 08:55:55 +00:00
def send_notification(vid, user, text):
2021-09-06 20:56:14 +00:00
2021-09-06 21:04:01 +00:00
if isinstance(user, int):
uid = user
else:
uid = user.id
2021-07-21 01:12:26 +00:00
text = text.replace('r/', 'r\/').replace('u/', 'u\/')
2021-09-13 11:45:03 +00:00
text_html = CustomRenderer().render(mistletoe.Document(text))
2021-07-21 01:12:26 +00:00
2021-08-21 12:57:16 +00:00
text_html = sanitize(text_html)
2021-07-21 01:12:26 +00:00
new_comment = Comment(author_id=vid,
2021-09-24 02:21:41 +00:00
parent_submission=None,
distinguish_level=6,
body=text,
body_html=text_html,
2021-09-27 21:50:35 +00:00
notifiedto=uid
2021-07-21 01:12:26 +00:00
)
2021-09-17 08:55:55 +00:00
g.db.add(new_comment)
2021-07-21 01:12:26 +00:00
2021-09-17 08:55:55 +00:00
g.db.flush()
2021-07-21 01:12:26 +00:00
notif = Notification(comment_id=new_comment.id,
2021-09-06 21:04:01 +00:00
user_id=uid)
2021-09-17 08:55:55 +00:00
g.db.add(notif)
2021-07-21 01:12:26 +00:00
def send_follow_notif(vid, user, text):
2021-09-13 11:45:03 +00:00
text_html = CustomRenderer().render(mistletoe.Document(text))
2021-08-21 12:57:16 +00:00
text_html = sanitize(text_html)
2021-07-21 01:12:26 +00:00
2021-08-21 11:06:28 +00:00
new_comment = Comment(author_id=NOTIFICATIONS_ACCOUNT,
2021-09-24 02:21:41 +00:00
parent_submission=None,
distinguish_level=6,
body=text,
body_html=text_html,
2021-07-21 01:12:26 +00:00
)
g.db.add(new_comment)
g.db.flush()
notif = Notification(comment_id=new_comment.id,
user_id=user,
followsender=vid)
g.db.add(notif)
def send_unfollow_notif(vid, user, text):
2021-09-13 11:45:03 +00:00
text_html = CustomRenderer().render(mistletoe.Document(text))
2021-08-21 12:57:16 +00:00
text_html = sanitize(text_html)
2021-07-21 01:12:26 +00:00
2021-08-21 11:06:28 +00:00
new_comment = Comment(author_id=NOTIFICATIONS_ACCOUNT,
2021-09-24 02:21:41 +00:00
parent_submission=None,
distinguish_level=6,
body=text,
body_html=text_html,
2021-07-21 01:12:26 +00:00
)
g.db.add(new_comment)
g.db.flush()
notif = Notification(comment_id=new_comment.id,
user_id=user,
unfollowsender=vid)
g.db.add(notif)
def send_block_notif(vid, user, text):
2021-09-13 11:45:03 +00:00
text_html = CustomRenderer().render(mistletoe.Document(text))
2021-08-21 12:57:16 +00:00
text_html = sanitize(text_html)
2021-07-21 01:12:26 +00:00
2021-08-21 11:06:28 +00:00
new_comment = Comment(author_id=NOTIFICATIONS_ACCOUNT,
2021-09-24 02:21:41 +00:00
parent_submission=None,
distinguish_level=6,
body=text,
body_html=text_html,
2021-07-21 01:12:26 +00:00
)
g.db.add(new_comment)
g.db.flush()
notif = Notification(comment_id=new_comment.id,
user_id=user,
blocksender=vid)
g.db.add(notif)
def send_unblock_notif(vid, user, text):
2021-09-13 11:45:03 +00:00
text_html = CustomRenderer().render(mistletoe.Document(text))
2021-08-21 12:57:16 +00:00
text_html = sanitize(text_html)
2021-07-21 01:12:26 +00:00
2021-08-21 11:06:28 +00:00
new_comment = Comment(author_id=NOTIFICATIONS_ACCOUNT,
2021-09-24 02:21:41 +00:00
parent_submission=None,
distinguish_level=6,
body=text,
body_html=text_html,
2021-07-21 01:12:26 +00:00
)
g.db.add(new_comment)
g.db.flush()
notif = Notification(comment_id=new_comment.id,
user_id=user,
unblocksender=vid)
g.db.add(notif)
2021-09-13 11:45:03 +00:00
2021-07-21 01:12:26 +00:00
def send_admin(vid, text):
2021-09-22 22:27:09 +00:00
text = re.sub('([^\n])\n([^\n])', r'\1\n\n\2', text)
2021-07-25 20:14:11 +00:00
2021-09-13 11:56:23 +00:00
text_html = Renderer().render(mistletoe.Document(text))
2021-07-21 01:12:26 +00:00
2021-08-31 21:54:34 +00:00
text_html = sanitize(text_html, True)
2021-07-21 01:12:26 +00:00
new_comment = Comment(author_id=vid,
parent_submission=None,
level=1,
2021-09-24 02:21:41 +00:00
sentto=0,
body=text,
body_html=text_html,
2021-07-21 01:12:26 +00:00
)
g.db.add(new_comment)
g.db.flush()
2021-09-17 08:29:05 +00:00
admins = g.db.query(User).options(lazyload('*')).filter(User.admin_level > 0).all()
2021-07-21 01:12:26 +00:00
for admin in admins:
notif = Notification(comment_id=new_comment.id, user_id=admin.id)
g.db.add(notif)