fix webpush

This commit is contained in:
jxxghp
2024-09-21 17:59:39 +08:00
parent c51826ba4c
commit fada22e892
3 changed files with 52 additions and 35 deletions
+4 -2
View File
@@ -485,8 +485,10 @@ class FileManagerModule(_ModuleBase):
return None, f"{fileitem.path} {target_storage} 重命名文件失败" return None, f"{fileitem.path} {target_storage} 重命名文件失败"
else: else:
return None, f"{target_file.parent} {target_storage} 目录获取失败" return None, f"{target_file.parent} {target_storage} 目录获取失败"
else:
return None, f"网盘内只支持移动操作"
return None, "不支持的整理操作" return None, "未知错误"
def __transfer_other_files(self, fileitem: FileItem, target_storage: str, target_file: Path, def __transfer_other_files(self, fileitem: FileItem, target_storage: str, target_file: Path,
transfer_type: str) -> Tuple[bool, str]: transfer_type: str) -> Tuple[bool, str]:
@@ -747,7 +749,7 @@ class FileManagerModule(_ModuleBase):
else: else:
logger.info(f"正在删除已存在的文件:{target_file}") logger.info(f"正在删除已存在的文件:{target_file}")
target_file.unlink() target_file.unlink()
logger.info(f"正在整理文件:{fileitem.path}{target_file}") logger.info(f"正在整理文件:{fileitem.storage}{fileitem.path}{target_storage}{target_file}")
new_item, errmsg = self.__transfer_command(fileitem=fileitem, new_item, errmsg = self.__transfer_command(fileitem=fileitem,
target_storage=target_storage, target_storage=target_storage,
target_file=target_file, target_file=target_file,
@@ -602,6 +602,7 @@ class AliPan(StorageBase):
if res: if res:
download_url = res.json().get("url") download_url = res.json().get("url")
if not download_url: if not download_url:
logger.warn(f"{fileitem.path} 未获取到下载链接")
return None return None
res = RequestUtils().get_res(download_url) res = RequestUtils().get_res(download_url)
if res: if res:
+47 -33
View File
@@ -4,6 +4,7 @@ from typing import Union, Tuple
from pywebpush import webpush, WebPushException from pywebpush import webpush, WebPushException
from app.core.config import global_vars, settings from app.core.config import global_vars, settings
from app.helper.notification import NotificationHelper
from app.log import logger from app.log import logger
from app.modules import _ModuleBase, _MessageBase from app.modules import _ModuleBase, _MessageBase
from app.schemas import Notification from app.schemas import Notification
@@ -11,7 +12,16 @@ from app.schemas import Notification
class WebPushModule(_ModuleBase, _MessageBase): class WebPushModule(_ModuleBase, _MessageBase):
def init_module(self) -> None: def init_module(self) -> None:
pass """
初始化模块
"""
clients = NotificationHelper().get_clients()
if not clients:
return
self._configs = {}
for client in clients:
if client.type == "webpush" and client.enabled:
self._configs[client.name] = client
@staticmethod @staticmethod
def get_name() -> str: def get_name() -> str:
@@ -35,36 +45,40 @@ class WebPushModule(_ModuleBase, _MessageBase):
:param message: 消息内容 :param message: 消息内容
:return: 成功或失败 :return: 成功或失败
""" """
if not message.title and not message.text: for conf in self._configs.values():
logger.warn("标题和内容不能同时为空") if not self.checkMessage(message, conf.name):
return continue
if not self.checkMessage(message): webpush_users = conf.config.get("WEBPUSH_USERNAME")
logger.warn("WebPush 通道未启用") if webpush_users:
return if message.username and message.username != message.userid:
try: continue
if message.title: if not message.title and not message.text:
caption = message.title logger.warn("标题和内容不能同时为空")
content = message.text return
else: try:
caption = message.text if message.title:
content = "" caption = message.title
for sub in global_vars.get_subscriptions(): content = message.text
logger.debug(f"{sub} 发送WebPush{caption} {content}") else:
try: caption = message.text
webpush( content = ""
subscription_info=sub, for sub in global_vars.get_subscriptions():
data=json.dumps({ logger.debug(f"{sub} 发送WebPush{caption} {content}")
"title": caption, try:
"body": content, webpush(
"url": message.link or "/?shotcut=message" subscription_info=sub,
}), data=json.dumps({
vapid_private_key=settings.VAPID.get("privateKey"), "title": caption,
vapid_claims={ "body": content,
"sub": settings.VAPID.get("subject") "url": message.link or "/?shotcut=message"
}, }),
) vapid_private_key=settings.VAPID.get("privateKey"),
except WebPushException as err: vapid_claims={
logger.error(f"WebPush发送失败: {str(err)}") "sub": settings.VAPID.get("subject")
},
)
except WebPushException as err:
logger.error(f"WebPush发送失败: {str(err)}")
except Exception as msg_e: except Exception as msg_e:
logger.error(f"发送消息失败:{msg_e}") logger.error(f"发送消息失败:{msg_e}")