#25: Limited notification management to admins.

This commit is contained in:
Peter Deltchev 2016-12-24 18:39:05 -08:00
parent b401a0ae7e
commit 79e77a2da0
6 changed files with 39 additions and 21 deletions

View file

@ -24,7 +24,6 @@ use DB;
use Poniverse\Ponyfm\Models\Image; use Poniverse\Ponyfm\Models\Image;
use Poniverse\Ponyfm\Models\User; use Poniverse\Ponyfm\Models\User;
use Gate; use Gate;
use Auth;
use Validator; use Validator;
class SaveAccountSettingsCommand extends CommandBase class SaveAccountSettingsCommand extends CommandBase
@ -111,20 +110,23 @@ class SaveAccountSettingsCommand extends CommandBase
$this->_user->save(); $this->_user->save();
// Sync email subscriptions // Sync email subscriptions
$emailSubscriptions = $this->_user->emailSubscriptions->keyBy('activity_type'); // TODO: [#25] Remove this when email notifications are rolled out to everyone.
foreach ($this->_input['notifications'] as $notificationSetting) { if (Gate::forUser($this->_user)->allows('receive-email-notifications')) {
$emailSubscriptions = $this->_user->emailSubscriptions->keyBy('activity_type');
foreach ($this->_input['notifications'] as $notificationSetting) {
if ( if (
$notificationSetting['receive_emails'] && $notificationSetting['receive_emails'] &&
!$emailSubscriptions->offsetExists($notificationSetting['activity_type']) !$emailSubscriptions->offsetExists($notificationSetting['activity_type'])
) { ) {
$this->_user->emailSubscriptions()->create(['activity_type' => $notificationSetting['activity_type']]); $this->_user->emailSubscriptions()->create(['activity_type' => $notificationSetting['activity_type']]);
} elseif ( } elseif (
!$notificationSetting['receive_emails'] && !$notificationSetting['receive_emails'] &&
$emailSubscriptions->offsetExists($notificationSetting['activity_type']) $emailSubscriptions->offsetExists($notificationSetting['activity_type'])
) { ) {
$emailSubscriptions->get($notificationSetting['activity_type'])->delete(); $emailSubscriptions->get($notificationSetting['activity_type'])->delete();
}
} }
} }
}); });

View file

@ -71,6 +71,8 @@ class AccountController extends ApiControllerBase
'gravatar' => $user->gravatar ? $user->gravatar : $user->email, 'gravatar' => $user->gravatar ? $user->gravatar : $user->email,
'avatar_url' => !$user->uses_gravatar ? $user->getAvatarUrl() : null, 'avatar_url' => !$user->uses_gravatar ? $user->getAvatarUrl() : null,
'uses_gravatar' => $user->uses_gravatar == 1, '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() 'notifications' => $user->getNotificationSettings()
], 200); ], 200);
} }

View file

@ -437,16 +437,25 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
', [$this->id]); ', [$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() { public function getNotificationSettings() {
$settings = []; $settings = [];
$emailSubscriptions = $this->emailSubscriptionsJoined(); $emailSubscriptions = $this->emailSubscriptionsJoined();
foreach($emailSubscriptions as $subscription) { foreach($emailSubscriptions as $subscription) {
$settings[] = [ // TODO: remove this check when news and album notifications are implemented
'description' => $subscription->description, if (!in_array($subscription->activity_type, [Activity::TYPE_NEWS, Activity::TYPE_PUBLISHED_ALBUM])) {
'activity_type' => $subscription->activity_type, $settings[] = [
'receive_emails' => $subscription->id !== NULL 'description' => $subscription->description,
]; 'activity_type' => $subscription->activity_type,
'receive_emails' => $subscription->id !== NULL
];
}
} }
return $settings; return $settings;

View file

@ -20,7 +20,7 @@
namespace Poniverse\Ponyfm\Providers; namespace Poniverse\Ponyfm\Providers;
use Illuminate\Support\Facades\Gate; use Gate;
use Illuminate\Contracts\Auth\Access\Gate as GateContract; use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Poniverse\Ponyfm\Models\Album; use Poniverse\Ponyfm\Models\Album;
@ -76,6 +76,11 @@ class AuthServiceProvider extends ServiceProvider
return $user->hasRole('admin'); 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(); $this->registerPolicies();
} }
} }

View file

@ -43,7 +43,7 @@
<div class="error" ng-show="errors.gravatar != null">{{errors.gravatar}}</div> <div class="error" ng-show="errors.gravatar != null">{{errors.gravatar}}</div>
</div> </div>
</div> </div>
<div class="col-md-8"> <div class="col-md-8" ng-if="::settings.can_manage_notifications">
<h3>Notification settings</h3> <h3>Notification settings</h3>
<p>On-site notifications are always on. That way, you can always see <p>On-site notifications are always on. That way, you can always see
what you&#39;ve missed whenever you log on to Pony.fm!</p> what you&#39;ve missed whenever you log on to Pony.fm!</p>

View file

@ -3,7 +3,7 @@
@section('content') @section('content')
<p> <p>
{{ $creatorName }} favourited your {{ $resourceType }}, {{ $creatorName }} favourited your {{ $resourceType }},
<em><a href="{{ $notificationUrl }}" target="_blank">{{ $resourceTitle }}</a></em> <em><a href="{{ $notificationUrl }}" target="_blank">{{ $resourceTitle }}</a></em>.
Yay! Yay!
</p> </p>
@endsection @endsection