Merge remote-tracking branch 'origin/main'

This commit is contained in:
San Liy 2023-12-22 00:17:32 +08:00
commit ed71cadd1d
3 changed files with 27 additions and 20 deletions

2
.gitignore vendored
View File

@ -5,3 +5,5 @@
/temp.json /temp.json
/db.* /db.*
env

40
cmds.py
View File

@ -4,7 +4,7 @@ from casbin import AsyncEnforcer
from sanic import SanicException from sanic import SanicException
from models import User, GiteaUser from models import User, GiteaUser
from sio_model import SioDecorator, Message, SioRequest from sio_model import SioDecorator, Message, SioRequest, ReplyMessage
def cmds(app, data): def cmds(app, data):
@ -24,6 +24,7 @@ def cmds(app, data):
parser.add_argument('-ust', '--ustatus', help='查询Gitea绑定状态', parser.add_argument('-ust', '--ustatus', help='查询Gitea绑定状态',
action="store_true") action="store_true")
args = parser.parse_args(sqt.args) args = parser.parse_args(sqt.args)
reply = ReplyMessage(id=sqt.message_id)
if args.validate: if args.validate:
state = secrets.token_urlsafe(16) state = secrets.token_urlsafe(16)
user = await User.filter(id=sqt.sender_id).get_or_none() user = await User.filter(id=sqt.sender_id).get_or_none()
@ -38,20 +39,20 @@ def cmds(app, data):
f'client_id={app.ctx.sio_config.client_id}&' f'client_id={app.ctx.sio_config.client_id}&'
f'redirect_uri={app.ctx.sio_config.localhost}/redirect&' f'redirect_uri={app.ctx.sio_config.localhost}/redirect&'
f'response_type=code&state={state}') f'response_type=code&state={state}')
msg = Message(content=f'click: \n{url}', room_id=sqt.room_id) msg = Message(content=f'click: \n{url}', room_id=sqt.room_id, reply_to=reply)
await sqt.app.ctx.sio.emit('sendMessage', msg.to_json()) await sqt.app.ctx.sio.emit('sendMessage', msg.to_json())
if args.ustatus: if args.ustatus:
g_user = await GiteaUser.filter(qid_id=sqt.sender_id).get_or_none() g_user = await GiteaUser.filter(qid_id=sqt.sender_id).get_or_none()
if g_user: if g_user:
msg = Message(content=f'您的Gitea账号是{g_user.name}', room_id=sqt.room_id) msg = Message(content=f'您的Gitea账号是{g_user.name}', room_id=sqt.room_id, reply_to=reply)
else: else:
try: try:
task = await sqt.app.get_task(str(sqt.sender_id)) task = await sqt.app.get_task(str(sqt.sender_id))
result = task.result() result = task.result()
msg = Message(content=f'绑定状态:{result}', room_id=sqt.room_id) msg = Message(content=f'绑定状态:{result}', room_id=sqt.room_id, reply_to=reply)
except SanicException: except SanicException:
msg = Message(content=f'你还没有绑定呢', room_id=sqt.room_id) msg = Message(content=f'你还没有绑定呢', room_id=sqt.room_id, reply_to=reply)
await sqt.app.ctx.sio.emit('sendMessage', msg.to_json()) await sqt.app.ctx.sio.emit('sendMessage', msg.to_json())
if len(sqt.args) == 0: if len(sqt.args) == 0:
@ -69,30 +70,31 @@ def cmds(app, data):
action="store_true") action="store_true")
args = parser.parse_args(sqt.args) args = parser.parse_args(sqt.args)
reply = ReplyMessage(id=sqt.message_id)
e: AsyncEnforcer = sqt.app.ctx.e e: AsyncEnforcer = sqt.app.ctx.e
msg = '' msg = ''
if args.addgroup: if args.addgroup:
if args.group and args.command: if args.group and args.command:
if await e.add_policy(args.group, args.command): if await e.add_policy(args.group, args.command):
msg = Message(content=f'添加成功p, {args.group}, {args.command}', room_id=sqt.room_id) msg = Message(content=f'添加成功p, {args.group}, {args.command}', room_id=sqt.room_id, reply_to=reply)
else: else:
msg = Message(content='添加失败,用户组已存在或其它错误', room_id=sqt.room_id) msg = Message(content='添加失败,用户组已存在或其它错误', room_id=sqt.room_id, reply_to=reply)
else: else:
msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id) msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
if args.adduser: if args.adduser:
if args.group: if args.group:
if await e.add_role_for_user(str(args.user or sqt.room_id), args.group): 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) msg = Message(content=f'添加成功g, {args.user or sqt.room_id}, {args.group}', room_id=sqt.room_id, reply_to=reply)
else: else:
msg = Message(content='添加失败,用户已在组内或其它错误', room_id=sqt.room_id) msg = Message(content='添加失败,用户已在组内或其它错误', room_id=sqt.room_id, reply_to=reply)
else: else:
msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id) msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
if len(sqt.args) == 0: if len(sqt.args) == 0:
parser.parse_args(["-h"]) parser.parse_args(["-h"])
else: else:
if msg == '': if msg == '':
msg = Message(content='参数错误,请使用-h查看帮助', room_id=sqt.room_id) msg = Message(content='参数错误,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
await sqt.app.ctx.sio.emit('sendMessage', msg.to_json()) await sqt.app.ctx.sio.emit('sendMessage', msg.to_json())
await e.save_policy() await e.save_policy()
@ -112,25 +114,25 @@ def cmds(app, data):
if args.rmgroup: if args.rmgroup:
if args.group and args.command: if args.group and args.command:
if await e.remove_policy(args.group, 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) msg = Message(content=f'移除成功p, {args.group}, {args.command}', room_id=sqt.room_id, reply_to=reply)
else: else:
msg = Message(content='移除失败,用户组已存在或其它错误', room_id=sqt.room_id) msg = Message(content='移除失败,用户组已存在或其它错误', room_id=sqt.room_id, reply_to=reply)
else: else:
msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id) msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
if args.rmuser: if args.rmuser:
if args.group: if args.group:
if await e.delete_role_for_user(str(args.user or sqt.room_id), 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) msg = Message(content=f'移除成功g, {args.user or sqt.room_id}, {args.group}', room_id=sqt.room_id, reply_to=reply)
else: else:
msg = Message(content='移除失败,用户已在组内或其它错误', room_id=sqt.room_id) msg = Message(content='移除失败,用户已在组内或其它错误', room_id=sqt.room_id, reply_to=reply)
else: else:
msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id) msg = Message(content='缺失参数,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
if len(sqt.args) == 0: if len(sqt.args) == 0:
parser.parse_args(["-h"]) parser.parse_args(["-h"])
else: else:
if msg == '': if msg == '':
msg = Message(content='参数错误,请使用-h查看帮助', room_id=sqt.room_id) msg = Message(content='参数错误,请使用-h查看帮助', room_id=sqt.room_id, reply_to=reply)
await sqt.app.ctx.sio.emit('sendMessage', msg.to_json()) await sqt.app.ctx.sio.emit('sendMessage', msg.to_json())
await e.save_policy() await e.save_policy()

View File

@ -115,6 +115,7 @@ class SioDecorator:
sender_id = self.data['message']['senderId'] sender_id = self.data['message']['senderId']
sender_name = self.data['message']['username'] sender_name = self.data['message']['username']
room_id = self.data['roomId'] room_id = self.data['roomId']
message_id = self.data['message']['_id']
if sender_id != self.app.ctx.sio_config.self_id: if sender_id != self.app.ctx.sio_config.self_id:
e: AsyncEnforcer = self.app.ctx.e e: AsyncEnforcer = self.app.ctx.e
@ -130,7 +131,8 @@ class SioDecorator:
sender_name=sender_name, sender_name=sender_name,
content=self._content, content=self._content,
room_id=room_id, room_id=room_id,
data=self.data) data=self.data,
message_id=message_id)
try: try:
await func(sqt) await func(sqt)
except PrintMessage as e: except PrintMessage as e:
@ -157,3 +159,4 @@ class SioRequest:
sender_name: str = '' sender_name: str = ''
content: str = '' content: str = ''
room_id: Optional[int] = None room_id: Optional[int] = None
message_id: str = ''