commit 2dd2ae4266d7ff7426d47d871987dc4448d1dc69 Author: Victor_Jay Date: Thu May 28 19:54:31 2026 +0800 feat: 彻底禁用 Flarum 邮件通知 - 从驱动层移除 EmailNotificationDriver,禁止所有邮件通知发送 - 新用户注册时自动关闭邮件通知偏好兜底 - 站点通知保持不变 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..49a2d96 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 MetaLab + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..05e900c --- /dev/null +++ b/composer.json @@ -0,0 +1,24 @@ +{ + "name": "metazone/disable-email-notifications", + "description": "彻底禁用 Flarum 邮件通知——从驱动层移除 EmailNotificationDriver,保留站点通知不受影响。", + "type": "flarum-extension", + "license": "MIT", + "require": { + "flarum/core": "^1.0" + }, + "autoload": { + "psr-4": { + "MetaZone\\DisableEmailNotifications\\": "src/" + } + }, + "extra": { + "flarum-extension": { + "title": "Disable Email Notifications", + "icon": { + "name": "fas fa-envelope", + "backgroundColor": "#e74c3c", + "color": "#fff" + } + } + } +} diff --git a/extend.php b/extend.php new file mode 100644 index 0000000..8e23702 --- /dev/null +++ b/extend.php @@ -0,0 +1,15 @@ +listen(Registered::class, DisableEmailNotificationsListener::class), + + // 从驱动层彻底移除邮件通知,不依赖偏好设置 + new RemoveEmailDriver(), +]; diff --git a/src/Listener/DisableEmailNotificationsListener.php b/src/Listener/DisableEmailNotificationsListener.php new file mode 100644 index 0000000..404210c --- /dev/null +++ b/src/Listener/DisableEmailNotificationsListener.php @@ -0,0 +1,46 @@ +user; + + try { + // 从容器中获取所有已注册的通知类型(懒加载,make 会触发首次解析) + // flarum.notification.blueprints 格式: [BlueprintClass => ['alert', 'email'], ...] + $blueprints = $this->container->make('flarum.notification.blueprints'); + + foreach ($blueprints as $blueprintClass => $driversEnabledByDefault) { + // 只处理支持邮件通知的类型 + if (!in_array('email', $driversEnabledByDefault)) { + continue; + } + + // 防御:跳过无法获取类型的 blueprint + if (!is_callable([$blueprintClass, 'getType'])) { + continue; + } + + $type = $blueprintClass::getType(); + $key = User::getNotificationPreferenceKey($type, 'email'); + $user->setPreference($key, false); + } + + $user->save(); + } catch (\Throwable $e) { + // 监听器内部错误不应阻断用户注册流程 + } + } +} diff --git a/src/RemoveEmailDriver.php b/src/RemoveEmailDriver.php new file mode 100644 index 0000000..f2a6fcc --- /dev/null +++ b/src/RemoveEmailDriver.php @@ -0,0 +1,18 @@ +extend('flarum.notification.drivers', function (array $drivers) { + unset($drivers['email']); + return $drivers; + }); + } +}