mirror of
https://github.com/flarum/core.git
synced 2025-10-22 12:16:07 +02:00
Upgrade to L5 + huge refactor + more. closes #2
New stuff: - Signup + email confirmation. - Updated authentication strategy with remember cookies. closes #5 - New search system with some example gambits! This is cool - check out the source. Fulltext drivers will be implemented as decorators overriding the EloquentPostRepository’s findByContent method. - Lay down the foundation for bootstrapping the Ember app. - Update Web layer’s asset manager to properly publish CSS/JS files. - Console commands to run installation migrations and seeds. Refactoring: - New structure: move models, repositories, commands, and events into their own namespaces, rather than grouping by entity. - All events are classes. - Use L5 middleware and command bus implementations. - Clearer use of repositories and the Active Record pattern. Repositories are used only for retrieval of ActiveRecord objects, and then save/delete operations are called directly on those ActiveRecords. This way, we don’t over-abstract at the cost of Eloquent magic, but testing is still easy. - Refactor of Web layer so that it uses the Actions routing architecture. - “Actor” concept instead of depending on Laravel’s Auth. - General cleanup!
This commit is contained in:
41
src/Api/Actions/Discussions/CreateAction.php
Normal file
41
src/Api/Actions/Discussions/CreateAction.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php namespace Flarum\Api\Actions\Discussions;
|
||||
|
||||
use Flarum\Core\Commands\StartDiscussionCommand;
|
||||
use Flarum\Core\Commands\ReadDiscussionCommand;
|
||||
use Flarum\Api\Actions\BaseAction;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
use Flarum\Api\Serializers\DiscussionSerializer;
|
||||
|
||||
class CreateAction extends BaseAction
|
||||
{
|
||||
/**
|
||||
* Start a new discussion.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
protected function run(ApiParams $params)
|
||||
{
|
||||
// By default, the only required attributes of a discussion are the
|
||||
// title and the content. We'll extract these from the rbaseequest data
|
||||
// and pass them through to the StartDiscussionCommand.
|
||||
$title = $params->get('discussions.title');
|
||||
$content = $params->get('discussions.content');
|
||||
$user = $this->actor->getUser();
|
||||
|
||||
$command = new StartDiscussionCommand($title, $content, $user, app('flarum.forum'));
|
||||
$discussion = $this->dispatch($command, $params);
|
||||
|
||||
// After creating the discussion, we assume that the user has seen all
|
||||
// of the posts in the discussion; thus, we will mark the discussion
|
||||
// as read if they are logged in.
|
||||
if ($user->exists) {
|
||||
$command = new ReadDiscussionCommand($discussion->id, $user, 1);
|
||||
$this->dispatch($command, $params);
|
||||
}
|
||||
|
||||
$serializer = new DiscussionSerializer(['posts']);
|
||||
$document = $this->document()->setPrimaryElement($serializer->resource($discussion));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
}
|
23
src/Api/Actions/Discussions/DeleteAction.php
Normal file
23
src/Api/Actions/Discussions/DeleteAction.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php namespace Flarum\Api\Actions\Discussions;
|
||||
|
||||
use Flarum\Core\Commands\DeleteDiscussionCommand;
|
||||
use Flarum\Api\Actions\BaseAction;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
|
||||
class DeleteAction extends BaseAction
|
||||
{
|
||||
/**
|
||||
* Delete a discussion.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
protected function run(ApiParams $params)
|
||||
{
|
||||
$discussionId = $params->get('id');
|
||||
|
||||
$command = new DeleteDiscussionCommand($discussionId, $this->actor->getUser());
|
||||
$this->dispatch($command, $params);
|
||||
|
||||
return $this->respondWithoutContent();
|
||||
}
|
||||
}
|
81
src/Api/Actions/Discussions/IndexAction.php
Normal file
81
src/Api/Actions/Discussions/IndexAction.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php namespace Flarum\Api\Actions\Discussions;
|
||||
|
||||
use Flarum\Core\Search\Discussions\DiscussionSearchCriteria;
|
||||
use Flarum\Core\Search\Discussions\DiscussionSearcher;
|
||||
use Flarum\Core\Support\Actor;
|
||||
use Flarum\Api\Actions\BaseAction;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
use Flarum\Api\Serializers\DiscussionSerializer;
|
||||
|
||||
class IndexAction extends BaseAction
|
||||
{
|
||||
/**
|
||||
* The discussion searcher.
|
||||
*
|
||||
* @var DiscussionSearcher
|
||||
*/
|
||||
protected $searcher;
|
||||
|
||||
/**
|
||||
* Instantiate the action.
|
||||
*
|
||||
* @param DiscussionSearcher $searcher
|
||||
*/
|
||||
public function __construct(Actor $actor, DiscussionSearcher $searcher)
|
||||
{
|
||||
$this->actor = $actor;
|
||||
$this->searcher = $searcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a list of discussions.
|
||||
*
|
||||
* @todo custom rate limit for this function? determined by if $key was valid?
|
||||
* @return Response
|
||||
*/
|
||||
protected function run(ApiParams $params)
|
||||
{
|
||||
$query = $params->get('q');
|
||||
$start = $params->start();
|
||||
$include = $params->included(['startPost', 'lastPost', 'relevantPosts']);
|
||||
$count = $params->count(20, 50);
|
||||
$sort = $params->sort(['', 'lastPost', 'replies', 'created']);
|
||||
|
||||
$relations = array_merge(['startUser', 'lastUser'], $include);
|
||||
|
||||
// Set up the discussion finder with our search criteria, and get the
|
||||
// requested range of results with the necessary relations loaded.
|
||||
$criteria = new DiscussionSearchCriteria($this->actor->getUser(), $query, $sort['field'], $sort['order']);
|
||||
$load = array_merge($relations, ['state']);
|
||||
|
||||
$results = $this->searcher->search($criteria, $count, $start, $load);
|
||||
|
||||
$document = $this->document();
|
||||
|
||||
if (($total = $results->getTotal()) !== null) {
|
||||
$document->addMeta('total', $total);
|
||||
}
|
||||
|
||||
// If there are more results, then we need to construct a URL to the
|
||||
// next results page and add that to the metadata. We do this by
|
||||
// compacting all of the valid query parameters which have been
|
||||
// specified.
|
||||
if ($results->areMoreResults()) {
|
||||
$start += $count;
|
||||
$include = implode(',', $include);
|
||||
$sort = $sort['string'];
|
||||
$input = array_filter(compact('query', 'sort', 'start', 'count', 'include'));
|
||||
$moreUrl = $this->buildUrl('discussions.index', [], $input);
|
||||
} else {
|
||||
$moreUrl = '';
|
||||
}
|
||||
$document->addMeta('moreUrl', $moreUrl);
|
||||
|
||||
// Finally, we can set up the discussion serializer and use it to create
|
||||
// a collection of discussion results.
|
||||
$serializer = new DiscussionSerializer($relations);
|
||||
$document->setPrimaryElement($serializer->collection($results->getDiscussions()));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
}
|
70
src/Api/Actions/Discussions/ShowAction.php
Normal file
70
src/Api/Actions/Discussions/ShowAction.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php namespace Flarum\Api\Actions\Discussions;
|
||||
|
||||
use Flarum\Core\Support\Actor;
|
||||
use Flarum\Core\Repositories\DiscussionRepositoryInterface as DiscussionRepository;
|
||||
use Flarum\Core\Repositories\PostRepositoryInterface as PostRepository;
|
||||
use Flarum\Api\Actions\BaseAction;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
use Flarum\Api\Actions\Posts\GetsPostsForDiscussion;
|
||||
use Flarum\Api\Serializers\DiscussionSerializer;
|
||||
|
||||
class ShowAction extends BaseAction
|
||||
{
|
||||
use GetsPostsForDiscussion;
|
||||
|
||||
/**
|
||||
* The discussion repository.
|
||||
*
|
||||
* @var DiscussionRepository
|
||||
*/
|
||||
protected $discussions;
|
||||
|
||||
/**
|
||||
* The post repository.
|
||||
*
|
||||
* @var PostRepository
|
||||
*/
|
||||
protected $posts;
|
||||
|
||||
/**
|
||||
* Instantiate the action.
|
||||
*
|
||||
* @param PostRepository $posts
|
||||
*/
|
||||
public function __construct(Actor $actor, DiscussionRepository $discussions, PostRepository $posts)
|
||||
{
|
||||
$this->actor = $actor;
|
||||
$this->discussions = $discussions;
|
||||
$this->posts = $posts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a single discussion.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
protected function run(ApiParams $params)
|
||||
{
|
||||
$include = $params->included(['startPost', 'lastPost', 'posts']);
|
||||
|
||||
$discussion = $this->discussions->findOrFail($params->get('id'), $this->actor->getUser());
|
||||
|
||||
if (in_array('posts', $include)) {
|
||||
$relations = ['user', 'user.groups', 'editUser', 'hideUser'];
|
||||
$discussion->posts = $this->getPostsForDiscussion($params, $discussion->id)->load($relations);
|
||||
|
||||
$include = array_merge($include, array_map(function ($relation) {
|
||||
return 'posts.'.$relation;
|
||||
}, $relations));
|
||||
}
|
||||
|
||||
// Set up the discussion serializer, which we will use to create the
|
||||
// document's primary resource. As well as including the requested
|
||||
// relations, we will specify that we want the 'posts' relation to be
|
||||
// linked so that a list of post IDs will show up in the response.
|
||||
$serializer = new DiscussionSerializer($include, ['posts']);
|
||||
$document = $this->document()->setPrimaryElement($serializer->resource($discussion));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
}
|
54
src/Api/Actions/Discussions/UpdateAction.php
Normal file
54
src/Api/Actions/Discussions/UpdateAction.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php namespace Flarum\Api\Actions\Discussions;
|
||||
|
||||
use Flarum\Core\Commands\EditDiscussionCommand;
|
||||
use Flarum\Core\Commands\ReadDiscussionCommand;
|
||||
use Flarum\Core\Exceptions\PermissionDeniedException;
|
||||
use Flarum\Api\Actions\BaseAction;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
use Flarum\Api\Serializers\DiscussionSerializer;
|
||||
|
||||
class UpdateAction extends BaseAction
|
||||
{
|
||||
/**
|
||||
* Edit a discussion. Allows renaming the discussion, and updating its read
|
||||
* state with regards to the current user.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
protected function run(ApiParams $params)
|
||||
{
|
||||
$discussionId = $params->get('id');
|
||||
$user = $this->actor->getUser();
|
||||
|
||||
// First, we will run the EditDiscussionCommand. This will update the
|
||||
// discussion's direct properties; by default, this is just the title.
|
||||
// As usual, however, we will fire an event to allow plugins to update
|
||||
// additional properties.
|
||||
if ($data = array_except($params->get('discussions'), ['readNumber'])) {
|
||||
$command = new EditDiscussionCommand($discussionId, $user);
|
||||
$this->hydrate($command, $params->get('discussions'));
|
||||
$discussion = $this->dispatch($command, $params);
|
||||
}
|
||||
|
||||
// Next, if a read number was specified in the request, we will run the
|
||||
// ReadDiscussionCommand.
|
||||
//
|
||||
// @todo Currently, if the user doesn't have permission to edit a
|
||||
// discussion, they're unable to update their readNumber because a
|
||||
// PermissionsDeniedException is thrown by the
|
||||
// EditDiscussionCommand above. So this needs to be extracted into
|
||||
// its own endpoint.
|
||||
if ($readNumber = $params->get('discussions.readNumber')) {
|
||||
$command = new ReadDiscussionCommand($discussionId, $user, $readNumber);
|
||||
$this->dispatch($command, $params);
|
||||
}
|
||||
|
||||
// Presumably, the discussion was updated successfully. (One of the command
|
||||
// handlers would have thrown an exception if not.) We set this
|
||||
// discussion as our document's primary element.
|
||||
$serializer = new DiscussionSerializer(['addedPosts', 'addedPosts.user']);
|
||||
$document = $this->document()->setPrimaryElement($serializer->resource($discussion));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user