1
0
mirror of https://github.com/flarum/core.git synced 2025-10-26 21:21:28 +01:00

Convert the rest of the API to new action architecture

Also make some tweaks:
- Merge SerializeAction::$include and
SerializeAction::$includeAvailable into a keyed boolean array
- Set defaults for SerializeAction::$limit and $limitMax
- Rename SerializeAction::$sortAvailable to $sortFields
This commit is contained in:
Toby Zerner
2015-05-03 12:04:43 +09:30
parent 4cb9ed851d
commit 8f29679b46
37 changed files with 713 additions and 551 deletions

View File

@@ -6,13 +6,12 @@ class EditPostCommand
public $user;
public $content;
public $data;
public $isHidden;
public function __construct($postId, $user)
public function __construct($postId, $user, $data)
{
$this->postId = $postId;
$this->user = $user;
$this->data = $data;
}
}

View File

@@ -6,21 +6,12 @@ class EditUserCommand
public $user;
public $username;
public $data;
public $email;
public $password;
public $bio;
public $readTime;
public $preferences;
public function __construct($userId, $user)
public function __construct($userId, $user, $data)
{
$this->userId = $userId;
$this->user = $user;
$this->data = $data;
}
}

View File

@@ -4,14 +4,14 @@ class PostReplyCommand
{
public $discussionId;
public $content;
public $user;
public function __construct($discussionId, $content, $user)
public $data;
public function __construct($discussionId, $user, $data)
{
$this->discussionId = $discussionId;
$this->content = $content;
$this->user = $user;
$this->data = $data;
}
}

View File

@@ -6,18 +6,12 @@ class RegisterUserCommand
public $user;
public $username;
public $data;
public $email;
public $password;
public function __construct($username, $email, $password, $user, $forum)
public function __construct($user, $forum, $data)
{
$this->username = $username;
$this->email = $email;
$this->password = $password;
$this->user = $user;
$this->forum = $forum;
$this->data = $data;
}
}

View File

@@ -22,14 +22,16 @@ class EditPostCommandHandler
$post->assertCan($user, 'edit');
if (isset($command->content)) {
$post->revise($command->content, $user);
if (isset($command->data['content'])) {
$post->revise($command->data['content'], $user);
}
if ($command->isHidden === true) {
$post->hide($user);
} elseif ($command->isHidden === false) {
$post->restore($user);
if (isset($command->data['isHidden'])) {
if ($command->data['isHidden']) {
$post->hide($user);
} else {
$post->restore($user);
}
}
event(new PostWillBeSaved($post, $command));

View File

@@ -22,23 +22,28 @@ class EditUserCommandHandler
$userToEdit->assertCan($user, 'edit');
if (isset($command->username)) {
$userToEdit->rename($command->username);
if (isset($command->data['username'])) {
$userToEdit->rename($command->data['username']);
}
if (isset($command->email)) {
$userToEdit->changeEmail($command->email);
if (isset($command->data['email'])) {
$userToEdit->changeEmail($command->data['email']);
}
if (isset($command->password)) {
$userToEdit->changePassword($command->password);
if (isset($command->data['password'])) {
$userToEdit->changePassword($command->data['password']);
}
if (isset($command->bio)) {
$userToEdit->changeBio($command->bio);
if (isset($command->data['bio'])) {
$userToEdit->changeBio($command->data['bio']);
}
if (! empty($command->readTime)) {
if (! empty($command->data['readTime'])) {
$userToEdit->markAllAsRead();
}
if (! empty($command->preferences)) {
foreach ($command->preferences as $k => $v) {
if (! empty($command->data['preferences'])) {
foreach ($command->data['preferences'] as $k => $v) {
$userToEdit->setPreference($k, $v);
}
}

View File

@@ -34,7 +34,7 @@ class PostReplyCommandHandler
// opportunity to alter the post entity based on data in the command.
$post = CommentPost::reply(
$command->discussionId,
$command->content,
array_get($command->data, 'content'),
$user->id
);

View File

@@ -20,9 +20,9 @@ class RegisterUserCommandHandler
// Before persistance, though, fire an event to give plugins an
// opportunity to alter the post entity based on data in the command.
$user = User::register(
$command->username,
$command->email,
$command->password
array_get($command->data, 'username'),
array_get($command->data, 'email'),
array_get($command->data, 'password')
);
event(new UserWillBeSaved($user, $command));

View File

@@ -41,7 +41,7 @@ class StartDiscussionCommandHandler
// will trigger a domain event that is slightly semantically incorrect
// in this situation (PostWasPosted), we may need to reconsider someday.
$post = $this->bus->dispatch(
new PostReplyCommand($discussion->id, array_get($command->data, 'content'), $command->user)
new PostReplyCommand($discussion->id, $command->user, $command->data)
);
return $post->discussion;

View File

@@ -19,6 +19,6 @@ class FulltextGambit extends GambitAbstract
$searcher->query()->whereIn('id', $users);
$searcher->setDefaultSort($users);
$searcher->setDefaultSort(['id' => $users]);
}
}

View File

@@ -8,13 +8,10 @@ class UserSearchCriteria
public $sort;
public $order;
public function __construct($user, $query, $sort, $order)
public function __construct($user, $query, $sort)
{
$this->user = $user;
$this->query = $query;
$this->sort = $sort;
$this->order = $order;
}
}

View File

@@ -4,23 +4,18 @@ use Flarum\Core\Models\User;
use Flarum\Core\Search\SearcherInterface;
use Flarum\Core\Search\GambitManager;
use Flarum\Core\Repositories\UserRepositoryInterface;
use Flarum\Core\Events\UserSearchWillBePerformed;
class UserSearcher implements SearcherInterface
{
public $query;
protected $query;
protected $sortMap = [
'username' => ['username', 'asc'],
'posts' => ['comments_count', 'desc'],
'discussions' => ['discussions_count', 'desc'],
'lastActive' => ['last_seen_time', 'desc'],
'created' => ['join_time', 'asc']
];
protected $defaultSort = 'username';
protected $gambits;
protected $users;
protected $defaultSort = ['username' => 'asc'];
public function __construct(GambitManager $gambits, UserRepositoryInterface $users)
{
$this->gambits = $gambits;
@@ -37,7 +32,7 @@ class UserSearcher implements SearcherInterface
return $this->query->getQuery();
}
public function search(UserSearchCriteria $criteria, $count = null, $start = 0, $load = [])
public function search(UserSearchCriteria $criteria, $limit = null, $offset = 0, $load = [])
{
$this->user = $criteria->user;
$this->query = $this->users->query()->whereCan($criteria->user, 'view');
@@ -46,24 +41,23 @@ class UserSearcher implements SearcherInterface
$total = $this->query->count();
$sort = $criteria->sort;
if (empty($sort)) {
$sort = $this->defaultSort;
}
if (is_array($sort)) {
foreach ($sort as $id) {
$this->query->orderByRaw('id != '.(int) $id);
$sort = $criteria->sort ?: $this->defaultSort;
foreach ($sort as $field => $order) {
if (is_array($order)) {
foreach ($order as $value) {
$this->query->orderByRaw(snake_case($field).' != ?', [$value]);
}
} else {
$this->query->orderBy(snake_case($field), $order);
}
} else {
list($column, $order) = $this->sortMap[$sort];
$this->query->orderBy($column, $criteria->order ?: $order);
}
if ($start > 0) {
$this->query->skip($start);
if ($offset > 0) {
$this->query->skip($offset);
}
if ($count > 0) {
$this->query->take($count + 1);
if ($limit > 0) {
$this->query->take($limit + 1);
}
$users = $this->query->get();