add alert & email notifications for suspend & unsuspend

This commit is contained in:
David Sevilla Martin 2017-05-18 20:32:23 -04:00
parent 820a9db4d2
commit 59de5cceac
11 changed files with 501 additions and 2 deletions

View File

@ -18,6 +18,7 @@ return function (Dispatcher $events) {
$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);
};

View File

@ -152,10 +152,115 @@ System.register('flarum/suspend/components/SuspendUserModal', ['flarum/component
});;
'use strict';
System.register('flarum/suspend/main', ['flarum/extend', 'flarum/app', 'flarum/utils/UserControls', 'flarum/components/Button', 'flarum/components/Badge', 'flarum/Model', 'flarum/models/User', 'flarum/suspend/components/SuspendUserModal'], function (_export, _context) {
System.register('flarum/suspend/components/UserSuspendedNotification', ['flarum/components/Notification', 'flarum/helpers/username', 'flarum/helpers/humanTime'], function (_export, _context) {
"use strict";
var extend, app, UserControls, Button, Badge, Model, User, SuspendUserModal;
var Notification, username, humanTime, UserSuspendedNotification;
return {
setters: [function (_flarumComponentsNotification) {
Notification = _flarumComponentsNotification.default;
}, function (_flarumHelpersUsername) {
username = _flarumHelpersUsername.default;
}, function (_flarumHelpersHumanTime) {
humanTime = _flarumHelpersHumanTime.default;
}],
execute: function () {
UserSuspendedNotification = function (_Notification) {
babelHelpers.inherits(UserSuspendedNotification, _Notification);
function UserSuspendedNotification() {
babelHelpers.classCallCheck(this, UserSuspendedNotification);
return babelHelpers.possibleConstructorReturn(this, (UserSuspendedNotification.__proto__ || Object.getPrototypeOf(UserSuspendedNotification)).apply(this, arguments));
}
babelHelpers.createClass(UserSuspendedNotification, [{
key: 'icon',
value: function icon() {
return 'ban';
}
}, {
key: 'href',
value: function href() {
return app.route.user(this.props.notification.subject());
}
}, {
key: 'content',
value: function content() {
var notification = this.props.notification;
var actor = notification.sender();
var suspendUntil = notification.content();
var timeReadable = moment(suspendUntil.date).from(notification.time(), true);
return app.translator.transChoice('flarum-suspend.forum.notifications.user_suspended_text', {
actor: actor,
timeReadable: timeReadable
});
}
}]);
return UserSuspendedNotification;
}(Notification);
_export('default', UserSuspendedNotification);
}
};
});;
'use strict';
System.register('flarum/suspend/components/UserUnsuspendedNotification', ['flarum/components/Notification', 'flarum/helpers/username', 'flarum/helpers/humanTime'], function (_export, _context) {
"use strict";
var Notification, username, humanTime, UserUnsuspendedNotification;
return {
setters: [function (_flarumComponentsNotification) {
Notification = _flarumComponentsNotification.default;
}, function (_flarumHelpersUsername) {
username = _flarumHelpersUsername.default;
}, function (_flarumHelpersHumanTime) {
humanTime = _flarumHelpersHumanTime.default;
}],
execute: function () {
UserUnsuspendedNotification = function (_Notification) {
babelHelpers.inherits(UserUnsuspendedNotification, _Notification);
function UserUnsuspendedNotification() {
babelHelpers.classCallCheck(this, UserUnsuspendedNotification);
return babelHelpers.possibleConstructorReturn(this, (UserUnsuspendedNotification.__proto__ || Object.getPrototypeOf(UserUnsuspendedNotification)).apply(this, arguments));
}
babelHelpers.createClass(UserUnsuspendedNotification, [{
key: 'icon',
value: function icon() {
return 'ban';
}
}, {
key: 'href',
value: function href() {
return app.route.user(this.props.notification.subject());
}
}, {
key: 'content',
value: function content() {
var notification = this.props.notification;
var actor = notification.sender();
return app.translator.transChoice('flarum-suspend.forum.notifications.user_unsuspended_text', {
actor: actor
});
}
}]);
return UserUnsuspendedNotification;
}(Notification);
_export('default', UserUnsuspendedNotification);
}
};
});;
'use strict';
System.register('flarum/suspend/main', ['flarum/extend', 'flarum/app', 'flarum/utils/UserControls', 'flarum/components/Button', 'flarum/components/Badge', 'flarum/Model', 'flarum/models/User', 'flarum/suspend/components/SuspendUserModal', 'flarum/suspend/components/UserSuspendedNotification', 'flarum/suspend/components/UserUnsuspendedNotification'], function (_export, _context) {
"use strict";
var extend, app, UserControls, Button, Badge, Model, User, SuspendUserModal, UserSuspendedNotification, UserUnsuspendedNotification;
return {
setters: [function (_flarumExtend) {
extend = _flarumExtend.extend;
@ -173,10 +278,17 @@ System.register('flarum/suspend/main', ['flarum/extend', 'flarum/app', 'flarum/u
User = _flarumModelsUser.default;
}, function (_flarumSuspendComponentsSuspendUserModal) {
SuspendUserModal = _flarumSuspendComponentsSuspendUserModal.default;
}, function (_flarumSuspendComponentsUserSuspendedNotification) {
UserSuspendedNotification = _flarumSuspendComponentsUserSuspendedNotification.default;
}, function (_flarumSuspendComponentsUserUnsuspendedNotification) {
UserUnsuspendedNotification = _flarumSuspendComponentsUserUnsuspendedNotification.default;
}],
execute: function () {
app.initializers.add('flarum-suspend', function () {
app.notificationComponents.userSuspended = UserSuspendedNotification;
app.notificationComponents.userUnsuspended = UserUnsuspendedNotification;
User.prototype.canSuspend = Model.attribute('canSuspend');
User.prototype.suspendUntil = Model.attribute('suspendUntil', Model.transformDate);

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 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);

View File

@ -0,0 +1,38 @@
<?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\Event;
use Flarum\Core\Post;
use Flarum\Core\User;
class UserWasSuspended
{
/**
* @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;
}
}

View File

@ -0,0 +1,38 @@
<?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\Event;
use Flarum\Core\Post;
use Flarum\Core\User;
class UserWasUnsuspended
{
/**
* @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;
}
}

View File

@ -15,6 +15,8 @@ use DateTime;
use Flarum\Core\Access\AssertPermissionTrait;
use Flarum\Event\UserWillBeSaved;
use Flarum\Suspend\SuspendValidator;
use Flarum\Suspend\Event\UserWasSuspended;
use Flarum\Suspend\Event\UserWasUnsuspended;
use Illuminate\Contracts\Events\Dispatcher;
class SaveSuspensionToDatabase
@ -60,6 +62,12 @@ class SaveSuspensionToDatabase
$this->assertCan($actor, 'suspend', $user);
$user->suspend_until = new DateTime($attributes['suspendUntil']);
if (isset($attributes['suspendUntil'])) {
$user->raise(new UserWasSuspended($user, $actor));
} else {
$user->raise(new UserWasUnsuspended($user, $actor));
}
}
}
}

View File

@ -0,0 +1,95 @@
<?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 DateTime;
use Flarum\Api\Serializer\UserBasicSerializer;
use Flarum\Core\Notification\NotificationSyncer;
use Flarum\Core\Post;
use Flarum\Core\User;
use Flarum\Event\ConfigureNotificationTypes;
use Flarum\Suspend\Event\UserWasSuspended;
use Flarum\Suspend\Event\UserWasUnsuspended;
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(UserWasSuspended::class, [$this, 'whenUserWasSuspended']);
$events->listen(UserWasUnsuspended::class, [$this, 'whenUserWasUnsuspended']);
}
/**
* @param ConfigureNotificationTypes $event
*/
public function registerNotificationType(ConfigureNotificationTypes $event)
{
$event->add(UserSuspendedBlueprint::class, UserBasicSerializer::class, ['alert', 'email']);
$event->add(UserUnsuspendedBlueprint::class, UserBasicSerializer::class, ['alert', 'email']);
}
/**
* @param UserWasSuspended $event
*/
public function whenUserWasSuspended(UserWasSuspended $event)
{
$this->sync($event->user, $event->actor, [$event->user]);
}
/**
* @param UserWasSuspended $event
*/
public function whenUserWasUnsuspended(UserWasUnsuspended $event)
{
$this->sync($event->user, $event->actor, [$event->user]);
}
/**
* @param User $user
* @param User $actor
* @param array $recipients
*/
public function sync(User $user, User $actor, array $recipients)
{
if ($user->suspend_until > new DateTime()) {
$this->notifications->sync(
new UserSuspendedBlueprint($user, $actor),
$recipients
);
} else {
$this->notifications->sync(
new UserUnsuspendedBlueprint($user, $actor),
$recipients
);
}
}
}

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\Core\Notification\BlueprintInterface;
use Flarum\Core\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\Core\Notification\BlueprintInterface;
use Flarum\Core\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;
}
}