diff --git a/server.py b/server.py index 1308bde..07a35cc 100644 --- a/server.py +++ b/server.py @@ -64,6 +64,25 @@ async def setup_before_start(_app): await _app.ctx.sio.connect(_app.ctx.sio_config.host) +async def push_webhook2users(_app: Sanic, full_name: str, msg: str): + async with aiohttp.ClientSession() as session: + url = f'{app.ctx.sio_config.gitea_host}/api/v1/repos/{full_name}/subscribers?page=&limit=' + async with session.get(url) as rps: + if rps.status == 200: + json = await rps.json() + q_user = set() + for g_user in json: + g_user = await GiteaUser.filter(id=g_user['id']).get_or_none() + if g_user: + _q_user = await g_user.qid.get() + q_user.add(_q_user.id) + for i in q_user: + message = Message(content=msg, room_id=i) + await app.ctx.sio.emit('sendMessage', message.to_json()) + else: + logger.warn(rps) + + @app.post('/receive') async def receive(rqt: Request): match (rqt.headers['X-Gitea-Event']): @@ -90,10 +109,13 @@ async def receive(rqt: Request): rsp = f"{rsp_sender}\n{rsp_title}\n------\n{rsp_ctx}\n------\n{rsp_link}\n{cancel_subscribe}" case _: + data = None rsp = "Unknown webhook type! Please contact the administrator." + logger.warn(rsp) + + if data: + rqt.app.add_task(push_webhook2users(app, data.repository.full_name, rsp)) - message = Message(content=rsp, room_id=-777186831) - await app.ctx.sio.emit('sendMessage', message.to_json()) return text(rsp)