Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8eb9d1
commit 66fc118
Showing
9 changed files
with
189 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Events; | ||
|
||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class ThreadReceivedNewReply | ||
{ | ||
use Dispatchable, SerializesModels; | ||
|
||
public $reply; | ||
|
||
/** | ||
* Create a new event instance. | ||
* | ||
* @param $reply | ||
*/ | ||
public function __construct($reply) | ||
{ | ||
$this->reply = $reply; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Listeners; | ||
|
||
use App\Events\ThreadReceivedNewReply; | ||
use App\Notifications\YouWereMentioned; | ||
use App\User; | ||
|
||
class NotifyMentionedUsers | ||
{ | ||
/** | ||
* Handle the event. | ||
* | ||
* @param ThreadReceivedNewReply $event | ||
* @return void | ||
*/ | ||
public function handle(ThreadReceivedNewReply $event) | ||
{ | ||
collect($event->reply->mentionedUsers()) | ||
->map(function ($name) { | ||
return User::where('name', $name)->first(); | ||
}) | ||
->filter() | ||
->each(function ($user) use ($event) { | ||
$user->notify(new YouWereMentioned($event->reply)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Listeners; | ||
|
||
use App\Events\ThreadReceivedNewReply; | ||
|
||
class NotifySubscribers | ||
{ | ||
/** | ||
* Handle the event. | ||
* | ||
* @param ThreadReceivedNewReply $event | ||
* @return void | ||
*/ | ||
public function handle(ThreadReceivedNewReply $event) | ||
{ | ||
$thread = $event->reply->thread; | ||
|
||
$thread->subscriptions | ||
->where('user_id', '!=', $event->reply->user_id) | ||
->each | ||
->notify($event->reply); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace App\Notifications; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Notifications\Notification; | ||
|
||
class YouWereMentioned extends Notification | ||
{ | ||
use Queueable; | ||
|
||
/** | ||
* @var \App\Reply | ||
*/ | ||
protected $reply; | ||
|
||
/** | ||
* Create a new notification instance. | ||
* | ||
* @param \App\Reply $reply | ||
*/ | ||
public function __construct($reply) | ||
{ | ||
$this->reply = $reply; | ||
} | ||
|
||
/** | ||
* Get the notification's delivery channels. | ||
* | ||
* @param mixed $notifiable | ||
* @return array | ||
*/ | ||
public function via($notifiable) | ||
{ | ||
return ['database']; | ||
} | ||
|
||
/** | ||
* Get the array representation of the notification. | ||
* | ||
* @param mixed $notifiable | ||
* @return array | ||
*/ | ||
public function toArray($notifiable) | ||
{ | ||
return [ | ||
'message' => $this->reply->owner->name . ' mentioned you in ' . $this->reply->thread->title, | ||
'link' => $this->reply->path() | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use Illuminate\Foundation\Testing\DatabaseMigrations; | ||
use Tests\TestCase; | ||
|
||
class MentionUsersTest extends TestCase | ||
{ | ||
use DatabaseMigrations; | ||
|
||
/** @test */ | ||
function mentioned_users_in_a_reply_are_notified() | ||
{ | ||
// Given we have a user, JohnDoe, who is signed in. | ||
$john = create('App\User', ['name' => 'JohnDoe']); | ||
|
||
$this->signIn($john); | ||
|
||
// And we also have a user, JaneDoe. | ||
$jane = create('App\User', ['name' => 'JaneDoe']); | ||
|
||
// If we have a thread | ||
$thread = create('App\Thread'); | ||
|
||
// And JohnDoe replies to that thread and mentions @JaneDoe. | ||
$reply = make('App\Reply', [ | ||
'body' => 'Hey @JaneDoe check this out.' | ||
]); | ||
|
||
$this->json('post', $thread->path() . '/replies', $reply->toArray()); | ||
|
||
// Then @JaneDoe should receive a notification. | ||
$this->assertCount(1, $jane->notifications); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters