Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: bind repository for easier extending, restructure php layout #110

Merged
merged 12 commits into from
Nov 12, 2024
40 changes: 16 additions & 24 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@

namespace FoF\BestAnswer;

use Flarum\Api\Controller\ListDiscussionsController;
use Flarum\Api\Controller\ListPostsController;
use Flarum\Api\Controller\ListUsersController;
use Flarum\Api\Controller\ShowDiscussionController;
use Flarum\Api\Controller\ShowPostController;
use Flarum\Api\Controller\UpdateDiscussionController;
use Flarum\Api\Controller;
use Flarum\Api\Serializer;
use Flarum\Discussion\Discussion;
use Flarum\Discussion\Event\Saving as DiscussionSaving;
Expand All @@ -29,7 +24,6 @@
use Flarum\Tags\Api\Serializer\TagSerializer;
use Flarum\Tags\Tag;
use Flarum\User\User;
use FoF\BestAnswer\Events\BestAnswerSet;

return [
(new Extend\Frontend('forum'))
Expand All @@ -42,6 +36,9 @@

new Extend\Locales(__DIR__.'/resources/locale'),

(new Extend\ServiceProvider())
->register(Providers\BestAnswerServiceProvider::class),

(new Extend\Model(Discussion::class))
->belongsTo('bestAnswerPost', Post::class, 'best_answer_post_id')
->belongsTo('bestAnswerUser', User::class, 'best_answer_user_id')
Expand All @@ -62,7 +59,7 @@

(new Extend\Event())
->listen(DiscussionSaving::class, Listeners\SaveBestAnswerToDatabase::class)
->listen(BestAnswerSet::class, Listeners\QueueNotificationJobs::class)
->listen(Events\BestAnswerSet::class, Listeners\QueueNotificationJobs::class)
->subscribe(Listeners\RecalculateBestAnswerCounts::class)
->listen(SettingsSaving::class, Listeners\SaveTagSettings::class),

Expand All @@ -72,17 +69,17 @@
->type(Notification\BestAnswerSetInDiscussionBlueprint::class, Serializer\BasicDiscussionSerializer::class, []),

(new Extend\ApiSerializer(Serializer\DiscussionSerializer::class))
->attributes(DiscussionAttributes::class),
->attributes(Api\DiscussionAttributes::class),

(new Extend\ApiSerializer(Serializer\BasicDiscussionSerializer::class))
->hasOne('bestAnswerPost', Serializer\BasicPostSerializer::class)
->hasOne('bestAnswerUser', Serializer\BasicUserSerializer::class)
->attributes(BasicDiscussionAttributes::class),
->attributes(Api\BasicDiscussionAttributes::class),

(new Extend\ApiSerializer(Serializer\UserSerializer::class))
->attributes(UserBestAnswerCount::class),
->attributes(Api\UserBestAnswerCount::class),

(new Extend\ApiController(ListUsersController::class))
(new Extend\ApiController(Controller\ListUsersController::class))
->addSortField('bestAnswerCount'),

(new Extend\Settings())
Expand All @@ -97,23 +94,23 @@
->serializeToForum('fof-best-answer.show_max_lines', 'fof-best-answer.show_max_lines', 'intVal'),

(new Extend\ApiSerializer(Serializer\ForumSerializer::class))
->attributes(ForumAttributes::class),
->attributes(Api\ForumAttributes::class),

(new Extend\ApiController(ShowDiscussionController::class))
(new Extend\ApiController(Controller\ShowDiscussionController::class))
->addInclude(['bestAnswerPost', 'bestAnswerUser', 'bestAnswerPost.user'])
->load(['bestAnswerPost', 'bestAnswerPost.user']),

(new Extend\ApiController(ListDiscussionsController::class))
(new Extend\ApiController(Controller\ListDiscussionsController::class))
->addOptionalInclude(['bestAnswerPost', 'bestAnswerUser', 'bestAnswerPost.discussion', 'bestAnswerPost.user']),

(new Extend\ApiController(UpdateDiscussionController::class))
(new Extend\ApiController(Controller\UpdateDiscussionController::class))
->addOptionalInclude('tags'),

(new Extend\ApiController(ListPostsController::class))
(new Extend\ApiController(Controller\ListPostsController::class))
->addInclude(['discussion', 'discussion.bestAnswerPost', 'discussion.bestAnswerUser', 'discussion.bestAnswerPost.user'])
->load(['discussion', 'discussion.bestAnswerUser', 'discussion.bestAnswerPost', 'discussion.bestAnswerPost.user']),

(new Extend\ApiController(ShowPostController::class))
(new Extend\ApiController(Controller\ShowPostController::class))
->addInclude(['discussion', 'discussion.bestAnswerPost', 'discussion.bestAnswerUser', 'discussion.bestAnswerPost.user'])
->load(['discussion', 'discussion.bestAnswerUser', 'discussion.bestAnswerPost', 'discussion.bestAnswerPost.user']),

Expand All @@ -132,10 +129,5 @@
->addFilter(Search\BestAnswerPostFilter::class),

(new Extend\ApiSerializer(TagSerializer::class))
->attributes(function (TagSerializer $serializer, Tag $tag, array $attributes) {
$attributes['isQnA'] = (bool) $tag->is_qna;
$attributes['reminders'] = (bool) $tag->qna_reminders;

return $attributes;
}),
->attributes(Api\AddTagAttributes::class),
];
2 changes: 1 addition & 1 deletion js/src/forum/extend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default [
new Extend.Model(Discussion) //
.hasOne<Post>('bestAnswerPost')
.hasOne<User>('bestAnswerUser')
.attribute<boolean>('hasBestAnswer')
.attribute<boolean | number>('hasBestAnswer')
.attribute<boolean>('canSelectBestAnswer')
.attribute('bestAnswerSetAt', Model.transformDate),

Expand Down
26 changes: 26 additions & 0 deletions src/Api/AddTagAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of fof/best-answer.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\BestAnswer\Api;

use Flarum\Tags\Api\Serializer\TagSerializer;
use Flarum\Tags\Tag;

class AddTagAttributes
{
public function __invoke(TagSerializer $serializer, Tag $tag, array $attributes): array
{
$attributes['isQnA'] = (bool) $tag->is_qna;
$attributes['reminders'] = (bool) $tag->qna_reminders;

return $attributes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace FoF\BestAnswer;
namespace FoF\BestAnswer\Api;

use Flarum\Api\Serializer\BasicDiscussionSerializer;
use Flarum\Discussion\Discussion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
* file that was distributed with this source code.
*/

namespace FoF\BestAnswer;
namespace FoF\BestAnswer\Api;

use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Discussion\Discussion;
use FoF\BestAnswer\Repository\BestAnswerRepository;

class DiscussionAttributes
{
Expand Down
2 changes: 1 addition & 1 deletion src/ForumAttributes.php → src/Api/ForumAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace FoF\BestAnswer;
namespace FoF\BestAnswer\Api;

use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Settings\SettingsRepositoryInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
* file that was distributed with this source code.
*/

namespace FoF\BestAnswer;
namespace FoF\BestAnswer\Api;

use Flarum\Api\Serializer\UserSerializer;
use Flarum\User\User;
use FoF\BestAnswer\Repository\BestAnswerRepository;

class UserBestAnswerCount
{
Expand Down
103 changes: 0 additions & 103 deletions src/BestAnswerRepository.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Console/UpdateBestAnswerCounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace FoF\BestAnswer\Console;

use Flarum\User\User;
use FoF\BestAnswer\BestAnswerRepository;
use FoF\BestAnswer\Repository\BestAnswerRepository;
use Illuminate\Console\Command;

class UpdateBestAnswerCounts extends Command
Expand Down
Loading
Loading