1
0
mirror of https://github.com/flarum/core.git synced 2025-10-18 10:16:09 +02:00

Implement notifications

This commit is contained in:
Toby Zerner
2015-03-24 15:07:38 +10:30
parent 1d1025dcd2
commit 4a1550215c
34 changed files with 808 additions and 38 deletions

View File

@@ -0,0 +1,28 @@
<?php namespace Flarum\Core\Handlers\Commands;
use Flarum\Core\Models\Notification;
use Flarum\Core\Exceptions\PermissionDeniedException;
use Flarum\Core\Support\DispatchesEvents;
class ReadNotificationCommandHandler
{
use DispatchesEvents;
public function handle($command)
{
$user = $command->user;
if (! $user->exists) {
throw new PermissionDeniedException;
}
$notification = Notification::where('user_id', $user->id)->findOrFail($command->notificationId);
$notification->read();
$notification->save();
$this->dispatchEventsFor($notification);
return $notification;
}
}

View File

@@ -0,0 +1,61 @@
<?php namespace Flarum\Core\Handlers\Events;
use Flarum\Core\Events\DiscussionWasRenamed;
use Flarum\Core\Models\RenamedPost;
use Flarum\Core\Models\Notification;
class DiscussionRenamedNotifier
{
/**
* Register the listeners for the subscriber.
*
* @param Illuminate\Events\Dispatcher $events
* @return array
*/
public function subscribe($events)
{
$events->listen('Flarum\Core\Events\DiscussionWasRenamed', __CLASS__.'@whenDiscussionWasRenamed');
}
public function whenDiscussionWasRenamed(DiscussionWasRenamed $event)
{
$post = $this->createRenamedPost($event);
$event->discussion->postWasAdded($post);
$this->createRenamedNotification($event, $post);
}
protected function createRenamedPost(DiscussionWasRenamed $event)
{
$post = RenamedPost::reply(
$event->discussion->id,
$event->user->id,
$event->oldTitle,
$event->discussion->title
);
$post->save();
return $post;
}
protected function createRenamedNotification(DiscussionWasRenamed $event, RenamedPost $post)
{
if ($event->discussion->start_user_id === $event->user->id) {
return false;
}
$notification = Notification::notify(
$event->discussion->start_user_id,
'renamed',
$event->user->id,
$event->discussion->id,
['number' => $post->number, 'oldTitle' => $event->oldTitle]
);
$notification->save();
return $notification;
}
}

View File

@@ -1,32 +0,0 @@
<?php namespace Flarum\Core\Handlers\Events;
use Flarum\Core\Events\DiscussionWasRenamed;
use Flarum\Core\Models\RenamedPost;
class RenamedPostCreator
{
/**
* Register the listeners for the subscriber.
*
* @param Illuminate\Events\Dispatcher $events
* @return array
*/
public function subscribe($events)
{
$events->listen('Flarum\Core\Events\DiscussionWasRenamed', __CLASS__.'@whenDiscussionWasRenamed');
}
public function whenDiscussionWasRenamed(DiscussionWasRenamed $event)
{
$post = RenamedPost::reply(
$event->discussion->id,
$event->user->id,
$event->oldTitle,
$event->discussion->title
);
$post->save();
$event->discussion->postWasAdded($post);
}
}