diff --git a/files/helpers/alerts.py b/files/helpers/alerts.py index 761f15d9b..e07df6a97 100644 --- a/files/helpers/alerts.py +++ b/files/helpers/alerts.py @@ -4,6 +4,8 @@ from sys import stdout import gevent from flask import g from pywebpush import webpush +import time +from sqlalchemy.sql import text from files.classes import Comment, Notification, PushSubscription, Group @@ -128,18 +130,28 @@ def NOTIFY_USERS(text, v): text = text.lower() notify_users = set() - for word, id in NOTIFIED_USERS.items(): - if word in text: - notify_users.add(id) - if FEATURES['PING_GROUPS']: billed = set() + everyone = False for i in group_mention_regex.finditer(text): - group = g.db.get(Group, i.group(2)) - if group: - if v.id not in group.member_ids: - billed.update(group.member_ids) - notify_users.update(group.member_ids) + if i.group(2) == 'everyone' and not v.shadowbanned: + everyone = True + break + else: + group = g.db.get(Group, i.group(2)) + if group: + if v.id not in group.member_ids: + billed.update(group.member_ids) + notify_users.update(group.member_ids) + + if everyone: + cost = g.db.query(User).count() * 5 + if cost > v.coins: + abort(403, f"You need {cost} coins for this!") + g.db.query(User).update({ User.coins: User.coins + 5 }) + v.coins -= cost + g.db.add(v) + return 'everyone' if billed: cost = len(billed) * 5 @@ -149,6 +161,9 @@ def NOTIFY_USERS(text, v): v.coins -= cost g.db.add(v) + for word, id in NOTIFIED_USERS.items(): + if word in text: + notify_users.add(id) names = set(m.group(2) for m in mention_regex.finditer(text)) user_ids = get_users(names, ids_only=True, graceful=True) @@ -200,3 +215,12 @@ def _push_notif_thread(subscriptions, title, body, url): vapid_claims={"sub": f"mailto:{EMAIL}"} ) except: continue + +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;""") + g.db.execute(_everyone_query) diff --git a/files/routes/comments.py b/files/routes/comments.py index 37b396683..31c077774 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -309,32 +309,35 @@ def comment(v:User): if not v.shadowbanned: notify_users = NOTIFY_USERS(body, v) - push_notif(notify_users, f'New mention of you by @{c.author_name}', c.body, c) + if notify_users == 'everyone': + alert_everyone(c.id) + else: + push_notif(notify_users, f'New mention of you by @{c.author_name}', c.body, c) - if c.level == 1 and posting_to_submission: - subscriber_ids = [x[0] for x in g.db.query(Subscription.user_id).filter(Subscription.submission_id == post_target.id, Subscription.user_id != v.id).all()] + if c.level == 1 and posting_to_submission: + subscriber_ids = [x[0] for x in g.db.query(Subscription.user_id).filter(Subscription.submission_id == post_target.id, Subscription.user_id != v.id).all()] - notify_users.update(subscriber_ids) + notify_users.update(subscriber_ids) - push_notif(subscriber_ids, f'New comment in subscribed thread by @{c.author_name}', c.body, c) + push_notif(subscriber_ids, f'New comment in subscribed thread by @{c.author_name}', c.body, c) - if parent_user.id != v.id: - notify_users.add(parent_user.id) + if parent_user.id != v.id: + notify_users.add(parent_user.id) - for x in notify_users-bots: - n = Notification(comment_id=c.id, user_id=x) - g.db.add(n) + for x in notify_users-bots: + n = Notification(comment_id=c.id, user_id=x) + g.db.add(n) - if parent_user.id != v.id and not v.shadowbanned: - if isinstance(parent, User): - title = f"New comment on your wall by @{c.author_name}" - else: - title = f'New reply by @{c.author_name}' + if parent_user.id != v.id and not v.shadowbanned: + if isinstance(parent, User): + title = f"New comment on your wall by @{c.author_name}" + else: + title = f'New reply by @{c.author_name}' - if len(c.body) > PUSH_NOTIF_LIMIT: notifbody = c.body[:PUSH_NOTIF_LIMIT] + '...' - else: notifbody = c.body + if len(c.body) > PUSH_NOTIF_LIMIT: notifbody = c.body[:PUSH_NOTIF_LIMIT] + '...' + else: notifbody = c.body - push_notif({parent_user.id}, title, notifbody, c) + push_notif({parent_user.id}, title, notifbody, c) vote = CommentVote(user_id=v.id, comment_id=c.id, @@ -660,13 +663,16 @@ def edit_comment(cid, v): notify_users = NOTIFY_USERS(body, v) - for x in notify_users-bots: - notif = g.db.query(Notification).filter_by(comment_id=c.id, user_id=x).one_or_none() - if not notif: - n = Notification(comment_id=c.id, user_id=x) - g.db.add(n) - if not v.shadowbanned: - push_notif({x}, f'New mention of you by @{c.author_name}', c.body, c) + if notify_users == 'everyone': + alert_everyone(c.id) + else: + for x in notify_users-bots: + notif = g.db.query(Notification).filter_by(comment_id=c.id, user_id=x).one_or_none() + if not notif: + n = Notification(comment_id=c.id, user_id=x) + g.db.add(n) + if not v.shadowbanned: + push_notif({x}, f'New mention of you by @{c.author_name}', c.body, c) g.db.commit() return {"body": c.body, "comment": c.realbody(v)} diff --git a/files/routes/posts.py b/files/routes/posts.py index d16d62845..05b0e2636 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -50,8 +50,11 @@ def publish(pid, v): if notify_users: cid, text = notif_comment2(p) - for x in notify_users: - add_notif(cid, x, text, pushnotif_url=p.permalink) + if notify_users == 'everyone': + alert_everyone(cid) + else: + for x in notify_users: + add_notif(cid, x, text, pushnotif_url=p.permalink) cache.delete_memoized(frontlist) @@ -641,8 +644,11 @@ def submit_post(v:User, sub=None): if notify_users: cid, text = notif_comment2(p) - for x in notify_users: - add_notif(cid, x, text, pushnotif_url=p.permalink) + if notify_users == 'everyone': + alert_everyone(cid) + else: + for x in notify_users: + add_notif(cid, x, text, pushnotif_url=p.permalink) if v.agendaposter and not v.marseyawarded and AGENDAPOSTER_PHRASE not in f'{p.body}{p.title}'.lower() and sub != 'chudrama': p.is_banned = True @@ -1026,8 +1032,11 @@ def edit_post(pid, v): notify_users = NOTIFY_USERS(f'{p.title} {p.body}', v) if notify_users: cid, text = notif_comment2(p) - for x in notify_users: - add_notif(cid, x, text, pushnotif_url=p.permalink) + if notify_users == 'everyone': + alert_everyone(cid) + else: + for x in notify_users: + add_notif(cid, x, text, pushnotif_url=p.permalink) if v.id == p.author_id: if int(time.time()) - p.created_utc > 60 * 3: p.edited_utc = int(time.time()) diff --git a/files/routes/settings.py b/files/routes/settings.py index ef02c9e3c..6ff09f4bf 100644 --- a/files/routes/settings.py +++ b/files/routes/settings.py @@ -259,8 +259,11 @@ def settings_personal_post(v): if notify_users: text = f"@{v.username} has added you to their friends list!" cid = notif_comment(text) - for x in notify_users: - add_notif(cid, x, text) + if notify_users == 'everyone': + alert_everyone(cid) + else: + for x in notify_users: + add_notif(cid, x, text) v.friends = friends[:1000] v.friends_html=friends_html @@ -284,8 +287,11 @@ def settings_personal_post(v): if notify_users: text = f"@{v.username} has added you to their enemies list!" cid = notif_comment(text) - for x in notify_users: - add_notif(cid, x, text) + if notify_users == 'everyone': + alert_everyone(cid) + else: + for x in notify_users: + add_notif(cid, x, text) v.enemies = enemies[:1000] v.enemies_html=enemies_html