Compare commits

..

2 Commits

Author SHA1 Message Date
ad557a8225 添加在线权限移除 2023-12-21 03:05:17 +08:00
c32378c595 添加按订阅私聊推送webhook 2023-12-21 02:49:07 +08:00
2 changed files with 66 additions and 5 deletions

45
cmds.py
View File

@ -60,10 +60,11 @@ def cmds(app, data):
@sio_decorator.cmd('authadd')
async def auth_add(sqt: SioRequest):
parser = sqt.parser
parser.add_argument('-ag', '--addgroup', help='添加用户组',
parser.add_argument('-ag', '--addgroup', help='添加用户组与其权限',
action="store_true")
parser.add_argument('-g', '--group', help='组名')
parser.add_argument('-m', '--command', help='命令名')
parser.add_argument('-u', '--user', help='用户名')
parser.add_argument('-au', '--adduser', help='添加用户/群到组',
action="store_true")
@ -80,8 +81,8 @@ def cmds(app, data):
msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id)
if args.adduser:
if args.group:
if await e.add_role_for_user(str(sqt.room_id), args.group):
msg = Message(content=f'添加成功g, {sqt.room_id}, {args.group}', room_id=sqt.room_id)
if await e.add_role_for_user(str(args.user or sqt.room_id), args.group):
msg = Message(content=f'添加成功g, {args.user or sqt.room_id}, {args.group}', room_id=sqt.room_id)
else:
msg = Message(content='添加失败,用户已在组内或其它错误', room_id=sqt.room_id)
else:
@ -95,4 +96,42 @@ def cmds(app, data):
await sqt.app.ctx.sio.emit('sendMessage', msg.to_json())
await e.save_policy()
@sio_decorator.cmd('authrm')
async def auth_add(sqt: SioRequest):
parser = sqt.parser
parser.add_argument('-rg', '--rmgroup', help='移除用户组的权限',
action="store_true")
parser.add_argument('-g', '--group', help='组名')
parser.add_argument('-u', '--user', help='用户名')
parser.add_argument('-ru', '--rmuser', help='从组移除用户/群',
action="store_true")
args = parser.parse_args(sqt.args)
e: AsyncEnforcer = sqt.app.ctx.e
msg = ''
if args.rmgroup:
if args.group and args.command:
if await e.remove_policy(args.group, args.command):
msg = Message(content=f'移除成功p, {args.group}, {args.command}', room_id=sqt.room_id)
else:
msg = Message(content='移除失败,用户组已存在或其它错误', room_id=sqt.room_id)
else:
msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id)
if args.rmuser:
if args.group:
if await e.delete_role_for_user(str(args.user or sqt.room_id), args.group):
msg = Message(content=f'移除成功g, {args.user or sqt.room_id}, {args.group}', room_id=sqt.room_id)
else:
msg = Message(content='移除失败,用户已在组内或其它错误', room_id=sqt.room_id)
else:
msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id)
if len(sqt.args) == 0:
parser.parse_args(["-h"])
else:
if msg == '':
msg = Message(content='参数错误,请使用-h查看帮助', room_id=sqt.room_id)
await sqt.app.ctx.sio.emit('sendMessage', msg.to_json())
await e.save_policy()
return sio_decorator

View File

@ -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)