1
0
mirror of https://github.com/flarum/core.git synced 2025-08-09 09:57:06 +02:00

attempt to be more decisive on forcing the new user preferences

This commit is contained in:
Daniël Klabbers
2019-11-13 14:35:42 +01:00
parent 12fff33763
commit 7c204c82ab
2 changed files with 29 additions and 24 deletions

View File

@@ -12,6 +12,7 @@
namespace Flarum\User; namespace Flarum\User;
use Flarum\Database\AbstractModel; use Flarum\Database\AbstractModel;
use Illuminate\Database\Eloquent\Builder;
use InvalidArgumentException; use InvalidArgumentException;
/** /**
@@ -34,15 +35,27 @@ class NotificationPreference extends AbstractModel
static::$channels[] = $channel; 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)) { if (in_array($channel, static::$channels)) {
$user->notificationPreferences() $attributes = [
->where('channel', $channel) 'channel' => $channel,
->where('type', $type) 'type' => $type
->update(['enabled' => $toggle]); ];
$user->notificationPreferences()->updateOrInsert($attributes, ['enabled' => $enabled]);
} else { } 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);
});
}
} }

View File

@@ -480,7 +480,7 @@ class User extends AbstractModel
*/ */
public function setPreferencesAttribute($value) 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 * @param string $type
* @return bool * @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 * @param string $type
* @return bool * @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) public function setPreference($key, $value)
{ {
if (isset(static::$preferences[$key])) { throw new \Exception('Deprecated since v0.1.0-beta.11');
$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;
} }
/** /**