add !everyone

master
Aevann 2023-03-02 02:32:51 +02:00
parent fdda74a008
commit 308bb03125
4 changed files with 89 additions and 44 deletions

View File

@ -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,19 +130,29 @@ 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):
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
if cost > v.coins:
@ -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)

View File

@ -309,6 +309,9 @@ def comment(v:User):
if not v.shadowbanned:
notify_users = NOTIFY_USERS(body, v)
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:
@ -660,6 +663,9 @@ def edit_comment(cid, v):
notify_users = NOTIFY_USERS(body, v)
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:

View File

@ -50,6 +50,9 @@ def publish(pid, v):
if notify_users:
cid, text = notif_comment2(p)
if notify_users == 'everyone':
alert_everyone(cid)
else:
for x in notify_users:
add_notif(cid, x, text, pushnotif_url=p.permalink)
@ -641,6 +644,9 @@ def submit_post(v:User, sub=None):
if notify_users:
cid, text = notif_comment2(p)
if notify_users == 'everyone':
alert_everyone(cid)
else:
for x in notify_users:
add_notif(cid, x, text, pushnotif_url=p.permalink)
@ -1026,6 +1032,9 @@ def edit_post(pid, v):
notify_users = NOTIFY_USERS(f'{p.title} {p.body}', v)
if notify_users:
cid, text = notif_comment2(p)
if notify_users == 'everyone':
alert_everyone(cid)
else:
for x in notify_users:
add_notif(cid, x, text, pushnotif_url=p.permalink)

View File

@ -259,6 +259,9 @@ def settings_personal_post(v):
if notify_users:
text = f"@{v.username} has added you to their friends list!"
cid = notif_comment(text)
if notify_users == 'everyone':
alert_everyone(cid)
else:
for x in notify_users:
add_notif(cid, x, text)
@ -284,6 +287,9 @@ def settings_personal_post(v):
if notify_users:
text = f"@{v.username} has added you to their enemies list!"
cid = notif_comment(text)
if notify_users == 'everyone':
alert_everyone(cid)
else:
for x in notify_users:
add_notif(cid, x, text)