Merge pull request #11 from datitisev/322-notificate-user-when-suspended

Add user notifications for suspending & unsuspending
This commit is contained in:
Toby Zerner 2018-05-13 07:21:06 +09:30 committed by GitHub
commit 682995ef2f
8 changed files with 287 additions and 0 deletions

View File

@ -27,6 +27,7 @@ return [
$events->subscribe(Listener\AddUserSuspendAttributes::class); $events->subscribe(Listener\AddUserSuspendAttributes::class);
$events->subscribe(Listener\RevokeAccessFromSuspendedUsers::class); $events->subscribe(Listener\RevokeAccessFromSuspendedUsers::class);
$events->subscribe(Listener\SaveSuspensionToDatabase::class); $events->subscribe(Listener\SaveSuspensionToDatabase::class);
$events->subscribe(Listener\SendNotificationWhenUserIsSuspended::class);
$events->subscribe(Access\UserPolicy::class); $events->subscribe(Access\UserPolicy::class);
} }

View File

@ -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,
});
}
}

View File

@ -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,
});
}
}

View File

@ -7,8 +7,13 @@ import Model from 'flarum/Model';
import User from 'flarum/models/User'; import User from 'flarum/models/User';
import SuspendUserModal from 'flarum/suspend/components/SuspendUserModal'; 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.initializers.add('flarum-suspend', () => {
app.notificationComponents.userSuspended = UserSuspendedNotification;
app.notificationComponents.userUnsuspended = UserUnsuspendedNotification;
User.prototype.canSuspend = Model.attribute('canSuspend'); User.prototype.canSuspend = Model.attribute('canSuspend');
User.prototype.suspendUntil = Model.attribute('suspendUntil', Model.transformDate); User.prototype.suspendUntil = Model.attribute('suspendUntil', Model.transformDate);

View File

@ -36,6 +36,7 @@ class SaveSuspensionToDatabase
/** /**
* @param SuspendValidator $validator * @param SuspendValidator $validator
* @param Dispatcher $events
*/ */
public function __construct(SuspendValidator $validator, Dispatcher $events) public function __construct(SuspendValidator $validator, Dispatcher $events)
{ {

View File

@ -0,0 +1,78 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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]
);
}
}

View File

@ -0,0 +1,78 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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;
}
}

View File

@ -0,0 +1,77 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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;
}
}