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

refactor: flarum 2.0 upgrade #62

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2

[*.{diff,md}]
trim_trailing_whitespace = false

[*.{php,xml,json}]
indent_size = 4
[*.md]

[{tsconfig.json,prettierrc.json}]
indent_size = 2
trim_trailing_whitespace = false

[*.neon]
indent_style = tab
2 changes: 1 addition & 1 deletion .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [workflow_dispatch, push, pull_request]

jobs:
run:
uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@main
uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@2.x
with:
enable_backend_testing: false
enable_phpstan: true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/frontend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [workflow_dispatch, push, pull_request]

jobs:
run:
uses: flarum/framework/.github/workflows/REUSABLE_frontend.yml@main
uses: flarum/framework/.github/workflows/REUSABLE_frontend.yml@2.x
with:
enable_bundlewatch: false
enable_prettier: true
Expand Down
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
"forum": "https://discuss.flarum.org/d/1903"
},
"require": {
"flarum/core": "^1.2.0"
"flarum/core": "^2.0.0"
},
"extra": {
"branch-alias": {
"dev-master": "2.0-dev"
},
"flarum-extension": {
"title": "FoF Split",
"category": "feature",
Expand Down Expand Up @@ -62,5 +65,7 @@
},
"scripts-descriptions": {
"analyse:phpstan": "Run static analysis"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
16 changes: 8 additions & 8 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@

namespace FoF\Split;

use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Database\AbstractModel;
use Flarum\Discussion\Event\Renamed;
use Flarum\Extend;
use FoF\Split\Events\DiscussionWasSplit;
use FoF\Split\Posts\DiscussionSplitPost;
use Flarum\Api\Context;
use Flarum\Api\Resource;
use Flarum\Api\Schema;

return [
(new Extend\Frontend('admin'))
Expand All @@ -38,10 +39,9 @@
(new Extend\Post())
->type(DiscussionSplitPost::class),

(new Extend\ApiSerializer(DiscussionSerializer::class))
->attributes(function (DiscussionSerializer $serializer, AbstractModel $discussion, array $attributes): array {
$attributes['canSplit'] = $serializer->getActor()->can('split', $discussion);

return $attributes;
}),
(new Extend\ApiResource(Resource\DiscussionResource::class))
->fields(fn (): array => [
Schema\Boolean::make('canSplit')
->get(fn ($discussion, Context $context) => $context->getActor()->can('split', $discussion)),
]),
];
4 changes: 2 additions & 2 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"prettier": "@flarum/prettier-config",
"dependencies": {
"@flarum/prettier-config": "^1.0.0",
"flarum-tsconfig": "^1.0.2",
"flarum-webpack-config": "^2.0.0",
"flarum-tsconfig": "^2.0.0",
"flarum-webpack-config": "^3.0.0",
"webpack": "^5.94.0",
"webpack-cli": "^5.1.4"
},
Expand Down
13 changes: 13 additions & 0 deletions js/src/admin/extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Extend from 'flarum/common/extenders';

export default [
(new Extend.Admin())
.permission(() =>
({
icon: 'fas fa-code-branch',
label: app.translator.trans('fof-split.admin.permissions.split_discussion_label'),
permission: 'discussion.split',
}),
'moderate'
)
];
11 changes: 3 additions & 8 deletions js/src/admin/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import app from 'flarum/admin/app';

export { default as extend } from './extend';

app.initializers.add('fof-split', () => {
app.extensionData.for('fof-split').registerPermission(
{
icon: 'fas fa-code-branch',
label: app.translator.trans('fof-split.admin.permissions.split_discussion_label'),
permission: 'discussion.split',
},
'moderate'
);
//
});
2 changes: 1 addition & 1 deletion js/src/forum/components/DiscussionSplit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import EventPost from 'flarum/common/components/EventPost';
import EventPost from 'flarum/forum/components/EventPost';
import Link from 'flarum/common/components/Link';

export default class DiscussionSplit extends EventPost {
Expand Down
9 changes: 5 additions & 4 deletions js/src/forum/components/SplitPostModal.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import Form from 'flarum/common/components/Form';
import app from 'flarum/forum/app';
import Modal from 'flarum/common/components/Modal';
import FormModal from 'flarum/common/components/FormModal';
import Button from 'flarum/common/components/Button';
import Stream from 'flarum/common/utils/Stream';

export default class SplitPostModal extends Modal {
export default class SplitPostModal extends FormModal {
oninit(vnode) {
super.oninit(vnode);

Expand All @@ -21,7 +22,7 @@ export default class SplitPostModal extends Modal {
content() {
return [
<div className="Modal-body">
<div className="Form Form--centered">
<Form className="Form--centered">
<div className="Form-group">
<label for="new_discussion_title">{app.translator.trans('fof-split.forum.modal.new_discussion_label')}</label>
<input className="FormControl" name="new_discussion_title" bidi={this.newDiscussionTitle} />
Expand All @@ -31,7 +32,7 @@ export default class SplitPostModal extends Modal {
{app.translator.trans('fof-split.forum.modal.submit_button')}
</Button>
</div>
</div>
</Form>
</div>,
];
}
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ parameters:
- src
excludePaths:
- *.blade.php
checkMissingIterableValueType: false
databaseMigrationsPath: ['migrations']

2 changes: 0 additions & 2 deletions src/Api/Commands/SplitDiscussionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ protected function assignPostsToDiscussion(
->whereBetween('number', [$start_post_number, $end_post_number])
->update(['discussion_id' => $discussion->id]);

$discussion->post_number_index = $end_post_number;
$discussion->save();

// Update relationship posts on new discussion.
Expand All @@ -169,7 +168,6 @@ protected function renumberDiscussion(Discussion $discussion)
$post->save();
});

$discussion->post_number_index = $number;
$discussion->save();
}

Expand Down
52 changes: 20 additions & 32 deletions src/Api/Controllers/SplitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,42 @@

namespace FoF\Split\Api\Controllers;

use Flarum\Api\Controller\AbstractShowController;
use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Api\JsonApi;
use Flarum\Api\Resource\DiscussionResource;
use Flarum\Http\RequestUtil;
use FoF\Split\Api\Commands\SplitDiscussion;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
use Psr\Http\Server\RequestHandlerInterface;

class SplitController extends AbstractShowController
class SplitController implements RequestHandlerInterface
{
/**
* The serializer instance for this request.
*
* @var string
*/
public $serializer = DiscussionSerializer::class;

/**
* @var Dispatcher
*/
protected $bus;

/**
* @param Dispatcher $bus
*/
public function __construct(Dispatcher $bus)
{
$this->bus = $bus;
public function __construct(
protected Dispatcher $bus,
protected JsonApi $api
) {
}

/**
* Get the data to be serialized and assigned to the response document.
*
* @param ServerRequestInterface $request
* @param Document $document
*
* @return mixed
*/
protected function data(ServerRequestInterface $request, Document $document)
public function handle(ServerRequestInterface $request): ResponseInterface
{
$title = Arr::get($request->getParsedBody(), 'title');
$start_post_id = Arr::get($request->getParsedBody(), 'start_post_id');
$end_post_number = Arr::get($request->getParsedBody(), 'end_post_number');
$actor = RequestUtil::getActor($request);

return $this->bus->dispatch(
$discussion = $this->bus->dispatch(
new SplitDiscussion($title, $start_post_id, $end_post_number, $actor)
);

return $this->api
->forResource(DiscussionResource::class)
->forEndpoint('show')
->handle(
$request->withUri($request->getUri()->withPath('/discussions/' . $discussion->id))
->withMethod('GET')
->withParsedBody([])
);
}
}
4 changes: 2 additions & 2 deletions src/Posts/DiscussionSplitPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DiscussionSplitPost extends AbstractEventPost implements MergeableInterfac
/**
* @var string
*/
public static $type = 'discussionSplit';
public static string $type = 'discussionSplit';

/**
* Save the model, given that it is going to appear immediately after the
Expand All @@ -38,7 +38,7 @@ class DiscussionSplitPost extends AbstractEventPost implements MergeableInterfac
* unsuccessful, this should be the current model instance. Otherwise,
* it should be the model that was merged into.
*/
public function saveAfter(Post $previous = null)
public function saveAfter(Post $previous = null): static
{
$this->save();

Expand Down
2 changes: 1 addition & 1 deletion src/Validators/SplitDiscussionValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SplitDiscussionValidator extends AbstractValidator
/**
* @return array
*/
protected function getRules()
protected function getRules(): array
{
return [
'start_post_id' => [
Expand Down