mirror of
https://github.com/flarum/core.git
synced 2025-08-12 03:14:33 +02:00
fixes #1236
- split up deprecated and remaining user notification logic - started building a test (needs work) - created new Model for NotificationPreference - created extender to register a NotificationChannel - created extender to maintain UserPreferences User preferences are still possible on the users table directly. Registering a user preference allows for transformation to happen. And provides easier accessors. Not sure we want this. ! tests need work.
This commit is contained in:
34
src/Extend/NotificationChannel.php
Normal file
34
src/Extend/NotificationChannel.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Flarum\Extend;
|
||||||
|
|
||||||
|
use Flarum\Extension\Extension;
|
||||||
|
use Flarum\User\NotificationPreference;
|
||||||
|
use Illuminate\Contracts\Container\Container;
|
||||||
|
|
||||||
|
class NotificationChannel implements ExtenderInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $channel;
|
||||||
|
|
||||||
|
private $enabled = [];
|
||||||
|
|
||||||
|
public function __construct(string $channel)
|
||||||
|
{
|
||||||
|
$this->channel = $channel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function extend(Container $container, Extension $extension = null)
|
||||||
|
{
|
||||||
|
NotificationPreference::addChannel($this->channel, $this->enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function enabled(string $type, bool $enabled = true)
|
||||||
|
{
|
||||||
|
$this->enabled[$type] = $enabled;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
22
src/Extend/UserPreferences.php
Normal file
22
src/Extend/UserPreferences.php
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Flarum\Extend;
|
||||||
|
|
||||||
|
use Flarum\Extension\Extension;
|
||||||
|
use Flarum\User\User;
|
||||||
|
use Illuminate\Contracts\Container\Container;
|
||||||
|
|
||||||
|
class UserPreferences implements ExtenderInterface
|
||||||
|
{
|
||||||
|
public function extend(Container $container, Extension $extension = null)
|
||||||
|
{
|
||||||
|
// TODO: Implement extend() method.
|
||||||
|
}
|
||||||
|
|
||||||
|
public function add(string $key, callable $transformer = null, $default = null)
|
||||||
|
{
|
||||||
|
User::addPreference($key, $transformer, $default);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
@@ -9,8 +9,6 @@
|
|||||||
|
|
||||||
namespace Flarum\User\Concerns;
|
namespace Flarum\User\Concerns;
|
||||||
|
|
||||||
use Illuminate\Support\Arr;
|
|
||||||
|
|
||||||
trait DeprecatedUserNotificationPreferences
|
trait DeprecatedUserNotificationPreferences
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -31,7 +29,7 @@ trait DeprecatedUserNotificationPreferences
|
|||||||
* @param string $type
|
* @param string $type
|
||||||
* @param string $method
|
* @param string $method
|
||||||
* @return string
|
* @return string
|
||||||
* @deprecated 0.1.0-beta.13: `users.preferences` is no longer used, use NotificationPreference::getPreferenceKey.
|
* @deprecated 0.1.0-beta.13: `users.preferences` is no longer used, use \Flarum\User\NotificationPreference.
|
||||||
*/
|
*/
|
||||||
public static function getNotificationPreferenceKey($type, $method)
|
public static function getNotificationPreferenceKey($type, $method)
|
||||||
{
|
{
|
||||||
|
@@ -22,7 +22,6 @@ trait UserPreferences
|
|||||||
*
|
*
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return array
|
* @return array
|
||||||
* @deprecated 0.1.0-beta.13: `users.preferences` is no longer used.
|
|
||||||
*/
|
*/
|
||||||
public function getPreferencesAttribute($value)
|
public function getPreferencesAttribute($value)
|
||||||
{
|
{
|
||||||
@@ -41,7 +40,6 @@ trait UserPreferences
|
|||||||
* @param string $key
|
* @param string $key
|
||||||
* @param mixed $default
|
* @param mixed $default
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @deprecated 0.1.0-beta.13: `users.preferences` is no longer used.
|
|
||||||
*/
|
*/
|
||||||
public function getPreference($key, $default = null)
|
public function getPreference($key, $default = null)
|
||||||
{
|
{
|
||||||
@@ -53,8 +51,6 @@ trait UserPreferences
|
|||||||
*
|
*
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return $this
|
|
||||||
* @deprecated 0.1.0-beta.13: `users.preferences` is no longer used.
|
|
||||||
*/
|
*/
|
||||||
public function setPreference($key, $value)
|
public function setPreference($key, $value)
|
||||||
{
|
{
|
||||||
|
@@ -28,14 +28,30 @@ class NotificationPreference extends AbstractModel
|
|||||||
return $this->belongsTo(User::class);
|
return $this->belongsTo(User::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function addChannel(string $channel)
|
public static function addChannel(string $channel, array $defaults = [])
|
||||||
{
|
{
|
||||||
static::$channels[] = $channel;
|
static::$channels[$channel] = $defaults;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function setNotificationPreference(User $user, string $type, string $channel, bool $enabled = true)
|
public static function getNotificationPreferences(User $user, string $channel = null, string $type = null)
|
||||||
{
|
{
|
||||||
if (in_array($channel, static::$channels)) {
|
$saved = $user->notificationPreferences()
|
||||||
|
->when('type', function ($query, $type) {
|
||||||
|
$query->where('type', $type);
|
||||||
|
})
|
||||||
|
->whereIn('channel', $channel ? [$channel] : array_keys(static::$channels))
|
||||||
|
->get();
|
||||||
|
|
||||||
|
if ($channel && $type) {
|
||||||
|
return $saved->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $saved;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function setNotificationPreference(User $user, string $channel, string $type, bool $enabled = true)
|
||||||
|
{
|
||||||
|
if (array_key_exists($channel, static::$channels)) {
|
||||||
$attributes = [
|
$attributes = [
|
||||||
'channel' => $channel,
|
'channel' => $channel,
|
||||||
'type' => $type
|
'type' => $type
|
||||||
|
44
tests/integration/extenders/NotificationChannelTest.php
Normal file
44
tests/integration/extenders/NotificationChannelTest.php
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Flarum\Tests\integration\extenders;
|
||||||
|
|
||||||
|
use Flarum\Extend\NotificationChannel;
|
||||||
|
use Flarum\Extend\UserPreferences;
|
||||||
|
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
||||||
|
use Flarum\Tests\integration\TestCase;
|
||||||
|
use Flarum\User\User;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
|
||||||
|
class NotificationChannelTest extends TestCase
|
||||||
|
{
|
||||||
|
use RetrievesAuthorizedUsers;
|
||||||
|
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->prepareDatabase([
|
||||||
|
'users' => [
|
||||||
|
$this->normalUser()
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function add_channel()
|
||||||
|
{
|
||||||
|
$this->extend(new NotificationChannel('test'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function can_add_notification_channel()
|
||||||
|
{
|
||||||
|
$this->add_channel();
|
||||||
|
|
||||||
|
/** @var User $user */
|
||||||
|
$user = User::find(2);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
61
tests/integration/extenders/UserPreferencesTest.php
Normal file
61
tests/integration/extenders/UserPreferencesTest.php
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Flarum\Tests\integration\extenders;
|
||||||
|
|
||||||
|
use Flarum\Extend\UserPreferences;
|
||||||
|
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
||||||
|
use Flarum\Tests\integration\TestCase;
|
||||||
|
use Flarum\User\User;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
|
||||||
|
class UserPreferencesTest extends TestCase
|
||||||
|
{
|
||||||
|
use RetrievesAuthorizedUsers;
|
||||||
|
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->prepareDatabase([
|
||||||
|
'users' => [
|
||||||
|
$this->normalUser()
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function add_preference()
|
||||||
|
{
|
||||||
|
$this->extend(
|
||||||
|
(new UserPreferences())
|
||||||
|
->add('test', 'boolval', false)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function can_add_user_preference()
|
||||||
|
{
|
||||||
|
$this->add_preference();
|
||||||
|
|
||||||
|
/** @var User $user */
|
||||||
|
$user = User::find(2);
|
||||||
|
$this->assertEquals(false, Arr::get($user->preferences, 'test'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function can_store_user_preference()
|
||||||
|
{
|
||||||
|
$this->add_preference();
|
||||||
|
|
||||||
|
/** @var User $user */
|
||||||
|
$user = User::find(2);
|
||||||
|
|
||||||
|
$user->setPreference('test', true);
|
||||||
|
|
||||||
|
$this->assertEquals(true, $user->test);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user