From 7c204c82ab6ff2bb0c3a1ddabb65548b0b132ce2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Klabbers?= Date: Wed, 13 Nov 2019 14:35:42 +0100 Subject: [PATCH] attempt to be more decisive on forcing the new user preferences --- src/User/NotificationPreference.php | 25 +++++++++++++++++++------ src/User/User.php | 28 ++++++++++------------------ 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/User/NotificationPreference.php b/src/User/NotificationPreference.php index 8358d7dfe..57e146549 100644 --- a/src/User/NotificationPreference.php +++ b/src/User/NotificationPreference.php @@ -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); + }); + } } diff --git a/src/User/User.php b/src/User/User.php index 49878bd2e..d9462f840 100644 --- a/src/User/User.php +++ b/src/User/User.php @@ -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'); } /**