Use new Model extender

This commit is contained in:
Franz Liedke 2020-04-24 15:55:19 +02:00
parent 92b18063f5
commit cf764d2c00
3 changed files with 9 additions and 40 deletions

@ -8,12 +8,14 @@
*/
use Flarum\Api\Event\Serializing;
use Flarum\Event\ConfigureModelDates;
use Flarum\Extend;
use Flarum\Flags\Api\Controller\CreateFlagController;
use Flarum\Flags\Api\Controller\DeleteFlagsController;
use Flarum\Flags\Api\Controller\ListFlagsController;
use Flarum\Flags\Flag;
use Flarum\Flags\Listener;
use Flarum\Post\Post;
use Flarum\User\User;
use Illuminate\Contracts\Events\Dispatcher;
return [
@ -29,8 +31,13 @@ return [
->post('/flags', 'flags.create', CreateFlagController::class)
->delete('/posts/{id}/flags', 'flags.delete', DeleteFlagsController::class),
(new Extend\Model(User::class))
->dateAttribute('read_flags_at'),
(new Extend\Model(Post::class))
->hasMany('flags', Flag::class, 'post_id'),
function (Dispatcher $events) {
$events->listen(ConfigureModelDates::class, Listener\AddFlagsApiDates::class);
$events->listen(Serializing::class, Listener\AddFlagsApiAttributes::class);
$events->subscribe(Listener\AddPostFlagsRelationship::class);

@ -1,23 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Flags\Listener;
use Flarum\Event\ConfigureModelDates;
use Flarum\User\User;
class AddFlagsApiDates
{
public function handle(ConfigureModelDates $event)
{
if ($event->isModel(User::class)) {
$event->dates[] = 'read_flags_at';
}
}
}

@ -14,12 +14,9 @@ use Flarum\Api\Event\WillGetData;
use Flarum\Api\Event\WillSerializeData;
use Flarum\Api\Serializer\PostSerializer;
use Flarum\Event\GetApiRelationship;
use Flarum\Event\GetModelRelationship;
use Flarum\Flags\Api\Controller\CreateFlagController;
use Flarum\Flags\Api\Serializer\FlagSerializer;
use Flarum\Flags\Flag;
use Flarum\Post\Event\Deleted;
use Flarum\Post\Post;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Eloquent\Collection;
@ -30,24 +27,12 @@ class AddPostFlagsRelationship
*/
public function subscribe(Dispatcher $events)
{
$events->listen(GetModelRelationship::class, [$this, 'getModelRelationship']);
$events->listen(Deleted::class, [$this, 'postWasDeleted']);
$events->listen(GetApiRelationship::class, [$this, 'getApiRelationship']);
$events->listen(WillGetData::class, [$this, 'includeFlagsRelationship']);
$events->listen(WillSerializeData::class, [$this, 'prepareApiData']);
}
/**
* @param GetModelRelationship $event
* @return \Illuminate\Database\Eloquent\Relations\HasMany|null
*/
public function getModelRelationship(GetModelRelationship $event)
{
if ($event->isRelationship(Post::class, 'flags')) {
return $event->model->hasMany(Flag::class, 'post_id');
}
}
/**
* @param Deleted $event
*/