MarseyWorld/files/helpers/alerts.py

234 lines
6.2 KiB
Python
Raw Normal View History

import json
[DO NOT MERGE] import detanglation (#442) * move Base definition to files.classes.__init__.py * fix ImportError * move userpage listing to users.py * don't import the app from classes * consts: set default values to avoid crashes consts: warn if the secret key is the default config value * card view: sneed (user db schema) * cloudflare: use DEFAULT_CONFIG_VALUE * const: set default values * decouple media.py from __main__ * pass database to avoid imports * import cleanup and import request not in const, but in the requests mega import * move asset_submissions site check to __init__ * asset submissions feature flag * flag * g.is_tor * don't import request where it's not needed * i think this is fine * mail: move to own routes and helper * wrappers * required wrappers move * unfuck wrappers a bit * move snappy quotes and marseys to stateful consts * marsify * :pepodrool: * fix missing import * import cache * ...and settings.py * and static.py * static needs cache * route * lmao all of the jinja shit was in feeds.py amazing * classes should only import what they need from flask * import Response * hdjbjdhbhjf * ... * dfdfdfdf * make get a non-required import * isort imports (mostly) * but actually * configs * reload config on import * fgfgfgfg * config * config * initialize snappy and test * cookie of doom debug * edfjnkf * xikscdfd * debug config * set session cookie domain, i think this fixes the can't login bug * sdfbgnhvfdsghbnjfbdvvfghnn * hrsfxgf * dump the entire config on a request * kyskyskyskyskyskyskyskyskys * duifhdskfjdfd * dfdfdfdfdfdfdfdfdfdfdfdf * dfdfdfdf * imoprt all of the consts beacuse fuck it * 😭 * dfdfdfdfdfdfsdasdf * print the entire session * rffdfdfjkfksj * fgbhffh * not the secret keys * minor bug fixes * be helpful in the warning * gfgfgfg * move warning lower * isort main imports (i hope this doesn't fuck something up) * test * session cookie domain redux * dfdfdfd * try only importing Flask * formkeys fix * y * :pepodrool: * route helper * remove before flight * dfdfdfdfdf * isort classes * isort helpers * move check_for_alts to routehelpers and also sort imports and get rid of unused ones * that previous commit but actkally * readd the cache in a dozen places they were implicitly imported * use g.is_tor instead of request.headers. bla bla bla * upgrade streamers to their own route file * get rid of unused imports in __main__ * fgfgf * don't pull in the entire ORM where we don't need it * features * explicit imports for the get helper * explicit imports for the get helper redux * testing allroutes * remove unused import * decouple flask from classes * syntax fix also remember these have side fx for some reason (why?) * move side effects out of the class * posts * testing on devrama * settings * reloading * settingssdsdsds * streamer features * site settings * testing settings on devrama * import * fix modlog * remove debug stuff * revert commit 67275b21ab6e2f2520819e84d10bfc1c746a15b6 * archiveorg to _archiveorg * skhudkfkjfd * fix cron for PCM * fix bugs that snekky wants me to * Fix call to realbody passing db, standardize kwarg * test * import check_for_alts from the right place * cloudflare * testing on devrama * fix cron i think * shadow properly * tasks * Remove print which will surely be annoying in prod. * v and create new session * use files.classes * make errors import little and fix rare 500 in /allow_nsfw * Revert "use files.classes" This reverts commit 98c10b876cf86ce058b7fb955cf1ec0bfb9996c6. * pass v to media functions rather than using g * fix * dfdfdfdfd * cleanup, py type checking is dumb so don't use it where it causes issues * Fix some merge bugs, add DEFAULT_RATELIMIT to main. * Fix imports on sqlalchemy expressions. * `from random import random` is an error. * Fix replies db param. * errors: fix missing import * fix rare 500: only send to GIFT_NOTIF_ID if it exists, and send them the right text * Fix signup formkey. * fix 2 500s * propagate db to submissions * fix replies * dfdfdfdf * Fix verifiedcolor. * is_manual * can't use getters outside of an app context * don't attempt to do gumroad on sites where it's not enabled * don't attempt to do gumraod on sites's where it's unnecessary * Revert "don't attempt to do gumroad on sites where it's not enabled" This reverts commit 6f8a6331878655492dfaf1907b27f8be513c14d3. * fix 500 * validate media type Co-authored-by: TLSM <duolsm@outlook.com>
2022-11-15 09:19:08 +00:00
from sys import stdout
import gevent
2022-05-04 23:09:46 +00:00
from flask import g
from pywebpush import webpush
2023-03-02 00:32:51 +00:00
import time
from sqlalchemy.sql import text
[DO NOT MERGE] import detanglation (#442) * move Base definition to files.classes.__init__.py * fix ImportError * move userpage listing to users.py * don't import the app from classes * consts: set default values to avoid crashes consts: warn if the secret key is the default config value * card view: sneed (user db schema) * cloudflare: use DEFAULT_CONFIG_VALUE * const: set default values * decouple media.py from __main__ * pass database to avoid imports * import cleanup and import request not in const, but in the requests mega import * move asset_submissions site check to __init__ * asset submissions feature flag * flag * g.is_tor * don't import request where it's not needed * i think this is fine * mail: move to own routes and helper * wrappers * required wrappers move * unfuck wrappers a bit * move snappy quotes and marseys to stateful consts * marsify * :pepodrool: * fix missing import * import cache * ...and settings.py * and static.py * static needs cache * route * lmao all of the jinja shit was in feeds.py amazing * classes should only import what they need from flask * import Response * hdjbjdhbhjf * ... * dfdfdfdf * make get a non-required import * isort imports (mostly) * but actually * configs * reload config on import * fgfgfgfg * config * config * initialize snappy and test * cookie of doom debug * edfjnkf * xikscdfd * debug config * set session cookie domain, i think this fixes the can't login bug * sdfbgnhvfdsghbnjfbdvvfghnn * hrsfxgf * dump the entire config on a request * kyskyskyskyskyskyskyskyskys * duifhdskfjdfd * dfdfdfdfdfdfdfdfdfdfdfdf * dfdfdfdf * imoprt all of the consts beacuse fuck it * 😭 * dfdfdfdfdfdfsdasdf * print the entire session * rffdfdfjkfksj * fgbhffh * not the secret keys * minor bug fixes * be helpful in the warning * gfgfgfg * move warning lower * isort main imports (i hope this doesn't fuck something up) * test * session cookie domain redux * dfdfdfd * try only importing Flask * formkeys fix * y * :pepodrool: * route helper * remove before flight * dfdfdfdfdf * isort classes * isort helpers * move check_for_alts to routehelpers and also sort imports and get rid of unused ones * that previous commit but actkally * readd the cache in a dozen places they were implicitly imported * use g.is_tor instead of request.headers. bla bla bla * upgrade streamers to their own route file * get rid of unused imports in __main__ * fgfgf * don't pull in the entire ORM where we don't need it * features * explicit imports for the get helper * explicit imports for the get helper redux * testing allroutes * remove unused import * decouple flask from classes * syntax fix also remember these have side fx for some reason (why?) * move side effects out of the class * posts * testing on devrama * settings * reloading * settingssdsdsds * streamer features * site settings * testing settings on devrama * import * fix modlog * remove debug stuff * revert commit 67275b21ab6e2f2520819e84d10bfc1c746a15b6 * archiveorg to _archiveorg * skhudkfkjfd * fix cron for PCM * fix bugs that snekky wants me to * Fix call to realbody passing db, standardize kwarg * test * import check_for_alts from the right place * cloudflare * testing on devrama * fix cron i think * shadow properly * tasks * Remove print which will surely be annoying in prod. * v and create new session * use files.classes * make errors import little and fix rare 500 in /allow_nsfw * Revert "use files.classes" This reverts commit 98c10b876cf86ce058b7fb955cf1ec0bfb9996c6. * pass v to media functions rather than using g * fix * dfdfdfdfd * cleanup, py type checking is dumb so don't use it where it causes issues * Fix some merge bugs, add DEFAULT_RATELIMIT to main. * Fix imports on sqlalchemy expressions. * `from random import random` is an error. * Fix replies db param. * errors: fix missing import * fix rare 500: only send to GIFT_NOTIF_ID if it exists, and send them the right text * Fix signup formkey. * fix 2 500s * propagate db to submissions * fix replies * dfdfdfdf * Fix verifiedcolor. * is_manual * can't use getters outside of an app context * don't attempt to do gumroad on sites where it's not enabled * don't attempt to do gumraod on sites's where it's unnecessary * Revert "don't attempt to do gumroad on sites where it's not enabled" This reverts commit 6f8a6331878655492dfaf1907b27f8be513c14d3. * fix 500 * validate media type Co-authored-by: TLSM <duolsm@outlook.com>
2022-11-15 09:19:08 +00:00
2023-02-24 06:31:06 +00:00
from files.classes import Comment, Notification, PushSubscription, Group
[DO NOT MERGE] import detanglation (#442) * move Base definition to files.classes.__init__.py * fix ImportError * move userpage listing to users.py * don't import the app from classes * consts: set default values to avoid crashes consts: warn if the secret key is the default config value * card view: sneed (user db schema) * cloudflare: use DEFAULT_CONFIG_VALUE * const: set default values * decouple media.py from __main__ * pass database to avoid imports * import cleanup and import request not in const, but in the requests mega import * move asset_submissions site check to __init__ * asset submissions feature flag * flag * g.is_tor * don't import request where it's not needed * i think this is fine * mail: move to own routes and helper * wrappers * required wrappers move * unfuck wrappers a bit * move snappy quotes and marseys to stateful consts * marsify * :pepodrool: * fix missing import * import cache * ...and settings.py * and static.py * static needs cache * route * lmao all of the jinja shit was in feeds.py amazing * classes should only import what they need from flask * import Response * hdjbjdhbhjf * ... * dfdfdfdf * make get a non-required import * isort imports (mostly) * but actually * configs * reload config on import * fgfgfgfg * config * config * initialize snappy and test * cookie of doom debug * edfjnkf * xikscdfd * debug config * set session cookie domain, i think this fixes the can't login bug * sdfbgnhvfdsghbnjfbdvvfghnn * hrsfxgf * dump the entire config on a request * kyskyskyskyskyskyskyskyskys * duifhdskfjdfd * dfdfdfdfdfdfdfdfdfdfdfdf * dfdfdfdf * imoprt all of the consts beacuse fuck it * 😭 * dfdfdfdfdfdfsdasdf * print the entire session * rffdfdfjkfksj * fgbhffh * not the secret keys * minor bug fixes * be helpful in the warning * gfgfgfg * move warning lower * isort main imports (i hope this doesn't fuck something up) * test * session cookie domain redux * dfdfdfd * try only importing Flask * formkeys fix * y * :pepodrool: * route helper * remove before flight * dfdfdfdfdf * isort classes * isort helpers * move check_for_alts to routehelpers and also sort imports and get rid of unused ones * that previous commit but actkally * readd the cache in a dozen places they were implicitly imported * use g.is_tor instead of request.headers. bla bla bla * upgrade streamers to their own route file * get rid of unused imports in __main__ * fgfgf * don't pull in the entire ORM where we don't need it * features * explicit imports for the get helper * explicit imports for the get helper redux * testing allroutes * remove unused import * decouple flask from classes * syntax fix also remember these have side fx for some reason (why?) * move side effects out of the class * posts * testing on devrama * settings * reloading * settingssdsdsds * streamer features * site settings * testing settings on devrama * import * fix modlog * remove debug stuff * revert commit 67275b21ab6e2f2520819e84d10bfc1c746a15b6 * archiveorg to _archiveorg * skhudkfkjfd * fix cron for PCM * fix bugs that snekky wants me to * Fix call to realbody passing db, standardize kwarg * test * import check_for_alts from the right place * cloudflare * testing on devrama * fix cron i think * shadow properly * tasks * Remove print which will surely be annoying in prod. * v and create new session * use files.classes * make errors import little and fix rare 500 in /allow_nsfw * Revert "use files.classes" This reverts commit 98c10b876cf86ce058b7fb955cf1ec0bfb9996c6. * pass v to media functions rather than using g * fix * dfdfdfdfd * cleanup, py type checking is dumb so don't use it where it causes issues * Fix some merge bugs, add DEFAULT_RATELIMIT to main. * Fix imports on sqlalchemy expressions. * `from random import random` is an error. * Fix replies db param. * errors: fix missing import * fix rare 500: only send to GIFT_NOTIF_ID if it exists, and send them the right text * Fix signup formkey. * fix 2 500s * propagate db to submissions * fix replies * dfdfdfdf * Fix verifiedcolor. * is_manual * can't use getters outside of an app context * don't attempt to do gumroad on sites where it's not enabled * don't attempt to do gumraod on sites's where it's unnecessary * Revert "don't attempt to do gumroad on sites where it's not enabled" This reverts commit 6f8a6331878655492dfaf1907b27f8be513c14d3. * fix 500 * validate media type Co-authored-by: TLSM <duolsm@outlook.com>
2022-11-15 09:19:08 +00:00
from .config.const import *
from .regex import *
[DO NOT MERGE] import detanglation (#442) * move Base definition to files.classes.__init__.py * fix ImportError * move userpage listing to users.py * don't import the app from classes * consts: set default values to avoid crashes consts: warn if the secret key is the default config value * card view: sneed (user db schema) * cloudflare: use DEFAULT_CONFIG_VALUE * const: set default values * decouple media.py from __main__ * pass database to avoid imports * import cleanup and import request not in const, but in the requests mega import * move asset_submissions site check to __init__ * asset submissions feature flag * flag * g.is_tor * don't import request where it's not needed * i think this is fine * mail: move to own routes and helper * wrappers * required wrappers move * unfuck wrappers a bit * move snappy quotes and marseys to stateful consts * marsify * :pepodrool: * fix missing import * import cache * ...and settings.py * and static.py * static needs cache * route * lmao all of the jinja shit was in feeds.py amazing * classes should only import what they need from flask * import Response * hdjbjdhbhjf * ... * dfdfdfdf * make get a non-required import * isort imports (mostly) * but actually * configs * reload config on import * fgfgfgfg * config * config * initialize snappy and test * cookie of doom debug * edfjnkf * xikscdfd * debug config * set session cookie domain, i think this fixes the can't login bug * sdfbgnhvfdsghbnjfbdvvfghnn * hrsfxgf * dump the entire config on a request * kyskyskyskyskyskyskyskyskys * duifhdskfjdfd * dfdfdfdfdfdfdfdfdfdfdfdf * dfdfdfdf * imoprt all of the consts beacuse fuck it * 😭 * dfdfdfdfdfdfsdasdf * print the entire session * rffdfdfjkfksj * fgbhffh * not the secret keys * minor bug fixes * be helpful in the warning * gfgfgfg * move warning lower * isort main imports (i hope this doesn't fuck something up) * test * session cookie domain redux * dfdfdfd * try only importing Flask * formkeys fix * y * :pepodrool: * route helper * remove before flight * dfdfdfdfdf * isort classes * isort helpers * move check_for_alts to routehelpers and also sort imports and get rid of unused ones * that previous commit but actkally * readd the cache in a dozen places they were implicitly imported * use g.is_tor instead of request.headers. bla bla bla * upgrade streamers to their own route file * get rid of unused imports in __main__ * fgfgf * don't pull in the entire ORM where we don't need it * features * explicit imports for the get helper * explicit imports for the get helper redux * testing allroutes * remove unused import * decouple flask from classes * syntax fix also remember these have side fx for some reason (why?) * move side effects out of the class * posts * testing on devrama * settings * reloading * settingssdsdsds * streamer features * site settings * testing settings on devrama * import * fix modlog * remove debug stuff * revert commit 67275b21ab6e2f2520819e84d10bfc1c746a15b6 * archiveorg to _archiveorg * skhudkfkjfd * fix cron for PCM * fix bugs that snekky wants me to * Fix call to realbody passing db, standardize kwarg * test * import check_for_alts from the right place * cloudflare * testing on devrama * fix cron i think * shadow properly * tasks * Remove print which will surely be annoying in prod. * v and create new session * use files.classes * make errors import little and fix rare 500 in /allow_nsfw * Revert "use files.classes" This reverts commit 98c10b876cf86ce058b7fb955cf1ec0bfb9996c6. * pass v to media functions rather than using g * fix * dfdfdfdfd * cleanup, py type checking is dumb so don't use it where it causes issues * Fix some merge bugs, add DEFAULT_RATELIMIT to main. * Fix imports on sqlalchemy expressions. * `from random import random` is an error. * Fix replies db param. * errors: fix missing import * fix rare 500: only send to GIFT_NOTIF_ID if it exists, and send them the right text * Fix signup formkey. * fix 2 500s * propagate db to submissions * fix replies * dfdfdfdf * Fix verifiedcolor. * is_manual * can't use getters outside of an app context * don't attempt to do gumroad on sites where it's not enabled * don't attempt to do gumraod on sites's where it's unnecessary * Revert "don't attempt to do gumroad on sites where it's not enabled" This reverts commit 6f8a6331878655492dfaf1907b27f8be513c14d3. * fix 500 * validate media type Co-authored-by: TLSM <duolsm@outlook.com>
2022-11-15 09:19:08 +00:00
from .sanitize import *
2022-05-04 23:09:46 +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,
body_html=text_html,
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
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
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
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)
push_notif({uid}, 'New notification', text, f'{SITE_FULL}/comment/{c.id}?read=true#context')
return
2022-05-04 23:09:46 +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)
push_notif({uid}, 'New notification', text, f'{SITE_FULL}/comment/{cid}?read=true#context')
2022-05-04 23:09:46 +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
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
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
g.db.flush()
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):
text = f"@{p.author_name} has mentioned you: [{p.title}](/post/{p.id})"
2023-02-24 02:28:10 +00:00
2022-05-04 23:09:46 +00:00
search_html = f'%</a> has mentioned you: <a href="/post/{p.id}">%'
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-26 11:22:16 +00:00
def add_notif(cid, uid, text, pushnotif_url=''):
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)
if not pushnotif_url:
pushnotif_url = f'{SITE_FULL}/comment/{cid}?read=true#context'
if ' has mentioned you: [' in text:
text = text.split(':')[0] + '!'
push_notif({uid}, 'New notification', text, pushnotif_url)
2023-02-24 02:28:10 +00:00
2022-05-04 23:09:46 +00:00
def NOTIFY_USERS(text, v, oldtext=None, ghost=False):
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
2023-03-02 00:32:51 +00:00
for word, id in NOTIFIED_USERS.items():
if word in text:
notify_users.add(id)
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-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:
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-03-01 20:28:34 +00:00
if FEATURES['PING_GROUPS']:
cost = 0
2023-03-12 10:27:22 +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:
continue
2023-03-11 05:07:10 +00:00
if i.group(1) == 'everyone' and not v.shadowbanned:
cost = g.db.query(User).count() * 5
if cost > v.coins:
2023-03-12 00:08:57 +00:00
abort(403, f"You need {cost} coins to mention these ping groups!")
g.db.query(User).update({ User.coins: User.coins + 5 })
2023-03-04 18:32:55 +00:00
v.charge_account('coins', cost)
return 'everyone'
else:
2023-03-11 05:07:10 +00:00
group = g.db.get(Group, i.group(1))
if not group: continue
members = group.member_ids - notify_users - v.all_twoway_blocks
notify_users.update(members)
if ghost or v.id not in group.member_ids:
if group.name == 'biofoids': mul = 10
else: mul = 5
cost += len(members) * mul
if cost > v.coins:
2023-03-12 00:08:57 +00:00
abort(403, f"You need {cost} coins to mention these ping groups!")
g.db.query(User).filter(User.id.in_(members)).update({ User.coins: User.coins + mul })
2023-03-04 18:32:55 +00:00
v.charge_account('coins', cost)
return notify_users - bots - {v.id, 0} - v.all_twoway_blocks
2022-06-10 12:28:46 +00:00
2023-02-24 03:00:22 +00:00
def push_notif(uids, title, body, url_or_comment):
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
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
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]
g.db.flush()
2022-12-02 23:25:48 +00:00
gevent.spawn(_push_notif_thread, subscriptions, title, body, url)
def _push_notif_thread(subscriptions, title, body, url):
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
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;""")
g.db.execute(_everyone_query)