Skip to content

Commit

Permalink
Add support for fof/merge-discussions
Browse files Browse the repository at this point in the history
All the views will be moved to the target discussion when merging.
  • Loading branch information
MichaelBelgium committed Jul 18, 2024
1 parent 54f584d commit 9ccad22
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"flarum/core": "^1.0",
"jaybizzle/crawler-detect": "^1.2"
},
"require-dev": {
"fof/merge-discussions": "^1.0"
},
"license": "MIT",
"authors": [
{
Expand All @@ -30,7 +33,10 @@
"name": "far fa-eye",
"backgroundColor": "#88CC23",
"color": "#fff"
}
},
"optional-dependencies": [
"fof/merge-discussions"
]
},
"flagrow": {
"discuss": "https://discuss.flarum.org/d/26269"
Expand Down
13 changes: 11 additions & 2 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
use Flarum\Discussion\Event\Saving;
use Flarum\Extend\ApiController;
use Flarum\Extend\ApiSerializer;
use Flarum\Extend\Conditional;
use Flarum\Extend\Event;
use FoF\MergeDiscussions\Events\MergingDiscussions;
use Michaelbelgium\Discussionviews\Listeners;
use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Extend\Locales;
use Flarum\Extend\Frontend;
use Flarum\Extend\Model;
Expand Down Expand Up @@ -81,4 +82,12 @@

(new Event())
->listen(Saving::class, Listeners\SaveDiscussionFromModal::class),
];

(new Conditional())
->whenExtensionEnabled('fof-merge-discussions', function() {
return [
(new Event())
->listen(MergingDiscussions::class, Listeners\MergeDiscussionHandler::class),
];
})
];
22 changes: 22 additions & 0 deletions src/Listeners/MergeDiscussionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Michaelbelgium\Discussionviews\Listeners;


use Flarum\Discussion\Discussion;
use FoF\MergeDiscussions\Events\MergingDiscussions;

class MergeDiscussionHandler
{
public function handle(MergingDiscussions $event)
{
$targetDiscussion = $event->discussion;

$targetDiscussion->increment('view_count', $event->mergedDiscussions->sum('view_count'));
$targetDiscussion->save();

$event->mergedDiscussions->each(function(Discussion $discussion) use ($targetDiscussion) {
$discussion->views()->update(['discussion_id' => $targetDiscussion->id]);
});
}
}

0 comments on commit 9ccad22

Please sign in to comment.