mirror of
https://github.com/flarum/core.git
synced 2025-08-11 10:55:47 +02:00
Update for composer branch
This commit is contained in:
57
extensions/flags/src/Api/Controller/CreateFlagController.php
Normal file
57
extensions/flags/src/Api/Controller/CreateFlagController.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Api\Controller;
|
||||
|
||||
use Flarum\Api\Controller\AbstractCreateController;
|
||||
use Flarum\Flags\Api\Serializer\FlagSerializer;
|
||||
use Flarum\Flags\Command\CreateFlag;
|
||||
use Illuminate\Contracts\Bus\Dispatcher;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Tobscure\JsonApi\Document;
|
||||
|
||||
class CreateFlagController extends AbstractCreateController
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $serializer = FlagSerializer::class;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $include = [
|
||||
'post',
|
||||
'post.flags'
|
||||
];
|
||||
|
||||
/**
|
||||
* @var Dispatcher
|
||||
*/
|
||||
protected $bus;
|
||||
|
||||
/**
|
||||
* @param Dispatcher $bus
|
||||
*/
|
||||
public function __construct(Dispatcher $bus)
|
||||
{
|
||||
$this->bus = $bus;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function data(ServerRequestInterface $request, Document $document)
|
||||
{
|
||||
return $this->bus->dispatch(
|
||||
new CreateFlag($request->getAttribute('actor'), array_get($request->getParsedBody(), 'data', []))
|
||||
);
|
||||
}
|
||||
}
|
@@ -8,14 +8,14 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Api;
|
||||
namespace Flarum\Flags\Api\Controller;
|
||||
|
||||
use Flarum\Flags\Commands\DeleteFlags;
|
||||
use Flarum\Api\Actions\DeleteAction as BaseDeleteAction;
|
||||
use Flarum\Api\Request;
|
||||
use Flarum\Api\Controller\AbstractDeleteController;
|
||||
use Flarum\Flags\Command\DeleteFlags;
|
||||
use Illuminate\Contracts\Bus\Dispatcher;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class DeleteAction extends BaseDeleteAction
|
||||
class DeleteFlagsController extends AbstractDeleteController
|
||||
{
|
||||
/**
|
||||
* @var Dispatcher
|
||||
@@ -31,14 +31,12 @@ class DeleteAction extends BaseDeleteAction
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete flags for a post.
|
||||
*
|
||||
* @param Request $request
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function delete(Request $request)
|
||||
protected function delete(ServerRequestInterface $request)
|
||||
{
|
||||
$this->bus->dispatch(
|
||||
new DeleteFlags($request->get('id'), $request->actor, $request->all())
|
||||
new DeleteFlags(array_get($request->getQueryParams(), 'id'), $request->getAttribute('actor'), $request->getParsedBody())
|
||||
);
|
||||
}
|
||||
}
|
52
extensions/flags/src/Api/Controller/ListFlagsController.php
Normal file
52
extensions/flags/src/Api/Controller/ListFlagsController.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Api\Controller;
|
||||
|
||||
use Flarum\Api\Controller\AbstractCollectionController;
|
||||
use Flarum\Flags\Api\Serializer\FlagSerializer;
|
||||
use Flarum\Flags\Flag;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Tobscure\JsonApi\Document;
|
||||
|
||||
class ListFlagsController extends AbstractCollectionController
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $serializer = FlagSerializer::class;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $include = [
|
||||
'user',
|
||||
'post',
|
||||
'post.user',
|
||||
'post.discussion'
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function data(ServerRequestInterface $request, Document $document)
|
||||
{
|
||||
$actor = $request->getAttribute('actor');
|
||||
|
||||
$actor->flags_read_time = time();
|
||||
$actor->save();
|
||||
|
||||
return Flag::whereVisibleTo($actor)
|
||||
->with($this->extractInclude($request))
|
||||
->latest('flags.time')
|
||||
->groupBy('post_id')
|
||||
->get();
|
||||
}
|
||||
}
|
@@ -1,58 +0,0 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Api;
|
||||
|
||||
use Flarum\Flags\Commands\CreateFlag;
|
||||
use Flarum\Api\Actions\CreateAction as BaseCreateAction;
|
||||
use Flarum\Api\JsonApiRequest;
|
||||
use Illuminate\Contracts\Bus\Dispatcher;
|
||||
|
||||
class CreateAction extends BaseCreateAction
|
||||
{
|
||||
/**
|
||||
* @var Dispatcher
|
||||
*/
|
||||
protected $bus;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public $serializer = 'Flarum\Flags\Api\FlagSerializer';
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public $include = [
|
||||
'post' => true,
|
||||
'post.flags' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* @param Dispatcher $bus
|
||||
*/
|
||||
public function __construct(Dispatcher $bus)
|
||||
{
|
||||
$this->bus = $bus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a flag according to input from the API request.
|
||||
*
|
||||
* @param JsonApiRequest $request
|
||||
* @return \Flarum\Flags\Flag
|
||||
*/
|
||||
protected function create(JsonApiRequest $request)
|
||||
{
|
||||
return $this->bus->dispatch(
|
||||
new CreateFlag($request->actor, $request->get('data'))
|
||||
);
|
||||
}
|
||||
}
|
@@ -1,53 +0,0 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Api;
|
||||
|
||||
use Flarum\Api\Actions\SerializeCollectionAction;
|
||||
use Flarum\Api\JsonApiRequest;
|
||||
use Flarum\Flags\Flag;
|
||||
use Tobscure\JsonApi\Document;
|
||||
|
||||
class IndexAction extends SerializeCollectionAction
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public $serializer = 'Flarum\Flags\Api\FlagSerializer';
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public $include = [
|
||||
'user' => true,
|
||||
'post' => true,
|
||||
'post.user' => true,
|
||||
'post.discussion' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public $link = [];
|
||||
|
||||
protected function data(JsonApiRequest $request, Document $document)
|
||||
{
|
||||
$actor = $request->actor;
|
||||
|
||||
$actor->flags_read_time = time();
|
||||
$actor->save();
|
||||
|
||||
return Flag::whereVisibleTo($actor)
|
||||
->with($request->include)
|
||||
->latest('flags.time')
|
||||
->groupBy('post_id')
|
||||
->get();
|
||||
}
|
||||
}
|
@@ -8,14 +8,22 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Api;
|
||||
namespace Flarum\Flags\Api\Serializer;
|
||||
|
||||
use Flarum\Api\Serializers\Serializer;
|
||||
use Flarum\Api\Serializer\AbstractSerializer;
|
||||
use Flarum\Api\Serializer\PostSerializer;
|
||||
use Flarum\Api\Serializer\UserBasicSerializer;
|
||||
|
||||
class FlagSerializer extends Serializer
|
||||
class FlagSerializer extends AbstractSerializer
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $type = 'flags';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getDefaultAttributes($flag)
|
||||
{
|
||||
return [
|
||||
@@ -25,13 +33,19 @@ class FlagSerializer extends Serializer
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Flarum\Api\Relationship\HasOneBuilder
|
||||
*/
|
||||
protected function post()
|
||||
{
|
||||
return $this->hasOne('Flarum\Api\Serializers\PostSerializer');
|
||||
return $this->hasOne(PostSerializer::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Flarum\Api\Relationship\HasOneBuilder
|
||||
*/
|
||||
protected function user()
|
||||
{
|
||||
return $this->hasOne('Flarum\Api\Serializers\UserBasicSerializer');
|
||||
return $this->hasOne(UserBasicSerializer::class);
|
||||
}
|
||||
}
|
@@ -8,9 +8,9 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Commands;
|
||||
namespace Flarum\Flags\Command;
|
||||
|
||||
use Flarum\Core\Users\User;
|
||||
use Flarum\Core\User;
|
||||
|
||||
class CreateFlag
|
||||
{
|
@@ -8,17 +8,26 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Commands;
|
||||
namespace Flarum\Flags\Command;
|
||||
|
||||
use Flarum\Core\Access\AssertPermissionTrait;
|
||||
use Flarum\Flags\Flag;
|
||||
use Flarum\Core\Posts\PostRepository;
|
||||
use Flarum\Core\Posts\CommentPost;
|
||||
use Exception;
|
||||
use Flarum\Core\Repository\PostRepository;
|
||||
use Flarum\Core\Post\CommentPost;
|
||||
use Tobscure\JsonApi\Exception\InvalidParameterException;
|
||||
|
||||
class CreateFlagHandler
|
||||
{
|
||||
private $posts;
|
||||
use AssertPermissionTrait;
|
||||
|
||||
/**
|
||||
* @var PostRepository
|
||||
*/
|
||||
protected $posts;
|
||||
|
||||
/**
|
||||
* @param PostRepository $posts
|
||||
*/
|
||||
public function __construct(PostRepository $posts)
|
||||
{
|
||||
$this->posts = $posts;
|
||||
@@ -27,6 +36,7 @@ class CreateFlagHandler
|
||||
/**
|
||||
* @param CreateFlag $command
|
||||
* @return Flag
|
||||
* @throws InvalidParameterException
|
||||
*/
|
||||
public function handle(CreateFlag $command)
|
||||
{
|
||||
@@ -37,11 +47,10 @@ class CreateFlagHandler
|
||||
$post = $this->posts->findOrFail($postId, $actor);
|
||||
|
||||
if (! ($post instanceof CommentPost)) {
|
||||
// TODO: throw 400(?) error
|
||||
throw new Exception;
|
||||
throw new InvalidParameterException;
|
||||
}
|
||||
|
||||
$post->assertCan($actor, 'flag');
|
||||
$this->assertCan($actor, 'flag', $post);
|
||||
|
||||
Flag::unguard();
|
||||
|
@@ -8,10 +8,9 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Commands;
|
||||
namespace Flarum\Flags\Command;
|
||||
|
||||
use Flarum\Flags\Flag;
|
||||
use Flarum\Core\Users\User;
|
||||
use Flarum\Core\User;
|
||||
|
||||
class DeleteFlags
|
||||
{
|
61
extensions/flags/src/Command/DeleteFlagsHandler.php
Normal file
61
extensions/flags/src/Command/DeleteFlagsHandler.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Command;
|
||||
|
||||
use Flarum\Core\Access\AssertPermissionTrait;
|
||||
use Flarum\Core\Repository\PostRepository;
|
||||
use Flarum\Flags\Event\FlagsWillBeDeleted;
|
||||
use Flarum\Flags\Flag;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class DeleteFlagsHandler
|
||||
{
|
||||
use AssertPermissionTrait;
|
||||
|
||||
/**
|
||||
* @var PostRepository
|
||||
*/
|
||||
protected $posts;
|
||||
|
||||
/**
|
||||
* @var Dispatcher
|
||||
*/
|
||||
protected $events;
|
||||
|
||||
/**
|
||||
* @param PostRepository $posts
|
||||
* @param Dispatcher $events
|
||||
*/
|
||||
public function __construct(PostRepository $posts, Dispatcher $events)
|
||||
{
|
||||
$this->posts = $posts;
|
||||
$this->events = $events;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DeleteFlags $command
|
||||
* @return Flag
|
||||
*/
|
||||
public function handle(DeleteFlags $command)
|
||||
{
|
||||
$actor = $command->actor;
|
||||
|
||||
$post = $this->posts->findOrFail($command->postId, $actor);
|
||||
|
||||
$this->assertCan($actor, 'viewFlags', $post->discussion);
|
||||
|
||||
$this->events->fire(new FlagsWillBeDeleted($post, $actor, $command->data));
|
||||
|
||||
$post->flags()->delete();
|
||||
|
||||
return $post;
|
||||
}
|
||||
}
|
@@ -1,45 +0,0 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Commands;
|
||||
|
||||
use Flarum\Flags\Flag;
|
||||
use Flarum\Core\Posts\PostRepository;
|
||||
use Flarum\Flags\Events\FlagsWillBeDeleted;
|
||||
|
||||
class DeleteFlagsHandler
|
||||
{
|
||||
protected $posts;
|
||||
|
||||
public function __construct(PostRepository $posts)
|
||||
{
|
||||
$this->posts = $posts;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DeleteFlag $command
|
||||
* @return Flag
|
||||
* @throws \Flarum\Core\Exceptions\PermissionDeniedException
|
||||
*/
|
||||
public function handle(DeleteFlags $command)
|
||||
{
|
||||
$actor = $command->actor;
|
||||
|
||||
$post = $this->posts->findOrFail($command->postId, $actor);
|
||||
|
||||
$post->discussion->assertCan($actor, 'viewFlags');
|
||||
|
||||
event(new FlagsWillBeDeleted($post, $actor, $command->data));
|
||||
|
||||
$post->flags()->delete();
|
||||
|
||||
return $post;
|
||||
}
|
||||
}
|
@@ -9,10 +9,10 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Events;
|
||||
namespace Flarum\Flags\Event;
|
||||
|
||||
use Flarum\Core\Posts\Post;
|
||||
use Flarum\Core\Users\User;
|
||||
use Flarum\Core\Post;
|
||||
use Flarum\Core\User;
|
||||
|
||||
class FlagsWillBeDeleted
|
||||
{
|
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags;
|
||||
|
||||
use Flarum\Support\Extension as BaseExtension;
|
||||
use Illuminate\Events\Dispatcher;
|
||||
use Flarum\Core\Posts\Post;
|
||||
|
||||
class Extension extends BaseExtension
|
||||
{
|
||||
public function listen(Dispatcher $events)
|
||||
{
|
||||
$events->subscribe('Flarum\Flags\Listeners\AddClientAssets');
|
||||
$events->subscribe('Flarum\Flags\Listeners\AddApiAttributes');
|
||||
$events->subscribe('Flarum\Flags\Listeners\AddModelRelationship');
|
||||
}
|
||||
}
|
@@ -10,24 +10,38 @@
|
||||
|
||||
namespace Flarum\Flags;
|
||||
|
||||
use Flarum\Core\Model;
|
||||
use Flarum\Core\Support\VisibleScope;
|
||||
use Flarum\Core\Post;
|
||||
use Flarum\Core\Support\ScopeVisibilityTrait;
|
||||
use Flarum\Core\User;
|
||||
use Flarum\Database\AbstractModel;
|
||||
|
||||
class Flag extends Model
|
||||
class Flag extends AbstractModel
|
||||
{
|
||||
use VisibleScope;
|
||||
use ScopeVisibilityTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $table = 'flags';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $dates = ['time'];
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function post()
|
||||
{
|
||||
return $this->belongsTo('Flarum\Core\Posts\Post');
|
||||
return $this->belongsTo(Post::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo('Flarum\Core\Users\User');
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
|
48
extensions/flags/src/Listener/AddClientAssets.php
Normal file
48
extensions/flags/src/Listener/AddClientAssets.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Listener;
|
||||
|
||||
use Flarum\Event\ConfigureClientView;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class AddClientAssets
|
||||
{
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(ConfigureClientView::class, [$this, 'addAssets']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ConfigureClientView $event
|
||||
*/
|
||||
public function addAssets(ConfigureClientView $event)
|
||||
{
|
||||
if ($event->isForum()) {
|
||||
$event->addAssets([
|
||||
__DIR__.'/../../js/forum/dist/extension.js',
|
||||
__DIR__.'/../../less/forum/extension.less'
|
||||
]);
|
||||
$event->addBootstrapper('flarum/flags/main');
|
||||
$event->addTranslations('flarum-flags.forum');
|
||||
}
|
||||
|
||||
if ($event->isAdmin()) {
|
||||
$event->addAssets([
|
||||
__DIR__.'/../../js/admin/dist/extension.js'
|
||||
]);
|
||||
$event->addBootstrapper('flarum/flags/main');
|
||||
$event->addTranslations('flarum-flags.admin');
|
||||
}
|
||||
}
|
||||
}
|
78
extensions/flags/src/Listener/AddFlagsApi.php
Executable file
78
extensions/flags/src/Listener/AddFlagsApi.php
Executable file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Listener;
|
||||
|
||||
use Flarum\Api\Serializer\ForumSerializer;
|
||||
use Flarum\Api\Serializer\PostSerializer;
|
||||
use Flarum\Core\User;
|
||||
use Flarum\Event\ConfigureApiRoutes;
|
||||
use Flarum\Event\ConfigureModelDates;
|
||||
use Flarum\Event\PrepareApiAttributes;
|
||||
use Flarum\Flags\Api\Controller;
|
||||
use Flarum\Flags\Flag;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class AddFlagsApi
|
||||
{
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(ConfigureModelDates::class, [$this, 'configureModelDates']);
|
||||
$events->listen(PrepareApiAttributes::class, [$this, 'prepareApiAttributes']);
|
||||
$events->listen(ConfigureApiRoutes::class, [$this, 'configureApiRoutes']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ConfigureModelDates $event
|
||||
*/
|
||||
public function configureModelDates(ConfigureModelDates $event)
|
||||
{
|
||||
if ($event->isModel(User::class)) {
|
||||
$event->dates[] = 'flags_read_time';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PrepareApiAttributes $event
|
||||
*/
|
||||
public function prepareApiAttributes(PrepareApiAttributes $event)
|
||||
{
|
||||
if ($event->isSerializer(ForumSerializer::class)) {
|
||||
$event->attributes['canViewFlags'] = $event->actor->hasPermissionLike('discussion.viewFlags');
|
||||
|
||||
if ($event->attributes['canViewFlags']) {
|
||||
$query = Flag::whereVisibleTo($event->actor);
|
||||
|
||||
if ($time = $event->actor->flags_read_time) {
|
||||
$query->where('flags.time', '>', $time);
|
||||
}
|
||||
|
||||
$event->attributes['unreadFlagsCount'] = $query->distinct('flags.post_id')->count();
|
||||
}
|
||||
}
|
||||
|
||||
if ($event->isSerializer(PostSerializer::class)) {
|
||||
$event->attributes['canFlag'] = $event->actor->can('flag', $event->model);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ConfigureApiRoutes $event
|
||||
*/
|
||||
public function configureApiRoutes(ConfigureApiRoutes $event)
|
||||
{
|
||||
$event->get('/flags', 'flags.index', Controller\ListFlagsController::class);
|
||||
$event->post('/flags', 'flags.create', Controller\CreateFlagController::class);
|
||||
$event->delete('/posts/{id}/flags', 'flags.delete', Controller\DeleteFlagsController::class);
|
||||
}
|
||||
}
|
137
extensions/flags/src/Listener/AddPostFlagsRelationship.php
Executable file
137
extensions/flags/src/Listener/AddPostFlagsRelationship.php
Executable file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Listener;
|
||||
|
||||
use Flarum\Api\Controller;
|
||||
use Flarum\Api\Serializer\PostSerializer;
|
||||
use Flarum\Core\Post;
|
||||
use Flarum\Event\ConfigureApiController;
|
||||
use Flarum\Event\GetApiRelationship;
|
||||
use Flarum\Event\GetModelRelationship;
|
||||
use Flarum\Event\PostWasDeleted;
|
||||
use Flarum\Event\PrepareApiData;
|
||||
use Flarum\Flags\Api\Controller\CreateFlagController;
|
||||
use Flarum\Flags\Api\Serializer\FlagSerializer;
|
||||
use Flarum\Flags\Flag;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class AddPostFlagsRelationship
|
||||
{
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(GetModelRelationship::class, [$this, 'getModelRelationship']);
|
||||
$events->listen(PostWasDeleted::class, [$this, 'postWasDeleted']);
|
||||
$events->listen(GetApiRelationship::class, [$this, 'getApiRelationship']);
|
||||
$events->listen(ConfigureApiController::class, [$this, 'includeFlagsRelationship']);
|
||||
$events->listen(PrepareApiData::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 PostWasDeleted $event
|
||||
*/
|
||||
public function postWasDeleted(PostWasDeleted $event)
|
||||
{
|
||||
$event->post->flags()->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param GetApiRelationship $event
|
||||
* @return \Flarum\Api\Relationship\HasManyBuilder|null
|
||||
*/
|
||||
public function getApiRelationship(GetApiRelationship $event)
|
||||
{
|
||||
if ($event->isRelationship(PostSerializer::class, 'flags')) {
|
||||
return $event->serializer->hasMany(FlagSerializer::class, 'flags');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ConfigureApiController $event
|
||||
*/
|
||||
public function includeFlagsRelationship(ConfigureApiController $event)
|
||||
{
|
||||
if ($event->isController(Controller\ShowDiscussionController::class)) {
|
||||
$event->addInclude([
|
||||
'posts.flags',
|
||||
'posts.flags.user'
|
||||
]);
|
||||
}
|
||||
|
||||
if ($event->isController(Controller\ListPostsController::class)
|
||||
|| $event->isController(Controller\ShowPostController::class)) {
|
||||
$event->addInclude([
|
||||
'flags',
|
||||
'flags.user'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PrepareApiData $event
|
||||
*/
|
||||
public function prepareApiData(PrepareApiData $event)
|
||||
{
|
||||
// For any API action that allows the 'flags' relationship to be
|
||||
// included, we need to preload this relationship onto the data (Post
|
||||
// models) so that we can selectively expose only the flags that the
|
||||
// user has permission to view.
|
||||
if ($event->isController(Controller\ShowDiscussionController::class)) {
|
||||
$posts = $event->data->posts;
|
||||
}
|
||||
|
||||
if ($event->isController(Controller\ListPostsController::class)) {
|
||||
$posts = $event->data->all();
|
||||
}
|
||||
|
||||
if ($event->isController(Controller\ShowPostController::class)) {
|
||||
$posts = [$event->data];
|
||||
}
|
||||
|
||||
if ($event->isController(CreateFlagController::class)) {
|
||||
$posts = [$event->data->post];
|
||||
}
|
||||
|
||||
if (isset($posts)) {
|
||||
$actor = $event->request->getAttribute('actor');
|
||||
$postsWithPermission = [];
|
||||
|
||||
foreach ($posts as $post) {
|
||||
$post->setRelation('flags', null);
|
||||
|
||||
if ($actor->can('viewFlags', $post->discussion)) {
|
||||
$postsWithPermission[] = $post;
|
||||
}
|
||||
}
|
||||
|
||||
if (count($postsWithPermission)) {
|
||||
(new Collection($postsWithPermission))
|
||||
->load('flags', 'flags.user');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -1,129 +0,0 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Listeners;
|
||||
|
||||
use Flarum\Events\ApiRelationship;
|
||||
use Flarum\Events\WillSerializeData;
|
||||
use Flarum\Events\BuildApiAction;
|
||||
use Flarum\Events\ApiAttributes;
|
||||
use Flarum\Events\RegisterApiRoutes;
|
||||
use Flarum\Api\Serializers\PostSerializer;
|
||||
use Flarum\Api\Serializers\ForumSerializer;
|
||||
use Flarum\Api\Actions\Posts;
|
||||
use Flarum\Api\Actions\Discussions;
|
||||
use Flarum\Flags\Flag;
|
||||
use Flarum\Flags\Api\CreateAction as FlagsCreateAction;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class AddApiAttributes
|
||||
{
|
||||
public function subscribe($events)
|
||||
{
|
||||
$events->listen(ApiRelationship::class, [$this, 'addFlagsRelationship']);
|
||||
$events->listen(WillSerializeData::class, [$this, 'loadFlagsRelationship']);
|
||||
$events->listen(BuildApiAction::class, [$this, 'includeFlagsRelationship']);
|
||||
$events->listen(ApiAttributes::class, [$this, 'addAttributes']);
|
||||
$events->listen(RegisterApiRoutes::class, [$this, 'addRoutes']);
|
||||
}
|
||||
|
||||
public function loadFlagsRelationship(WillSerializeData $event)
|
||||
{
|
||||
// For any API action that allows the 'flags' relationship to be
|
||||
// included, we need to preload this relationship onto the data (Post
|
||||
// models) so that we can selectively expose only the flags that the
|
||||
// user has permission to view.
|
||||
if ($event->action instanceof Discussions\ShowAction) {
|
||||
$discussion = $event->data;
|
||||
$posts = $discussion->posts->all();
|
||||
}
|
||||
|
||||
if ($event->action instanceof Posts\IndexAction) {
|
||||
$posts = $event->data->all();
|
||||
}
|
||||
|
||||
if ($event->action instanceof Posts\ShowAction) {
|
||||
$posts = [$event->data];
|
||||
}
|
||||
|
||||
if ($event->action instanceof FlagsCreateAction) {
|
||||
$flag = $event->data;
|
||||
$posts = [$flag->post];
|
||||
}
|
||||
|
||||
if (isset($posts)) {
|
||||
$actor = $event->request->actor;
|
||||
$postsWithPermission = [];
|
||||
|
||||
foreach ($posts as $post) {
|
||||
$post->setRelation('flags', null);
|
||||
|
||||
if ($post->discussion->can($actor, 'viewFlags')) {
|
||||
$postsWithPermission[] = $post;
|
||||
}
|
||||
}
|
||||
|
||||
if (count($postsWithPermission)) {
|
||||
(new Collection($postsWithPermission))
|
||||
->load('flags', 'flags.user');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function addFlagsRelationship(ApiRelationship $event)
|
||||
{
|
||||
if ($event->serializer instanceof PostSerializer &&
|
||||
$event->relationship === 'flags') {
|
||||
return $event->serializer->hasMany('Flarum\Flags\Api\FlagSerializer', 'flags');
|
||||
}
|
||||
}
|
||||
|
||||
public function includeFlagsRelationship(BuildApiAction $event)
|
||||
{
|
||||
if ($event->action instanceof Discussions\ShowAction) {
|
||||
$event->addInclude('posts.flags');
|
||||
$event->addInclude('posts.flags.user');
|
||||
}
|
||||
|
||||
if ($event->action instanceof Posts\IndexAction ||
|
||||
$event->action instanceof Posts\ShowAction) {
|
||||
$event->addInclude('flags');
|
||||
$event->addInclude('flags.user');
|
||||
}
|
||||
}
|
||||
|
||||
public function addAttributes(ApiAttributes $event)
|
||||
{
|
||||
if ($event->serializer instanceof ForumSerializer) {
|
||||
$event->attributes['canViewFlags'] = $event->actor->hasPermissionLike('discussion.viewFlags');
|
||||
|
||||
if ($event->attributes['canViewFlags']) {
|
||||
$query = Flag::whereVisibleTo($event->actor);
|
||||
|
||||
if ($time = $event->actor->flags_read_time) {
|
||||
$query->where('flags.time', '>', $time);
|
||||
}
|
||||
|
||||
$event->attributes['unreadFlagsCount'] = $query->distinct('flags.post_id')->count();
|
||||
}
|
||||
}
|
||||
|
||||
if ($event->serializer instanceof PostSerializer) {
|
||||
$event->attributes['canFlag'] = $event->model->can($event->actor, 'flag');
|
||||
}
|
||||
}
|
||||
|
||||
public function addRoutes(RegisterApiRoutes $event)
|
||||
{
|
||||
$event->get('/flags', 'flags.index', 'Flarum\Flags\Api\IndexAction');
|
||||
$event->post('/flags', 'flags.create', 'Flarum\Flags\Api\CreateAction');
|
||||
$event->delete('/posts/{id}/flags', 'flags.delete', 'Flarum\Flags\Api\DeleteAction');
|
||||
}
|
||||
}
|
@@ -1,60 +0,0 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Listeners;
|
||||
|
||||
use Flarum\Events\RegisterLocales;
|
||||
use Flarum\Events\BuildClientView;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class AddClientAssets
|
||||
{
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(RegisterLocales::class, [$this, 'addLocale']);
|
||||
$events->listen(BuildClientView::class, [$this, 'addAssets']);
|
||||
}
|
||||
|
||||
public function addLocale(RegisterLocales $event)
|
||||
{
|
||||
$event->addTranslations('en', __DIR__.'/../../locale/en.yml');
|
||||
}
|
||||
|
||||
public function addAssets(BuildClientView $event)
|
||||
{
|
||||
$event->forumAssets([
|
||||
__DIR__.'/../../js/forum/dist/extension.js',
|
||||
__DIR__.'/../../less/forum/extension.less'
|
||||
]);
|
||||
|
||||
$event->forumBootstrapper('flags/main');
|
||||
|
||||
$event->forumTranslations([
|
||||
'flags.reason_off_topic',
|
||||
'flags.reason_spam',
|
||||
'flags.reason_inappropriate',
|
||||
'flags.reason_other',
|
||||
'flags.flagged_by',
|
||||
'flags.flagged_by_with_reason',
|
||||
'flags.no_flags'
|
||||
]);
|
||||
|
||||
$event->adminAssets([
|
||||
__DIR__.'/../../js/admin/dist/extension.js',
|
||||
__DIR__.'/../../less/admin/extension.less'
|
||||
]);
|
||||
|
||||
$event->adminBootstrapper('flags/main');
|
||||
|
||||
$event->adminTranslations([
|
||||
// 'flag.hello_world'
|
||||
]);
|
||||
}
|
||||
}
|
@@ -1,47 +0,0 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Flags\Listeners;
|
||||
|
||||
use Flarum\Events\ModelRelationship;
|
||||
use Flarum\Events\ModelDates;
|
||||
use Flarum\Events\PostWasDeleted;
|
||||
use Flarum\Core\Posts\Post;
|
||||
use Flarum\Core\Users\User;
|
||||
use Flarum\Flags\Flag;
|
||||
|
||||
class AddModelRelationship
|
||||
{
|
||||
public function subscribe($events)
|
||||
{
|
||||
$events->listen(ModelRelationship::class, [$this, 'addFlagsRelationship']);
|
||||
$events->listen(ModelDates::class, [$this, 'modelDates']);
|
||||
$events->listen(PostWasDeleted::class, [$this, 'deleteFlags']);
|
||||
}
|
||||
|
||||
public function addFlagsRelationship(ModelRelationship $event)
|
||||
{
|
||||
if ($event->model instanceof Post && $event->relationship === 'flags') {
|
||||
return $event->model->hasMany('Flarum\Flags\Flag', 'post_id');
|
||||
}
|
||||
}
|
||||
|
||||
public function modelDates(ModelDates $event)
|
||||
{
|
||||
if ($event->model instanceof User) {
|
||||
$event->dates[] = 'flags_read_time';
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteFlags(PostWasDeleted $event)
|
||||
{
|
||||
$event->post->flags()->delete();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user