From 5c6763f8a8f0d80e5e75ee432d95b56cde6f99ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80?= <78488229+viktor138irk@users.noreply.github.com> Date: Sat, 9 May 2026 21:34:10 +0900 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20Fa?= =?UTF-8?q?stAPI=20Pulse=20Push=20Gateway?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- push-gateway/main.py | 60 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 push-gateway/main.py diff --git a/push-gateway/main.py b/push-gateway/main.py new file mode 100644 index 0000000..ff2f944 --- /dev/null +++ b/push-gateway/main.py @@ -0,0 +1,60 @@ +from fastapi import FastAPI, Request +from fastapi.responses import JSONResponse +import firebase_admin +from firebase_admin import credentials, messaging +import os + +app = FastAPI(title='Pulse Push Gateway') + +firebase_path = os.getenv('FIREBASE_SERVICE_ACCOUNT', '/opt/swchat/push-gateway/firebase-service-account.json') + +if not firebase_admin._apps: + cred = credentials.Certificate(firebase_path) + firebase_admin.initialize_app(cred) + + +@app.get('/health') +async def health(): + return { + 'status': 'ok', + 'service': 'pulse-push-gateway' + } + + +@app.post('/_matrix/push/v1/notify') +async def notify(request: Request): + body = await request.json() + + notifications = body.get('notification', {}) + devices = notifications.get('devices', []) + + sent = 0 + + for device in devices: + pushkey = device.get('pushkey') + + if not pushkey: + continue + + message = messaging.Message( + token=pushkey, + notification=messaging.Notification( + title='Pulse', + body='Новое сообщение', + ), + data={ + 'room_id': notifications.get('room_id', ''), + 'event_id': notifications.get('event_id', ''), + } + ) + + try: + messaging.send(message) + sent += 1 + except Exception as e: + print(f'[Pulse Push Gateway] Firebase send failed: {e}') + + return JSONResponse({ + 'rejected': [], + 'sent': sent, + })