diff --git a/files/routes/comments.py b/files/routes/comments.py index f67dd6786..323b9f2b6 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -28,6 +28,34 @@ if PUSHER_ID != '3435tdfsdudebussylmaoxxt43': WORDLE_COLOR_MAPPINGS = {-1: "🟥", 0: "🟨", 1: "🟩"} +def pusher_thread(interests, c): + if len(c.body) > 500: notifbody = c.body[:500] + '...' + else: notifbody = c.body + + beams_client.publish_to_interests( + interests=[interests], + publish_body={ + 'web': { + 'notification': { + 'title': f'New reply by @{c.author_name}', + 'body': notifbody, + 'deep_link': f'{SITE_FULL}/comment/{c.id}?context=8&read=true#context', + 'icon': f'{SITE_FULL}/assets/images/{SITE_NAME}/icon.webp?a=1011', + } + }, + 'fcm': { + 'notification': { + 'title': f'New reply by @{c.author_name}', + 'body': notifbody, + }, + 'data': { + 'url': f'/comment/{c.id}?context=8&read=true#context', + } + } + }, + ) + stdout.flush() + @app.get("/comment/") @app.get("/post///") @app.get("/logged_out/comment/") @@ -602,33 +630,10 @@ def api_comment(v): n = Notification(comment_id=c.id, user_id=x) g.db.add(n) - if parent.author.id != v.id and PUSHER_ID != '3435tdfsdudebussylmaoxxt43': - if len(c.body) > 500: notifbody = c.body[:500] + '...' - else: notifbody = c.body - - beams_client.publish_to_interests( - interests=[f'{request.host}{parent.author.id}'], - publish_body={ - 'web': { - 'notification': { - 'title': f'New reply by @{c.author_name}', - 'body': notifbody, - 'deep_link': f'{SITE_FULL}/comment/{c.id}?context=8&read=true#context', - 'icon': f'{SITE_FULL}/assets/images/{SITE_NAME}/icon.webp?a=1011', - } - }, - 'fcm': { - 'notification': { - 'title': f'New reply by @{c.author_name}', - 'body': notifbody, - }, - 'data': { - 'url': f'/comment/{c.id}?context=8&read=true#context', - } - } - }, - ) + if parent.author.id != v.id and PUSHER_ID != '3435tdfsdudebussylmaoxxt43': + gevent.spawn(pusher_thread, f'{request.host}{parent.author.id}', c) + vote = CommentVote(user_id=v.id, comment_id=c.id, diff --git a/files/routes/users.py b/files/routes/users.py index 94ca41d1d..c80cce672 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -12,10 +12,36 @@ from files.__main__ import app, limiter, db_session from pusher_push_notifications import PushNotifications from collections import Counter import gevent +from sys import stdout if PUSHER_ID != '3435tdfsdudebussylmaoxxt43': beams_client = PushNotifications(instance_id=PUSHER_ID, secret_key=PUSHER_KEY) +def pusher_thread2(interests, notifbody, username): + beams_client.publish_to_interests( + interests=[interests], + publish_body={ + 'web': { + 'notification': { + 'title': f'New message from @{username}', + 'body': notifbody, + 'deep_link': f'{SITE_FULL}/notifications?messages=true', + 'icon': f'{SITE_FULL}/assets/images/{SITE_NAME}/icon.webp?a=1011', + } + }, + 'fcm': { + 'notification': { + 'title': f'New message from @{username}', + 'body': notifbody, + }, + 'data': { + 'url': '/notifications?messages=true', + } + } + }, + ) + stdout.flush() + def leaderboard_thread(): global users9, users9_25, users13, users13_25 @@ -41,6 +67,7 @@ def leaderboard_thread(): users13_25 = users13[:25] db.close() + stdout.flush() gevent.spawn(leaderboard_thread()) @app.get("/grassed") @@ -477,46 +504,25 @@ def message2(v, username): if existing: return {"error": "Message already exists."}, 403 - new_comment = Comment(author_id=v.id, + c = Comment(author_id=v.id, parent_submission=None, level=1, sentto=user.id, body_html=text_html, ) - g.db.add(new_comment) + g.db.add(c) g.db.flush() - notif = Notification(comment_id=new_comment.id, user_id=user.id) + notif = Notification(comment_id=c.id, user_id=user.id) g.db.add(notif) if PUSHER_ID != '3435tdfsdudebussylmaoxxt43': if len(message) > 500: notifbody = message[:500] + '...' else: notifbody = message - beams_client.publish_to_interests( - interests=[f'{request.host}{user.id}'], - publish_body={ - 'web': { - 'notification': { - 'title': f'New message from @{v.username}', - 'body': notifbody, - 'deep_link': f'{SITE_FULL}/notifications?messages=true', - 'icon': f'{SITE_FULL}/assets/images/{SITE_NAME}/icon.webp?a=1011', - } - }, - 'fcm': { - 'notification': { - 'title': f'New message from @{v.username}', - 'body': notifbody, - }, - 'data': { - 'url': '/notifications?messages=true', - } - } - }, - ) + gevent.spawn(pusher_thread2, f'{request.host}{user.id}', notifbody, v.username) g.db.commit()