herpaderpaldent / seat-notifications
使用此SeAT软件包,您可以设置和管理通知。它旨在从软件包内部或另一个软件包中进行扩展。
Requires
- php: >=7.1
- ext-json: *
- erusev/parsedown: ^1.7
- eveseat/web: ~3.0.15
- jolicode/slack-php-api: ^1.0
- laravel/framework: 5.5.*
- php-http/curl-client: ^1.7
- restcord/restcord: ^0.3
- textalk/websocket: 1.0.*
README
使用此SeAT软件包,您可以设置和管理通知。它旨在从软件包内部或另一个软件包中进行扩展。请向下阅读更多相关信息。
重要:seat-notifications正在开发中,肯定存在一些错误,请将发现的任何问题报告给seat-slack并报告为问题.
安装
- 切换到
/var/www/seat - 输入
composer require herpaderpaldent/seat-notifications - 运行迁移
php artisan migrate
启用通知通道
要启用发送通知的seat-notifications功能,请为您的通知通道创建一个机器人。默认情况下,seat-notifications提供两个通知通道,这些通道可以被其他软件包扩展:slack和discord:
有关oAuth创建的更详细指南将随后提供,现在以下表格足以说明:
| 通知通道 | 重定向URL | 注释 |
|---|---|---|
| Discord | {seat_url}/seatnotifications/discord/configuration/callback/server | 此回调URL需要配置您的Discord机器人。 |
| Discord | {seat_url}/seatnotifications/discord/callback/user | 此回调URL需要验证Discord用户。 |
| Slack | {seat_url}/seatnotifications/slack/configuration/callback/server | 此回调URL需要配置您的Slack机器人。 |
| Slack | {seat_url}/seatnotifications/slack/callback/user | 此回调URL需要验证Slack用户。 |
注意:您可以根据自己的意愿配置一个通知通道。然而,对于Discord,您必须在您的应用程序中创建一个机器人。对于Slack,您需要将机器人权限添加到您的oauth作用域中。
设置角色
要能够订阅通知,用户需要适当的权限。请设置一个包含所需权限的角色,并将该角色分配给应能够接收通知的用户。
重新启动工作进程
由于通知是由您的工人发送的,因此您需要重新启动它们以获取新代码。如果您使用Docker,则通过docker-compose restart seat-worker执行此操作;在Linux发行版上,通过supervisorctl restart worker执行此操作。
身份验证
用户在接收通知之前需要对你的设置通知渠道进行身份验证。他们可以在通知页面上的注册框中完成此操作。
当前包的功能是什么
- 当删除
refresh_token时,向 Discord 和/或 Slack 发送RefreshTokenDeleted通知。 - 使用模型观察器分发通知
- 通知是可排队并通过队列发送的。
- RefreshTokenNotification 可以根据用户偏好发送到频道或通过 DM。
开发
最重要的是:注意 Laravel 的通知文档。其次,查看 此包服务提供者,因为它有助于正确合并服务数组。提供的 RefreshTokenDeletedNotification 示例基于它。通知是通过使用 Notification 门面发送的
Notification::send($receipients, (new RefreshTokenDeletedNotification($refresh_token)));
其中 $receipients 是应该接收通知的模型集合,而 $refresh_token 是传递给构造函数的已删除的 refresh_token。在这个例子中,我们使用观察器来发送通知:观察器。
架构
添加新驱动程序
如果你编写了一个新的通知驱动程序,希望用于向用户发送通知,你可以像提供的示例一样扩展 config/services.php
'seat-notification-channel' => [
'discord' => Herpaderpaldent\Seat\SeatNotifications\Drivers\DiscordNotificationDriver::class,
],
你的驱动程序应该扩展 Herpaderpaldent\Seat\SeatNotifications\Drivers\AbstractNotificationDriver 并包含以下内容;
/**
* The view name which will be used to store the channel settings.
*
* @return string
*/
public static function getSettingsView() : string;
/**
* The label which will be applied to the subscription button.
*
* @return string
*/
public static function getButtonLabel() : string;
/**
* The CSS class which have to be append into the subscription button.
*
* @return string
*/
public static function getButtonIconClass() : string;
/**
* @return array
*/
public static function getChannels() : array;
/**
* Determine if a channel has been properly setup.
*
* @return bool
*/
public static function isSetup(): bool;
添加新通知
如果你想扩展可用的通知,你需要扩展 config/services.php 中的 seat-notification 数组
'seat-notification' => [
// notification => [provider => implementation]
Herpaderpaldent\Seat\SeatNotifications\Notifications\RefreshToken\AbstractRefreshTokenNotification::class => [
'discord' => Herpaderpaldent\Seat\SeatNotifications\Notifications\RefreshToken\DiscordRefreshTokenNotification::class,
'slack' => Herpaderpaldent\Seat\SeatNotifications\Notifications\RefreshToken\SlackRefreshTokenNotification::class,
],
你的抽象通知必须扩展 Herpaderpaldent\Seat\SeatNotifications\Notifications\AbstractNotification 并包含以下方法以将你的通知添加到用户的通知列表
/**
* Return a title for the notification which will be displayed in UI notification list.
* @return string
*/
public static function getTitle(): string;
/**
* Return a description for the notification which will be displayed in UI notification list.
* @return string
*/
public static function getDescription(): string;
/**
* Determine if a notification can target public channel (forum category, chat, etc...).
* @return bool
*/
public static function isPublic(): bool;
/**
* Determine if a notification can target personal channel (private message, e-mail, etc...).
* @return bool
*/
public static function isPersonal(): bool;
/**
* Determine the permission needed to represent driver buttons.
* @return string
*/
public static function getPermission(): string;
使用通知调度器
为了仅向订阅了通知的接收者发送通知,建议使用自定义调度作业,例如。
public function handle() { Redis::funnel('soft_delete:refresh_token_' . $this->refresh_token->user->id)->limit(1)->then(function () { logger()->info('SoftDelete detected of ' . $this->refresh_token->user->name); $recipients = NotificationRecipient::all() ->filter(function ($recipient) { return $recipient->shouldReceive(AbstractRefreshTokenNotification::class); }); if($recipients->isEmpty()){ logger()->debug('No Receiver found for this Notification. This job is going to be deleted.'); $this->delete(); } $recipients->groupBy('driver') ->each(function ($grouped_recipients) { $driver = (string) $grouped_recipients->first()->driver; $notification_class = AbstractRefreshTokenNotification::getDriverImplementation($driver); Notification::send($grouped_recipients, (new $notification_class($this->refresh_token))); }); }, function () { logger()->debug('A Soft-Delete job is already running for ' . $this->refresh_token->user->name); $this->delete(); }); }
