started refactoring the User class to the Notification Preference class

This commit is contained in:
Daniël Klabbers 2019-10-28 10:27:38 +01:00
parent 603367a41a
commit 12fff33763
2 changed files with 26 additions and 0 deletions

View File

@ -12,6 +12,7 @@
namespace Flarum\User;
use Flarum\Database\AbstractModel;
use InvalidArgumentException;
/**
* @property int $user_id
@ -21,8 +22,27 @@ use Flarum\Database\AbstractModel;
*/
class NotificationPreference extends AbstractModel
{
static protected $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 $toggle = true)
{
if (in_array($channel, static::$channels)) {
$user->notificationPreferences()
->where('channel', $channel)
->where('type', $type)
->update(['enabled' => $toggle]);
} else {
throw new InvalidArgumentException("Channel is not registered.");
}
}
}

View File

@ -459,6 +459,7 @@ class User extends AbstractModel
*
* @param string $value
* @return array
* @deprecated 0.1.0-beta.11: `users.preferences` is no longer used.
*/
public function getPreferencesAttribute($value)
{
@ -475,6 +476,7 @@ class User extends AbstractModel
* Encode an array of preferences for storage in the database.
*
* @param mixed $value
* @deprecated 0.1.0-beta.11: `users.preferences` is no longer used.
*/
public function setPreferencesAttribute($value)
{
@ -511,6 +513,7 @@ class User extends AbstractModel
* @param string $key
* @param mixed $default
* @return mixed
* @deprecated 0.1.0-beta.11: `users.preferences` is no longer used.
*/
public function getPreference($key, $default = null)
{
@ -523,6 +526,7 @@ class User extends AbstractModel
* @param string $key
* @param mixed $value
* @return $this
* @deprecated 0.1.0-beta.11: `users.preferences` is no longer used.
*/
public function setPreference($key, $value)
{
@ -736,6 +740,7 @@ class User extends AbstractModel
* @param string $key
* @param callable $transformer
* @param mixed $default
* @deprecated 0.1.0-beta.11: `users.preferences` is no longer used, use NotificationPreference::addPreference.
*/
public static function addPreference($key, callable $transformer = null, $default = null)
{
@ -749,6 +754,7 @@ class User extends AbstractModel
* @param string $type
* @param string $method
* @return string
* @deprecated 0.1.0-beta.11: `users.preferences` is no longer used, use NotificationPreference::getPreferenceKey.
*/
public static function getNotificationPreferenceKey($type, $method)
{