mirror of
https://github.com/flarum/core.git
synced 2025-07-31 21:50:50 +02:00
Implement user preferences API
Preferences must be registered (optionally with a callback to transform data, and a default value) on the User model.
This commit is contained in:
@@ -24,6 +24,7 @@ class CreateUsersTable extends Migration {
|
|||||||
$table->text('bio')->nullable();
|
$table->text('bio')->nullable();
|
||||||
$table->text('bio_html')->nullable();
|
$table->text('bio_html')->nullable();
|
||||||
$table->string('avatar_path')->nullable();
|
$table->string('avatar_path')->nullable();
|
||||||
|
$table->binary('preferences')->nullable();
|
||||||
$table->dateTime('join_time')->nullable();
|
$table->dateTime('join_time')->nullable();
|
||||||
$table->dateTime('last_seen_time')->nullable();
|
$table->dateTime('last_seen_time')->nullable();
|
||||||
$table->dateTime('read_time')->nullable();
|
$table->dateTime('read_time')->nullable();
|
||||||
|
@@ -44,7 +44,8 @@ class UserSerializer extends UserBasicSerializer
|
|||||||
if ($user->id === $actorUser->id) {
|
if ($user->id === $actorUser->id) {
|
||||||
$attributes += [
|
$attributes += [
|
||||||
'readTime' => $user->read_time ? $user->read_time->toRFC3339String() : null,
|
'readTime' => $user->read_time ? $user->read_time->toRFC3339String() : null,
|
||||||
'unreadNotificationsCount' => $user->getUnreadNotificationsCount()
|
'unreadNotificationsCount' => $user->getUnreadNotificationsCount(),
|
||||||
|
'preferences' => $user->preferences
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -16,6 +16,8 @@ class EditUserCommand
|
|||||||
|
|
||||||
public $readTime;
|
public $readTime;
|
||||||
|
|
||||||
|
public $preferences;
|
||||||
|
|
||||||
public function __construct($userId, $user)
|
public function __construct($userId, $user)
|
||||||
{
|
{
|
||||||
$this->userId = $userId;
|
$this->userId = $userId;
|
||||||
|
@@ -146,6 +146,9 @@ class CoreServiceProvider extends ServiceProvider
|
|||||||
|
|
||||||
User::setHasher($this->app['hash']);
|
User::setHasher($this->app['hash']);
|
||||||
User::setFormatter($this->app['flarum.formatter']);
|
User::setFormatter($this->app['flarum.formatter']);
|
||||||
|
|
||||||
|
User::registerPreference('discloseOnline', 'boolval', true);
|
||||||
|
User::registerPreference('indexProfile', 'boolval', true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function registerPermissions()
|
public function registerPermissions()
|
||||||
|
@@ -37,6 +37,11 @@ class EditUserCommandHandler
|
|||||||
if (! empty($command->readTime)) {
|
if (! empty($command->readTime)) {
|
||||||
$userToEdit->markAllAsRead();
|
$userToEdit->markAllAsRead();
|
||||||
}
|
}
|
||||||
|
if (! empty($command->preferences)) {
|
||||||
|
foreach ($command->preferences as $k => $v) {
|
||||||
|
$userToEdit->setPreference($k, $v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
event(new UserWillBeSaved($userToEdit, $command));
|
event(new UserWillBeSaved($userToEdit, $command));
|
||||||
|
|
||||||
|
@@ -61,6 +61,8 @@ class User extends Model
|
|||||||
*/
|
*/
|
||||||
protected static $hasher;
|
protected static $hasher;
|
||||||
|
|
||||||
|
protected static $preferences = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Raise an event when a post is deleted.
|
* 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'));
|
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.
|
* Check whether or not the user is an administrator.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user