mirror of
https://github.com/flarum/core.git
synced 2025-10-20 03:06:07 +02:00
Massive refactor
- Use contextual namespaces within Flarum\Core - Clean up and docblock everything - Refactor Activity/Notification blueprint stuff - Refactor Formatter stuff - Refactor Search stuff - Upgrade to JSON-API 1.0 - Removed “addedPosts” and “removedPosts” relationships from discussion API. This was used for adding/removing event posts after renaming a discussion etc. Instead we should make an additional request to get all new posts Todo: - Fix Extenders and extensions - Get rid of repository interfaces - Fix other bugs I’ve inevitably introduced
This commit is contained in:
41
src/Core/Posts/Commands/DeletePost.php
Normal file
41
src/Core/Posts/Commands/DeletePost.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php namespace Flarum\Core\Posts\Commands;
|
||||
|
||||
use Flarum\Core\Users\User;
|
||||
|
||||
class DeletePost
|
||||
{
|
||||
/**
|
||||
* The ID of the post to delete.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $postId;
|
||||
|
||||
/**
|
||||
* The user performing the action.
|
||||
*
|
||||
* @var User
|
||||
*/
|
||||
public $actor;
|
||||
|
||||
/**
|
||||
* Any other user input associated with the action. This is unused by
|
||||
* default, but may be used by extensions.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $data;
|
||||
|
||||
/**
|
||||
* @param int $postId The ID of the post to delete.
|
||||
* @param User $actor The user performing the action.
|
||||
* @param array $data Any other user input associated with the action. This
|
||||
* is unused by default, but may be used by extensions.
|
||||
*/
|
||||
public function __construct($postId, User $actor, array $data = [])
|
||||
{
|
||||
$this->postId = $postId;
|
||||
$this->actor = $actor;
|
||||
$this->data = $data;
|
||||
}
|
||||
}
|
44
src/Core/Posts/Commands/DeletePostHandler.php
Normal file
44
src/Core/Posts/Commands/DeletePostHandler.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php namespace Flarum\Core\Posts\Commands;
|
||||
|
||||
use Flarum\Core\Posts\PostRepositoryInterface;
|
||||
use Flarum\Core\Posts\Events\PostWillBeDeleted;
|
||||
use Flarum\Core\Support\DispatchesEvents;
|
||||
|
||||
class DeletePostHandler
|
||||
{
|
||||
use DispatchesEvents;
|
||||
|
||||
/**
|
||||
* @var PostRepositoryInterface
|
||||
*/
|
||||
protected $posts;
|
||||
|
||||
/**
|
||||
* @param PostRepositoryInterface $posts
|
||||
*/
|
||||
public function __construct(PostRepositoryInterface $posts)
|
||||
{
|
||||
$this->posts = $posts;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DeletePost $command
|
||||
* @return \Flarum\Core\Posts\Post
|
||||
*/
|
||||
public function handle(DeletePost $command)
|
||||
{
|
||||
$actor = $command->actor;
|
||||
|
||||
$post = $this->posts->findOrFail($command->postId, $actor);
|
||||
|
||||
$post->assertCan($actor, 'delete');
|
||||
|
||||
event(new PostWillBeDeleted($post, $actor, $command->data));
|
||||
|
||||
$post->delete();
|
||||
|
||||
$this->dispatchEventsFor($post);
|
||||
|
||||
return $post;
|
||||
}
|
||||
}
|
39
src/Core/Posts/Commands/EditPost.php
Normal file
39
src/Core/Posts/Commands/EditPost.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php namespace Flarum\Core\Posts\Commands;
|
||||
|
||||
use Flarum\Core\Users\User;
|
||||
|
||||
class EditPost
|
||||
{
|
||||
/**
|
||||
* The ID of the post to edit.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $postId;
|
||||
|
||||
/**
|
||||
* The user performing the action.
|
||||
*
|
||||
* @var User
|
||||
*/
|
||||
public $actor;
|
||||
|
||||
/**
|
||||
* The attributes to update on the post.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $data;
|
||||
|
||||
/**
|
||||
* @param int $postId The ID of the post to edit.
|
||||
* @param User $actor The user performing the action.
|
||||
* @param array $data The attributes to update on the post.
|
||||
*/
|
||||
public function __construct($postId, User $actor, array $data)
|
||||
{
|
||||
$this->postId = $postId;
|
||||
$this->actor = $actor;
|
||||
$this->data = $data;
|
||||
}
|
||||
}
|
65
src/Core/Posts/Commands/EditPostHandler.php
Normal file
65
src/Core/Posts/Commands/EditPostHandler.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php namespace Flarum\Core\Posts\Commands;
|
||||
|
||||
use Flarum\Core\Posts\PostRepositoryInterface;
|
||||
use Flarum\Core\Posts\Events\PostWillBeSaved;
|
||||
use Flarum\Core\Support\DispatchesEvents;
|
||||
use Flarum\Core\Posts\CommentPost;
|
||||
|
||||
class EditPostHandler
|
||||
{
|
||||
use DispatchesEvents;
|
||||
|
||||
/**
|
||||
* @var PostRepositoryInterface
|
||||
*/
|
||||
protected $posts;
|
||||
|
||||
/**
|
||||
* @param PostRepositoryInterface $posts
|
||||
*/
|
||||
public function __construct(PostRepositoryInterface $posts)
|
||||
{
|
||||
$this->posts = $posts;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param EditPost $command
|
||||
* @return \Flarum\Core\Posts\Post
|
||||
* @throws \Flarum\Core\Exceptions\PermissionDeniedException
|
||||
*/
|
||||
public function handle(EditPost $command)
|
||||
{
|
||||
$actor = $command->actor;
|
||||
$data = $command->data;
|
||||
|
||||
$post = $this->posts->findOrFail($command->postId, $actor);
|
||||
|
||||
if ($post instanceof CommentPost) {
|
||||
$attributes = array_get($data, 'attributes', []);
|
||||
|
||||
if (isset($attributes['content'])) {
|
||||
$post->assertCan($actor, 'edit');
|
||||
|
||||
$post->revise($attributes['content'], $actor);
|
||||
}
|
||||
|
||||
if (isset($attributes['isHidden'])) {
|
||||
$post->assertCan($actor, 'edit');
|
||||
|
||||
if ($attributes['isHidden']) {
|
||||
$post->hide($actor);
|
||||
} else {
|
||||
$post->restore();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
event(new PostWillBeSaved($post, $actor, $data));
|
||||
|
||||
$post->save();
|
||||
|
||||
$this->dispatchEventsFor($post);
|
||||
|
||||
return $post;
|
||||
}
|
||||
}
|
39
src/Core/Posts/Commands/PostReply.php
Normal file
39
src/Core/Posts/Commands/PostReply.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php namespace Flarum\Core\Posts\Commands;
|
||||
|
||||
use Flarum\Core\Users\User;
|
||||
|
||||
class PostReply
|
||||
{
|
||||
/**
|
||||
* The ID of the discussion to post the reply to.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $discussionId;
|
||||
|
||||
/**
|
||||
* The user who is performing the action.
|
||||
*
|
||||
* @var User
|
||||
*/
|
||||
public $actor;
|
||||
|
||||
/**
|
||||
* The attributes to assign to the new post.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $data;
|
||||
|
||||
/**
|
||||
* @param int $discussionId The ID of the discussion to post the reply to.
|
||||
* @param User $actor The user who is performing the action.
|
||||
* @param array $data The attributes to assign to the new post.
|
||||
*/
|
||||
public function __construct($discussionId, User $actor, array $data)
|
||||
{
|
||||
$this->discussionId = $discussionId;
|
||||
$this->actor = $actor;
|
||||
$this->data = $data;
|
||||
}
|
||||
}
|
70
src/Core/Posts/Commands/PostReplyHandler.php
Normal file
70
src/Core/Posts/Commands/PostReplyHandler.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php namespace Flarum\Core\Posts\Commands;
|
||||
|
||||
use Flarum\Core\Posts\Events\PostWillBeSaved;
|
||||
use Flarum\Core\Discussions\DiscussionRepositoryInterface;
|
||||
use Flarum\Core\Posts\CommentPost;
|
||||
use Flarum\Core\Support\DispatchesEvents;
|
||||
use Flarum\Core\Notifications\NotificationSyncer;
|
||||
|
||||
class PostReplyHandler
|
||||
{
|
||||
use DispatchesEvents;
|
||||
|
||||
/**
|
||||
* @var DiscussionRepositoryInterface
|
||||
*/
|
||||
protected $discussions;
|
||||
|
||||
/**
|
||||
* @var NotificationSyncer
|
||||
*/
|
||||
protected $notifications;
|
||||
|
||||
/**
|
||||
* @param DiscussionRepositoryInterface $discussions
|
||||
* @param NotificationSyncer $notifications
|
||||
*/
|
||||
public function __construct(DiscussionRepositoryInterface $discussions, NotificationSyncer $notifications)
|
||||
{
|
||||
$this->discussions = $discussions;
|
||||
$this->notifications = $notifications;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostReply $command
|
||||
* @return CommentPost
|
||||
* @throws \Flarum\Core\Exceptions\PermissionDeniedException
|
||||
*/
|
||||
public function handle(PostReply $command)
|
||||
{
|
||||
$actor = $command->actor;
|
||||
|
||||
// Make sure the user has permission to reply to this discussion. First,
|
||||
// make sure the discussion exists and that the user has permission to
|
||||
// view it; if not, fail with a ModelNotFound exception so we don't give
|
||||
// away the existence of the discussion. If the user is allowed to view
|
||||
// it, check if they have permission to reply.
|
||||
$discussion = $this->discussions->findOrFail($command->discussionId, $actor);
|
||||
|
||||
$discussion->assertCan($actor, 'reply');
|
||||
|
||||
// Create a new Post entity, persist it, and dispatch domain events.
|
||||
// Before persistence, though, fire an event to give plugins an
|
||||
// opportunity to alter the post entity based on data in the command.
|
||||
$post = CommentPost::reply(
|
||||
$command->discussionId,
|
||||
array_get($command->data, 'attributes.content'),
|
||||
$actor->id
|
||||
);
|
||||
|
||||
event(new PostWillBeSaved($post, $actor, $command->data));
|
||||
|
||||
$post->save();
|
||||
|
||||
$this->notifications->onePerUser(function () use ($post) {
|
||||
$this->dispatchEventsFor($post);
|
||||
});
|
||||
|
||||
return $post;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user