From 8c7fe0ea3377eff12ccacb4b3b6c0fd44a1d2fe0 Mon Sep 17 00:00:00 2001 From: Peter Deltchev Date: Fri, 30 Dec 2016 13:23:52 -0800 Subject: [PATCH] #25: Enable certain email notifications by default for new users. Also fixes a bug with storing OAuth tokens for new users! --- app/Http/Controllers/AuthController.php | 8 +++++++- app/Models/Activity.php | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index ecde126f..f693e4ea 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -26,6 +26,7 @@ use League\OAuth2\Client\Provider\Exception\IdentityProviderException; use League\OAuth2\Client\Token\AccessToken; use Log; use Poniverse\Lib\Client; +use Poniverse\Ponyfm\Models\Activity; use Poniverse\Ponyfm\Models\User; use Auth; use DB; @@ -106,12 +107,17 @@ class AuthController extends Controller // Check by login name to see if they already have an account $user = User::findOrCreate($poniverseUser->username, $poniverseUser->display_name, $poniverseUser->email); - if (!$user->wasRecentlyCreated) { + if ($user->wasRecentlyCreated) { // We need to insert a new token row :O $setData['user_id'] = $user->id; $setData['external_user_id'] = $poniverseUser->id; $setData['service'] = 'poniverse'; DB::table('oauth2_tokens')->insert($setData); + + // Subscribe the user to default email notifications + foreach (Activity::DEFAULT_EMAIL_TYPES as $activityType) { + $user->emailSubscriptions()->create(['activity_type' => $activityType]); + } } diff --git a/app/Models/Activity.php b/app/Models/Activity.php index 63ca536b..08501e90 100644 --- a/app/Models/Activity.php +++ b/app/Models/Activity.php @@ -79,6 +79,18 @@ class Activity extends Model const TYPE_NEW_COMMENT = 6; const TYPE_CONTENT_FAVOURITED = 7; + /** + * Activity types to subscribe new users to email notifications for. + */ + const DEFAULT_EMAIL_TYPES = [ + Activity::TYPE_PUBLISHED_TRACK, + Activity::TYPE_PUBLISHED_ALBUM, + Activity::TYPE_PUBLISHED_PLAYLIST, + Activity::TYPE_NEW_FOLLOWER, + Activity::TYPE_NEW_COMMENT, + Activity::TYPE_CONTENT_FAVOURITED, + ]; + /** * These "target" constants are an implementation detail of this model and * should not be used directly in other classes. They're used to efficiently