Skip to content

Commit

Permalink
Creating html email, check for unsubscribed_at on all emails.
Browse files Browse the repository at this point in the history
  • Loading branch information
clone1018 committed Jan 31, 2015
1 parent 5aa6b23 commit 4e14029
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 35 deletions.
29 changes: 20 additions & 9 deletions app/commands/SendUpdatesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ public function fire()
$usersNeedingUpdates = DB::table('user_project_updates')->where('emailed_at', null)->where('notification_level_id', $level->id)->groupBy('user_id')->get(['user_id']);

foreach($usersNeedingUpdates as $upu) {
$user = User::find($upu->user_id)->firstOrFail();
if( ! $this->rightTime($level, $user)) {
$user = User::where('id', $upu->user_id)->firstOrFail();
if( ! $this->rightTime($level, $user) || !is_null($user->unsubscribe_at)) {
$this->info("{$user->username} has updates but it's not the right time.");
continue;
}

Expand All @@ -53,33 +54,42 @@ public function fire()
$title = $this->goodTitle($level, $user);

// Do this in the background, we have a doublecheck in SendUpdates for emailed_at
Mail::queue(
['emails.updates.grouped.html', 'emails.updates.grouped.text'],
Mail::send(
['emails/html/updates/grouped', 'emails/text/updates/grouped'],
['user' => $user, 'projectsUpdated' => $projectsUpdated],
function($message) use ($user, $title) {
$message->from(Config::get('patchnotes.emails.updates.from.address'), Config::get('patchnotes.emails.updates.from.name'));

$message->to($user->email, $user->fullname)->subject($title);
});

foreach($projectsUpdated as $p) {
foreach($p as $pu) {
$this->info("Mailed project_update_id={$pu->project_update_id} to $user->email");

$pu->emailed_at = new DateTime();
$pu->save();
}
}
}
}

private function goodTitle(NotificationLevel $level, $user) {
switch($level->key) {
case 'immediate':
return "Immediate updates from ";
return "Important Updates";

break;
case 'daily':
$now = new DateTime('now', new DateTimeZone($user->preferences->timezone));
return "Daily Digest for " . $now->format('F jS');
return "Daily Update Digest for " . $now->format('F jS');

break;
case 'weekly':
$now = new DateTime('now', new DateTimeZone($user->preferences->timezone));
$plusOneWeek = new DateTime('now', new DateTimeZone($user->preferences->timezone));
$plusOneWeek->add(new DateInterval("P7D"));
return "Weekly Digest for " . $now->format('F jS') . " - " . $plusOneWeek->format('F jS');
return "Weekly Update Digest for " . $now->format('F jS') . " - " . $plusOneWeek->format('F jS');

break;
}
Expand All @@ -93,9 +103,10 @@ private function goodTitle(NotificationLevel $level, $user) {
* @return bool
*/
private function rightTime(NotificationLevel $level, User $user) {
// Currently in USER timezone
$currently = new DateTime();
$preferences = $user->preferences;

// Currently in USER timezone
$currently = new DateTime('now', new DateTimeZone($preferences->timezone));
switch($level->key) {
case 'immediate':
return true;
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/Account/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public function oauthUserIsValid($oauthUser)
*/
public function oauthUserRequiresValidation($oauthUser)
{
if(!is_null($oauthUser->user->unsubscribed_at)) {
App::abort(404, "Can't validate user when unsubscribed_at is not null.");
}

Mail::send('emails.auth.oauth-validation', array('oauth' => $oauthUser), function ($m) use ($oauthUser) {
$m->to($oauthUser->user->email)->subject('PatchNotes oAuth validation');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public function up()
$table->unique('user_id');
$table->foreign('user_id')->references('id')->on('users');
});

foreach(User::all() as $user) {
if(!count($user->preferences)) {
$preference = new UserPreference();
$preference->user_id = $user->id;
$preference->save();
}
}
}

/**
Expand Down
16 changes: 11 additions & 5 deletions app/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,10 @@ class User extends \Cartalyst\Sentry\Users\Eloquent\User implements Models\Inter
),
);

public function __construct(array $attributes = array())
public static function boot()
{

parent::__construct($attributes);

parent::boot();
self::events();

}

public function preferences() {
Expand Down Expand Up @@ -238,6 +235,15 @@ private static function events()
$user->unsubscribe_token = str_random(42);
});

self::created(function($user) {
if(empty($user->preferences)) {
$preference = new UserPreference();

$user->preferences()->save($preference);
}

});

self::updating(function ($user) {
$org = Organization::where('slug', $user->slug)->first();

Expand Down
6 changes: 6 additions & 0 deletions app/tasks/UserEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
class UserEvents {

public function onRegister($data, $activationCode) {
if(!is_null($data->unsubscribed_at)) {
return;
}
Mail::send('emails.auth.register', array('user' => $data, 'activationCode' => $activationCode), function ($m) use ($data) {
$m->to($data->email)->subject('Welcome to PatchNotes!');
});
}

public function onForgot($data, $forgotCode) {
if(!is_null($data->unsubscribed_at)) {
return;
}
Mail::send('emails.auth.forgot', array('user' => $data, 'forgotCode' => $forgotCode), function ($m) use ($data) {
$m->to($data->email)->subject('Forgot Password?');
}
Expand Down
2 changes: 1 addition & 1 deletion app/views/account/dashboard/preferences.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<label for="inputTimezone" class="col-sm-4 control-label">Timezone</label>

<div class="col-sm-8">
{{ Form::select('timezone', UserPreference::timezoneSelectBox(), $preference->timezone, [
{{ Form::select('timezone', UserPreference::timezoneSelectBox(), (isset($preference->timezone) ? $preference->timezone : "UTC"), [
'class' => 'form-control',
'id' => 'inputTimezone'
]) }}
Expand Down
28 changes: 14 additions & 14 deletions app/views/emails/html/updates/grouped.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
<meta charset="utf-8">
</head>
<body>
<h2>Welcome to PatchNotes!</h2>

<div>
<p>In case you forgot, <a href="{{URL::to('/')}}">PatchNotes</a> INSERT SOME DESCRIPTION OF THE WEBSITE HERE.</p>
<p>Hey there {{{ $user->fullname }}},</p>

<p>Anyways, here's the stuff you need to know to login: </p>
<p>Here's your digest of updates!</p>

<ol>
<li><a href="{{ URL::to('account/validate', array($user->email, $activationCode)) }}">Validate Your Email</a>[1]</li>
</ol>
@foreach($projectsUpdated as $projectId => $userUpdates)
<h4>{{{ Project::where('id', $projectId)->first()->name }}}</h4>
<ul>
@foreach($userUpdates as $userUpdate)
<li><a href="{{ $userUpdate->project_update->href }}">{{{ $userUpdate->project_update->title }}}</a></li>
@endforeach
</ul>
@endforeach

<p>We hope that's pretty simple, if not, let us know!</p>
<p>See something you don't like? Curate your subscriptions on <a href="{{ URL::action('Account\\DashboardController@getSubscriptions') }}">Your Dashboard</a></p>

<p>Thanks,<br>
<i>The Robots @ PatchNotes</i><br></p>
<hr>
<p>Thanks,</p>
<p>{{ Config::get('patchnotes.emails.updates.from.name') }}</p>

<ol>
<li>{{ URL::to('account/validate', array($user->email, $activationCode)) }}</li>
</ol>
<p><a href="{{ URL::action('HomeController@getUnsubscribe') }}?email={{ $user->email }}&token={{ $user->unsubscribe_token }}">Unsubscribe from all emails from PatchNotes.</a></p>
</div>
</body>
</html>
15 changes: 9 additions & 6 deletions app/views/emails/text/updates/grouped.blade.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
Hey there {{{ $user->fullname }}},

Below are your updates:
@foreach($projectsUpdated as $projectId => $userUpdates)
<?php $project = Project::find($projectId)->firstOrFail(); ?>
Here's your digest of updates!

{{{ $project->name }}}
@foreach($projectsUpdated as $projectId => $userUpdates)
{{{ Project::where('id', $projectId)->first()->name }}}
@foreach($userUpdates as $userUpdate)
* {{{ $userUpdate->project_update->title }}} [{{ $userUpdate->project_update->href }}]
@endforeach
Unsubscribe from emails about {{ $project->name }}: {{ $project->unsubscribe_href }}

@endforeach

See something you don't like? Curate your subscriptions at {{ URL::action('Account\\DashboardController@getSubscriptions') }}

Thanks,
{{ Config::get('patchnotes.emails.updates.from.name') }}
{{ Config::get('patchnotes.emails.updates.from.name') }}

Unsubscribe from all emails from PatchNotes at {{ URL::action('HomeController@getUnsubscribe') }}?email={{ $user->email }}&token={{ $user->unsubscribe_token }}

0 comments on commit 4e14029

Please sign in to comment.