add push notification support
This commit is contained in:
parent
6a69c5a34d
commit
1b1dcc3755
12 changed files with 175 additions and 10 deletions
|
|
@ -1,9 +1,16 @@
|
|||
import asyncio
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
|
||||
import MySQLdb
|
||||
|
||||
import config
|
||||
from minio import Minio
|
||||
from redis import Redis
|
||||
from redis.asyncio import Redis
|
||||
from pywebpush import WebPusher
|
||||
|
||||
from .sql import SessionMaker, AlertEndpoints, ergoQueryFetchOne
|
||||
|
||||
from fastapi import UploadFile
|
||||
|
||||
|
|
@ -49,7 +56,38 @@ class ErgoClient:
|
|||
@retry
|
||||
async def init(self):
|
||||
self.reader, self.writer = await asyncio.open_connection(config.ERGO_ADDR, config.ERGO_PORT)
|
||||
await asyncio.get_running_loop().create_task(self.readEvents())
|
||||
|
||||
@retry
|
||||
async def readEvents(self):
|
||||
while 1:
|
||||
rawLine = await self.reader.readline()
|
||||
if not rawLine: break
|
||||
line = rawLine.decode("utf8").strip().split()
|
||||
if line[0] == "MENTION":
|
||||
await self.handleMention(line[1], line[2], line[3])
|
||||
|
||||
|
||||
async def handleMention(self, username: str, channel: str, msgid: str):
|
||||
session = SessionMaker()
|
||||
for target in session.query(AlertEndpoints).filter(AlertEndpoints.username == username):
|
||||
pusher = WebPusher({
|
||||
"endpoint": target.url,
|
||||
"keys": {
|
||||
"auth": target.auth,
|
||||
"p256dh": target.p256dh
|
||||
}
|
||||
})
|
||||
messageQuery = ergoQueryFetchOne("SELECT `data` FROM `history` WHERE `msgid` = :id", id=int(msgid)).data
|
||||
message = json.loads(messageQuery.decode("utf8"))
|
||||
encoded = json.dumps({
|
||||
"channel": channel,
|
||||
"from": message["AccountName"],
|
||||
"content": message["Message"]["Message"]
|
||||
}).encode("utf8")
|
||||
|
||||
await pusher.send_async(encoded)
|
||||
session.close()
|
||||
|
||||
|
||||
@retry
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue