Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WillReactToPost event #61

Merged
merged 4 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/Event/WillReactToPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of fof/reactions.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\Reactions\Event;

use Flarum\Post\Post;
use Flarum\User\User;
use FoF\Reactions\Reaction;

class WillReactToPost
{
/**
* @var Post
*/
public $post;

/**
* @var User
*/
public $user;

/**
* @var Reaction
*/
public $reaction;

/**
* @var bool
*/
public $changed;

/**
* WillReactToPost constructor.
*
* @param Post $post
* @param User $user
* @param $reaction
* @param bool $changed
*/
public function __construct(Post $post, User $user, Reaction $reaction, $changed = false)
{
$this->post = $post;
$this->user = $user;
$this->reaction = $reaction;
}
}
13 changes: 11 additions & 2 deletions src/Listener/SaveReactionsToDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
use FoF\Gamification\Listeners\SaveVotesToDatabase;
use FoF\Reactions\Event\PostWasReacted;
use FoF\Reactions\Event\PostWasUnreacted;
use FoF\Reactions\Event\WillReactToPost;
use FoF\Reactions\PostReaction;
use FoF\Reactions\Reaction;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
use Pusher;
use Symfony\Contracts\Translation\TranslatorInterface;
Expand All @@ -44,11 +46,17 @@ class SaveReactionsToDatabase
*/
protected $extensions;

public function __construct(SettingsRepositoryInterface $settings, TranslatorInterface $translator, ExtensionManager $extensions)
/**
* @var Dispatcher
*/
protected $events;

public function __construct(SettingsRepositoryInterface $settings, TranslatorInterface $translator, ExtensionManager $extensions, Dispatcher $events)
{
$this->settings = $settings;
$this->translator = $translator;
$this->extensions = $extensions;
$this->events = $events;
}

/**
Expand All @@ -66,12 +74,13 @@ public function handle(Saving $event)
$actor = $event->actor;

$reactionId = Arr::get($data, 'attributes.reaction');
$reactionIdentifier = null;

$actor->assertCan('react', $post);

$reaction = !is_null($reactionId) ? Reaction::where('id', $reactionId)->first() : null;

$this->events->dispatch(new WillReactToPost($post, $actor, $reaction));

$gamification = $this->extensions->isEnabled('fof-gamification');
$likes = $this->extensions->isEnabled('flarum-likes');

Expand Down