2022-05-04 23:09:46 +00:00
|
|
|
from files.classes import *
|
|
|
|
from flask import g
|
|
|
|
from .sanitize import *
|
|
|
|
from .const import *
|
2022-06-24 14:30:59 +00:00
|
|
|
from .regex import *
|
2022-06-27 19:02:24 +00:00
|
|
|
from pusher_push_notifications import PushNotifications
|
|
|
|
from sys import stdout
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-08 19:03:04 +00:00
|
|
|
def create_comment(text_html):
|
|
|
|
new_comment = Comment(author_id=AUTOJANNY_ID,
|
2022-05-04 23:09:46 +00:00
|
|
|
parent_submission=None,
|
2022-06-07 10:03:51 +00:00
|
|
|
body_html=text_html,
|
2022-07-08 19:03:04 +00:00
|
|
|
distinguish_level=6,
|
|
|
|
is_bot=True)
|
2022-05-04 23:09:46 +00:00
|
|
|
g.db.add(new_comment)
|
|
|
|
g.db.flush()
|
|
|
|
|
|
|
|
new_comment.top_comment_id = new_comment.id
|
|
|
|
|
|
|
|
return new_comment.id
|
|
|
|
|
2022-07-08 19:03:04 +00:00
|
|
|
def send_repeatable_notification(uid, text):
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-05-28 02:20:31 +00:00
|
|
|
if uid in bots: return
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
text_html = sanitize(text)
|
|
|
|
|
2022-07-08 19:03:04 +00:00
|
|
|
existing_comment = g.db.query(Comment.id).filter_by(author_id=AUTOJANNY_ID, parent_submission=None, body_html=text_html, is_bot=True).first()
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
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()
|
2022-07-08 19:03:04 +00:00
|
|
|
if existing_notif: cid = create_comment(text_html)
|
|
|
|
else: cid = create_comment(text_html)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
notif = Notification(comment_id=cid, user_id=uid)
|
|
|
|
g.db.add(notif)
|
|
|
|
|
|
|
|
|
2022-07-08 19:03:04 +00:00
|
|
|
def send_notification(uid, text):
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-05-28 02:20:31 +00:00
|
|
|
if uid in bots: return
|
2022-07-08 19:03:04 +00:00
|
|
|
cid = notif_comment(text)
|
2022-05-04 23:09:46 +00:00
|
|
|
add_notif(cid, uid)
|
|
|
|
|
|
|
|
|
2022-07-08 19:03:04 +00:00
|
|
|
def notif_comment(text):
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-06-22 22:12:47 +00:00
|
|
|
text_html = sanitize(text)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-08-25 15:04:33 +00:00
|
|
|
try: existing = g.db.query(Comment.id).filter_by(author_id=AUTOJANNY_ID, parent_submission=None, body_html=text_html, is_bot=True).one_or_none()
|
|
|
|
except:
|
|
|
|
existing = g.db.query(Comment).filter_by(author_id=AUTOJANNY_ID, parent_submission=None, body_html=text_html, is_bot=True).all()
|
|
|
|
|
|
|
|
notifs = g.db.query(Notification).filter(Notification.comment_id.in_([x.id for x in existing])).all()
|
|
|
|
for c in notifs: g.db.delete(c)
|
|
|
|
g.db.flush()
|
|
|
|
|
|
|
|
for c in existing: g.db.delete(c)
|
|
|
|
g.db.flush()
|
|
|
|
existing = g.db.query(Comment.id).filter_by(author_id=AUTOJANNY_ID, parent_submission=None, body_html=text_html, is_bot=True).one_or_none()
|
2022-07-10 14:29:47 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
if existing: return existing[0]
|
2022-07-08 19:03:04 +00:00
|
|
|
else: return create_comment(text_html)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def notif_comment2(p):
|
|
|
|
|
|
|
|
search_html = f'%</a> has mentioned you: <a href="/post/{p.id}">%'
|
|
|
|
|
2022-07-08 19:03:04 +00:00
|
|
|
existing = g.db.query(Comment.id).filter(Comment.author_id == AUTOJANNY_ID, Comment.parent_submission == None, Comment.body_html.like(search_html)).first()
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
if existing: return existing[0]
|
|
|
|
else:
|
|
|
|
text = f"@{p.author.username} has mentioned you: [{p.title}](/post/{p.id})"
|
|
|
|
if p.sub: text += f" in <a href='/h/{p.sub}'>/h/{p.sub}"
|
2022-06-22 22:12:47 +00:00
|
|
|
text_html = sanitize(text)
|
2022-05-04 23:09:46 +00:00
|
|
|
return create_comment(text_html)
|
|
|
|
|
|
|
|
|
|
|
|
def add_notif(cid, uid):
|
2022-05-28 02:20:31 +00:00
|
|
|
if uid in bots: return
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
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):
|
2022-07-29 19:55:12 +00:00
|
|
|
# Restrict young accounts from generating notifications
|
|
|
|
if v.age < NOTIFICATION_SPAM_AGE_THRESHOLD:
|
|
|
|
return set()
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
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)
|
|
|
|
|
2022-08-21 17:20:09 +00:00
|
|
|
names = set(m.group(2) for m in mention_regex.finditer(text))
|
|
|
|
for user in get_users(names, graceful=True):
|
|
|
|
if v.id != user.id and not v.any_block_exists(user):
|
|
|
|
notify_users.add(user.id)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-08-18 09:52:37 +00:00
|
|
|
if SITE_NAME == "WPD" and 'daisy' in text.lower():
|
|
|
|
admin_ids = [x[0] for x in g.db.query(User.id).filter(User.admin_level > 0).all()]
|
|
|
|
notify_users.update(admin_ids)
|
|
|
|
|
2022-06-10 12:28:46 +00:00
|
|
|
return notify_users - bots
|
|
|
|
|
2022-06-27 19:02:24 +00:00
|
|
|
if PUSHER_ID != 'blahblahblah':
|
|
|
|
beams_client = PushNotifications(instance_id=PUSHER_ID, secret_key=PUSHER_KEY)
|
|
|
|
|
|
|
|
def pusher_thread(interests, title, notifbody, url):
|
2022-08-11 11:40:36 +00:00
|
|
|
title = censor_slurs(title, None)
|
2022-08-11 11:45:30 +00:00
|
|
|
notifbody = censor_slurs(notifbody, None)
|
2022-08-11 11:27:52 +00:00
|
|
|
|
2022-06-27 19:02:24 +00:00
|
|
|
beams_client.publish_to_interests(
|
|
|
|
interests=[interests],
|
|
|
|
publish_body={
|
|
|
|
'web': {
|
|
|
|
'notification': {
|
|
|
|
'title': title,
|
|
|
|
'body': notifbody,
|
|
|
|
'deep_link': url,
|
2022-08-24 22:22:44 +00:00
|
|
|
'icon': f'{SITE_FULL}/assets/images/{SITE_NAME}/icon.webp?v=3009',
|
2022-06-27 19:02:24 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'fcm': {
|
|
|
|
'notification': {
|
|
|
|
'title': title,
|
|
|
|
'body': notifbody,
|
|
|
|
},
|
|
|
|
'data': {
|
|
|
|
'url': url,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2022-07-08 19:03:04 +00:00
|
|
|
stdout.flush()
|