#25: Enabled email notifications for all users.

This also includes some fixes to the account merging script.
This commit is contained in:
Peter Deltchev 2016-12-30 11:22:44 -08:00
parent f85e8fc43a
commit 7ebab328d4
8 changed files with 175 additions and 36 deletions

View file

@ -24,9 +24,11 @@ use Carbon\Carbon;
use DB;
use Poniverse\Ponyfm\Models\Album;
use Poniverse\Ponyfm\Models\Comment;
use Poniverse\Ponyfm\Models\EmailSubscription;
use Poniverse\Ponyfm\Models\Favourite;
use Poniverse\Ponyfm\Models\Follower;
use Poniverse\Ponyfm\Models\Image;
use Poniverse\Ponyfm\Models\Notification;
use Poniverse\Ponyfm\Models\PinnedPlaylist;
use Poniverse\Ponyfm\Models\Playlist;
use Poniverse\Ponyfm\Models\ResourceLogItem;
@ -46,6 +48,10 @@ class MergeAccountsCommand extends CommandBase
}
/**
* Note: OAuth tokens are intentionally left untouched by the merge process.
* The Artisan script performs some sanity checks to alert the admin to the
* consequences of this.
*
* @throws \Exception
* @return CommandResponse
*/
@ -84,8 +90,6 @@ class MergeAccountsCommand extends CommandBase
$image->save();
}
DB::table('oauth2_tokens')->whereIn('user_id', $accountIds)->update(['user_id' => $this->destinationAccount->id]);
foreach (PinnedPlaylist::whereIn('user_id', $accountIds)->get() as $playlist) {
$playlist->user_id = $this->destinationAccount->id;
$playlist->save();
@ -106,11 +110,24 @@ class MergeAccountsCommand extends CommandBase
$item->save();
}
/** @var Track $track */
foreach (Track::whereIn('user_id', $accountIds)->get() as $track) {
$track->user_id = $this->destinationAccount->id;
$track->save();
}
/** @var EmailSubscription $emailSubscription */
foreach($this->sourceAccount->emailSubscriptions()->withTrashed()->get() as $emailSubscription) {
// This keeps emails from being sent to disabled accounts.
$emailSubscription->delete();
}
/** @var Notification $notification */
foreach ($this->sourceAccount->notifications()->get() as $notification) {
$notification->user_id = $this->destinationAccount->id;
$notification->save();
}
$this->sourceAccount->disabled_at = Carbon::now();
$this->sourceAccount->save();
});

View file

@ -110,23 +110,20 @@ class SaveAccountSettingsCommand extends CommandBase
$this->_user->save();
// Sync email subscriptions
// 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) {
$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();
}
}
});

View file

@ -77,9 +77,30 @@ class MergeAccounts extends Command
$sourceAccount = User::find($sourceAccountId);
$destinationAccount = User::find($destinationAccountId);
// Sanity checks
if (null !== $sourceAccount->getAccessToken()) {
$this->warn("WARNING: The source account (ID {$sourceAccountId}) is linked to a Poniverse account! Normally, the destination account should be the one that's linked to a Poniverse account as that's the one that the artist will be logging into.");
$this->line('');
$this->warn("If you continue with this merge, the Poniverse account linked to the source Pony.fm account will no longer be able to log into Pony.fm.");
if (!$this->confirm('Continue merging this set of source and destination accounts?')){
$this->error('Merge aborted.');
return 1;
}
}
if (null === $destinationAccount->getAccessToken()) {
$this->warn("WARNING: The destination account (ID {$destinationAccountId}) is not linked to a Poniverse account!");
$this->warn("This is normal if you're merging two archived profiles but not if you're helping an artist claim their profile.");
if (!$this->confirm('Continue merging this set of source and destination accounts?')){
$this->error('Merge aborted.');
return 1;
}
}
$this->info("Merging {$sourceAccount->display_name} ({$sourceAccountId}) into {$destinationAccount->display_name} ({$destinationAccountId})...");
$command = new MergeAccountsCommand($sourceAccount, $destinationAccount);
$command->execute();
return 0;
}
}

View file

@ -71,8 +71,7 @@ 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'),
'notification_email' => $user->email,
'notifications' => $user->getNotificationSettings()
], 200);
}

View file

@ -174,16 +174,21 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
/**
* Gets this user's OAuth access token record.
*
* @return AccessToken
* @return AccessToken|null
*/
public function getAccessToken():AccessToken {
public function getAccessToken() {
$accessTokenRecord = DB::table('oauth2_tokens')->where('user_id', '=', $this->id)->first();
return new AccessToken([
'access_token' => $accessTokenRecord->access_token,
'refresh_token' => $accessTokenRecord->refresh_token,
'expires' => Carbon::parse($accessTokenRecord->expires)->timestamp,
'resource_owner_id' => $accessTokenRecord->external_user_id,
]);
if ($accessTokenRecord === null) {
return null;
} else {
return new AccessToken([
'access_token' => $accessTokenRecord->access_token,
'refresh_token' => $accessTokenRecord->refresh_token,
'expires' => Carbon::parse($accessTokenRecord->expires)->timestamp,
'resource_owner_id' => $accessTokenRecord->external_user_id,
]);
}
}
/**

View file

@ -76,11 +76,6 @@ 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();
}
}

View file

@ -0,0 +1,102 @@
<?php
/**
* Pony.fm - A community for pony fan music.
* Copyright (C) 2016 Peter Deltchev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Poniverse\Ponyfm\Models\User;
class EnableEmailNotifications extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
DB::table('email_subscriptions')->delete();
User::whereNull('disabled_at')
->where('is_archived', false)
->chunk(100, function ($users) {
/** @var User $user */
foreach ($users as $user) {
$now = \Carbon\Carbon::now();
$userId = $user->id;
DB::table('email_subscriptions')
->insert([
[
'id' => \Webpatser\Uuid\Uuid::generate(4),
'user_id' => $userId,
'activity_type' => 2,
'created_at' => $now,
'updated_at' => $now,
],
[
'id' => \Webpatser\Uuid\Uuid::generate(4),
'user_id' => $userId,
'activity_type' => 3,
'created_at' => $now,
'updated_at' => $now,
],
[
'id' => \Webpatser\Uuid\Uuid::generate(4),
'user_id' => $userId,
'activity_type' => 4,
'created_at' => $now,
'updated_at' => $now,
],
[
'id' => \Webpatser\Uuid\Uuid::generate(4),
'user_id' => $userId,
'activity_type' => 5,
'created_at' => $now,
'updated_at' => $now,
],
[
'id' => \Webpatser\Uuid\Uuid::generate(4),
'user_id' => $userId,
'activity_type' => 6,
'created_at' => $now,
'updated_at' => $now,
],
[
'id' => \Webpatser\Uuid\Uuid::generate(4),
'user_id' => $userId,
'activity_type' => 7,
'created_at' => $now,
'updated_at' => $now,
]
]);
}
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
DB::table('email_subscriptions')->delete();
}
}

View file

@ -43,7 +43,7 @@
<div class="error" ng-show="errors.gravatar != null">{{errors.gravatar}}</div>
</div>
</div>
<div class="col-md-8" ng-if="::settings.can_manage_notifications">
<div class="col-md-8">
<div class="alert alert-warning" ng-if="unsubscribeMessage">
<p>{{ ::unsubscribeMessage }}</p>
</div>
@ -52,15 +52,18 @@
<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>
<p>Email notifications will be sent to <strong>{{ ::settings.notification_email }}</strong>.
You can change this address in your <a href="https://poniverse.net/account" target="_blank">Poniverse account settings</a>.</p>
<table class="table table-hover">
<thead>
<th></th>
<th>When&hellip;</th>
<th>Email me!</th>
<th>Give me a push notification!</th>
</thead>
<tr ng-repeat="notification in ::settings.notifications track by notification.activity_type">
<td><label>{{ ::notification.description }}</label></td>
<td><input type="checkbox" ng-change="touchModel()" ng-model="notification.receive_emails" /></td>
<td><label>&hellip;{{ ::notification.description }}:</label></td>
<td><input title="Email me when {{ ::notification.description }}" type="checkbox" ng-change="touchModel()" ng-model="notification.receive_emails" /></td>
<td>Coming soon!</td>
</tr>
</table>