2015-02-24 18:03:18 +08:00
|
|
|
<?php namespace Flarum\Api;
|
|
|
|
|
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-07-01 21:04:11 +08:00
|
|
|
|
|
|
|
$this->app->register('Flarum\Locale\LocaleServiceProvider');
|
2015-06-17 06:16:35 +08:00
|
|
|
}
|
|
|
|
|
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->registerActivitySerializers();
|
|
|
|
$this->registerNotificationSerializers();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register activity serializers.
|
|
|
|
*/
|
|
|
|
protected function registerActivitySerializers()
|
|
|
|
{
|
|
|
|
$blueprints = [];
|
|
|
|
$serializers = [
|
|
|
|
'posted' => 'Flarum\Api\Serializers\PostBasicSerializer',
|
|
|
|
'startedDiscussion' => 'Flarum\Api\Serializers\PostBasicSerializer',
|
|
|
|
'joined' => 'Flarum\Api\Serializers\UserSerializer'
|
|
|
|
];
|
|
|
|
|
|
|
|
event(new RegisterActivityTypes($blueprints, $serializers));
|
|
|
|
|
|
|
|
foreach ($serializers as $type => $serializer) {
|
|
|
|
ActivitySerializer::setSubjectSerializer($type, $serializer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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')
|
|
|
|
);
|
|
|
|
|
|
|
|
// 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')
|
|
|
|
);
|
|
|
|
|
|
|
|
// 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')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Show a single group
|
|
|
|
$routes->get(
|
2015-06-17 16:44:41 +08:00
|
|
|
'/groups/{id}',
|
2015-06-17 06:16:35 +08:00
|
|
|
'flarum.api.groups.show',
|
|
|
|
$this->action('Flarum\Api\Actions\Groups\ShowAction')
|
|
|
|
);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
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-06-18 10:54:51 +08:00
|
|
|
$input = array_merge($httpRequest->getQueryParams(), $httpRequest->getAttributes(), $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
|
|
|
}
|
|
|
|
}
|