Skip to content

Commit

Permalink
Offload notifications to the queue (#68)
Browse files Browse the repository at this point in the history
* 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
imorland and StyleCIBot authored Jan 7, 2022
1 parent b6c597c commit 1a35ee8
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 51 deletions.
3 changes: 2 additions & 1 deletion extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@
->listen(Saving::class, Listeners\SaveVotesToDatabase::class)
->listen(Posted::class, Listeners\AddVoteHandler::class)
->listen(Deleted::class, Listeners\RemoveVoteHandler::class)
->listen(Started::class, Listeners\AddDiscussionVotes::class),
->listen(Started::class, Listeners\AddDiscussionVotes::class)
->subscribe(Listeners\QueueJobs::class),

(new Extend\ApiSerializer(Serializer\PostSerializer::class))
->hasMany('upvotes', Serializer\BasicUserSerializer::class)
Expand Down
64 changes: 64 additions & 0 deletions src/Jobs/VoteNotificationsJob.php
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]
);
}
}
}
}
31 changes: 31 additions & 0 deletions src/Listeners/QueueJobs.php
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)
);
}
}
60 changes: 10 additions & 50 deletions src/Listeners/SaveVotesToDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

use Carbon\Carbon;
use Flarum\Foundation\DispatchEventsTrait;
use Flarum\Notification\Notification;
use Flarum\Notification\NotificationSyncer;
use Flarum\Post\Event\Saving;
use Flarum\Post\Exception\FloodingException;
use Flarum\Post\Post;
Expand All @@ -23,7 +21,6 @@
use FoF\Gamification\Events\PostWasVoted;
use FoF\Gamification\Events\UserPointsUpdated;
use FoF\Gamification\Gamification;
use FoF\Gamification\Notification\VoteBlueprint;
use FoF\Gamification\Rank;
use FoF\Gamification\Vote;
use Illuminate\Contracts\Container\Container;
Expand All @@ -39,11 +36,6 @@ class SaveVotesToDatabase
*/
protected $events;

/**
* @var NotificationSyncer
*/
protected $notifications;

/**
* @var Gamification
*/
Expand All @@ -61,14 +53,12 @@ class SaveVotesToDatabase

/**
* @param Dispatcher $events
* @param NotificationSyncer $notifications
* @param Gamification $gamification
* @param SettingsRepositoryInterface $settings
*/
public function __construct(Dispatcher $events, NotificationSyncer $notifications, Gamification $gamification, SettingsRepositoryInterface $settings, Container $container)
public function __construct(Dispatcher $events, Gamification $gamification, SettingsRepositoryInterface $settings, Container $container)
{
$this->events = $events;
$this->notifications = $notifications;
$this->gamification = $gamification;
$this->settings = $settings;
$this->container = $container;
Expand Down Expand Up @@ -145,10 +135,18 @@ public function vote($post, $isDownvoted, $isUpvoted, $actor, $user)

$this->updatePoints($user, $post);

$this->sendData($vote);
if ($voteUser = $vote->post->user) {
$ranks = Rank::where('points', '<=', $voteUser->votes)->pluck('id');

$voteUser->ranks()->sync($ranks);
}

$actor->last_vote_time = Carbon::now();
$actor->save();

$this->events->dispatch(
new PostWasVoted($vote)
);
}

/**
Expand All @@ -173,44 +171,6 @@ public function updatePoints(?User $user, Post $post)
}
}

public function sendData(Vote $vote)
{
$post = $vote->post;
$user = $post->user;

$notif = Notification::query()->where([
'from_user_id' => $vote->user->id,
'type' => 'vote',
'subject_id' => $post->id,
])->first();

if ($notif) {
if ($vote->value === 0) {
$notif->delete();
} else {
$notif->data = $vote->value;
$notif->save();
}
} elseif ($user && $user->id !== $vote->user->id && $vote->value !== 0) {
if ($user->can('canSeeVoters', $post->discussion)) {
$this->notifications->sync(
new VoteBlueprint($vote),
[$user]
);
}
}

$this->events->dispatch(
new PostWasVoted($vote)
);

if ($user) {
$ranks = Rank::where('points', '<=', $user->votes)->pluck('id');

$user->ranks()->sync($ranks);
}
}

public function pushNewVote(Vote $vote)
{
if ($this->container->bound(Pusher::class)) {
Expand Down

0 comments on commit 1a35ee8

Please sign in to comment.