-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Offload notifications to the queue (#68)
* Offload notifications to the queue * Apply fixes from StyleCI * Remove syncer from save class * Dispatch event last Co-authored-by: StyleCI Bot <[email protected]>
- Loading branch information
1 parent
b6c597c
commit 1a35ee8
Showing
4 changed files
with
107 additions
and
51 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
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,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fof/gamification. | ||
* | ||
* Copyright (c) FriendsOfFlarum. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FoF\Gamification\Jobs; | ||
|
||
use Flarum\Notification\Notification; | ||
use Flarum\Notification\NotificationSyncer; | ||
use FoF\Gamification\Notification\VoteBlueprint; | ||
use FoF\Gamification\Vote; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class VoteNotificationsJob implements ShouldQueue | ||
{ | ||
use Queueable; | ||
use SerializesModels; | ||
|
||
/** | ||
* @var Vote | ||
*/ | ||
protected $vote; | ||
|
||
public function __construct(Vote $vote) | ||
{ | ||
$this->vote = $vote; | ||
} | ||
|
||
public function handle(NotificationSyncer $notifications) | ||
{ | ||
$post = $this->vote->post; | ||
$user = $post->user; | ||
|
||
$notif = Notification::query()->where([ | ||
'from_user_id' => $this->vote->user->id, | ||
'type' => 'vote', | ||
'subject_id' => $post->id, | ||
])->first(); | ||
|
||
if ($notif) { | ||
if ($this->vote->value === 0) { | ||
$notif->delete(); | ||
} else { | ||
$notif->data = $this->vote->value; | ||
$notif->save(); | ||
} | ||
} elseif ($user && $user->id !== $this->vote->user->id && $this->vote->value !== 0) { | ||
if ($user->can('canSeeVoters', $post->discussion)) { | ||
$notifications->sync( | ||
new VoteBlueprint($this->vote), | ||
[$user] | ||
); | ||
} | ||
} | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fof/gamification. | ||
* | ||
* Copyright (c) FriendsOfFlarum. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FoF\Gamification\Listeners; | ||
|
||
use FoF\Gamification\Events\PostWasVoted; | ||
use FoF\Gamification\Jobs; | ||
use Illuminate\Contracts\Events\Dispatcher; | ||
|
||
class QueueJobs | ||
{ | ||
public function subscribe(Dispatcher $events) | ||
{ | ||
$events->listen(PostWasVoted::class, [$this, 'notifications']); | ||
} | ||
|
||
public function notifications(PostWasVoted $event) | ||
{ | ||
resolve('flarum.queue.connection')->push( | ||
new Jobs\VoteNotificationsJob($event->vote) | ||
); | ||
} | ||
} |
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