2015-08-27 07:40:18 +08:00
|
|
|
<?php
|
2015-08-26 14:48:58 +08:00
|
|
|
/*
|
|
|
|
* 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\Api;
|
2015-02-24 18:03:18 +08:00
|
|
|
|
2015-07-18 21:29:47 +08:00
|
|
|
use Flarum\Api\Serializers\ActivitySerializer;
|
|
|
|
use Flarum\Api\Serializers\NotificationSerializer;
|
2015-07-04 10:54:48 +08:00
|
|
|
use Flarum\Core\Users\Guest;
|
2015-07-18 21:29:47 +08:00
|
|
|
use Flarum\Events\RegisterApiRoutes;
|
|
|
|
use Flarum\Events\RegisterActivityTypes;
|
|
|
|
use Flarum\Events\RegisterNotificationTypes;
|
2015-06-17 06:16:35 +08:00
|
|
|
use Flarum\Http\RouteCollection;
|
|
|
|
use Flarum\Http\UrlGenerator;
|
2015-02-24 18:03:18 +08:00
|
|
|
use Illuminate\Support\ServiceProvider;
|
2015-06-17 06:16:35 +08:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2015-02-24 18:03:18 +08:00
|
|
|
|
|
|
|
class ApiServiceProvider extends ServiceProvider
|
|
|
|
{
|
2015-06-17 06:16:35 +08:00
|
|
|
/**
|
|
|
|
* Register the service provider.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function register()
|
|
|
|
{
|
2015-07-04 10:54:48 +08:00
|
|
|
$this->app->bind('flarum.actor', function () {
|
|
|
|
return new Guest;
|
|
|
|
});
|
2015-06-17 06:16:35 +08:00
|
|
|
|
|
|
|
$this->app->singleton(
|
|
|
|
'Flarum\Http\UrlGeneratorInterface',
|
|
|
|
function () {
|
|
|
|
return new UrlGenerator($this->app->make('flarum.api.routes'));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-02-24 18:03:18 +08:00
|
|
|
/**
|
|
|
|
* Bootstrap the application events.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function boot()
|
|
|
|
{
|
2015-06-17 06:16:35 +08:00
|
|
|
$this->routes();
|
2015-07-18 21:29:47 +08:00
|
|
|
|
|
|
|
$this->registerNotificationSerializers();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register notification serializers.
|
|
|
|
*/
|
|
|
|
protected function registerNotificationSerializers()
|
|
|
|
{
|
|
|
|
$blueprints = [];
|
|
|
|
$serializers = [
|
|
|
|
'discussionRenamed' => 'Flarum\Api\Serializers\DiscussionBasicSerializer'
|
|
|
|
];
|
|
|
|
|
|
|
|
event(new RegisterNotificationTypes($blueprints, $serializers));
|
|
|
|
|
|
|
|
foreach ($serializers as $type => $serializer) {
|
|
|
|
NotificationSerializer::setSubjectSerializer($type, $serializer);
|
|
|
|
}
|
2015-06-17 06:16:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function routes()
|
|
|
|
{
|
|
|
|
$this->app->instance('flarum.api.routes', $routes = new RouteCollection);
|
|
|
|
|
|
|
|
// Get forum information
|
|
|
|
$routes->get(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/forum',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.forum.show',
|
|
|
|
$this->action('Flarum\Api\Actions\Forum\ShowAction')
|
|
|
|
);
|
|
|
|
|
2015-07-29 19:30:09 +08:00
|
|
|
// Save forum information
|
|
|
|
$routes->patch(
|
|
|
|
'/forum',
|
|
|
|
'flarum.api.forum.update',
|
|
|
|
$this->action('Flarum\Api\Actions\Forum\UpdateAction')
|
|
|
|
);
|
|
|
|
|
2015-06-17 06:16:35 +08:00
|
|
|
// Retrieve authentication token
|
|
|
|
$routes->post(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/token',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.token',
|
|
|
|
$this->action('Flarum\Api\Actions\TokenAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Send forgot password email
|
|
|
|
$routes->post(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/forgot',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.forgot',
|
|
|
|
$this->action('Flarum\Api\Actions\ForgotAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Users
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
// List users
|
|
|
|
$routes->get(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/users',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.users.index',
|
|
|
|
$this->action('Flarum\Api\Actions\Users\IndexAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Register a user
|
|
|
|
$routes->post(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/users',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.users.create',
|
|
|
|
$this->action('Flarum\Api\Actions\Users\CreateAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Get a single user
|
|
|
|
$routes->get(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/users/{id}',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.users.show',
|
|
|
|
$this->action('Flarum\Api\Actions\Users\ShowAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Edit a user
|
2015-07-04 10:54:48 +08:00
|
|
|
$routes->patch(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/users/{id}',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.users.update',
|
|
|
|
$this->action('Flarum\Api\Actions\Users\UpdateAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Delete a user
|
|
|
|
$routes->delete(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/users/{id}',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.users.delete',
|
|
|
|
$this->action('Flarum\Api\Actions\Users\DeleteAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Upload avatar
|
|
|
|
$routes->post(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/users/{id}/avatar',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.users.avatar.upload',
|
|
|
|
$this->action('Flarum\Api\Actions\Users\UploadAvatarAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Remove avatar
|
|
|
|
$routes->delete(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/users/{id}/avatar',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.users.avatar.delete',
|
|
|
|
$this->action('Flarum\Api\Actions\Users\DeleteAvatarAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Activity
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
// List activity
|
|
|
|
$routes->get(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/activity',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.activity.index',
|
|
|
|
$this->action('Flarum\Api\Actions\Activity\IndexAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// List notifications for the current user
|
|
|
|
$routes->get(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/notifications',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.notifications.index',
|
|
|
|
$this->action('Flarum\Api\Actions\Notifications\IndexAction')
|
|
|
|
);
|
|
|
|
|
2015-09-29 15:11:05 +08:00
|
|
|
// Mark all notifications as read
|
|
|
|
$routes->post(
|
|
|
|
'/notifications/read',
|
|
|
|
'flarum.api.notifications.readAll',
|
|
|
|
$this->action('Flarum\Api\Actions\Notifications\ReadAllAction')
|
|
|
|
);
|
|
|
|
|
2015-06-17 06:16:35 +08:00
|
|
|
// Mark a single notification as read
|
2015-07-04 10:54:48 +08:00
|
|
|
$routes->patch(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/notifications/{id}',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.notifications.update',
|
|
|
|
$this->action('Flarum\Api\Actions\Notifications\UpdateAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Discussions
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
// List discussions
|
|
|
|
$routes->get(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/discussions',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.discussions.index',
|
|
|
|
$this->action('Flarum\Api\Actions\Discussions\IndexAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Create a discussion
|
|
|
|
$routes->post(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/discussions',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.discussions.create',
|
2015-07-05 20:16:57 +08:00
|
|
|
$this->action('Flarum\Api\Actions\Discussions\CreateAction')
|
|
|
|
);
|
2015-06-17 06:16:35 +08:00
|
|
|
|
|
|
|
// Show a single discussion
|
|
|
|
$routes->get(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/discussions/{id}',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.discussions.show',
|
|
|
|
$this->action('Flarum\Api\Actions\Discussions\ShowAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Edit a discussion
|
2015-07-04 10:54:48 +08:00
|
|
|
$routes->patch(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/discussions/{id}',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.discussions.update',
|
|
|
|
$this->action('Flarum\Api\Actions\Discussions\UpdateAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Delete a discussion
|
|
|
|
$routes->delete(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/discussions/{id}',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.discussions.delete',
|
|
|
|
$this->action('Flarum\Api\Actions\Discussions\DeleteAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Posts
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
// List posts, usually for a discussion
|
|
|
|
$routes->get(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/posts',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.posts.index',
|
|
|
|
$this->action('Flarum\Api\Actions\Posts\IndexAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Create a post
|
|
|
|
$routes->post(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/posts',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.posts.create',
|
|
|
|
$this->action('Flarum\Api\Actions\Posts\CreateAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Show a single or multiple posts by ID
|
|
|
|
$routes->get(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/posts/{id}',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.posts.show',
|
|
|
|
$this->action('Flarum\Api\Actions\Posts\ShowAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Edit a post
|
2015-07-04 10:54:48 +08:00
|
|
|
$routes->patch(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/posts/{id}',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.posts.update',
|
|
|
|
$this->action('Flarum\Api\Actions\Posts\UpdateAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Delete a post
|
|
|
|
$routes->delete(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/posts/{id}',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.posts.delete',
|
|
|
|
$this->action('Flarum\Api\Actions\Posts\DeleteAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Groups
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
// List groups
|
|
|
|
$routes->get(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/groups',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.groups.index',
|
|
|
|
$this->action('Flarum\Api\Actions\Groups\IndexAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Create a group
|
|
|
|
$routes->post(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/groups',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.groups.create',
|
|
|
|
$this->action('Flarum\Api\Actions\Groups\CreateAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Edit a group
|
2015-07-04 10:54:48 +08:00
|
|
|
$routes->patch(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/groups/{id}',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.groups.update',
|
|
|
|
$this->action('Flarum\Api\Actions\Groups\UpdateAction')
|
|
|
|
);
|
2015-05-27 06:29:31 +08:00
|
|
|
|
2015-06-17 06:16:35 +08:00
|
|
|
// Delete a group
|
|
|
|
$routes->delete(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/groups/{id}',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.groups.delete',
|
|
|
|
$this->action('Flarum\Api\Actions\Groups\DeleteAction')
|
|
|
|
);
|
2015-07-18 21:29:47 +08:00
|
|
|
|
2015-08-03 10:33:30 +08:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
2015-08-04 09:10:04 +08:00
|
|
|
| Administration
|
2015-08-03 10:33:30 +08:00
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Toggle an extension
|
|
|
|
$routes->patch(
|
|
|
|
'/extensions/{name}',
|
|
|
|
'flarum.api.extensions.update',
|
|
|
|
$this->action('Flarum\Api\Actions\Extensions\UpdateAction')
|
2015-08-14 11:18:29 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
// Uninstall an extension
|
|
|
|
$routes->delete(
|
|
|
|
'/extensions/{name}',
|
|
|
|
'flarum.api.extensions.delete',
|
|
|
|
$this->action('Flarum\Api\Actions\Extensions\DeleteAction')
|
2015-08-03 10:33:30 +08:00
|
|
|
);
|
|
|
|
|
2015-08-04 09:10:04 +08:00
|
|
|
// Update config settings
|
|
|
|
$routes->post(
|
|
|
|
'/config',
|
|
|
|
'flarum.api.config',
|
|
|
|
$this->action('Flarum\Api\Actions\ConfigAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Update a permission
|
|
|
|
$routes->post(
|
|
|
|
'/permission',
|
|
|
|
'flarum.api.permission',
|
|
|
|
$this->action('Flarum\Api\Actions\PermissionAction')
|
|
|
|
);
|
|
|
|
|
2015-07-18 21:29:47 +08:00
|
|
|
event(new RegisterApiRoutes($routes));
|
2015-02-24 18:03:18 +08:00
|
|
|
}
|
|
|
|
|
2015-06-17 06:16:35 +08:00
|
|
|
protected function action($class)
|
2015-02-24 18:03:18 +08:00
|
|
|
{
|
2015-06-17 06:16:35 +08:00
|
|
|
return function (ServerRequestInterface $httpRequest, $routeParams) use ($class) {
|
|
|
|
$action = app($class);
|
2015-07-04 10:54:48 +08:00
|
|
|
$actor = app('flarum.actor');
|
2015-06-17 06:16:35 +08:00
|
|
|
|
2015-08-04 19:52:34 +08:00
|
|
|
$input = array_merge(
|
|
|
|
$httpRequest->getQueryParams(),
|
|
|
|
$httpRequest->getAttributes(),
|
|
|
|
$httpRequest->getParsedBody(),
|
|
|
|
$routeParams
|
|
|
|
);
|
|
|
|
|
2015-06-17 06:16:35 +08:00
|
|
|
$request = new Request($input, $actor, $httpRequest);
|
|
|
|
|
|
|
|
return $action->handle($request);
|
|
|
|
};
|
2015-02-24 18:03:18 +08:00
|
|
|
}
|
|
|
|
}
|