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

feat: dispatch a merging event #51

Merged
merged 1 commit into from
Aug 1, 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
7 changes: 6 additions & 1 deletion src/Api/Commands/MergeDiscussionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Flarum\Post\Post;
use Flarum\User\UserRepository;
use FoF\MergeDiscussions\Events\DiscussionWasMerged;
use FoF\MergeDiscussions\Events\MergingDiscussions;
use FoF\MergeDiscussions\Models\Redirection;
use FoF\MergeDiscussions\Validators\MergeDiscussionValidator;
use Illuminate\Database\Eloquent\Collection;
Expand Down Expand Up @@ -93,7 +94,7 @@ public function handle(MergeDiscussion $command)
}

if ($command->merge) {
resolve('db.connection')->transaction(function () use ($discussions, $discussion, $command) {
resolve('db.connection')->transaction(function () use ($discussions, $discussion, $command, $posts) {
try {
// Set the relations using the bumped `number`, so we are sure we won't hit any integrity constraints
$discussion->push();
Expand All @@ -111,6 +112,10 @@ public function handle(MergeDiscussion $command)
}
}

$this->events->dispatch(
new MergingDiscussions($command->actor, $posts, $discussion, $discussions)
);

try {
$discussion
->refresh()
Expand Down
48 changes: 48 additions & 0 deletions src/Events/MergingDiscussions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

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

namespace FoF\MergeDiscussions\Events;

use Flarum\Discussion\Discussion;
use Flarum\Post\Post;
use Flarum\User\User;
use Illuminate\Support\Collection;

class MergingDiscussions
{
/**
* @var User
*/
public $actor;

/**
* @var Post[]|Collection
*/
public $posts;

/**
* @var Discussion
*/
public $discussion;

/**
* @var Discussion[]|Collection Discussion
*/
public $mergedDiscussions;

public function __construct(User $actor, Collection $posts, Discussion $discussion, Collection $mergedDiscussions)
{
$this->actor = $actor;
$this->posts = $posts;
$this->discussion = $discussion;
$this->mergedDiscussions = $mergedDiscussions;
}
}