diff --git a/extensions/suspend/bootstrap.php b/extensions/suspend/bootstrap.php index b24e346af..98d44bdcc 100644 --- a/extensions/suspend/bootstrap.php +++ b/extensions/suspend/bootstrap.php @@ -27,6 +27,7 @@ return [ $events->subscribe(Listener\AddUserSuspendAttributes::class); $events->subscribe(Listener\RevokeAccessFromSuspendedUsers::class); $events->subscribe(Listener\SaveSuspensionToDatabase::class); + $events->subscribe(Listener\SendNotificationWhenUserIsSuspended::class); $events->subscribe(Access\UserPolicy::class); } diff --git a/extensions/suspend/js/forum/src/components/UserSuspendedNotification.js b/extensions/suspend/js/forum/src/components/UserSuspendedNotification.js new file mode 100644 index 000000000..008c83b7d --- /dev/null +++ b/extensions/suspend/js/forum/src/components/UserSuspendedNotification.js @@ -0,0 +1,25 @@ +import Notification from 'flarum/components/Notification'; +import username from 'flarum/helpers/username'; +import humanTime from 'flarum/helpers/humanTime'; + +export default class UserSuspendedNotification extends Notification { + icon() { + return 'ban'; + } + + href() { + return app.route.user(this.props.notification.subject()); + } + + content() { + const notification = this.props.notification; + const actor = notification.sender(); + const suspendUntil = notification.content(); + const timeReadable = moment(suspendUntil.date).from(notification.time(), true); + + return app.translator.transChoice('flarum-suspend.forum.notifications.user_suspended_text', { + actor, + timeReadable, + }); + } +} diff --git a/extensions/suspend/js/forum/src/components/UserUnsuspendedNotification.js b/extensions/suspend/js/forum/src/components/UserUnsuspendedNotification.js new file mode 100644 index 000000000..3a92009c6 --- /dev/null +++ b/extensions/suspend/js/forum/src/components/UserUnsuspendedNotification.js @@ -0,0 +1,22 @@ +import Notification from 'flarum/components/Notification'; +import username from 'flarum/helpers/username'; +import humanTime from 'flarum/helpers/humanTime'; + +export default class UserUnsuspendedNotification extends Notification { + icon() { + return 'ban'; + } + + href() { + return app.route.user(this.props.notification.subject()); + } + + content() { + const notification = this.props.notification; + const actor = notification.sender(); + + return app.translator.transChoice('flarum-suspend.forum.notifications.user_unsuspended_text', { + actor, + }); + } +} diff --git a/extensions/suspend/js/forum/src/main.js b/extensions/suspend/js/forum/src/main.js index 173605220..248f531f1 100644 --- a/extensions/suspend/js/forum/src/main.js +++ b/extensions/suspend/js/forum/src/main.js @@ -7,8 +7,13 @@ import Model from 'flarum/Model'; import User from 'flarum/models/User'; import SuspendUserModal from 'flarum/suspend/components/SuspendUserModal'; +import UserSuspendedNotification from 'flarum/suspend/components/UserSuspendedNotification'; +import UserUnsuspendedNotification from 'flarum/suspend/components/UserUnsuspendedNotification'; app.initializers.add('flarum-suspend', () => { + app.notificationComponents.userSuspended = UserSuspendedNotification; + app.notificationComponents.userUnsuspended = UserUnsuspendedNotification; + User.prototype.canSuspend = Model.attribute('canSuspend'); User.prototype.suspendUntil = Model.attribute('suspendUntil', Model.transformDate); diff --git a/extensions/suspend/src/Listener/SaveSuspensionToDatabase.php b/extensions/suspend/src/Listener/SaveSuspensionToDatabase.php index 79440a512..c9961defb 100755 --- a/extensions/suspend/src/Listener/SaveSuspensionToDatabase.php +++ b/extensions/suspend/src/Listener/SaveSuspensionToDatabase.php @@ -36,6 +36,7 @@ class SaveSuspensionToDatabase /** * @param SuspendValidator $validator + * @param Dispatcher $events */ public function __construct(SuspendValidator $validator, Dispatcher $events) { diff --git a/extensions/suspend/src/Listener/SendNotificationWhenUserIsSuspended.php b/extensions/suspend/src/Listener/SendNotificationWhenUserIsSuspended.php new file mode 100644 index 000000000..b73fbe4dd --- /dev/null +++ b/extensions/suspend/src/Listener/SendNotificationWhenUserIsSuspended.php @@ -0,0 +1,78 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Suspend\Listener; + +use Flarum\Api\Serializer\BasicUserSerializer; +use Flarum\Event\ConfigureNotificationTypes; +use Flarum\Notification\NotificationSyncer; +use Flarum\Suspend\Event\Suspended; +use Flarum\Suspend\Event\Unsuspended; +use Flarum\Suspend\Notification\UserSuspendedBlueprint; +use Flarum\Suspend\Notification\UserUnsuspendedBlueprint; +use Illuminate\Contracts\Events\Dispatcher; + +class SendNotificationWhenUserIsSuspended +{ + /** + * @var NotificationSyncer + */ + protected $notifications; + + /** + * @param NotificationSyncer $notifications + */ + public function __construct(NotificationSyncer $notifications) + { + $this->notifications = $notifications; + } + + /** + * @param Dispatcher $events + */ + public function subscribe(Dispatcher $events) + { + $events->listen(ConfigureNotificationTypes::class, [$this, 'registerNotificationType']); + $events->listen(Suspended::class, [$this, 'whenSuspended']); + $events->listen(Unsuspended::class, [$this, 'whenUnsuspended']); + } + + /** + * @param ConfigureNotificationTypes $event + */ + public function registerNotificationType(ConfigureNotificationTypes $event) + { + $event->add(UserSuspendedBlueprint::class, BasicUserSerializer::class, ['alert', 'email']); + $event->add(UserUnsuspendedBlueprint::class, BasicUserSerializer::class, ['alert', 'email']); + } + + /** + * @param Suspended $event + */ + public function whenSuspended(Suspended $event) + { + $this->notifications->sync( + new UserSuspendedBlueprint($event->user, $event->actor), + [$event->user] + ); + } + + /** + * @param Unsuspended $event + */ + public function whenUnsuspended(Unsuspended $event) + { + $this->notifications->sync( + new UserUnsuspendedBlueprint($event->user, $event->actor), + [$event->user] + ); + } +} diff --git a/extensions/suspend/src/Notification/UserSuspendedBlueprint.php b/extensions/suspend/src/Notification/UserSuspendedBlueprint.php new file mode 100644 index 000000000..52ac92d4a --- /dev/null +++ b/extensions/suspend/src/Notification/UserSuspendedBlueprint.php @@ -0,0 +1,78 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Suspend\Notification; + +use Flarum\Notification\Blueprint\BlueprintInterface; +use Flarum\User\User; + +class UserSuspendedBlueprint implements BlueprintInterface +{ + /** + * @var User + */ + public $user; + + /** + * @var User + */ + public $actor; + + /** + * @param User $user + * @param User $actor + */ + public function __construct(User $user, User $actor) + { + $this->user = $user; + $this->actor = $actor; + } + + /** + * {@inheritdoc} + */ + public function getSubject() + { + return $this->user; + } + + /** + * {@inheritdoc} + */ + public function getSender() + { + return $this->actor; + } + + /** + * {@inheritdoc} + */ + public function getData() + { + return $this->user->suspend_until; + } + + /** + * {@inheritdoc} + */ + public static function getType() + { + return 'userSuspended'; + } + + /** + * {@inheritdoc} + */ + public static function getSubjectModel() + { + return User::class; + } +} diff --git a/extensions/suspend/src/Notification/UserUnsuspendedBlueprint.php b/extensions/suspend/src/Notification/UserUnsuspendedBlueprint.php new file mode 100644 index 000000000..d9e1806a8 --- /dev/null +++ b/extensions/suspend/src/Notification/UserUnsuspendedBlueprint.php @@ -0,0 +1,77 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Suspend\Notification; + +use Flarum\Notification\Blueprint\BlueprintInterface; +use Flarum\User\User; + +class UserUnsuspendedBlueprint implements BlueprintInterface +{ + /** + * @var User + */ + public $user; + + /** + * @var User + */ + public $actor; + + /** + * @param User $user + * @param User $actor + */ + public function __construct(User $user, User $actor) + { + $this->user = $user; + $this->actor = $actor; + } + + /** + * {@inheritdoc} + */ + public function getSubject() + { + return $this->user; + } + + /** + * {@inheritdoc} + */ + public function getSender() + { + return $this->actor; + } + + /** + * {@inheritdoc} + */ + public function getData() + { + } + + /** + * {@inheritdoc} + */ + public static function getType() + { + return 'userUnsuspended'; + } + + /** + * {@inheritdoc} + */ + public static function getSubjectModel() + { + return User::class; + } +}