From 89e96ee27a7b30213b9274e6e356571d75535447 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sun, 5 Jan 2025 13:21:41 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E6=B6=88=E6=81=AF=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=AE=A1=E7=90=86=E5=91=98+=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=90=8C=E6=97=B6=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chain/__init__.py | 71 ++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/app/chain/__init__.py b/app/chain/__init__.py index 5a2bdc6f..3198684f 100644 --- a/app/chain/__init__.py +++ b/app/chain/__init__.py @@ -1,3 +1,4 @@ +import copy import gc import pickle import traceback @@ -488,32 +489,54 @@ class ChainBase(metaclass=ABCMeta): f"title={message.title}, " f"text={message.text}," f"userid={message.userid}") - if not message.userid and message.mtype: - # 没有指定用户ID时,按规则确定发送对象 - # 默认发送全体 - to_targets = None - notify_action = ServiceConfigHelper.get_notification_switch(message.mtype) - if notify_action == "admin": - # 仅发送管理员 - logger.info(f"已设置 {message.mtype} 的消息只发送给管理员") - to_targets = self.useroper.get_settings(settings.SUPERUSER) - elif notify_action == "user": - # 发送对应用户 - if message.username: - logger.info(f"已设置 {message.mtype} 的消息只发送给用户 {message.username}") - to_targets = self.useroper.get_settings(message.username) - if not message.username or to_targets is None: - if message.username: - logger.info(f"没有 {message.username} 这个用户,该消息将发送给管理员") - # 回滚发送管理员 - to_targets = self.useroper.get_settings(settings.SUPERUSER) - message.targets = to_targets - # 发送事件 - self.eventmanager.send_event(etype=EventType.NoticeMessage, data={**message.dict(), "type": message.mtype}) - # 保存消息 + # 保存原消息 self.messagehelper.put(message, role="user", title=message.title) self.messageoper.add(**message.dict()) - # 发送 + # 发送消息按设置隔离 + if not message.userid and message.mtype: + # 消息隔离设置 + notify_action = ServiceConfigHelper.get_notification_switch(message.mtype) + if notify_action: + # 'admin' 'user,admin' 'user' 'all' + actions = notify_action.split(",") + # 是否已发送管理员标志 + admin_sended = False + for action in actions: + send_message = copy.deepcopy(message) + if action == "admin": + # 仅发送管理员 + logger.info(f"{send_message.mtype} 的消息已设置发送给管理员") + # 读取管理员消息IDS + send_message.targets = self.useroper.get_settings(settings.SUPERUSER) + admin_sended = True + elif action == "user" and send_message.username: + # 发送对应用户 + logger.info(f"{send_message.mtype} 的消息已设置发送给用户 {send_message.username}") + # 读取用户消息IDS + send_message.targets = self.useroper.get_settings(send_message.username) + if send_message.targets is None: + # 没有找到用户 + if not admin_sended: + # 回滚发送管理员 + logger.info(f"用户 {send_message.username} 不存在,消息将发送给管理员") + # 读取管理员消息IDS + send_message.targets = self.useroper.get_settings(settings.SUPERUSER) + admin_sended = True + else: + # 管理员发过了,此消息不发了 + logger.info(f"用户 {send_message.username} 不存在,消息无法发送到对应用户") + continue + else: + # 按原消息发送全体 + break + # 按设定发送 + self.eventmanager.send_event(etype=EventType.NoticeMessage, + data={**send_message.dict(), "type": send_message.mtype}) + self.run_module("post_message", message=send_message) + return + # 发送消息事件 + self.eventmanager.send_event(etype=EventType.NoticeMessage, data={**message.dict(), "type": message.mtype}) + # 按原消息发送 self.run_module("post_message", message=message) def post_medias_message(self, message: Notification, medias: List[MediaInfo]) -> None: