From c32378c595f90c8c71889398f7f0bc3a7bbbc052 Mon Sep 17 00:00:00 2001 From: adk23333 <2633103794@qq.com> Date: Thu, 21 Dec 2023 02:49:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8C=89=E8=AE=A2=E9=98=85?= =?UTF-8?q?=E7=A7=81=E8=81=8A=E6=8E=A8=E9=80=81webhook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) 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)