添加按订阅私聊推送webhook

This commit is contained in:
San Liy 2023-12-21 02:49:07 +08:00
parent ff6c1627b0
commit c32378c595

View File

@ -64,6 +64,25 @@ async def setup_before_start(_app):
await _app.ctx.sio.connect(_app.ctx.sio_config.host) 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') @app.post('/receive')
async def receive(rqt: Request): async def receive(rqt: Request):
match (rqt.headers['X-Gitea-Event']): 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}" rsp = f"{rsp_sender}\n{rsp_title}\n------\n{rsp_ctx}\n------\n{rsp_link}\n{cancel_subscribe}"
case _: case _:
data = None
rsp = "Unknown webhook type! Please contact the administrator." 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) return text(rsp)