Skip to content

Commit

Permalink
chore(2.0): try to refactor the remainder of the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideIadeluca committed Dec 18, 2024
1 parent 18f1d53 commit b72a314
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
27 changes: 10 additions & 17 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@

namespace FoF\FrontPage;

use Flarum\Api\Controller\ListDiscussionsController;
use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Search\Database\DatabaseSearchDriver;
use Flarum\Discussion\Discussion;
use Flarum\Discussion\Event\Saving;
use Flarum\Discussion\Filter\DiscussionFilterer;
use Flarum\Discussion\Search\DiscussionSearcher;
use Flarum\Extend;
use Flarum\Api\Context;
use Flarum\Api\Endpoint;
use Flarum\Api\Resource;
use Flarum\Api\Schema;
use Flarum\Api\Sort\SortColumn;
use FoF\FrontPage\Api\DiscussionResourceFields;

return [
(new Extend\Frontend('forum'))
Expand All @@ -32,27 +28,24 @@
->js(__DIR__.'/js/dist/admin.js'),
new Extend\Locales(__DIR__.'/resources/locale'),

(new Extend\Event())
->listen(Saving::class, Listeners\SaveFrontToDatabase::class),

(new Extend\Model(Discussion::class))
->cast('frontdate', 'datetime')
->cast('frontpage', 'bool'),

// @TODO: Replace with the new implementation https://docs.flarum.org/2.x/extend/api#extending-api-resources
(new Extend\ApiSerializer(DiscussionSerializer::class))
->attributes(Listeners\AddApiAttributes::class),
(new Extend\ApiResource(Resource\DiscussionResource::class))
->fields(DiscussionResourceFields::class),

// @TODO: Replace with the new implementation https://docs.flarum.org/2.x/extend/api#extending-api-resources
(new Extend\ApiController(ListDiscussionsController::class))
->addSortField('frontdate'),
(new Extend\ApiResource(Resource\DiscussionResource::class))
->sorts(fn () => [
SortColumn::make('frontdate'),
]),

(new Extend\ServiceProvider())
->register(Provider\FrontpageSortmapProvider::class),

(new Extend\Middleware('forum'))
->add(Middleware\AddFrontpageFilter::class),

(new Extend\SearchDriver(\Flarum\Search\Database\DatabaseSearchDriver::class))
(new Extend\SearchDriver(DatabaseSearchDriver::class))
->addFilter(DiscussionSearcher::class, Query\FrontFilter::class),
];
29 changes: 29 additions & 0 deletions src/Api/DiscussionResourceFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace FoF\FrontPage\Api;

use Flarum\Api\Context;
use Flarum\Api\Endpoint\Update;
use Flarum\Api\Schema\Boolean;
use Flarum\Api\Schema\Date;
use Flarum\Discussion\Discussion;

class DiscussionResourceFields
{
public function __invoke(): array
{
return [
Boolean::make('frontpage')
->writable(function (Discussion $discussion, Context $context) {
return $context->endpoint instanceof Update
&& $context->getActor()->can('front', $discussion);
}),

Date::make('frontdate')
->get(fn (Discussion $discussion) => $discussion->frontdate),

Boolean::make('front')
->get(fn (Discussion $discussion, Context $context) => $context->getActor()->can('front', $discussion)),
];
}
}

0 comments on commit b72a314

Please sign in to comment.