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
|
2023-03-02 00:32:51 +00:00
|
|
|
import time
|
|
|
|
from sqlalchemy.sql import text
|
2023-07-27 19:46:29 +00:00
|
|
|
from sqlalchemy.orm import load_only
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2023-02-24 06:31:06 +00:00
|
|
|
from files.classes import Comment, Notification, PushSubscription, Group
|
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 *
|
2023-09-22 17:01:30 +00:00
|
|
|
from .slurs_and_profanities import censor_slurs_profanities
|
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,
|
2023-06-23 13:46:42 +00:00
|
|
|
parent_post=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)
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(new_comment)
|
|
|
|
g.db.flush()
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
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):
|
2023-05-12 22:29:34 +00:00
|
|
|
if uid in BOT_IDs: return
|
2022-05-28 02:20:31 +00:00
|
|
|
|
2023-10-04 19:34:00 +00:00
|
|
|
if not (hasattr(g, 'v') and g.v and v.shadowbanned) or g.db.query(User.admin_level).filter_by(id=uid).one()[0] >= PERMS['USER_SHADOWBAN']:
|
|
|
|
return
|
|
|
|
|
2023-02-07 03:31:49 +00:00
|
|
|
text_html = sanitize(text, blackjack="notification")
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-06-23 13:46:42 +00:00
|
|
|
existing_comments = g.db.query(Comment.id).filter_by(author_id=AUTOJANNY_ID, parent_post=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:
|
2023-03-16 06:27:58 +00:00
|
|
|
existing_notif = g.db.query(Notification.user_id).filter_by(user_id=uid, comment_id=c.id).one_or_none()
|
2022-09-02 01:59:05 +00:00
|
|
|
if not existing_notif:
|
|
|
|
notif = Notification(comment_id=c.id, user_id=uid)
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(notif)
|
2023-02-24 02:09:49 +00:00
|
|
|
|
2023-08-23 10:42:25 +00:00
|
|
|
push_notif({uid}, 'New notification', text, f'{SITE_FULL}/notification/{c.id}')
|
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)
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(notif)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-08-23 10:42:25 +00:00
|
|
|
push_notif({uid}, 'New notification', text, f'{SITE_FULL}/notification/{cid}')
|
2023-02-24 02:09:49 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2022-07-08 19:03:04 +00:00
|
|
|
def send_notification(uid, text):
|
2023-05-12 22:29:34 +00:00
|
|
|
if uid in BOT_IDs: return
|
2023-10-04 19:34:00 +00:00
|
|
|
|
|
|
|
if not (hasattr(g, 'v') and g.v and v.shadowbanned) or g.db.query(User.admin_level).filter_by(id=uid).one()[0] >= PERMS['USER_SHADOWBAN']:
|
|
|
|
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-03-16 06:27:58 +00:00
|
|
|
existing = g.db.query(Comment.id).filter(
|
2023-01-25 01:09:32 +00:00
|
|
|
Comment.author_id == AUTOJANNY_ID,
|
2023-06-23 13:46:42 +00:00
|
|
|
Comment.parent_post == None,
|
2023-01-25 01:09:32 +00:00
|
|
|
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:]]
|
|
|
|
|
2023-08-05 19:26:42 +00:00
|
|
|
for n in g.db.query(Notification).filter(Notification.comment_id.in_(replaced)):
|
2023-01-25 01:09:32 +00:00
|
|
|
n.comment_id = replace_with
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(n)
|
2023-01-25 01:09:32 +00:00
|
|
|
|
2023-08-05 19:26:42 +00:00
|
|
|
for c in g.db.query(Comment).filter(Comment.id.in_(replaced)):
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.delete(c)
|
2023-01-25 01:09:32 +00:00
|
|
|
|
|
|
|
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-07-03 23:43:29 +00:00
|
|
|
text = f"@{p.author_name} has mentioned you: [{p.title}](/post/{p.id})"
|
2023-02-24 02:28:10 +00:00
|
|
|
|
2023-07-03 23:43:29 +00:00
|
|
|
search_html = f'%</a> has mentioned you: <a href="/post/{p.id}"%'
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-06-23 13:46:42 +00:00
|
|
|
existing = g.db.query(Comment.id).filter(Comment.author_id == AUTOJANNY_ID, Comment.parent_post == 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-07-27 15:41:39 +00:00
|
|
|
text_html = sanitize(text, blackjack="notification", post_mention_notif=True)
|
2023-02-24 02:28:10 +00:00
|
|
|
return create_comment(text_html), text
|
2022-05-04 23:09:46 +00:00
|
|
|
|
|
|
|
|
2023-02-26 11:22:16 +00:00
|
|
|
def add_notif(cid, uid, text, pushnotif_url=''):
|
2023-05-12 22:29:34 +00:00
|
|
|
if uid in BOT_IDs: return
|
2022-05-28 02:20:31 +00:00
|
|
|
|
2023-03-16 06:27:58 +00:00
|
|
|
existing = g.db.query(Notification.user_id).filter_by(comment_id=cid, user_id=uid).one_or_none()
|
2022-05-04 23:09:46 +00:00
|
|
|
if not existing:
|
|
|
|
notif = Notification(comment_id=cid, user_id=uid)
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(notif)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-02-26 09:37:44 +00:00
|
|
|
if not pushnotif_url:
|
2023-08-23 10:42:25 +00:00
|
|
|
pushnotif_url = f'{SITE_FULL}/notification/{cid}'
|
2023-02-26 09:37:44 +00:00
|
|
|
|
|
|
|
if ' has mentioned you: [' in text:
|
|
|
|
text = text.split(':')[0] + '!'
|
|
|
|
|
2023-08-02 08:27:03 +00:00
|
|
|
if not request.path.startswith('/submit'):
|
|
|
|
push_notif({uid}, 'New notification', text, pushnotif_url)
|
2023-02-24 02:28:10 +00:00
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-09-05 18:24:10 +00:00
|
|
|
def NOTIFY_USERS(text, v, oldtext=None, ghost=False, log_cost=None, followers_ping=True, commenters_ping_post_id=None):
|
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()
|
2023-08-15 21:35:43 +00:00
|
|
|
|
|
|
|
if oldtext:
|
|
|
|
oldtext = oldtext.lower()
|
|
|
|
|
2022-05-04 23:09:46 +00:00
|
|
|
notify_users = set()
|
2023-02-18 16:58:59 +00:00
|
|
|
|
2023-03-02 00:32:51 +00:00
|
|
|
for word, id in NOTIFIED_USERS.items():
|
|
|
|
if word in text:
|
|
|
|
notify_users.add(id)
|
2023-02-28 19:27:38 +00:00
|
|
|
|
2023-03-12 10:27:22 +00:00
|
|
|
names = set(m.group(1) for m in mention_regex.finditer(text))
|
2023-03-11 05:07:10 +00:00
|
|
|
|
2023-08-15 21:35:43 +00:00
|
|
|
if oldtext:
|
|
|
|
oldnames = set(m.group(1) for m in mention_regex.finditer(oldtext))
|
|
|
|
names = names - oldnames
|
|
|
|
|
2023-03-01 20:28:34 +00:00
|
|
|
user_ids = get_users(names, ids_only=True, graceful=True)
|
|
|
|
notify_users.update(user_ids)
|
2022-05-04 23:09:46 +00:00
|
|
|
|
2023-02-18 16:58:59 +00:00
|
|
|
if SITE_NAME == "WPD" and 'daisy' in text:
|
2023-08-11 13:15:34 +00:00
|
|
|
admin_ids = [x[0] for x in g.db.query(User.id).filter(User.admin_level >= PERMS['NOTIFICATIONS_SPECIFIC_WPD_COMMENTS'])]
|
2022-08-18 09:52:37 +00:00
|
|
|
notify_users.update(admin_ids)
|
|
|
|
|
2023-03-01 20:28:34 +00:00
|
|
|
|
2023-03-02 19:56:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
if FEATURES['PING_GROUPS']:
|
|
|
|
cost = 0
|
2023-08-09 13:44:19 +00:00
|
|
|
coin_receivers = set()
|
2023-03-02 19:56:43 +00:00
|
|
|
|
2023-08-20 00:58:58 +00:00
|
|
|
for i in group_mention_regex.finditer(text):
|
2023-03-11 05:07:10 +00:00
|
|
|
if oldtext and i.group(1) in oldtext:
|
2023-03-02 19:56:43 +00:00
|
|
|
continue
|
|
|
|
|
2023-10-04 19:34:00 +00:00
|
|
|
if i.group(1) == 'everyone':
|
2023-08-20 16:36:13 +00:00
|
|
|
cost = g.db.query(User).count() * 5
|
2023-08-20 19:37:26 +00:00
|
|
|
if cost > v.coins + v.marseybux:
|
|
|
|
abort(403, f"You need {cost} currency to mention these ping groups!")
|
2023-07-02 21:42:34 +00:00
|
|
|
|
2023-08-09 13:10:44 +00:00
|
|
|
v.charge_account('combined', cost)
|
2023-06-29 20:48:27 +00:00
|
|
|
if log_cost:
|
2023-08-13 16:02:55 +00:00
|
|
|
log_cost.ping_cost += cost
|
2023-03-02 19:56:43 +00:00
|
|
|
return 'everyone'
|
2023-07-21 14:27:45 +00:00
|
|
|
elif i.group(1) == 'jannies':
|
|
|
|
group = None
|
2023-08-11 13:15:34 +00:00
|
|
|
member_ids = set([x[0] for x in g.db.query(User.id).filter(User.admin_level > 0, User.id != AEVANN_ID)])
|
2023-08-11 13:34:56 +00:00
|
|
|
elif i.group(1) == 'followers':
|
2023-08-11 13:38:49 +00:00
|
|
|
if not followers_ping:
|
|
|
|
abort(403, f"You can't use !followers in posts!")
|
2023-08-11 13:34:56 +00:00
|
|
|
group = None
|
|
|
|
member_ids = set([x[0] for x in g.db.query(Follow.user_id).filter_by(target_id=v.id)])
|
2023-09-05 18:24:10 +00:00
|
|
|
elif i.group(1) == 'commenters':
|
|
|
|
if not commenters_ping_post_id:
|
|
|
|
abort(403, "You can only use !commenters in comments made under posts!")
|
|
|
|
group = None
|
|
|
|
member_ids = set([x[0] for x in g.db.query(User.id).join(Comment, Comment.author_id == User.id).filter(Comment.parent_post == commenters_ping_post_id)]) - {v.id}
|
2023-03-02 19:56:43 +00:00
|
|
|
else:
|
2023-03-16 06:27:58 +00:00
|
|
|
group = g.db.get(Group, i.group(1))
|
2023-03-02 19:56:43 +00:00
|
|
|
if not group: continue
|
2023-07-21 14:27:45 +00:00
|
|
|
member_ids = group.member_ids
|
2023-03-02 19:56:43 +00:00
|
|
|
|
2023-09-06 18:02:47 +00:00
|
|
|
members = member_ids - notify_users - BOT_IDs - v.all_twoway_blocks - v.muters
|
2023-05-05 21:45:25 +00:00
|
|
|
|
2023-07-21 14:27:45 +00:00
|
|
|
notify_users.update(members)
|
2023-03-02 19:56:43 +00:00
|
|
|
|
2023-08-11 13:34:56 +00:00
|
|
|
if (ghost or v.id not in member_ids) and i.group(1) != 'followers':
|
2023-08-03 05:16:57 +00:00
|
|
|
if group and group.name == 'verifiedrich':
|
2023-08-01 21:56:13 +00:00
|
|
|
abort(403, f"Only !verifiedrich members can mention it!")
|
2023-08-20 16:36:13 +00:00
|
|
|
cost += len(members) * 5
|
2023-08-20 19:37:26 +00:00
|
|
|
if cost > v.coins + v.marseybux:
|
|
|
|
abort(403, f"You need {cost} currency to mention these ping groups!")
|
2023-03-02 19:56:43 +00:00
|
|
|
|
2023-07-21 14:27:45 +00:00
|
|
|
if log_cost:
|
2023-08-13 16:02:55 +00:00
|
|
|
log_cost.ping_cost += cost
|
2023-06-24 20:31:12 +00:00
|
|
|
|
2023-08-11 13:34:56 +00:00
|
|
|
if i.group(1) in {'biofoids','neofoids','jannies'}:
|
2023-08-09 13:45:19 +00:00
|
|
|
coin_receivers.update(member_ids)
|
2023-08-09 13:44:19 +00:00
|
|
|
|
2023-08-09 11:48:02 +00:00
|
|
|
if cost:
|
2023-08-09 13:10:44 +00:00
|
|
|
v.charge_account('combined', cost)
|
2023-03-02 19:56:43 +00:00
|
|
|
|
2023-08-09 13:45:19 +00:00
|
|
|
if coin_receivers:
|
|
|
|
g.db.query(User).options(load_only(User.id)).filter(User.id.in_(coin_receivers)).update({ User.coins: User.coins + 10 })
|
2023-08-09 13:44:19 +00:00
|
|
|
|
2023-08-31 10:46:07 +00:00
|
|
|
if len(notify_users) > 400 and v.admin_level < PERMS['POST_COMMENT_INFINITE_PINGS']:
|
2023-08-20 02:11:59 +00:00
|
|
|
abort(403, "You can only notify a maximum of 400 users.")
|
2023-08-20 00:58:58 +00:00
|
|
|
|
2023-09-06 18:02:47 +00:00
|
|
|
return notify_users - BOT_IDs - {v.id, 0} - v.all_twoway_blocks - v.muters
|
2022-06-10 12:28:46 +00:00
|
|
|
|
2022-12-02 22:21:18 +00:00
|
|
|
|
2023-02-24 03:00:22 +00:00
|
|
|
def push_notif(uids, title, body, url_or_comment):
|
2023-08-20 08:17:38 +00:00
|
|
|
if hasattr(g, 'v') and g.v and g.v.shadowbanned:
|
2023-10-04 19:44:55 +00:00
|
|
|
uids = [x[0] for x in g.db.query(User.id).filter(User.id.in_(uids), User.admin_level >= PERMS['USER_SHADOWBAN']).all()]
|
2023-10-04 19:34:00 +00:00
|
|
|
if not uids:
|
|
|
|
return
|
2023-08-20 02:36:43 +00:00
|
|
|
|
2022-12-02 22:21:18 +00:00
|
|
|
if VAPID_PUBLIC_KEY == DEFAULT_CONFIG_VALUE:
|
|
|
|
return
|
|
|
|
|
2023-02-24 03:00:22 +00:00
|
|
|
if isinstance(url_or_comment, Comment):
|
|
|
|
c = url_or_comment
|
2023-03-24 22:19:14 +00:00
|
|
|
if c.is_banned: return
|
|
|
|
|
2023-02-24 03:00:22 +00:00
|
|
|
if c.wall_user_id:
|
|
|
|
url = f'{SITE_FULL}/@{c.wall_user.username}/wall/comment/{c.id}?read=true#context'
|
2023-02-24 01:56:46 +00:00
|
|
|
else:
|
2023-02-24 03:00:22 +00:00
|
|
|
url = f'{SITE_FULL}/comment/{c.id}?read=true#context'
|
|
|
|
else:
|
|
|
|
url = url_or_comment
|
2023-02-24 01:56:46 +00:00
|
|
|
|
2022-12-02 22:21:18 +00:00
|
|
|
if len(body) > PUSH_NOTIF_LIMIT:
|
|
|
|
body = body[:PUSH_NOTIF_LIMIT] + "..."
|
|
|
|
|
2023-09-26 15:37:07 +00:00
|
|
|
body = censor_slurs_profanities(body, None, True)
|
2023-03-12 14:56:44 +00:00
|
|
|
|
2023-03-16 06:27:58 +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]
|
2023-08-20 16:24:26 +00:00
|
|
|
gevent.spawn(_push_notif_thread, subscriptions, title, body, url)
|
2022-12-02 23:25:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
2023-07-26 12:20:32 +00:00
|
|
|
webpush(
|
2022-12-02 23:25:48 +00:00
|
|
|
subscription_info=json.loads(subscription),
|
|
|
|
data=json.dumps({
|
|
|
|
"title": title,
|
|
|
|
"body": body,
|
|
|
|
'url': url,
|
2023-06-29 20:14:30 +00:00
|
|
|
'icon': f'{SITE_FULL}/icon.webp?x=6',
|
2022-12-02 23:25:48 +00:00
|
|
|
}),
|
|
|
|
vapid_private_key=VAPID_PRIVATE_KEY,
|
|
|
|
vapid_claims={"sub": f"mailto:{EMAIL}"}
|
|
|
|
)
|
|
|
|
except: continue
|
2023-03-02 00:32:51 +00:00
|
|
|
|
|
|
|
def alert_everyone(cid):
|
|
|
|
cid = int(cid)
|
|
|
|
t = int(time.time())
|
|
|
|
_everyone_query = text(f"""
|
|
|
|
insert into notifications
|
|
|
|
select id, {cid}, false, {t} from users
|
|
|
|
on conflict do nothing;""")
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.execute(_everyone_query)
|