Skip to content

Commit

Permalink
Add WillReactToPost event (#61)
Browse files Browse the repository at this point in the history
* remove dead code

* Add `WillReactToPost` event

* fix code style

* fix import order
  • Loading branch information
iPurpl3x authored Mar 9, 2023
1 parent 734bcd9 commit c4e3412
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
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

0 comments on commit c4e3412

Please sign in to comment.