From 37825f18498eb28d77bf570b33321b7fa0a4c667 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sat, 28 Mar 2015 11:50:02 +1030 Subject: [PATCH] Implement user preferences API Preferences must be registered (optionally with a callback to transform data, and a default value) on the User model. --- .../2015_02_24_000000_create_users_table.php | 1 + .../src/Api/Serializers/UserSerializer.php | 3 +- .../src/Core/Commands/EditUserCommand.php | 2 + .../core/src/Core/CoreServiceProvider.php | 3 ++ .../Commands/EditUserCommandHandler.php | 5 ++ framework/core/src/Core/Models/User.php | 48 +++++++++++++++++++ 6 files changed, 61 insertions(+), 1 deletion(-) diff --git a/framework/core/migrations/2015_02_24_000000_create_users_table.php b/framework/core/migrations/2015_02_24_000000_create_users_table.php index 8bc363b52..4c0a5989a 100644 --- a/framework/core/migrations/2015_02_24_000000_create_users_table.php +++ b/framework/core/migrations/2015_02_24_000000_create_users_table.php @@ -24,6 +24,7 @@ class CreateUsersTable extends Migration { $table->text('bio')->nullable(); $table->text('bio_html')->nullable(); $table->string('avatar_path')->nullable(); + $table->binary('preferences')->nullable(); $table->dateTime('join_time')->nullable(); $table->dateTime('last_seen_time')->nullable(); $table->dateTime('read_time')->nullable(); diff --git a/framework/core/src/Api/Serializers/UserSerializer.php b/framework/core/src/Api/Serializers/UserSerializer.php index 62b470357..15bc1148f 100644 --- a/framework/core/src/Api/Serializers/UserSerializer.php +++ b/framework/core/src/Api/Serializers/UserSerializer.php @@ -44,7 +44,8 @@ class UserSerializer extends UserBasicSerializer if ($user->id === $actorUser->id) { $attributes += [ 'readTime' => $user->read_time ? $user->read_time->toRFC3339String() : null, - 'unreadNotificationsCount' => $user->getUnreadNotificationsCount() + 'unreadNotificationsCount' => $user->getUnreadNotificationsCount(), + 'preferences' => $user->preferences ]; } diff --git a/framework/core/src/Core/Commands/EditUserCommand.php b/framework/core/src/Core/Commands/EditUserCommand.php index fb22897e6..9b0ccba38 100644 --- a/framework/core/src/Core/Commands/EditUserCommand.php +++ b/framework/core/src/Core/Commands/EditUserCommand.php @@ -16,6 +16,8 @@ class EditUserCommand public $readTime; + public $preferences; + public function __construct($userId, $user) { $this->userId = $userId; diff --git a/framework/core/src/Core/CoreServiceProvider.php b/framework/core/src/Core/CoreServiceProvider.php index 041d21e47..ae274bc75 100644 --- a/framework/core/src/Core/CoreServiceProvider.php +++ b/framework/core/src/Core/CoreServiceProvider.php @@ -146,6 +146,9 @@ class CoreServiceProvider extends ServiceProvider User::setHasher($this->app['hash']); User::setFormatter($this->app['flarum.formatter']); + + User::registerPreference('discloseOnline', 'boolval', true); + User::registerPreference('indexProfile', 'boolval', true); } public function registerPermissions() diff --git a/framework/core/src/Core/Handlers/Commands/EditUserCommandHandler.php b/framework/core/src/Core/Handlers/Commands/EditUserCommandHandler.php index 86210ff5f..0ee550d2f 100644 --- a/framework/core/src/Core/Handlers/Commands/EditUserCommandHandler.php +++ b/framework/core/src/Core/Handlers/Commands/EditUserCommandHandler.php @@ -37,6 +37,11 @@ class EditUserCommandHandler if (! empty($command->readTime)) { $userToEdit->markAllAsRead(); } + if (! empty($command->preferences)) { + foreach ($command->preferences as $k => $v) { + $userToEdit->setPreference($k, $v); + } + } event(new UserWillBeSaved($userToEdit, $command)); diff --git a/framework/core/src/Core/Models/User.php b/framework/core/src/Core/Models/User.php index 55f7527e6..1ff3700dc 100755 --- a/framework/core/src/Core/Models/User.php +++ b/framework/core/src/Core/Models/User.php @@ -61,6 +61,8 @@ class User extends Model */ protected static $hasher; + protected static $preferences = []; + /** * Raise an event when a post is deleted. * @@ -346,6 +348,52 @@ class User extends Model return $this->notifications()->where('time', '>', $this->notification_read_time ?: 0)->where('is_read', 0)->count(\DB::raw('DISTINCT type, subject_id')); } + public function getPreferencesAttribute($value) + { + $defaults = []; + + foreach (static::$preferences as $k => $v) { + $defaults[$k] = $v['default']; + } + + return array_merge($defaults, (array) json_decode($value, true)); + } + + public function setPreferencesAttribute($value) + { + $this->attributes['preferences'] = json_encode($value); + } + + public static function registerPreference($key, $transformer = null, $default = null) + { + static::$preferences[$key] = [ + 'transformer' => $transformer, + 'default' => $default + ]; + } + + public function preference($key, $default = null) + { + return array_get($this->preferences, $key, $default); + } + + public function setPreference($key, $value) + { + if (isset(static::$preferences[$key])) { + $preferences = $this->preferences; + + if (! is_null($transformer = static::$preferences[$key]['transformer'])) { + $preferences[$key] = call_user_func($transformer, $value); + } else { + $preferences[$key] = $value; + } + + $this->preferences = $preferences; + } + + return $this; + } + /** * Check whether or not the user is an administrator. *