mirror of
https://github.com/flarum/core.git
synced 2025-08-06 08:27:42 +02:00
Moved deprecated user notification preferences logic into a dedicated trait,
did the same for user preferences to one that we can retain; those for user columns re-added migrations, fixed most of the fallback methods
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* For detailed copyright and license information, please view the
|
||||||
|
* LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Schema\Builder;
|
||||||
|
|
||||||
|
return [
|
||||||
|
'up' => function (Builder $schema) {
|
||||||
|
$schema->create('notification_preferences', function (Blueprint $table) {
|
||||||
|
$table->integer('user_id')->unsigned();
|
||||||
|
$table->string('type');
|
||||||
|
$table->string('channel');
|
||||||
|
$table->boolean('enabled')->default(false);
|
||||||
|
|
||||||
|
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
'down' => function (Builder $schema) {
|
||||||
|
$schema->drop('notification_preferences');
|
||||||
|
}
|
||||||
|
];
|
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* For detailed copyright and license information, please view the
|
||||||
|
* LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Schema\Builder;
|
||||||
|
|
||||||
|
return [
|
||||||
|
'up' => function (Builder $schema) {
|
||||||
|
$schema->table('users', function (Blueprint $table) {
|
||||||
|
$table->boolean('disclose_online')->default(false);
|
||||||
|
$table->string('locale')->nullable();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
'down' => function (Builder $schema) {
|
||||||
|
$schema->table('users', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('disclose_online');
|
||||||
|
$table->dropColumn('locale');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
];
|
@@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* For detailed copyright and license information, please view the
|
||||||
|
* LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use Illuminate\Database\Schema\Builder;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
return [
|
||||||
|
'up' => function (Builder $builder) {
|
||||||
|
$db = $builder->getConnection();
|
||||||
|
|
||||||
|
$db->table('users')
|
||||||
|
->select(['id', 'preferences'])
|
||||||
|
->whereNotNull('preferences')
|
||||||
|
->orderBy('id')
|
||||||
|
->each(function ($user) use ($db) {
|
||||||
|
collect(json_decode($user->preferences ?? '{}'))
|
||||||
|
->each(function ($value, $key) use ($user, $db) {
|
||||||
|
if (in_array($key, ['discloseOnline', 'followAfterReply'])) {
|
||||||
|
$db->table('users')
|
||||||
|
->where('id', $user->id)
|
||||||
|
->update([Str::snake($key) => (bool) $value]);
|
||||||
|
}
|
||||||
|
if ($key === 'locale') {
|
||||||
|
$db->table('users')
|
||||||
|
->where('id', $user->id)
|
||||||
|
->update(['locale' => $value]);
|
||||||
|
}
|
||||||
|
if (preg_match('/^notify_(?<type>[^_]+)_(?<channel>.*)$/', $key, $matches)) {
|
||||||
|
$db->table('notification_preferences')
|
||||||
|
->insert([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'type' => $matches['type'],
|
||||||
|
'channel' => $matches['channel'],
|
||||||
|
'enabled' => (bool) $value
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
'down' => function (Builder $builder) {
|
||||||
|
$db = $builder->getConnection();
|
||||||
|
|
||||||
|
$db->table('notification_preferences')->truncate();
|
||||||
|
}
|
||||||
|
];
|
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* For detailed copyright and license information, please view the
|
||||||
|
* LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Schema\Builder;
|
||||||
|
|
||||||
|
return [
|
||||||
|
'up' => function (Builder $schema) {
|
||||||
|
$schema->table('users', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('preferences');
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
'down' => function (Builder $schema) {
|
||||||
|
$schema->table('users', function (Blueprint $table) {
|
||||||
|
$table->binary('preferences')->nullable();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
];
|
39
src/User/Concerns/DeprecatedUserNotificationPreferences.php
Normal file
39
src/User/Concerns/DeprecatedUserNotificationPreferences.php
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* For detailed copyright and license information, please view the
|
||||||
|
* LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Flarum\User\Concerns;
|
||||||
|
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
|
||||||
|
trait DeprecatedUserNotificationPreferences
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode an array of preferences for storage in the database.
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
* @deprecated 0.1.0-beta.13: `users.preferences` is no longer used.
|
||||||
|
*/
|
||||||
|
public function setPreferencesAttribute($value)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the key for a preference which flags whether or not the user will
|
||||||
|
* receive a notification for $type via $method.
|
||||||
|
*
|
||||||
|
* @param string $type
|
||||||
|
* @param string $method
|
||||||
|
* @return string
|
||||||
|
* @deprecated 0.1.0-beta.13: `users.preferences` is no longer used, use NotificationPreference::getPreferenceKey.
|
||||||
|
*/
|
||||||
|
public static function getNotificationPreferenceKey($type, $method)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
84
src/User/Concerns/UserPreferences.php
Normal file
84
src/User/Concerns/UserPreferences.php
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Flarum\User\Concerns;
|
||||||
|
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
|
||||||
|
trait UserPreferences
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* An array of registered user preferences. Each preference is defined with
|
||||||
|
* a key, and its value is an array containing the following keys:.
|
||||||
|
*
|
||||||
|
* - transformer: a callback that confines the value of the preference
|
||||||
|
* - default: a default value if the preference isn't set
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected static $preferences = [];
|
||||||
|
/**
|
||||||
|
* Get the values of all registered preferences for this user, by
|
||||||
|
* transforming their stored preferences and merging them with the defaults.
|
||||||
|
*
|
||||||
|
* @param string $value
|
||||||
|
* @return array
|
||||||
|
* @deprecated 0.1.0-beta.13: `users.preferences` is no longer used.
|
||||||
|
*/
|
||||||
|
public function getPreferencesAttribute($value)
|
||||||
|
{
|
||||||
|
$defaults = array_map(function ($value) {
|
||||||
|
return $value['default'];
|
||||||
|
}, static::$preferences);
|
||||||
|
|
||||||
|
$user = Arr::only($this->notificationPreferences->toArray(), array_keys(static::$preferences));
|
||||||
|
|
||||||
|
return array_merge($defaults, $user);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the value of a preference for this user.
|
||||||
|
*
|
||||||
|
* @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)
|
||||||
|
{
|
||||||
|
return $this->$key ?? $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the value of a preference for this user.
|
||||||
|
*
|
||||||
|
* @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)
|
||||||
|
{
|
||||||
|
$preference = static::$preferences[$key];
|
||||||
|
|
||||||
|
// If a user preference is registered, transform the value.
|
||||||
|
if ($preference) {
|
||||||
|
$value = $value === null ? $preference['default'] : $value;
|
||||||
|
$value = $preference['transformer']($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->{$key} = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a preference with a transformer and a default value.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param callable $transformer
|
||||||
|
* @param mixed $default
|
||||||
|
*/
|
||||||
|
public static function addPreference($key, callable $transformer = null, $default = null)
|
||||||
|
{
|
||||||
|
static::$preferences[$key] = compact('transformer', 'default');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
59
src/User/NotificationPreference.php
Normal file
59
src/User/NotificationPreference.php
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* For detailed copyright and license information, please view the
|
||||||
|
* LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Flarum\User;
|
||||||
|
|
||||||
|
use Flarum\Database\AbstractModel;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property int $user_id
|
||||||
|
* @property string $type
|
||||||
|
* @property string $channel
|
||||||
|
* @property bool $enabled
|
||||||
|
*/
|
||||||
|
class NotificationPreference extends AbstractModel
|
||||||
|
{
|
||||||
|
protected static $channels = [];
|
||||||
|
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function addChannel(string $channel)
|
||||||
|
{
|
||||||
|
static::$channels[] = $channel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function setNotificationPreference(User $user, string $type, string $channel, bool $enabled = true)
|
||||||
|
{
|
||||||
|
if (in_array($channel, static::$channels)) {
|
||||||
|
$attributes = [
|
||||||
|
'channel' => $channel,
|
||||||
|
'type' => $type
|
||||||
|
];
|
||||||
|
|
||||||
|
$user->notificationPreferences()->updateOrInsert($attributes, ['enabled' => $enabled]);
|
||||||
|
} else {
|
||||||
|
throw new InvalidArgumentException("Channel '$channel' is not registered.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function scopeShouldBeNotified(Builder $query, string $type, string $channel = null)
|
||||||
|
{
|
||||||
|
return $query
|
||||||
|
->where('enabled', true)
|
||||||
|
->where('type', $type)
|
||||||
|
->when($channel, function ($query, $channel) {
|
||||||
|
$query->where('channel', $channel);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@@ -57,6 +57,8 @@ class User extends AbstractModel
|
|||||||
{
|
{
|
||||||
use EventGeneratorTrait;
|
use EventGeneratorTrait;
|
||||||
use ScopeVisibilityTrait;
|
use ScopeVisibilityTrait;
|
||||||
|
use Concerns\DeprecatedUserNotificationPreferences;
|
||||||
|
use Concerns\UserPreferences;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The attributes that should be mutated to dates.
|
* The attributes that should be mutated to dates.
|
||||||
@@ -82,17 +84,6 @@ class User extends AbstractModel
|
|||||||
*/
|
*/
|
||||||
protected $session;
|
protected $session;
|
||||||
|
|
||||||
/**
|
|
||||||
* An array of registered user preferences. Each preference is defined with
|
|
||||||
* a key, and its value is an array containing the following keys:.
|
|
||||||
*
|
|
||||||
* - transformer: a callback that confines the value of the preference
|
|
||||||
* - default: a default value if the preference isn't set
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $preferences = [];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The hasher with which to hash passwords.
|
* The hasher with which to hash passwords.
|
||||||
*
|
*
|
||||||
@@ -446,34 +437,6 @@ class User extends AbstractModel
|
|||||||
})->count();
|
})->count();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the values of all registered preferences for this user, by
|
|
||||||
* transforming their stored preferences and merging them with the defaults.
|
|
||||||
*
|
|
||||||
* @param string $value
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getPreferencesAttribute($value)
|
|
||||||
{
|
|
||||||
$defaults = array_map(function ($value) {
|
|
||||||
return $value['default'];
|
|
||||||
}, static::$preferences);
|
|
||||||
|
|
||||||
$user = Arr::only((array) json_decode($value, true), array_keys(static::$preferences));
|
|
||||||
|
|
||||||
return array_merge($defaults, $user);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode an array of preferences for storage in the database.
|
|
||||||
*
|
|
||||||
* @param mixed $value
|
|
||||||
*/
|
|
||||||
public function setPreferencesAttribute($value)
|
|
||||||
{
|
|
||||||
$this->attributes['preferences'] = json_encode($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether or not the user should receive an alert for a notification
|
* Check whether or not the user should receive an alert for a notification
|
||||||
* type.
|
* type.
|
||||||
@@ -498,42 +461,6 @@ class User extends AbstractModel
|
|||||||
return (bool) $this->getPreference(static::getNotificationPreferenceKey($type, 'email'));
|
return (bool) $this->getPreference(static::getNotificationPreferenceKey($type, 'email'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the value of a preference for this user.
|
|
||||||
*
|
|
||||||
* @param string $key
|
|
||||||
* @param mixed $default
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getPreference($key, $default = null)
|
|
||||||
{
|
|
||||||
return Arr::get($this->preferences, $key, $default);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the value of a preference for this user.
|
|
||||||
*
|
|
||||||
* @param string $key
|
|
||||||
* @param mixed $value
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the user as being last seen just now.
|
* Set the user as being last seen just now.
|
||||||
*
|
*
|
||||||
@@ -606,6 +533,11 @@ class User extends AbstractModel
|
|||||||
return $this->belongsToMany(Group::class);
|
return $this->belongsToMany(Group::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function notificationPreferences()
|
||||||
|
{
|
||||||
|
return $this->hasMany(NotificationPreference::class);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define the relationship with the user's notifications.
|
* Define the relationship with the user's notifications.
|
||||||
*
|
*
|
||||||
@@ -723,31 +655,6 @@ class User extends AbstractModel
|
|||||||
static::$hasher = $hasher;
|
static::$hasher = $hasher;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Register a preference with a transformer and a default value.
|
|
||||||
*
|
|
||||||
* @param string $key
|
|
||||||
* @param callable $transformer
|
|
||||||
* @param mixed $default
|
|
||||||
*/
|
|
||||||
public static function addPreference($key, callable $transformer = null, $default = null)
|
|
||||||
{
|
|
||||||
static::$preferences[$key] = compact('transformer', 'default');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the key for a preference which flags whether or not the user will
|
|
||||||
* receive a notification for $type via $method.
|
|
||||||
*
|
|
||||||
* @param string $type
|
|
||||||
* @param string $method
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function getNotificationPreferenceKey($type, $method)
|
|
||||||
{
|
|
||||||
return 'notify_'.$type.'_'.$method;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Refresh the user's comments count.
|
* Refresh the user's comments count.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user