From 5ab5f2082e7060bcb6b0790de2a8353d6b8e071e Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Fri, 22 Dec 2023 00:14:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=A6=EF=BC=81=20=E4=B8=BA=E4=BB=80?= =?UTF-8?q?=E4=B9=88=E4=B8=8D=E8=A7=A3=E6=9E=90=20id=EF=BC=88=E6=81=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ cmds.py | 11 ++++++----- sio_model.py | 5 ++++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 2ddcb2f..ac16d78 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ /temp.json /db.* + +env \ No newline at end of file diff --git a/cmds.py b/cmds.py index 773a686..23e4a40 100644 --- a/cmds.py +++ b/cmds.py @@ -4,7 +4,7 @@ from casbin import AsyncEnforcer from sanic import SanicException from models import User, GiteaUser -from sio_model import SioDecorator, Message, SioRequest +from sio_model import SioDecorator, Message, SioRequest, ReplyMessage def cmds(app, data): @@ -24,6 +24,7 @@ def cmds(app, data): parser.add_argument('-ust', '--ustatus', help='查询Gitea绑定状态', action="store_true") args = parser.parse_args(sqt.args) + reply = ReplyMessage(id=sqt.message_id) if args.validate: state = secrets.token_urlsafe(16) 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'redirect_uri={app.ctx.sio_config.localhost}/redirect&' 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()) if args.ustatus: g_user = await GiteaUser.filter(qid_id=sqt.sender_id).get_or_none() 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: try: task = await sqt.app.get_task(str(sqt.sender_id)) 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: - 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()) if len(sqt.args) == 0: diff --git a/sio_model.py b/sio_model.py index ff61d9c..877ae38 100644 --- a/sio_model.py +++ b/sio_model.py @@ -115,6 +115,7 @@ class SioDecorator: sender_id = self.data['message']['senderId'] sender_name = self.data['message']['username'] room_id = self.data['roomId'] + message_id = self.data['message']['_id'] if sender_id != self.app.ctx.sio_config.self_id: e: AsyncEnforcer = self.app.ctx.e @@ -130,7 +131,8 @@ class SioDecorator: sender_name=sender_name, content=self._content, room_id=room_id, - data=self.data) + data=self.data, + message_id=message_id) try: await func(sqt) except PrintMessage as e: @@ -157,3 +159,4 @@ class SioRequest: sender_name: str = '' content: str = '' room_id: Optional[int] = None + message_id: str = '' From 0c29753b687b62c02d8995c1debebb30a5ff60d9 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Fri, 22 Dec 2023 00:17:10 +0800 Subject: [PATCH 2/2] Fix command handling and error messages --- cmds.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/cmds.py b/cmds.py index 23e4a40..a2d184b 100644 --- a/cmds.py +++ b/cmds.py @@ -70,30 +70,31 @@ def cmds(app, data): action="store_true") args = parser.parse_args(sqt.args) + reply = ReplyMessage(id=sqt.message_id) e: AsyncEnforcer = sqt.app.ctx.e msg = '' if args.addgroup: if args.group and 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: - msg = Message(content='添加失败,用户组已存在或其它错误', room_id=sqt.room_id) + msg = Message(content='添加失败,用户组已存在或其它错误', room_id=sqt.room_id, reply_to=reply) 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.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: - msg = Message(content='添加失败,用户已在组内或其它错误', room_id=sqt.room_id) + msg = Message(content='添加失败,用户已在组内或其它错误', room_id=sqt.room_id, reply_to=reply) 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: parser.parse_args(["-h"]) else: 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 e.save_policy() @@ -113,25 +114,25 @@ def cmds(app, data): 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) + msg = Message(content=f'移除成功:p, {args.group}, {args.command}', room_id=sqt.room_id, reply_to=reply) else: - msg = Message(content='移除失败,用户组已存在或其它错误', room_id=sqt.room_id) + msg = Message(content='移除失败,用户组已存在或其它错误', room_id=sqt.room_id, reply_to=reply) 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.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: - msg = Message(content='移除失败,用户已在组内或其它错误', room_id=sqt.room_id) + msg = Message(content='移除失败,用户已在组内或其它错误', room_id=sqt.room_id, reply_to=reply) 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: parser.parse_args(["-h"]) else: 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 e.save_policy()