2022-12-02 22:21:18 +00:00
|
|
|
import json
|
2022-11-15 09:19:08 +00:00
|
|
|
from sys import stdout
|
|
|
|
|
2022-12-02 23:15:41 +00:00
|
|
|
import gevent
|
2022-05-04 23:09:46 +00:00
|
|
|
from flask import g
|
2022-12-02 22:21:18 +00:00
|
|
|
from pywebpush import webpush
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2022-12-02 22:21:18 +00:00
|
|
|
from files.classes import Comment, Notification, PushSubscription
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2022-12-11 23:44:34 +00:00
|
|
|
from .config.const import *
|
2022-06-24 14:30:59 +00:00
|
|
|
from .regex import *
|
2022-11-15 09:19:08 +00:00
|
|
|
from .sanitize import *
|
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
|
|
|
|
|
2023-02-07 03:31:49 +00:00
|
|
|
text_html = sanitize(text, blackjack="notification")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-09-02 01:59:05 +00:00
|
|
|
existing_comments = g.db.query(Comment.id).filter_by(author_id=AUTOJANNY_ID, parent_submission=None, body_html=text_html, is_bot=True).order_by(Comment.id).all()
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-09-02 01:59:05 +00:00
|
|
|
for c in existing_comments:
|
|
|
|
existing_notif = g.db.query(Notification.user_id).filter_by(user_id=uid, comment_id=c.id).one_or_none()
|
|
|
|
if not existing_notif:
|
|
|
|
notif = Notification(comment_id=c.id, user_id=uid)
|
|
|
|
g.db.add(notif)
|
2023-02-24 02:09:49 +00:00
|
|
|
|
|
|
|
g.db.flush()
|
|
|
|
push_notif({uid}, 'New notification', text, f'{SITE_FULL}/comment/{c.id}?read=true#context')
|
2022-09-02 01:59:05 +00:00
|
|
|
return
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-09-02 01:59:05 +00:00
|
|
|
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)
|
|
|
|
|
2023-02-24 02:09:49 +00:00
|
|
|
g.db.flush()
|
|
|
|
push_notif({uid}, 'New notification', text, f'{SITE_FULL}/comment/{cid}?read=true#context')
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
|
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)
|
2023-02-24 02:28:10 +00:00
|
|
|
add_notif(cid, uid, text)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
2022-07-08 19:03:04 +00:00
|
|
|
def notif_comment(text):
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-02-07 03:31:49 +00:00
|
|
|
text_html = sanitize(text, blackjack="notification")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-01-25 01:09:32 +00:00
|
|
|
g.db.flush()
|
2022-12-26 20:08:42 +00:00
|
|
|
|
2023-01-25 01:09:32 +00:00
|
|
|
existing = g.db.query(Comment.id).filter(
|
|
|
|
Comment.author_id == AUTOJANNY_ID,
|
|
|
|
Comment.parent_submission == None,
|
|
|
|
Comment.body_html == text_html,
|
|
|
|
Comment.is_bot == True,
|
|
|
|
).order_by(Comment.id).all()
|
|
|
|
|
|
|
|
if len(existing) > 1:
|
|
|
|
replace_with = existing[0][0]
|
|
|
|
replaced = [x[0] for x in existing[1:]]
|
|
|
|
|
|
|
|
for n in g.db.query(Notification).filter(Notification.comment_id.in_(replaced)).all():
|
|
|
|
n.comment_id = replace_with
|
|
|
|
g.db.add(n)
|
|
|
|
|
|
|
|
g.db.flush()
|
|
|
|
|
|
|
|
for c in g.db.query(Comment).filter(Comment.id.in_(replaced)).all():
|
|
|
|
g.db.delete(c)
|
|
|
|
|
|
|
|
return replace_with
|
|
|
|
elif existing:
|
|
|
|
return existing[0][0]
|
|
|
|
else:
|
|
|
|
return create_comment(text_html)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def notif_comment2(p):
|
|
|
|
|
2023-02-24 02:28:10 +00:00
|
|
|
text = f"@{p.author.username} has mentioned you: [{p.title}](/post/{p.id})"
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
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()
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2023-02-24 02:28:10 +00:00
|
|
|
if existing: return existing[0], text
|
2022-05-04 23:09:46 +00:00
|
|
|
else:
|
|
|
|
if p.sub: text += f" in <a href='/h/{p.sub}'>/h/{p.sub}"
|
2023-02-07 03:31:49 +00:00
|
|
|
text_html = sanitize(text, blackjack="notification")
|
2023-02-24 02:28:10 +00:00
|
|
|
return create_comment(text_html), text
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
2023-02-24 02:28:10 +00:00
|
|
|
def add_notif(cid, uid, text):
|
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)
|
|
|
|
|
2023-02-24 02:28:10 +00:00
|
|
|
g.db.flush()
|
|
|
|
push_notif({uid}, 'New notification', text, f'{SITE_FULL}/comment/{cid}?read=true#context')
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2023-02-18 16:58:59 +00:00
|
|
|
text = text.lower()
|
2022-05-04 23:09:46 +00:00
|
|
|
notify_users = set()
|
2023-02-18 16:58:59 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
for word, id in NOTIFIED_USERS.items():
|
2023-02-18 16:58:59 +00:00
|
|
|
if word in text and id not in notify_users:
|
|
|
|
notify_users.add(id)
|
|
|
|
|
2023-02-18 18:35:22 +00:00
|
|
|
if v.id != AEVANN_ID and '!biofoids' in text and SITE == 'rdrama.net':
|
2023-02-18 17:09:33 +00:00
|
|
|
if v.id not in BIOFOIDS:
|
|
|
|
abort(403, "Only members of the ping group can ping it!")
|
|
|
|
notify_users.update(BIOFOIDS)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
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):
|
2022-10-30 22:41:26 +00:00
|
|
|
if v.id != user.id and not v.any_block_exists(user):
|
2022-08-21 17:20:09 +00:00
|
|
|
notify_users.add(user.id)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-02-18 16:58:59 +00:00
|
|
|
if SITE_NAME == "WPD" and 'daisy' in text:
|
2022-10-06 07:38:33 +00:00
|
|
|
admin_ids = [x[0] for x in g.db.query(User.id).filter(User.admin_level >= PERMS['NOTIFICATIONS_SPECIFIC_WPD_COMMENTS']).all()]
|
2022-08-18 09:52:37 +00:00
|
|
|
notify_users.update(admin_ids)
|
|
|
|
|
2023-02-18 16:58:59 +00:00
|
|
|
return notify_users - bots - {v.id, 0}
|
2022-06-10 12:28:46 +00:00
|
|
|
|
2022-12-02 22:21:18 +00:00
|
|
|
|
2022-12-25 20:30:50 +00:00
|
|
|
def push_notif(uids, title, body, url):
|
2022-12-02 22:21:18 +00:00
|
|
|
if VAPID_PUBLIC_KEY == DEFAULT_CONFIG_VALUE:
|
|
|
|
return
|
|
|
|
|
2023-02-24 01:56:46 +00:00
|
|
|
if isinstance(url, tuple):
|
|
|
|
cid, posting_to_submission = url
|
|
|
|
if posting_to_submission:
|
|
|
|
url = f'{SITE_FULL}/comment/{cid}?read=true#context'
|
|
|
|
else:
|
|
|
|
url = f'{SITE_FULL}/@{c.wall_user.username}/wall/comment/{cid}?read=true#context'
|
|
|
|
|
2022-12-02 22:21:18 +00:00
|
|
|
if len(body) > PUSH_NOTIF_LIMIT:
|
|
|
|
body = body[:PUSH_NOTIF_LIMIT] + "..."
|
|
|
|
|
2022-12-25 20:30:50 +00:00
|
|
|
subscriptions = g.db.query(PushSubscription.subscription_json).filter(PushSubscription.user_id.in_(uids)).all()
|
2022-12-02 23:25:48 +00:00
|
|
|
subscriptions = [x[0] for x in subscriptions]
|
|
|
|
gevent.spawn(_push_notif_thread, subscriptions, title, body, url)
|
|
|
|
|
|
|
|
|
|
|
|
def _push_notif_thread(subscriptions, title, body, url):
|
2022-12-02 22:21:18 +00:00
|
|
|
for subscription in subscriptions:
|
2022-12-02 23:25:48 +00:00
|
|
|
try:
|
|
|
|
response = webpush(
|
|
|
|
subscription_info=json.loads(subscription),
|
|
|
|
data=json.dumps({
|
|
|
|
"title": title,
|
|
|
|
"body": body,
|
|
|
|
'url': url,
|
|
|
|
'icon': f'{SITE_FULL}/icon.webp?v=1',
|
|
|
|
}),
|
|
|
|
vapid_private_key=VAPID_PRIVATE_KEY,
|
|
|
|
vapid_claims={"sub": f"mailto:{EMAIL}"}
|
|
|
|
)
|
|
|
|
except: continue
|