From c9b1759c4136aa0968f103da9a7d0646a4eb78df Mon Sep 17 00:00:00 2001 From: Clark Winkelmann Date: Tue, 6 Apr 2021 22:47:42 +0200 Subject: [PATCH 1/4] Use vote_count attributes instead of loading all votes This massively improves performance on forums with many voters The websocket integration had to be adapted The old includes and websocket package are still available for BC --- extend.php | 12 +++- js/src/forum/addPollToDiscussion.js | 60 +++++++++------- js/src/forum/components/DiscussionPoll.js | 67 ++++++----------- js/src/forum/models/Poll.js | 2 + js/src/forum/models/PollOption.js | 1 + ...06_000000_alter_options_add_vote_count.php | 7 ++ ...4_06_000001_alter_polls_add_vote_count.php | 7 ++ src/Api/Controllers/VotePollController.php | 4 +- src/Api/Serializers/PollOptionSerializer.php | 1 + src/Api/Serializers/PollSerializer.php | 14 ++++ src/Commands/VotePollHandler.php | 71 ++++++++++++++++--- src/Console/RefreshVoteCountCommand.php | 35 +++++++++ src/Poll.php | 28 +++++++- src/PollOption.php | 10 ++- 14 files changed, 230 insertions(+), 89 deletions(-) create mode 100644 migrations/2021_04_06_000000_alter_options_add_vote_count.php create mode 100644 migrations/2021_04_06_000001_alter_polls_add_vote_count.php create mode 100644 src/Console/RefreshVoteCountCommand.php diff --git a/extend.php b/extend.php index e65d0043..e3d723d2 100755 --- a/extend.php +++ b/extend.php @@ -60,11 +60,17 @@ ->addInclude('poll'), (new Extend\ApiController(Controller\ShowDiscussionController::class)) - ->addInclude(['poll', 'poll.options', 'poll.votes', 'poll.votes.user', 'poll.votes.option']), + ->addInclude(['poll', 'poll.options', 'poll.myVotes', 'poll.myVotes.option']) + ->addOptionalInclude(['poll.votes', 'poll.votes.user', 'poll.votes.option']), (new Extend\ApiController(Controller\CreateDiscussionController::class)) - ->addInclude(['poll', 'poll.options', 'poll.votes', 'poll.votes.user', 'poll.votes.option']), + ->addInclude(['poll', 'poll.options', 'poll.myVotes', 'poll.myVotes.option']) + ->addOptionalInclude(['poll.votes', 'poll.votes.user', 'poll.votes.option']), (new Extend\ApiController(Controller\UpdateDiscussionController::class)) - ->addInclude(['poll', 'poll.options', 'poll.votes', 'poll.votes.user', 'poll.votes.option']), + ->addInclude(['poll', 'poll.options', 'poll.myVotes', 'poll.myVotes.option']) + ->addOptionalInclude(['poll.votes', 'poll.votes.user', 'poll.votes.option']), + + (new Extend\Console()) + ->command(Console\RefreshVoteCountCommand::class), ]; diff --git a/js/src/forum/addPollToDiscussion.js b/js/src/forum/addPollToDiscussion.js index 5418d915..f7b5b284 100755 --- a/js/src/forum/addPollToDiscussion.js +++ b/js/src/forum/addPollToDiscussion.js @@ -1,10 +1,7 @@ import { extend } from 'flarum/extend'; import CommentPost from 'flarum/components/CommentPost'; -import Stream from 'flarum/utils/Stream'; import DiscussionPoll from './components/DiscussionPoll'; -// import PollVote from './components/PollVote'; - export default () => { extend(CommentPost.prototype, 'content', function (content) { const discussion = this.attrs.post.discussion(); @@ -12,49 +9,58 @@ export default () => { if (discussion.poll() && this.attrs.post.number() === 1) { content.push( DiscussionPoll.component({ + discussion, poll: discussion.poll(), }) ); } }); - extend(CommentPost.prototype, 'oncreate', function (call, vnode) { - if (app.pusher) { - app.pusher.then((channels) => { - channels.main.bind('newPollVote', (data) => { - var userId = parseInt(data['user_id']); + extend(CommentPost.prototype, 'oninit', function () { + this.subtree.check(() => { + const discussion = this.attrs.post.discussion(); - if (app.session.user && userId == app.session.user.id()) return; + if (!discussion.poll() || this.attrs.post.number() !== 1) { + return ''; + } - let poll = app.store.getById('polls', this.attrs.post.discussion().poll().id()); + // Make the post redraw everytime the poll or option vote count changed, or when the user vote changed + return JSON.stringify([ + discussion.poll().voteCount(), + (discussion.poll().myVotes() || []).map(vote => vote.option().id()), + discussion.poll().options().map(option => option.voteCount()), + ]); + }); + }); - if (parseInt(poll.id()) === parseInt(data['poll_id'])) { - let vote = {}; + extend(CommentPost.prototype, 'oncreate', function (call, vnode) { + if (app.pusher) { + app.pusher.then((channels) => { + // We will listen for updates to all polls and options + // Even if that model is not in the current discussion, it doesn't really matter + channels.main.bind('updatedPollOption', (data) => { + const poll = app.store.getById('polls', data['pollId']); - Object.keys(data).map((key) => { - vote[key] = Stream(data[key]); + if (poll) { + poll.pushAttributes({ + voteCount: data['pollVoteCount'], }); - vote['option'] = Stream(app.store.getById('poll_options', data['option_id'])); - vote['user'] = Stream(app.store.getById('users', data['user_id'])); + // Not redrawing here, as the option below should trigger the redraw already + } - let newVotes = poll.votes(); + const option = app.store.getById('poll_options', data['optionId']); - newVotes.some((vote, i) => { - if (parseInt(vote.user() && vote.user().id()) === userId) { - newVotes.splice(i, 1); - } + if (option) { + option.pushAttributes({ + voteCount: data['optionVoteCount'], }); - newVotes.push(vote); - - poll.votes = Stream(newVotes); - - m.redraw.sync(); + m.redraw(); } }); - extend(vnode, 'onremove', () => channels.main.unbind('newPollVote')); + extend(vnode, 'onremove', () => channels.main.unbind('updatedPollOption')); }); } }); diff --git a/js/src/forum/components/DiscussionPoll.js b/js/src/forum/components/DiscussionPoll.js index 0a47c2f4..aee57321 100755 --- a/js/src/forum/components/DiscussionPoll.js +++ b/js/src/forum/components/DiscussionPoll.js @@ -1,7 +1,6 @@ import Component from 'flarum/Component'; import Button from 'flarum/components/Button'; import LogInModal from 'flarum/components/LogInModal'; -import Stream from 'flarum/utils/Stream'; import ListVotersModal from './ListVotersModal'; export default class DiscussionPoll extends Component { @@ -9,45 +8,29 @@ export default class DiscussionPoll extends Component { super.oninit(vnode); this.poll = this.attrs.poll; - this.vote = Stream(); - this.voted = Stream(false); - this.updateData(); } view() { + const hasVoted = this.myVotes.length > 0; + return (

{this.poll.question()}

{this.options.map((opt) => { - const hasVoted = this.voted(); - const voted = this.vote() && this.vote().option().id() === opt.id(); - const votes = this.votes.filter((v) => v.option().id() === opt.id()).length; - const percent = Math.round((votes / this.poll.votes().length) * 100); - - const attrs = voted - ? { - title: - hasVoted && app.translator.transChoice('fof-polls.forum.tooltip.votes', votes, { count: String(votes) }).join(''), - oncreate: function (vnode) { - $(vnode.dom).tooltip({ placement: 'right' }); - }, - } - : {}; - - const inputAttrs = voted - ? { - checked: true, - } - : {}; + const voted = this.myVotes.some(vote => vote.option() === opt); + const votes = opt.voteCount(); + const percent = Math.round((votes / this.poll.voteCount()) * 100); + + const title = hasVoted && app.translator.transChoice('fof-polls.forum.tooltip.votes', votes, {count: String(votes)}).join(''); return (
-
+
{((!this.poll.hasEnded() && app.session.user && app.session.user.canVotePolls()) || !app.session.user) && ( )} @@ -95,13 +78,8 @@ export default class DiscussionPoll extends Component { } updateData() { - this.poll = app.store.getById('polls', this.poll.id()); this.options = this.poll.options() || []; - this.votes = this.poll.votes() || []; - - this.vote(app.session.user ? this.votes.find((v) => v.user() && v.user().id() === app.session.user.id()) : null); - - this.voted(!!this.vote()); + this.myVotes = this.poll.myVotes() || []; } onError(evt, error) { @@ -117,13 +95,8 @@ export default class DiscussionPoll extends Component { return; } - if (this.vote() && option.id() === this.vote().option().id()) option = null; - - if (!this.vote()) { - this.vote(app.store.createRecord('poll_votes')); - - this.vote().pollId(this.poll.id()); - } + // if we click on our current vote, we want to "un-vote" + if (this.myVotes.some(vote => vote.option() === option)) option = null; app.request({ method: 'PATCH', @@ -137,20 +110,20 @@ export default class DiscussionPoll extends Component { }).then((res) => { app.store.pushPayload(res); - if (!option) app.store.remove(this.vote()); - this.updateData(); - if (!option) { - m.redraw.sync(); - } m.redraw(); }); } showVoters() { - app.modal.show(ListVotersModal, { - poll: this.poll, - }); + // Load all the votes only when opening the votes list + app.store.find('discussions', this.attrs.discussion.id(), { + include: 'poll.votes,poll.votes.user,poll.votes.option', + }).then(() => { + app.modal.show(ListVotersModal, { + poll: this.poll, + }); + }) } } diff --git a/js/src/forum/models/Poll.js b/js/src/forum/models/Poll.js index a5f568d9..cd2fd5e5 100755 --- a/js/src/forum/models/Poll.js +++ b/js/src/forum/models/Poll.js @@ -6,9 +6,11 @@ export default class Poll extends mixin(Model, { hasEnded: Model.attribute('hasEnded'), endDate: Model.attribute('endDate'), publicPoll: Model.attribute('publicPoll'), + voteCount: Model.attribute('voteCount'), options: Model.hasMany('options'), votes: Model.hasMany('votes'), + myVotes: Model.hasMany('myVotes'), }) { apiEndpoint() { return `/fof/polls${this.exists ? `/${this.data.id}` : ''}`; diff --git a/js/src/forum/models/PollOption.js b/js/src/forum/models/PollOption.js index d81652c1..070707b9 100755 --- a/js/src/forum/models/PollOption.js +++ b/js/src/forum/models/PollOption.js @@ -3,6 +3,7 @@ import mixin from 'flarum/utils/mixin'; export default class PollOption extends mixin(Model, { answer: Model.attribute('answer'), + voteCount: Model.attribute('voteCount'), poll: Model.hasOne('polls'), votes: Model.hasMany('votes'), diff --git a/migrations/2021_04_06_000000_alter_options_add_vote_count.php b/migrations/2021_04_06_000000_alter_options_add_vote_count.php new file mode 100644 index 00000000..9c7bf37d --- /dev/null +++ b/migrations/2021_04_06_000000_alter_options_add_vote_count.php @@ -0,0 +1,7 @@ + ['integer', 'unsigned' => true, 'default' => 0], +]); diff --git a/migrations/2021_04_06_000001_alter_polls_add_vote_count.php b/migrations/2021_04_06_000001_alter_polls_add_vote_count.php new file mode 100644 index 00000000..53fcd164 --- /dev/null +++ b/migrations/2021_04_06_000001_alter_polls_add_vote_count.php @@ -0,0 +1,7 @@ + ['integer', 'unsigned' => true, 'default' => 0], +]); diff --git a/src/Api/Controllers/VotePollController.php b/src/Api/Controllers/VotePollController.php index 53ad61bf..a5b8ff84 100755 --- a/src/Api/Controllers/VotePollController.php +++ b/src/Api/Controllers/VotePollController.php @@ -26,7 +26,9 @@ class VotePollController extends AbstractShowController */ public $serializer = PollSerializer::class; - public $include = ['options', 'votes', 'votes.option', 'votes.user']; + public $include = ['options', 'myVotes', 'myVotes.option']; + + public $optionalInclude = ['votes', 'votes.option', 'votes.user']; /** * @var Dispatcher diff --git a/src/Api/Serializers/PollOptionSerializer.php b/src/Api/Serializers/PollOptionSerializer.php index 770291f4..4c70fd7c 100755 --- a/src/Api/Serializers/PollOptionSerializer.php +++ b/src/Api/Serializers/PollOptionSerializer.php @@ -32,6 +32,7 @@ protected function getDefaultAttributes($option) { return [ 'answer' => $option->answer, + 'voteCount' => (int)$option->vote_count, 'createdAt' => $this->formatDate($option->created_at), 'updatedAt' => $this->formatDate($option->updated_at), ]; diff --git a/src/Api/Serializers/PollSerializer.php b/src/Api/Serializers/PollSerializer.php index 6cf316b9..d81e5fd1 100755 --- a/src/Api/Serializers/PollSerializer.php +++ b/src/Api/Serializers/PollSerializer.php @@ -34,6 +34,7 @@ protected function getDefaultAttributes($poll) 'question' => $poll->question, 'hasEnded' => $poll->hasEnded(), 'publicPoll' => (bool) $poll->public_poll, + 'voteCount' => (int) $poll->vote_count, 'endDate' => $this->formatDate($poll->end_date), 'createdAt' => $this->formatDate($poll->created_at), 'updatedAt' => $this->formatDate($poll->updated_at), @@ -55,4 +56,17 @@ public function votes($model) PollVoteSerializer::class ); } + + public function myVotes($model) + { + Poll::setStateUser($this->actor); + + // When called inside ShowDiscussionController, Flarum has already pre-loaded our relationship incorrectly + $model->unsetRelation('myVotes'); + + return $this->hasMany( + $model, + PollVoteSerializer::class + ); + } } diff --git a/src/Commands/VotePollHandler.php b/src/Commands/VotePollHandler.php index b066d7f3..7f97ca26 100755 --- a/src/Commands/VotePollHandler.php +++ b/src/Commands/VotePollHandler.php @@ -15,10 +15,11 @@ use Flarum\User\User; use FoF\Polls\Events\PollWasVoted; use FoF\Polls\Poll; +use FoF\Polls\PollOption; use FoF\Polls\PollVote; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Support\Arr; -use Pusher\Pusher; +use Pusher; class VotePollHandler { @@ -38,9 +39,12 @@ public function __construct(Dispatcher $events) public function handle(VotePoll $command) { /** - * @var User + * @var $actor User */ $actor = $command->actor; + /** + * @var $poll Poll + */ $poll = Poll::findOrFail($command->pollId); $data = $command->data; @@ -53,29 +57,59 @@ public function handle(VotePoll $command) } /** - * @var PollVote|null + * @var $vote PollVote|null */ $vote = $poll->votes()->where('user_id', $actor->id)->first(); + $previousOption = null; + if ($optionId === null && $vote !== null) { + $previousOption = $vote->option; + $vote->delete(); + $vote = null; } elseif ($optionId !== null) { - $vote = $poll->votes()->updateOrCreate([ - 'user_id' => $actor->id, - ], [ - 'option_id' => $optionId, - ]); + if ($vote) { + $previousOption = $vote->option; + + $vote->option_id = $optionId; + $vote->save(); + } else { + $vote = $poll->votes()->create([ + 'user_id' => $actor->id, + 'option_id' => $optionId, + ]); + } + + // Forget the relation in case is was loaded for $previousOption + $vote->unsetRelation('option'); + + $vote->option->refreshVoteCount()->save(); app('events')->dispatch(new PollWasVoted($actor, $poll, $vote, $vote !== null)); $this->pushNewVote($vote); } + $poll->refreshVoteCount()->save(); + + if ($previousOption) { + $previousOption->refreshVoteCount()->save(); + + $this->pushUpdatedOption($previousOption); + } + + if ($vote) { + $this->pushUpdatedOption($vote->option); + } + return $poll; } /** - * @param $vote + * Pushes a new vote through websocket. Kept for backward compatibility, but we are no longer using it + * + * @param PollVote $vote * * @throws \Pusher\PusherException */ @@ -86,6 +120,25 @@ public function pushNewVote($vote) } } + /** + * Pushes an updated option through websocket + * + * @param PollOption $option + * + * @throws \Pusher\PusherException + */ + public function pushUpdatedOption(PollOption $option) + { + if ($pusher = $this->getPusher()) { + $pusher->trigger('public', 'updatedPollOption', [ + 'pollId' => $option->poll->id, + 'pollVoteCount' => $option->poll->vote_count, + 'optionId' => $option->id, + 'optionVoteCount' => $option->vote_count, + ]); + } + } + /** * @throws \Pusher\PusherException * diff --git a/src/Console/RefreshVoteCountCommand.php b/src/Console/RefreshVoteCountCommand.php new file mode 100644 index 00000000..52134fbe --- /dev/null +++ b/src/Console/RefreshVoteCountCommand.php @@ -0,0 +1,35 @@ +output->createProgressBar(Poll::query()->count() + PollOption::query()->count()); + + Poll::query()->each(function (Poll $poll) use ($progress) { + $poll->refreshVoteCount()->save(); + + $progress->advance(); + }); + + PollOption::query()->each(function (PollOption $option) use ($progress) { + $option->refreshVoteCount()->save(); + + $progress->advance(); + }); + + $progress->finish(); + + $this->info('Done.'); + } +} diff --git a/src/Poll.php b/src/Poll.php index 5393ad5a..856fe782 100755 --- a/src/Poll.php +++ b/src/Poll.php @@ -14,18 +14,23 @@ use Flarum\Database\AbstractModel; use Flarum\Discussion\Discussion; use Flarum\User\User; +use Illuminate\Database\Eloquent\Collection; /** * @property int $id * @property string $question * @property bool $public_poll + * @property int $vote_count * @property Discussion $discussion - * @property USer $user + * @property User $user * @property int $discussion_id * @property int $user_id * @property \Carbon\Carbon $end_date * @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $updated_at + * + * @property PollVote[]|Collection $votes + * @property PollVote[]|Collection $myVotes */ class Poll extends AbstractModel { @@ -101,4 +106,25 @@ public function votes() { return $this->hasMany(PollVote::class); } + + public function refreshVoteCount(): self + { + $this->vote_count = $this->votes()->count(); + + return $this; + } + + protected static $stateUser; + + public function myVotes(User $user = null) + { + $user = $user ?: static::$stateUser; + + return $this->votes()->where('user_id', $user ? $user->id : null); + } + + public static function setStateUser(User $user) + { + static::$stateUser = $user; + } } diff --git a/src/PollOption.php b/src/PollOption.php index 429b27c5..956371a8 100755 --- a/src/PollOption.php +++ b/src/PollOption.php @@ -18,6 +18,7 @@ * @property string $answer * @property Poll $poll * @property int $poll_id + * @property int $vote_count * @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $updated_at */ @@ -56,6 +57,13 @@ public function poll() public function votes() { - return $this->hasMany(PollVote::class); + return $this->hasMany(PollVote::class, 'option_id'); + } + + public function refreshVoteCount(): self + { + $this->vote_count = $this->votes()->count(); + + return $this; } } From 9bbec9411a42202ff12622f7c3de5643f054e61c Mon Sep 17 00:00:00 2001 From: Clark Winkelmann Date: Tue, 6 Apr 2021 20:48:06 +0000 Subject: [PATCH 2/4] Apply fixes from StyleCI --- extend.php | 2 +- .../2019_07_01_000000_add_polls_table.php | 2 +- ...19_07_01_000001_add_poll_options_table.php | 2 +- ...2019_07_01_000002_add_poll_votes_table.php | 2 +- ...06_000000_alter_options_add_vote_count.php | 9 +++++++ ...4_06_000001_alter_polls_add_vote_count.php | 9 +++++++ src/Api/Controllers/DeletePollController.php | 2 +- src/Api/Controllers/EditPollController.php | 2 +- src/Api/Controllers/VotePollController.php | 2 +- src/Api/Serializers/PollOptionSerializer.php | 4 +-- src/Api/Serializers/PollSerializer.php | 2 +- src/Api/Serializers/PollVoteSerializer.php | 2 +- src/Commands/DeletePoll.php | 2 +- src/Commands/DeletePollHandler.php | 2 +- src/Commands/EditPoll.php | 2 +- src/Commands/EditPollHandler.php | 2 +- src/Commands/VotePoll.php | 2 +- src/Commands/VotePollHandler.php | 14 +++++------ src/Console/RefreshVoteCountCommand.php | 9 +++++++ src/Events/PollWasCreated.php | 2 +- src/Events/PollWasVoted.php | 2 +- src/Listeners/SavePollsToDatabase.php | 2 +- src/Migrations/AbstractMigration.php | 2 +- src/Poll.php | 25 +++++++++---------- src/PollOption.php | 2 +- src/PollVote.php | 2 +- src/Validators/PollOptionValidator.php | 2 +- src/Validators/PollValidator.php | 2 +- 28 files changed, 70 insertions(+), 44 deletions(-) diff --git a/extend.php b/extend.php index e3d723d2..ba18c0a4 100755 --- a/extend.php +++ b/extend.php @@ -3,7 +3,7 @@ /* * This file is part of fof/polls. * - * Copyright (c) 2019 FriendsOfFlarum. + * Copyright (c) FriendsOfFlarum. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/migrations/2019_07_01_000000_add_polls_table.php b/migrations/2019_07_01_000000_add_polls_table.php index 4e100ed5..02e65fb0 100755 --- a/migrations/2019_07_01_000000_add_polls_table.php +++ b/migrations/2019_07_01_000000_add_polls_table.php @@ -3,7 +3,7 @@ /* * This file is part of fof/polls. * - * Copyright (c) 2019 FriendsOfFlarum. + * Copyright (c) FriendsOfFlarum. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/migrations/2019_07_01_000001_add_poll_options_table.php b/migrations/2019_07_01_000001_add_poll_options_table.php index e25edbf4..a2c31c6d 100755 --- a/migrations/2019_07_01_000001_add_poll_options_table.php +++ b/migrations/2019_07_01_000001_add_poll_options_table.php @@ -3,7 +3,7 @@ /* * This file is part of fof/polls. * - * Copyright (c) 2019 FriendsOfFlarum. + * Copyright (c) FriendsOfFlarum. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/migrations/2019_07_01_000002_add_poll_votes_table.php b/migrations/2019_07_01_000002_add_poll_votes_table.php index 10a1573f..aa194f4f 100755 --- a/migrations/2019_07_01_000002_add_poll_votes_table.php +++ b/migrations/2019_07_01_000002_add_poll_votes_table.php @@ -3,7 +3,7 @@ /* * This file is part of fof/polls. * - * Copyright (c) 2019 FriendsOfFlarum. + * Copyright (c) FriendsOfFlarum. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/migrations/2021_04_06_000000_alter_options_add_vote_count.php b/migrations/2021_04_06_000000_alter_options_add_vote_count.php index 9c7bf37d..07a55cd6 100644 --- a/migrations/2021_04_06_000000_alter_options_add_vote_count.php +++ b/migrations/2021_04_06_000000_alter_options_add_vote_count.php @@ -1,5 +1,14 @@ $option->answer, - 'voteCount' => (int)$option->vote_count, + 'voteCount' => (int) $option->vote_count, 'createdAt' => $this->formatDate($option->created_at), 'updatedAt' => $this->formatDate($option->updated_at), ]; diff --git a/src/Api/Serializers/PollSerializer.php b/src/Api/Serializers/PollSerializer.php index d81e5fd1..8d468229 100755 --- a/src/Api/Serializers/PollSerializer.php +++ b/src/Api/Serializers/PollSerializer.php @@ -3,7 +3,7 @@ /* * This file is part of fof/polls. * - * Copyright (c) 2019 FriendsOfFlarum. + * Copyright (c) FriendsOfFlarum. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Api/Serializers/PollVoteSerializer.php b/src/Api/Serializers/PollVoteSerializer.php index 337d2b39..684aead7 100755 --- a/src/Api/Serializers/PollVoteSerializer.php +++ b/src/Api/Serializers/PollVoteSerializer.php @@ -3,7 +3,7 @@ /* * This file is part of fof/polls. * - * Copyright (c) 2019 FriendsOfFlarum. + * Copyright (c) FriendsOfFlarum. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Commands/DeletePoll.php b/src/Commands/DeletePoll.php index ed688e88..d85c60e7 100755 --- a/src/Commands/DeletePoll.php +++ b/src/Commands/DeletePoll.php @@ -3,7 +3,7 @@ /* * This file is part of fof/polls. * - * Copyright (c) 2019 FriendsOfFlarum. + * Copyright (c) FriendsOfFlarum. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Commands/DeletePollHandler.php b/src/Commands/DeletePollHandler.php index 5e3aef9e..3716dd01 100755 --- a/src/Commands/DeletePollHandler.php +++ b/src/Commands/DeletePollHandler.php @@ -3,7 +3,7 @@ /* * This file is part of fof/polls. * - * Copyright (c) 2019 FriendsOfFlarum. + * Copyright (c) FriendsOfFlarum. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Commands/EditPoll.php b/src/Commands/EditPoll.php index 0292ebe5..465bbe06 100755 --- a/src/Commands/EditPoll.php +++ b/src/Commands/EditPoll.php @@ -3,7 +3,7 @@ /* * This file is part of fof/polls. * - * Copyright (c) 2019 FriendsOfFlarum. + * Copyright (c) FriendsOfFlarum. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Commands/EditPollHandler.php b/src/Commands/EditPollHandler.php index 0c90ef6d..c1b622ca 100755 --- a/src/Commands/EditPollHandler.php +++ b/src/Commands/EditPollHandler.php @@ -3,7 +3,7 @@ /* * This file is part of fof/polls. * - * Copyright (c) 2019 FriendsOfFlarum. + * Copyright (c) FriendsOfFlarum. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Commands/VotePoll.php b/src/Commands/VotePoll.php index 6e00be6b..ae2740b2 100755 --- a/src/Commands/VotePoll.php +++ b/src/Commands/VotePoll.php @@ -3,7 +3,7 @@ /* * This file is part of fof/polls. * - * Copyright (c) 2019 FriendsOfFlarum. + * Copyright (c) FriendsOfFlarum. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Commands/VotePollHandler.php b/src/Commands/VotePollHandler.php index 7f97ca26..39c52994 100755 --- a/src/Commands/VotePollHandler.php +++ b/src/Commands/VotePollHandler.php @@ -3,7 +3,7 @@ /* * This file is part of fof/polls. * - * Copyright (c) 2019 FriendsOfFlarum. + * Copyright (c) FriendsOfFlarum. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -76,7 +76,7 @@ public function handle(VotePoll $command) $vote->save(); } else { $vote = $poll->votes()->create([ - 'user_id' => $actor->id, + 'user_id' => $actor->id, 'option_id' => $optionId, ]); } @@ -107,7 +107,7 @@ public function handle(VotePoll $command) } /** - * Pushes a new vote through websocket. Kept for backward compatibility, but we are no longer using it + * Pushes a new vote through websocket. Kept for backward compatibility, but we are no longer using it. * * @param PollVote $vote * @@ -121,7 +121,7 @@ public function pushNewVote($vote) } /** - * Pushes an updated option through websocket + * Pushes an updated option through websocket. * * @param PollOption $option * @@ -131,9 +131,9 @@ public function pushUpdatedOption(PollOption $option) { if ($pusher = $this->getPusher()) { $pusher->trigger('public', 'updatedPollOption', [ - 'pollId' => $option->poll->id, - 'pollVoteCount' => $option->poll->vote_count, - 'optionId' => $option->id, + 'pollId' => $option->poll->id, + 'pollVoteCount' => $option->poll->vote_count, + 'optionId' => $option->id, 'optionVoteCount' => $option->vote_count, ]); } diff --git a/src/Console/RefreshVoteCountCommand.php b/src/Console/RefreshVoteCountCommand.php index 52134fbe..7f5a99ea 100644 --- a/src/Console/RefreshVoteCountCommand.php +++ b/src/Console/RefreshVoteCountCommand.php @@ -1,5 +1,14 @@ Date: Wed, 7 Apr 2021 00:06:13 +0200 Subject: [PATCH 3/4] Use constructor injection where possible --- src/Commands/VotePollHandler.php | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Commands/VotePollHandler.php b/src/Commands/VotePollHandler.php index 39c52994..a27edec8 100755 --- a/src/Commands/VotePollHandler.php +++ b/src/Commands/VotePollHandler.php @@ -11,8 +11,8 @@ namespace FoF\Polls\Commands; +use Flarum\Settings\SettingsRepositoryInterface; use Flarum\User\Exception\PermissionDeniedException; -use Flarum\User\User; use FoF\Polls\Events\PollWasVoted; use FoF\Polls\Poll; use FoF\Polls\PollOption; @@ -28,19 +28,23 @@ class VotePollHandler */ private $events; + /** + * @var SettingsRepositoryInterface + */ + private $settings; + /** * @param Dispatcher $events + * @param SettingsRepositoryInterface $settings */ - public function __construct(Dispatcher $events) + public function __construct(Dispatcher $events, SettingsRepositoryInterface $settings) { $this->events = $events; + $this->settings = $settings; } public function handle(VotePoll $command) { - /** - * @var $actor User - */ $actor = $command->actor; /** * @var $poll Poll @@ -153,18 +157,16 @@ private function getPusher() if (app()->bound(Pusher::class)) { return app(Pusher::class); } else { - $settings = app('flarum.settings'); - $options = []; - if ($cluster = $settings->get('flarum-pusher.app_cluster')) { + if ($cluster = $this->settings->get('flarum-pusher.app_cluster')) { $options['cluster'] = $cluster; } return new Pusher( - $settings->get('flarum-pusher.app_key'), - $settings->get('flarum-pusher.app_secret'), - $settings->get('flarum-pusher.app_id'), + $this->settings->get('flarum-pusher.app_key'), + $this->settings->get('flarum-pusher.app_secret'), + $this->settings->get('flarum-pusher.app_id'), $options ); } From c6a67096bc66c10bfe4a901ce081feda1be2792b Mon Sep 17 00:00:00 2001 From: Clark Winkelmann Date: Tue, 6 Apr 2021 22:06:23 +0000 Subject: [PATCH 4/4] Apply fixes from StyleCI --- src/Commands/VotePollHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/VotePollHandler.php b/src/Commands/VotePollHandler.php index a27edec8..79acf794 100755 --- a/src/Commands/VotePollHandler.php +++ b/src/Commands/VotePollHandler.php @@ -34,7 +34,7 @@ class VotePollHandler private $settings; /** - * @param Dispatcher $events + * @param Dispatcher $events * @param SettingsRepositoryInterface $settings */ public function __construct(Dispatcher $events, SettingsRepositoryInterface $settings)