mirror of
https://github.com/flarum/framework.git
synced 2025-01-19 18:12:59 +08:00
attempt to be more decisive on forcing the new user preferences
This commit is contained in:
parent
12fff33763
commit
7c204c82ab
|
@ -12,6 +12,7 @@
|
|||
namespace Flarum\User;
|
||||
|
||||
use Flarum\Database\AbstractModel;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
|
@ -34,15 +35,27 @@ class NotificationPreference extends AbstractModel
|
|||
static::$channels[] = $channel;
|
||||
}
|
||||
|
||||
public static function setNotificationPreference(User $user, string $type, string $channel, bool $toggle = true)
|
||||
public static function setNotificationPreference(User $user, string $type, string $channel, bool $enabled = true)
|
||||
{
|
||||
if (in_array($channel, static::$channels)) {
|
||||
$user->notificationPreferences()
|
||||
->where('channel', $channel)
|
||||
->where('type', $type)
|
||||
->update(['enabled' => $toggle]);
|
||||
$attributes = [
|
||||
'channel' => $channel,
|
||||
'type' => $type
|
||||
];
|
||||
|
||||
$user->notificationPreferences()->updateOrInsert($attributes, ['enabled' => $enabled]);
|
||||
} else {
|
||||
throw new InvalidArgumentException("Channel is not registered.");
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -480,7 +480,7 @@ class User extends AbstractModel
|
|||
*/
|
||||
public function setPreferencesAttribute($value)
|
||||
{
|
||||
$this->attributes['preferences'] = json_encode($value);
|
||||
throw new \Exception("user.preferences table is deprecated");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -490,9 +490,11 @@ class User extends AbstractModel
|
|||
* @param string $type
|
||||
* @return bool
|
||||
*/
|
||||
public function shouldAlert($type)
|
||||
public function shouldAlert(string $type): bool
|
||||
{
|
||||
return (bool) $this->getPreference(static::getNotificationPreferenceKey($type, 'alert'));
|
||||
return $this->notificationPreferences()
|
||||
->shouldBeNotified($type, 'email')
|
||||
->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -502,9 +504,11 @@ class User extends AbstractModel
|
|||
* @param string $type
|
||||
* @return bool
|
||||
*/
|
||||
public function shouldEmail($type)
|
||||
public function shouldEmail(string $type): bool
|
||||
{
|
||||
return (bool) $this->getPreference(static::getNotificationPreferenceKey($type, 'email'));
|
||||
return $this->notificationPreferences()
|
||||
->shouldBeNotified($type, 'email')
|
||||
->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -530,19 +534,7 @@ class User extends AbstractModel
|
|||
*/
|
||||
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;
|
||||
throw new \Exception('Deprecated since v0.1.0-beta.11');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue
Block a user