forked from rDrama/rDrama
1
0
Fork 0
rDrama/files/helpers/alerts.py

137 lines
3.4 KiB
Python
Raw Normal View History

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 *
2021-10-23 19:40:49 +00:00
def send_notification(uid, text, autojanny=False):
2021-10-15 14:08:27 +00:00
text = text.replace('r/', 'r\/').replace('u/', 'u\/')
text_html = CustomRenderer().render(mistletoe.Document(text))
text_html = sanitize(text_html)
2021-11-18 14:21:19 +00:00
if autojanny: author_id = AUTOJANNY_ID
2021-11-23 22:44:16 +00:00
else:
author_id = NOTIFICATIONS_ID
existing = g.db.query(Comment.id).filter(Comment.author_id == author_id, Comment.body_html == text_html, Comment.notifiedto == uid).first()
if existing: return
2021-11-22 14:16:58 +00:00
2021-10-23 19:40:49 +00:00
new_comment = Comment(author_id=author_id,
2021-10-15 14:08:27 +00:00
parent_submission=None,
2021-11-22 14:16:58 +00:00
distinguish_level=6,
2021-10-15 14:08:27 +00:00
body_html=text_html,
notifiedto=uid
)
g.db.add(new_comment)
g.db.flush()
notif = Notification(comment_id=new_comment.id,
user_id=uid)
g.db.add(notif)
def send_follow_notif(vid, user, text):
text_html = CustomRenderer().render(mistletoe.Document(text))
text_html = sanitize(text_html)
2021-11-18 14:21:19 +00:00
new_comment = Comment(author_id=NOTIFICATIONS_ID,
2021-10-15 14:08:27 +00:00
parent_submission=None,
distinguish_level=6,
body_html=text_html,
)
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):
text_html = CustomRenderer().render(mistletoe.Document(text))
text_html = sanitize(text_html)
2021-11-18 14:21:19 +00:00
new_comment = Comment(author_id=NOTIFICATIONS_ID,
2021-10-15 14:08:27 +00:00
parent_submission=None,
distinguish_level=6,
body_html=text_html,
)
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):
text_html = CustomRenderer().render(mistletoe.Document(text))
text_html = sanitize(text_html)
2021-11-18 14:21:19 +00:00
new_comment = Comment(author_id=NOTIFICATIONS_ID,
2021-10-15 14:08:27 +00:00
parent_submission=None,
distinguish_level=6,
body_html=text_html,
)
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):
text_html = CustomRenderer().render(mistletoe.Document(text))
text_html = sanitize(text_html)
2021-11-18 14:21:19 +00:00
new_comment = Comment(author_id=NOTIFICATIONS_ID,
2021-10-15 14:08:27 +00:00
parent_submission=None,
distinguish_level=6,
body_html=text_html,
)
g.db.add(new_comment)
g.db.flush()
notif = Notification(comment_id=new_comment.id,
user_id=user,
unblocksender=vid)
g.db.add(notif)
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()
2021-11-06 15:52:48 +00:00
admins = g.db.query(User).filter(User.admin_level > 0).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
def NOTIFY_USERS(text, vid):
text = text.lower()
notify_users = set()
for word, id in NOTIFIED_USERS:
if id == 0: continue
if word in text and id not in notify_users and vid != id: notify_users.add(id)
return notify_users