2016-12-09 11:53:32 +01:00
|
|
|
<?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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Poniverse\Ponyfm\Mail;
|
|
|
|
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Mail\Mailable;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2016-12-23 14:08:41 +01:00
|
|
|
use Poniverse\Ponyfm\Models\Activity;
|
2016-12-09 11:53:32 +01:00
|
|
|
use Poniverse\Ponyfm\Models\Email;
|
|
|
|
|
|
|
|
abstract class BaseNotification extends Mailable {
|
|
|
|
use Queueable, SerializesModels;
|
|
|
|
|
|
|
|
/** @var Email */
|
|
|
|
protected $emailRecord;
|
|
|
|
|
|
|
|
/** @var \Poniverse\Ponyfm\Models\Notification */
|
|
|
|
protected $notificationRecord;
|
|
|
|
|
|
|
|
/** @var \Poniverse\Ponyfm\Models\Activity */
|
|
|
|
protected $activityRecord;
|
|
|
|
|
|
|
|
/** @var \Poniverse\Ponyfm\Models\User */
|
|
|
|
protected $initiatingUser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new message instance.
|
|
|
|
*
|
|
|
|
* @param Email $email
|
|
|
|
*/
|
|
|
|
public function __construct(Email $email) {
|
|
|
|
$this->emailRecord = $email;
|
|
|
|
$this->notificationRecord = $email->notification;
|
|
|
|
$this->activityRecord = $email->notification->activity;
|
|
|
|
$this->initiatingUser = $email->notification->activity->initiatingUser;
|
|
|
|
}
|
|
|
|
|
2016-12-23 14:08:41 +01:00
|
|
|
/**
|
|
|
|
* Factory method that instantiates the appropriate {@link BaseNotification}
|
|
|
|
* subclass for the given activity type and {@link Email} record.
|
|
|
|
*
|
|
|
|
* @param Activity $activity
|
|
|
|
* @param Email $email
|
|
|
|
* @return BaseNotification
|
|
|
|
*/
|
|
|
|
static public function factory(Activity $activity, Email $email): BaseNotification {
|
|
|
|
switch ($activity->activity_type) {
|
|
|
|
case Activity::TYPE_NEWS:
|
|
|
|
break;
|
|
|
|
case Activity::TYPE_PUBLISHED_TRACK:
|
|
|
|
return new NewTrack($email);
|
|
|
|
case Activity::TYPE_PUBLISHED_ALBUM:
|
|
|
|
break;
|
|
|
|
case Activity::TYPE_PUBLISHED_PLAYLIST:
|
|
|
|
return new NewPlaylist($email);
|
|
|
|
case Activity::TYPE_NEW_FOLLOWER:
|
|
|
|
return new NewFollower($email);
|
|
|
|
case Activity::TYPE_NEW_COMMENT:
|
|
|
|
return new NewComment($email);
|
|
|
|
case Activity::TYPE_CONTENT_FAVOURITED:
|
|
|
|
return new ContentFavourited($email);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
throw new \InvalidArgumentException("Email notifications for activity type {$activity->activity_type} are not implemented!");
|
|
|
|
}
|
|
|
|
|
2016-12-09 11:53:32 +01:00
|
|
|
/**
|
|
|
|
* Build the message.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
abstract public function build();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates an unsubscribe URL unique to the user.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function generateUnsubscribeUrl() {
|
2016-12-25 03:11:42 +01:00
|
|
|
return route('email:unsubscribe', ['subscriptionKey' => $this->emailRecord->getSubscription()->id]);
|
2016-12-09 11:53:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a trackable URL to the content item this email is about.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function generateNotificationUrl() {
|
2016-12-25 03:11:42 +01:00
|
|
|
return route('email:click', ['emailKey' => $this->emailRecord->id]);
|
2016-12-09 11:53:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-23 14:08:41 +01:00
|
|
|
* Helper method to eliminate duplication between different types of
|
|
|
|
* notifications. Use it inside the build() method on this class's children.
|
|
|
|
*
|
|
|
|
* Note that data common to all notification types is merged into the
|
|
|
|
* template variable array.
|
2016-12-09 11:53:32 +01:00
|
|
|
*
|
|
|
|
* @param string $templateName
|
|
|
|
* @param string $subject
|
|
|
|
* @param array $extraVariables
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
protected function renderEmail(string $templateName, string $subject, array $extraVariables) {
|
|
|
|
return $this
|
|
|
|
->subject($subject)
|
2016-12-26 16:50:13 +01:00
|
|
|
->view("emails.html.notifications.{$templateName}")
|
|
|
|
->text("emails.plaintext.notifications.{$templateName}")
|
2016-12-09 11:53:32 +01:00
|
|
|
->with(array_merge($extraVariables, [
|
|
|
|
'notificationUrl' => $this->generateNotificationUrl(),
|
2016-12-26 16:50:13 +01:00
|
|
|
'unsubscribeUrl' => $this->generateUnsubscribeUrl(),
|
|
|
|
'thumbnailUrl' => $this->activityRecord->thumbnail_url
|
2016-12-09 11:53:32 +01:00
|
|
|
]));
|
|
|
|
}
|
|
|
|
}
|