Skip to content

Commit

Permalink
Use new model extender
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelBelgium committed May 21, 2020
1 parent 8f92f25 commit c2a7709
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Add views to flarum discussions",
"type": "flarum-extension",
"require": {
"flarum/core": "^0.1.0-beta.8"
"flarum/core": "^0.1.0-beta.13"
},
"license": "MIT",
"authors": [
Expand Down
14 changes: 14 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
<?php

use Flarum\Database\AbstractModel;
use Flarum\Discussion\Discussion;
use Michaelbelgium\Discussionviews\Listeners;
use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Extend\Locales;
use Flarum\Extend\Frontend;
use Flarum\Extend\Model;
use Flarum\Settings\SettingsRepositoryInterface;
use Michaelbelgium\Discussionviews\Listeners\AddRelationship;
use Michaelbelgium\Discussionviews\Models\DiscussionView;

$settings = app(SettingsRepositoryInterface::class);

return [
(new Frontend('forum'))
Expand All @@ -15,6 +23,12 @@

new Locales(__DIR__ . '/locale'),

(new Model(Discussion::class))->relationship(AddRelationship::RELATIONSHIP, function (AbstractModel $model) {
return $model->hasMany(DiscussionView::class)->orderBy('visited_at', 'DESC');
})->relationship(AddRelationship::RELATIONSHIP_LATEST, function (AbstractModel $model) use ($settings) {
return $model->views()->limit($settings->get('michaelbelgium-discussionviews.max_listcount', 5));
}),

function (Dispatcher $events) {
$events->subscribe(Listeners\AddRelationship::class);
$events->subscribe(Listeners\AddDiscussionViewHandler::class);
Expand Down
22 changes: 0 additions & 22 deletions src/Listeners/AddRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,25 @@
use Flarum\Api\Event\WillGetData;
use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Api\Event\Serializing;
use Flarum\Discussion\Discussion;
use Flarum\Event\GetApiRelationship;
use Flarum\Event\GetModelRelationship;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Events\Dispatcher;
use Michaelbelgium\Discussionviews\Models\DiscussionView;
use Michaelbelgium\Discussionviews\Serializers\DiscussionViewSerializer;

class AddRelationship
{
const RELATIONSHIP = 'views'; //$discussion->views()
const RELATIONSHIP_LATEST = 'latestViews';

private $settings;

public function __construct(SettingsRepositoryInterface $settings) {
$this->settings = $settings;
}

/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(GetModelRelationship::class, [$this, "addRelationship"]);
$events->listen(GetApiRelationship::class, [$this, 'addApiRelationship']);
$events->listen(WillGetData::class, [$this, 'includeRelationship']);
$events->listen(Serializing::class, [$this, 'prepareApiAttributes']);
}

public function addRelationship(GetModelRelationship $event)
{
if($event->isRelationship(Discussion::class, self::RELATIONSHIP)) {
return $event->model->hasMany(DiscussionView::class)->orderBy('visited_at', 'DESC');
}

if($event->isRelationship(Discussion::class, self::RELATIONSHIP_LATEST)) {
return $event->model->views()->limit($this->settings->get('michaelbelgium-discussionviews.max_listcount', 5));
}
}

public function addApiRelationship(GetApiRelationship $event)
{
if($event->isRelationship(DiscussionSerializer::class, self::RELATIONSHIP_LATEST)) {
Expand Down

0 comments on commit c2a7709

Please sign in to comment.