feat: 彻底禁用 Flarum 邮件通知
- 从驱动层移除 EmailNotificationDriver,禁止所有邮件通知发送 - 新用户注册时自动关闭邮件通知偏好兜底 - 站点通知保持不变
This commit is contained in:
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -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.
|
||||||
24
composer.json
Normal file
24
composer.json
Normal file
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
extend.php
Normal file
15
extend.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MetaZone\DisableEmailNotifications;
|
||||||
|
|
||||||
|
use Flarum\Extend;
|
||||||
|
use Flarum\User\Event\Registered;
|
||||||
|
use MetaZone\DisableEmailNotifications\Listener\DisableEmailNotificationsListener;
|
||||||
|
|
||||||
|
return [
|
||||||
|
(new Extend\Event)
|
||||||
|
->listen(Registered::class, DisableEmailNotificationsListener::class),
|
||||||
|
|
||||||
|
// 从驱动层彻底移除邮件通知,不依赖偏好设置
|
||||||
|
new RemoveEmailDriver(),
|
||||||
|
];
|
||||||
46
src/Listener/DisableEmailNotificationsListener.php
Normal file
46
src/Listener/DisableEmailNotificationsListener.php
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MetaZone\DisableEmailNotifications\Listener;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Container\Container;
|
||||||
|
use Flarum\User\Event\Registered;
|
||||||
|
use Flarum\User\User;
|
||||||
|
|
||||||
|
class DisableEmailNotificationsListener
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
protected Container $container,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle(Registered $event): void
|
||||||
|
{
|
||||||
|
$user = $event->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) {
|
||||||
|
// 监听器内部错误不应阻断用户注册流程
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/RemoveEmailDriver.php
Normal file
18
src/RemoveEmailDriver.php
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MetaZone\DisableEmailNotifications;
|
||||||
|
|
||||||
|
use Flarum\Extend\ExtenderInterface;
|
||||||
|
use Flarum\Extension\Extension;
|
||||||
|
use Illuminate\Contracts\Container\Container;
|
||||||
|
|
||||||
|
class RemoveEmailDriver implements ExtenderInterface
|
||||||
|
{
|
||||||
|
public function extend(Container $container, Extension $extension = null): void
|
||||||
|
{
|
||||||
|
$container->extend('flarum.notification.drivers', function (array $drivers) {
|
||||||
|
unset($drivers['email']);
|
||||||
|
return $drivers;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user