mirror of
https://github.com/flarum/core.git
synced 2025-07-24 18:21:33 +02:00
Add a serializer and API action to get information about the forum
This commit is contained in:
29
framework/core/src/Api/Actions/Forum/ShowAction.php
Normal file
29
framework/core/src/Api/Actions/Forum/ShowAction.php
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<?php namespace Flarum\Api\Actions\Forum;
|
||||||
|
|
||||||
|
use Flarum\Core\Models\Forum;
|
||||||
|
use Flarum\Api\Actions\SerializeResourceAction;
|
||||||
|
use Flarum\Api\JsonApiRequest;
|
||||||
|
use Flarum\Api\JsonApiResponse;
|
||||||
|
|
||||||
|
class ShowAction extends SerializeResourceAction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name of the serializer class to output results with.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public static $serializer = 'Flarum\Api\Serializers\ForumSerializer';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the forum, ready to be serialized and assigned to the JsonApi
|
||||||
|
* response.
|
||||||
|
*
|
||||||
|
* @param \Flarum\Api\JsonApiRequest $request
|
||||||
|
* @param \Flarum\Api\JsonApiResponse $response
|
||||||
|
* @return \Flarum\Core\Models\Forum
|
||||||
|
*/
|
||||||
|
protected function data(JsonApiRequest $request, JsonApiResponse $response)
|
||||||
|
{
|
||||||
|
return app('flarum.forum');
|
||||||
|
}
|
||||||
|
}
|
@@ -4,6 +4,7 @@ use Tobscure\JsonApi\SerializerAbstract;
|
|||||||
use Flarum\Api\Events\SerializeAttributes;
|
use Flarum\Api\Events\SerializeAttributes;
|
||||||
use Flarum\Api\Events\SerializeRelationship;
|
use Flarum\Api\Events\SerializeRelationship;
|
||||||
use Flarum\Support\Actor;
|
use Flarum\Support\Actor;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||||
use Closure;
|
use Closure;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -54,7 +55,16 @@ abstract class BaseSerializer extends SerializerAbstract
|
|||||||
$data = $relation($model, $include);
|
$data = $relation($model, $include);
|
||||||
} else {
|
} else {
|
||||||
if ($include) {
|
if ($include) {
|
||||||
$data = !is_null($model->$relation) ? $model->$relation : $model->$relation()->getResults();
|
if (! is_null($model->$relation)) {
|
||||||
|
$data = $model->$relation;
|
||||||
|
} else {
|
||||||
|
$relation = $model->$relation();
|
||||||
|
if ($relation instanceof Relation) {
|
||||||
|
$data = $relation->getResults();
|
||||||
|
} else {
|
||||||
|
$data = $relation->get();
|
||||||
|
}
|
||||||
|
}
|
||||||
} elseif ($many) {
|
} elseif ($many) {
|
||||||
$relationIds = $relation.'_ids';
|
$relationIds = $relation.'_ids';
|
||||||
$data = $model->$relationIds ?: $model->$relation()->get(['id'])->fetch('id')->all();
|
$data = $model->$relationIds ?: $model->$relation()->get(['id'])->fetch('id')->all();
|
||||||
|
31
framework/core/src/Api/Serializers/ForumSerializer.php
Normal file
31
framework/core/src/Api/Serializers/ForumSerializer.php
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<?php namespace Flarum\Api\Serializers;
|
||||||
|
|
||||||
|
class ForumSerializer extends BaseSerializer
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The resource type.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $type = 'forums';
|
||||||
|
|
||||||
|
protected function id($forum)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serialize attributes of a Forum model for JSON output.
|
||||||
|
*
|
||||||
|
* @param Forum $forum The Forum model to serialize.
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function attributes($forum)
|
||||||
|
{
|
||||||
|
$attributes = [
|
||||||
|
'title' => $forum->title
|
||||||
|
];
|
||||||
|
|
||||||
|
return $this->extendAttributes($forum, $attributes);
|
||||||
|
}
|
||||||
|
}
|
@@ -28,6 +28,8 @@ class Discussion extends Model
|
|||||||
'last_post_number' => 'integer'
|
'last_post_number' => 'integer'
|
||||||
];
|
];
|
||||||
|
|
||||||
|
protected static $relationships = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The table associated with the model.
|
* The table associated with the model.
|
||||||
*
|
*
|
||||||
|
@@ -7,6 +7,8 @@ class Forum extends Model
|
|||||||
{
|
{
|
||||||
use Permissible;
|
use Permissible;
|
||||||
|
|
||||||
|
protected static $relationships = [];
|
||||||
|
|
||||||
public function getTitleAttribute()
|
public function getTitleAttribute()
|
||||||
{
|
{
|
||||||
return Core::config('forum_title');
|
return Core::config('forum_title');
|
||||||
|
@@ -22,10 +22,18 @@ class IndexAction extends BaseAction
|
|||||||
public function handle(Request $request, $params = [])
|
public function handle(Request $request, $params = [])
|
||||||
{
|
{
|
||||||
$config = DB::table('config')->whereIn('key', ['base_url', 'api_url', 'forum_title', 'welcome_title', 'welcome_message'])->lists('value', 'key');
|
$config = DB::table('config')->whereIn('key', ['base_url', 'api_url', 'forum_title', 'welcome_title', 'welcome_message'])->lists('value', 'key');
|
||||||
$data = [];
|
|
||||||
$session = [];
|
$session = [];
|
||||||
$alert = Session::get('alert');
|
$alert = Session::get('alert');
|
||||||
|
|
||||||
|
$response = app('Flarum\Api\Actions\Forum\ShowAction')
|
||||||
|
->handle(new ApiRequest([], $this->actor))
|
||||||
|
->content->toArray();
|
||||||
|
|
||||||
|
$data = [$response['data']];
|
||||||
|
if (isset($response['included'])) {
|
||||||
|
$data = array_merge($data, $response['included']);
|
||||||
|
}
|
||||||
|
|
||||||
if (($user = $this->actor->getUser()) && $user->exists) {
|
if (($user = $this->actor->getUser()) && $user->exists) {
|
||||||
$session = [
|
$session = [
|
||||||
'userId' => $user->id,
|
'userId' => $user->id,
|
||||||
@@ -36,7 +44,7 @@ class IndexAction extends BaseAction
|
|||||||
->handle(new ApiRequest(['id' => $user->id], $this->actor))
|
->handle(new ApiRequest(['id' => $user->id], $this->actor))
|
||||||
->content->toArray();
|
->content->toArray();
|
||||||
|
|
||||||
$data = [$response['data']];
|
$data = array_merge($data, [$response['data']]);
|
||||||
if (isset($response['included'])) {
|
if (isset($response['included'])) {
|
||||||
$data = array_merge($data, $response['included']);
|
$data = array_merge($data, $response['included']);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user