1
0
mirror of https://github.com/flarum/core.git synced 2025-08-12 19:34:18 +02:00
- 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:
Daniël Klabbers
2020-03-24 10:58:28 +01:00
parent 49d8559599
commit 46e049ecb0
7 changed files with 182 additions and 11 deletions

View 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;
}
}

View 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;
}
}

View File

@@ -9,8 +9,6 @@
namespace Flarum\User\Concerns;
use Illuminate\Support\Arr;
trait DeprecatedUserNotificationPreferences
{
@@ -31,7 +29,7 @@ trait DeprecatedUserNotificationPreferences
* @param string $type
* @param string $method
* @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)
{

View File

@@ -22,7 +22,6 @@ trait UserPreferences
*
* @param string $value
* @return array
* @deprecated 0.1.0-beta.13: `users.preferences` is no longer used.
*/
public function getPreferencesAttribute($value)
{
@@ -41,7 +40,6 @@ trait UserPreferences
* @param string $key
* @param mixed $default
* @return mixed
* @deprecated 0.1.0-beta.13: `users.preferences` is no longer used.
*/
public function getPreference($key, $default = null)
{
@@ -53,8 +51,6 @@ trait UserPreferences
*
* @param string $key
* @param mixed $value
* @return $this
* @deprecated 0.1.0-beta.13: `users.preferences` is no longer used.
*/
public function setPreference($key, $value)
{

View File

@@ -28,14 +28,30 @@ class NotificationPreference extends AbstractModel
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 = [
'channel' => $channel,
'type' => $type

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

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