2016-05-27 21:12:40 +02: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\Library\Notifications;
|
|
|
|
|
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
|
|
|
use Poniverse\Ponyfm\Contracts\Favouritable;
|
|
|
|
use Poniverse\Ponyfm\Contracts\NotificationHandler;
|
|
|
|
use Poniverse\Ponyfm\Jobs\SendNotifications;
|
2016-12-09 11:53:32 +01:00
|
|
|
use Poniverse\Ponyfm\Library\Notifications\Drivers\EmailDriver;
|
2016-06-12 01:58:10 +02:00
|
|
|
use Poniverse\Ponyfm\Library\Notifications\Drivers\NativeDriver;
|
2016-05-27 21:12:40 +02:00
|
|
|
use Poniverse\Ponyfm\Library\Notifications\Drivers\PonyfmDriver;
|
2016-12-09 11:53:32 +01:00
|
|
|
use Poniverse\Ponyfm\Models\Activity;
|
2016-05-27 21:12:40 +02:00
|
|
|
use Poniverse\Ponyfm\Models\Comment;
|
|
|
|
use Poniverse\Ponyfm\Models\Playlist;
|
2016-06-12 01:58:10 +02:00
|
|
|
use Poniverse\Ponyfm\Models\Subscription;
|
2016-05-27 21:12:40 +02:00
|
|
|
use Poniverse\Ponyfm\Models\Track;
|
|
|
|
use Poniverse\Ponyfm\Models\User;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class RecipientFinder
|
|
|
|
* @package Poniverse\Ponyfm\Library\Notifications
|
2016-09-30 00:26:31 +02:00
|
|
|
*
|
2016-05-27 21:12:40 +02:00
|
|
|
* This class returns a list of users who are to receive a particular notification.
|
|
|
|
* It is instantiated on a per-driver basis.
|
|
|
|
*/
|
2016-09-30 00:26:31 +02:00
|
|
|
class RecipientFinder implements NotificationHandler
|
|
|
|
{
|
2016-05-27 21:12:40 +02:00
|
|
|
/**
|
|
|
|
* @var string class name of a notification driver
|
|
|
|
*/
|
|
|
|
private $notificationDriver;
|
|
|
|
|
2016-09-30 00:26:31 +02:00
|
|
|
public function __construct(string $notificationDriver)
|
|
|
|
{
|
2016-05-27 21:12:40 +02:00
|
|
|
$this->notificationDriver = $notificationDriver;
|
|
|
|
}
|
|
|
|
|
2016-09-30 00:26:31 +02:00
|
|
|
private function fail()
|
|
|
|
{
|
2016-05-27 21:12:40 +02:00
|
|
|
throw new \InvalidArgumentException("Unknown notification driver given: {$this->notificationDriver}");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2016-09-30 00:26:31 +02:00
|
|
|
public function publishedNewTrack(Track $track)
|
|
|
|
{
|
2016-05-27 21:12:40 +02:00
|
|
|
switch ($this->notificationDriver) {
|
|
|
|
case PonyfmDriver::class:
|
2016-12-09 11:53:32 +01:00
|
|
|
return $track->user->followers();
|
|
|
|
|
|
|
|
case EmailDriver::class:
|
|
|
|
return $track->user->followers()->whereHas('emailSubscriptions', function($query) {
|
|
|
|
$query->where('activity_type', Activity::TYPE_PUBLISHED_TRACK);
|
|
|
|
})->get();
|
|
|
|
|
2016-06-12 01:58:10 +02:00
|
|
|
case NativeDriver::class:
|
|
|
|
$followerIds = [];
|
|
|
|
$subIds = [];
|
|
|
|
$rawSubIds = Subscription::select('id')->get();
|
|
|
|
|
|
|
|
foreach ($track->user->followers as $follower) {
|
|
|
|
array_push($followerIds, $follower->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($rawSubIds as $sub) {
|
|
|
|
array_push($subIds, $sub->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$targetIds = array_intersect($followerIds, $subIds);
|
|
|
|
return Subscription::whereIn('user_id', $targetIds)->get();
|
2016-05-27 21:12:40 +02:00
|
|
|
default:
|
|
|
|
return $this->fail();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2016-09-30 00:26:31 +02:00
|
|
|
public function publishedNewPlaylist(Playlist $playlist)
|
|
|
|
{
|
2016-05-27 21:12:40 +02:00
|
|
|
switch ($this->notificationDriver) {
|
|
|
|
case PonyfmDriver::class:
|
|
|
|
return $playlist->user->followers;
|
2016-06-12 01:58:10 +02:00
|
|
|
case NativeDriver::class:
|
|
|
|
$followerIds = [];
|
|
|
|
$subIds = [];
|
|
|
|
$rawSubIds = Subscription::select('id')->get();
|
|
|
|
|
|
|
|
foreach ($playlist->user->followers as $follower) {
|
|
|
|
array_push($followerIds, $follower->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($rawSubIds as $sub) {
|
|
|
|
array_push($subIds, $sub->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$targetIds = array_intersect($followerIds, $subIds);
|
|
|
|
return Subscription::whereIn('user_id', $targetIds)->get();
|
2016-05-27 21:12:40 +02:00
|
|
|
default:
|
|
|
|
return $this->fail();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2016-09-30 00:26:31 +02:00
|
|
|
public function newFollower(User $userBeingFollowed, User $follower)
|
|
|
|
{
|
2016-05-27 21:12:40 +02:00
|
|
|
switch ($this->notificationDriver) {
|
|
|
|
case PonyfmDriver::class:
|
2016-12-09 11:53:32 +01:00
|
|
|
case EmailDriver::class:
|
2016-05-27 21:12:40 +02:00
|
|
|
return [$userBeingFollowed];
|
2016-06-12 01:58:10 +02:00
|
|
|
case NativeDriver::class:
|
|
|
|
return Subscription::where('user_id', '=', $userBeingFollowed->id)->get();
|
2016-05-27 21:12:40 +02:00
|
|
|
default:
|
|
|
|
return $this->fail();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2016-09-30 00:26:31 +02:00
|
|
|
public function newComment(Comment $comment)
|
|
|
|
{
|
2016-05-27 21:12:40 +02:00
|
|
|
switch ($this->notificationDriver) {
|
|
|
|
case PonyfmDriver::class:
|
|
|
|
return
|
|
|
|
$comment->user->id === $comment->resource->user->id
|
|
|
|
? []
|
|
|
|
: [$comment->resource->user];
|
2016-06-12 01:58:10 +02:00
|
|
|
case NativeDriver::class:
|
|
|
|
return Subscription::where('user_id', '=', $comment->resource->user->id)->get();
|
2016-05-27 21:12:40 +02:00
|
|
|
default:
|
|
|
|
return $this->fail();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2016-09-30 00:26:31 +02:00
|
|
|
public function newFavourite(Favouritable $entityBeingFavourited, User $favouriter)
|
|
|
|
{
|
2016-05-27 21:12:40 +02:00
|
|
|
switch ($this->notificationDriver) {
|
|
|
|
case PonyfmDriver::class:
|
|
|
|
return
|
|
|
|
$favouriter->id === $entityBeingFavourited->user->id
|
|
|
|
? []
|
|
|
|
: [$entityBeingFavourited->user];
|
2016-06-12 01:58:10 +02:00
|
|
|
case NativeDriver::class:
|
|
|
|
return Subscription::where('user_id', '=', $entityBeingFavourited->user->id)->get();
|
2016-05-27 21:12:40 +02:00
|
|
|
default:
|
|
|
|
return $this->fail();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|