diff --git a/app/Commands/SaveAccountSettingsCommand.php b/app/Commands/SaveAccountSettingsCommand.php index 68e1b545..e3fdd2e2 100644 --- a/app/Commands/SaveAccountSettingsCommand.php +++ b/app/Commands/SaveAccountSettingsCommand.php @@ -24,7 +24,6 @@ use DB; use Poniverse\Ponyfm\Models\Image; use Poniverse\Ponyfm\Models\User; use Gate; -use Auth; use Validator; class SaveAccountSettingsCommand extends CommandBase @@ -111,20 +110,23 @@ class SaveAccountSettingsCommand extends CommandBase $this->_user->save(); // Sync email subscriptions - $emailSubscriptions = $this->_user->emailSubscriptions->keyBy('activity_type'); - foreach ($this->_input['notifications'] as $notificationSetting) { + // TODO: [#25] Remove this when email notifications are rolled out to everyone. + if (Gate::forUser($this->_user)->allows('receive-email-notifications')) { + $emailSubscriptions = $this->_user->emailSubscriptions->keyBy('activity_type'); + foreach ($this->_input['notifications'] as $notificationSetting) { - if ( - $notificationSetting['receive_emails'] && - !$emailSubscriptions->offsetExists($notificationSetting['activity_type']) - ) { - $this->_user->emailSubscriptions()->create(['activity_type' => $notificationSetting['activity_type']]); + if ( + $notificationSetting['receive_emails'] && + !$emailSubscriptions->offsetExists($notificationSetting['activity_type']) + ) { + $this->_user->emailSubscriptions()->create(['activity_type' => $notificationSetting['activity_type']]); - } elseif ( - !$notificationSetting['receive_emails'] && - $emailSubscriptions->offsetExists($notificationSetting['activity_type']) - ) { - $emailSubscriptions->get($notificationSetting['activity_type'])->delete(); + } elseif ( + !$notificationSetting['receive_emails'] && + $emailSubscriptions->offsetExists($notificationSetting['activity_type']) + ) { + $emailSubscriptions->get($notificationSetting['activity_type'])->delete(); + } } } }); diff --git a/app/Http/Controllers/Api/Web/AccountController.php b/app/Http/Controllers/Api/Web/AccountController.php index b86a85ba..d0853d6b 100644 --- a/app/Http/Controllers/Api/Web/AccountController.php +++ b/app/Http/Controllers/Api/Web/AccountController.php @@ -71,6 +71,8 @@ class AccountController extends ApiControllerBase 'gravatar' => $user->gravatar ? $user->gravatar : $user->email, 'avatar_url' => !$user->uses_gravatar ? $user->getAvatarUrl() : null, 'uses_gravatar' => $user->uses_gravatar == 1, + // TODO: [#25] Remove this when email notifications are rolled out to everyone. + 'can_manage_notifications' => Gate::forUser($user)->allows('receive-email-notifications'), 'notifications' => $user->getNotificationSettings() ], 200); } diff --git a/app/Models/User.php b/app/Models/User.php index 7be462c2..8b1febd2 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -437,16 +437,25 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon ', [$this->id]); } + /** + * Generates an array of the user's notification settings. It's meant to be + * used for the notification settings screen. + * + * @return array + */ public function getNotificationSettings() { $settings = []; $emailSubscriptions = $this->emailSubscriptionsJoined(); foreach($emailSubscriptions as $subscription) { - $settings[] = [ - 'description' => $subscription->description, - 'activity_type' => $subscription->activity_type, - 'receive_emails' => $subscription->id !== NULL - ]; + // TODO: remove this check when news and album notifications are implemented + if (!in_array($subscription->activity_type, [Activity::TYPE_NEWS, Activity::TYPE_PUBLISHED_ALBUM])) { + $settings[] = [ + 'description' => $subscription->description, + 'activity_type' => $subscription->activity_type, + 'receive_emails' => $subscription->id !== NULL + ]; + } } return $settings; diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index aceaf21b..2e17adf7 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -20,7 +20,7 @@ namespace Poniverse\Ponyfm\Providers; -use Illuminate\Support\Facades\Gate; +use Gate; use Illuminate\Contracts\Auth\Access\Gate as GateContract; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; use Poniverse\Ponyfm\Models\Album; @@ -76,6 +76,11 @@ class AuthServiceProvider extends ServiceProvider return $user->hasRole('admin'); }); + // TODO: [#25] Remove this when email notifications are rolled out to everyone. + Gate::define('receive-email-notifications', function (User $user) { + return $user->hasRole('admin'); + }); + $this->registerPolicies(); } } diff --git a/public/templates/account/settings.html b/public/templates/account/settings.html index fb73c5a9..4618a743 100644 --- a/public/templates/account/settings.html +++ b/public/templates/account/settings.html @@ -43,7 +43,7 @@
On-site notifications are always on. That way, you can always see what you've missed whenever you log on to Pony.fm!
diff --git a/resources/views/emails/notifications/content-favourited.blade.php b/resources/views/emails/notifications/content-favourited.blade.php index 565700e2..ec6425fc 100644 --- a/resources/views/emails/notifications/content-favourited.blade.php +++ b/resources/views/emails/notifications/content-favourited.blade.php @@ -3,7 +3,7 @@ @section('content'){{ $creatorName }} favourited your {{ $resourceType }}, - {{ $resourceTitle }} + {{ $resourceTitle }}. Yay!
@endsection