mirror of
https://github.com/flarum/framework.git
synced 2025-02-14 02:42:53 +08:00
![Toby Zerner](/assets/img/avatar_default.png)
- 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
285 lines
8.0 KiB
PHP
285 lines
8.0 KiB
PHP
<?php namespace Flarum\Api;
|
|
|
|
use Flarum\Core\Users\Guest;
|
|
use Flarum\Http\RouteCollection;
|
|
use Flarum\Http\UrlGenerator;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
class ApiServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Register the service provider.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register()
|
|
{
|
|
$this->app->bind('flarum.actor', function () {
|
|
return new Guest;
|
|
});
|
|
|
|
$this->app->singleton(
|
|
'Flarum\Http\UrlGeneratorInterface',
|
|
function () {
|
|
return new UrlGenerator($this->app->make('flarum.api.routes'));
|
|
}
|
|
);
|
|
|
|
$this->app->register('Flarum\Locale\LocaleServiceProvider');
|
|
}
|
|
|
|
/**
|
|
* Bootstrap the application events.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function boot()
|
|
{
|
|
$this->routes();
|
|
}
|
|
|
|
protected function routes()
|
|
{
|
|
$this->app->instance('flarum.api.routes', $routes = new RouteCollection);
|
|
|
|
// Get forum information
|
|
$routes->get(
|
|
'/forum',
|
|
'flarum.api.forum.show',
|
|
$this->action('Flarum\Api\Actions\Forum\ShowAction')
|
|
);
|
|
|
|
// Retrieve authentication token
|
|
$routes->post(
|
|
'/token',
|
|
'flarum.api.token',
|
|
$this->action('Flarum\Api\Actions\TokenAction')
|
|
);
|
|
|
|
// Send forgot password email
|
|
$routes->post(
|
|
'/forgot',
|
|
'flarum.api.forgot',
|
|
$this->action('Flarum\Api\Actions\ForgotAction')
|
|
);
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Users
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
// List users
|
|
$routes->get(
|
|
'/users',
|
|
'flarum.api.users.index',
|
|
$this->action('Flarum\Api\Actions\Users\IndexAction')
|
|
);
|
|
|
|
// Register a user
|
|
$routes->post(
|
|
'/users',
|
|
'flarum.api.users.create',
|
|
$this->action('Flarum\Api\Actions\Users\CreateAction')
|
|
);
|
|
|
|
// Get a single user
|
|
$routes->get(
|
|
'/users/{id}',
|
|
'flarum.api.users.show',
|
|
$this->action('Flarum\Api\Actions\Users\ShowAction')
|
|
);
|
|
|
|
// Edit a user
|
|
$routes->patch(
|
|
'/users/{id}',
|
|
'flarum.api.users.update',
|
|
$this->action('Flarum\Api\Actions\Users\UpdateAction')
|
|
);
|
|
|
|
// Delete a user
|
|
$routes->delete(
|
|
'/users/{id}',
|
|
'flarum.api.users.delete',
|
|
$this->action('Flarum\Api\Actions\Users\DeleteAction')
|
|
);
|
|
|
|
// Upload avatar
|
|
$routes->post(
|
|
'/users/{id}/avatar',
|
|
'flarum.api.users.avatar.upload',
|
|
$this->action('Flarum\Api\Actions\Users\UploadAvatarAction')
|
|
);
|
|
|
|
// Remove avatar
|
|
$routes->delete(
|
|
'/users/{id}/avatar',
|
|
'flarum.api.users.avatar.delete',
|
|
$this->action('Flarum\Api\Actions\Users\DeleteAvatarAction')
|
|
);
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Activity
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
// List activity
|
|
$routes->get(
|
|
'/activity',
|
|
'flarum.api.activity.index',
|
|
$this->action('Flarum\Api\Actions\Activity\IndexAction')
|
|
);
|
|
|
|
// List notifications for the current user
|
|
$routes->get(
|
|
'/notifications',
|
|
'flarum.api.notifications.index',
|
|
$this->action('Flarum\Api\Actions\Notifications\IndexAction')
|
|
);
|
|
|
|
// Mark a single notification as read
|
|
$routes->patch(
|
|
'/notifications/{id}',
|
|
'flarum.api.notifications.update',
|
|
$this->action('Flarum\Api\Actions\Notifications\UpdateAction')
|
|
);
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Discussions
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
// List discussions
|
|
$routes->get(
|
|
'/discussions',
|
|
'flarum.api.discussions.index',
|
|
$this->action('Flarum\Api\Actions\Discussions\IndexAction')
|
|
);
|
|
|
|
// Create a discussion
|
|
$routes->post(
|
|
'/discussions',
|
|
'flarum.api.discussions.create',
|
|
$this->action('Flarum\Api\Actions\Discussions\CreateAction'));
|
|
|
|
// Show a single discussion
|
|
$routes->get(
|
|
'/discussions/{id}',
|
|
'flarum.api.discussions.show',
|
|
$this->action('Flarum\Api\Actions\Discussions\ShowAction')
|
|
);
|
|
|
|
// Edit a discussion
|
|
$routes->patch(
|
|
'/discussions/{id}',
|
|
'flarum.api.discussions.update',
|
|
$this->action('Flarum\Api\Actions\Discussions\UpdateAction')
|
|
);
|
|
|
|
// Delete a discussion
|
|
$routes->delete(
|
|
'/discussions/{id}',
|
|
'flarum.api.discussions.delete',
|
|
$this->action('Flarum\Api\Actions\Discussions\DeleteAction')
|
|
);
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Posts
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
// List posts, usually for a discussion
|
|
$routes->get(
|
|
'/posts',
|
|
'flarum.api.posts.index',
|
|
$this->action('Flarum\Api\Actions\Posts\IndexAction')
|
|
);
|
|
|
|
// Create a post
|
|
$routes->post(
|
|
'/posts',
|
|
'flarum.api.posts.create',
|
|
$this->action('Flarum\Api\Actions\Posts\CreateAction')
|
|
);
|
|
|
|
// Show a single or multiple posts by ID
|
|
$routes->get(
|
|
'/posts/{id}',
|
|
'flarum.api.posts.show',
|
|
$this->action('Flarum\Api\Actions\Posts\ShowAction')
|
|
);
|
|
|
|
// Edit a post
|
|
$routes->patch(
|
|
'/posts/{id}',
|
|
'flarum.api.posts.update',
|
|
$this->action('Flarum\Api\Actions\Posts\UpdateAction')
|
|
);
|
|
|
|
// Delete a post
|
|
$routes->delete(
|
|
'/posts/{id}',
|
|
'flarum.api.posts.delete',
|
|
$this->action('Flarum\Api\Actions\Posts\DeleteAction')
|
|
);
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Groups
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
// List groups
|
|
$routes->get(
|
|
'/groups',
|
|
'flarum.api.groups.index',
|
|
$this->action('Flarum\Api\Actions\Groups\IndexAction')
|
|
);
|
|
|
|
// Create a group
|
|
$routes->post(
|
|
'/groups',
|
|
'flarum.api.groups.create',
|
|
$this->action('Flarum\Api\Actions\Groups\CreateAction')
|
|
);
|
|
|
|
// Show a single group
|
|
$routes->get(
|
|
'/groups/{id}',
|
|
'flarum.api.groups.show',
|
|
$this->action('Flarum\Api\Actions\Groups\ShowAction')
|
|
);
|
|
|
|
// Edit a group
|
|
$routes->patch(
|
|
'/groups/{id}',
|
|
'flarum.api.groups.update',
|
|
$this->action('Flarum\Api\Actions\Groups\UpdateAction')
|
|
);
|
|
|
|
// Delete a group
|
|
$routes->delete(
|
|
'/groups/{id}',
|
|
'flarum.api.groups.delete',
|
|
$this->action('Flarum\Api\Actions\Groups\DeleteAction')
|
|
);
|
|
}
|
|
|
|
protected function action($class)
|
|
{
|
|
return function (ServerRequestInterface $httpRequest, $routeParams) use ($class) {
|
|
$action = app($class);
|
|
$actor = app('flarum.actor');
|
|
|
|
$input = array_merge($httpRequest->getQueryParams(), $httpRequest->getAttributes(), $routeParams);
|
|
$request = new Request($input, $actor, $httpRequest);
|
|
|
|
return $action->handle($request);
|
|
};
|
|
}
|
|
}
|