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 #108

Draft
wants to merge 20 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
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@1.x
uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@2.x
with:
enable_backend_testing: true
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@1.x
uses: flarum/framework/.github/workflows/REUSABLE_frontend.yml@2.x
with:
enable_bundlewatch: false
enable_prettier: true
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
],
"require": {
"flarum/core": "^1.8.3",
"flarum/core": "^2.0.0",
"flarum/tags": "*"
},
"replace": {
Expand Down Expand Up @@ -86,12 +86,12 @@
"test:setup": "Sets up a database for use with integration tests. Execute this only once."
},
"require-dev": {
"flarum/phpstan": "^1.8",
"flarum/testing": "^1.0.0"
"flarum/phpstan": "^2.0.0",
"flarum/testing": "^2.0.0"
},
"autoload-dev": {
"psr-4": {
"FoF\\BestAnswer\\Tests\\": "tests/"
}
}
}
}
86 changes: 37 additions & 49 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,20 @@

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\UpdateDiscussionController;
use Flarum\Api\Serializer;
use Flarum\Discussion\Discussion;
use Flarum\Discussion\Event\Saving as DiscussionSaving;
use Flarum\Discussion\Filter\DiscussionFilterer;
use Flarum\Discussion\Search\DiscussionSearcher;
use Flarum\Extend;
use Flarum\Post\Post;
use Flarum\Settings\Event\Saving as SettingsSaving;
use Flarum\Tags\Api\Serializer\TagSerializer;
use Flarum\Tags\Tag;
use Flarum\User\User;
use FoF\BestAnswer\Events\BestAnswerSet;
use Flarum\Api\Resource;
use Flarum\Api\Endpoint;
use Flarum\Api\Schema;
use Flarum\Api\Sort;

return [
(new Extend\Frontend('forum'))
Expand Down Expand Up @@ -59,29 +56,44 @@
->cast('best_answer_count', 'int'),

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

(new Extend\Notification())
->type(Notification\SelectBestAnswerBlueprint::class, Serializer\BasicDiscussionSerializer::class, ['alert', 'email'])
->type(Notification\AwardedBestAnswerBlueprint::class, Serializer\BasicDiscussionSerializer::class, ['alert'])
->type(Notification\BestAnswerSetInDiscussionBlueprint::class, Serializer\BasicDiscussionSerializer::class, []),

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

(new Extend\ApiSerializer(Serializer\BasicDiscussionSerializer::class))
->hasOne('bestAnswerPost', Serializer\BasicPostSerializer::class)
->hasOne('bestAnswerUser', Serializer\BasicUserSerializer::class)
->attributes(BasicDiscussionAttributes::class),
->type(Notification\SelectBestAnswerBlueprint::class, ['alert', 'email'])
->type(Notification\AwardedBestAnswerBlueprint::class, ['alert'])
->type(Notification\BestAnswerSetInDiscussionBlueprint::class, []),

(new Extend\ApiResource(Resource\DiscussionResource::class))
->fields(DiscussionAttributes::class)
->endpoint(Endpoint\Show::class, function (Endpoint\Show $endpoint) {
return $endpoint
->addDefaultInclude(['bestAnswerPost', 'bestAnswerUser'])
->eagerLoad(['bestAnswerPost.user']);
}),

(new Extend\ApiSerializer(Serializer\UserSerializer::class))
->attributes(UserBestAnswerCount::class),
(new Extend\ApiResource(Resource\PostResource::class))
->endpoint(Endpoint\Index::class, function (Endpoint\Index $endpoint) {
return $endpoint
->addDefaultInclude(['discussion.bestAnswerPost', 'discussion.bestAnswerUser', 'discussion.bestAnswerPost.user']); // @todo: same
}),

(new Extend\ApiController(ListUsersController::class))
->addSortField('bestAnswerCount'),
(new Extend\ApiResource(Resource\UserResource::class))
->fields(UserBestAnswerCount::class)
->sorts(fn () => [
Sort\SortColumn::make('bestAnswerCount'),
]),

(new Extend\Conditional())
->whenExtensionEnabled('flarum-tags', fn () => [
(new Extend\ApiResource(\Flarum\Tags\Api\Resource\TagResource::class))
->fields(fn () => [
Schema\Boolean::make('isQnA'),
Schema\Boolean::make('reminders')
->property('qna_reminders'),
])
]),

(new Extend\Settings())
->default('fof-best-answer.schedule_on_one_server', false)
Expand All @@ -97,35 +109,11 @@
->serializeToForum('solutionSearchEnabled', 'fof-best-answer.search.solution_search', 'boolVal')
->serializeToForum('bestAnswerDiscussionSidebarJumpButton', 'fof-best-answer.discussion_sidebar_jump_button', 'boolVal'),

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

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

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

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

(new Extend\SimpleFlarumSearch(DiscussionSearcher::class))
->addGambit(Search\BestAnswerFilterGambit::class),

(new Extend\Console())
->command(Console\NotifyCommand::class)
->command(Console\UpdateBestAnswerCounts::class)
->schedule(Console\NotifyCommand::class, Console\NotifySchedule::class),

(new Extend\Filter(DiscussionFilterer::class))
->addFilter(Search\BestAnswerFilterGambit::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;
}),
(new Extend\SearchDriver(\Flarum\Search\Database\DatabaseSearchDriver::class))
->addFilter(DiscussionSearcher::class, Search\BestAnswerFilter::class),
];
4 changes: 2 additions & 2 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"prettier": "@flarum/prettier-config",
"dependencies": {
"@flarum/prettier-config": "^1.0.0",
"flarum-tsconfig": "^1.0.2",
"flarum-webpack-config": "^2.0.2",
"flarum-tsconfig": "^2.0.0",
"flarum-webpack-config": "^3.0.0",
"webpack": "^5.88.0",
"webpack-cli": "^5.1.4"
},
Expand Down
25 changes: 24 additions & 1 deletion js/src/admin/extend.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
import Extend from 'flarum/common/extenders';
import commonExtend from '../common/extend';
import BestAnswerSettingsPage from './components/BestAnswerSettingsPage';

export default [...commonExtend];
export default [
...commonExtend,

new Extend.Admin()
.page(BestAnswerSettingsPage)
.permission(
() => ({
icon: 'far fa-comment',
label: app.translator.trans('fof-best-answer.admin.permissions.best_answer'),
permission: 'discussion.selectBestAnswerOwnDiscussion',
}),
'reply'
)
.permission(
() => ({
icon: 'far fa-comment',
label: app.translator.trans('fof-best-answer.admin.permissions.best_answer_not_own_discussion'),
permission: 'discussion.selectBestAnswerNotOwnDiscussion',
}),
'reply'
),
];
21 changes: 0 additions & 21 deletions js/src/admin/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
import app from 'flarum/admin/app';
import addBestAnswerCountSort from '../common/addBestAnswerCountSort';
import BestAnswerSettingsPage from './components/BestAnswerSettingsPage';

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

app.initializers.add(
'fof-best-answer',
() => {
app.extensionData
.for('fof-best-answer')
.registerPage(BestAnswerSettingsPage)
.registerPermission(
{
icon: 'far fa-comment',
label: app.translator.trans('fof-best-answer.admin.permissions.best_answer'),
permission: 'discussion.selectBestAnswerOwnDiscussion',
},
'reply'
)
.registerPermission(
{
icon: 'far fa-comment',
label: app.translator.trans('fof-best-answer.admin.permissions.best_answer_not_own_discussion'),
permission: 'discussion.selectBestAnswerNotOwnDiscussion',
},
'reply'
);

addBestAnswerCountSort();
},
5
Expand Down
7 changes: 3 additions & 4 deletions js/src/common/addBestAnswerCountSort.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { override } from 'flarum/common/extend';
import SortMap from 'ext:fof/user-directory/common/utils/SortMap';

export default () => {
const FoFUserDirectory = require('@fof-user-directory');
if (!SortMap) return;

if (!FoFUserDirectory) return;

override(FoFUserDirectory.SortMap.prototype, 'sortMap', (map) => ({
override(SortMap.prototype, 'sortMap', (map) => ({
...map(),
most_best_answers: '-bestAnswerCount',
least_best_answers: 'bestAnswerCount',
Expand Down
4 changes: 3 additions & 1 deletion js/src/common/extend.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import BestAnswerGambit from './gambits/BestAnswerGambit';
import Extend from 'flarum/common/extenders';
// @ts-ignore
import Tag from 'flarum/tags/models/Tag';
import Tag from 'ext:flarum/tags/common/models/Tag';

export default [
new Extend.Model(Tag) //
.attribute<boolean>('isQnA')
.attribute<boolean>('reminders'),
new Extend.Search().gambit('discussions', BestAnswerGambit),
];
12 changes: 12 additions & 0 deletions js/src/common/gambits/BestAnswerGambit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import app from 'flarum/common/app';
import { BooleanGambit } from 'flarum/common/query/IGambit';

export default class BestAnswerGambit extends BooleanGambit {
key() {
return app.translator.trans('fof-best-answer.lib.gambits.solved-discussions.key', {}, true);
}

filterKey() {
return 'solved-discussions';
}
}
4 changes: 1 addition & 3 deletions js/src/forum/addBestAnswerAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ export default () => {
discussion
.save(
{
bestAnswerPostId: isBestAnswer ? post.id() : 0,
bestAnswerUserId: app.session.user.id(),
relationships: isBestAnswer ? { bestAnswerPost: post, bestAnswerUser: app.session.user } : { bestAnswerPost: null },
relationships: { bestAnswerPost: isBestAnswer ? post : { data: null } },
},
{
params: {
Expand Down
4 changes: 2 additions & 2 deletions js/src/forum/addBestAnswerCountToUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import app from 'flarum/forum/app';
import { extend } from 'flarum/common/extend';
import ItemList from 'flarum/common/utils/ItemList';
import UserCard from 'flarum/forum/components/UserCard';
import icon from 'flarum/common/helpers/icon';
import Icon from 'flarum/common/components/Icon';

import type Mithril from 'mithril';

Expand All @@ -13,7 +13,7 @@ export default function addBestAnswerCountToUsers() {
items.add(
'best-answer-count',
<span className="UserCard-bestAnswerCount">
{icon('fas fa-check')}
<Icon name="fas fa-check" />
{app.translator.trans('fof-best-answer.forum.user.best-answer-count', {
count: user.bestAnswerCount(),
})}
Expand Down
6 changes: 4 additions & 2 deletions js/src/forum/addBestAnswerView.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ export default () => {
<ul>
<li className="item-user">
<div className="PostUser">
{user && userOnline(user)}
<h3>{user ? <Link href={app.route.user(user)}>{username(user)}</Link> : username(user)}</h3>
<h3 className="PostUser-name">
{user && userOnline(user)}
{user ? <Link href={app.route.user(user)}>{username(user)}</Link> : username(user)}
</h3>
</div>
</li>
{post.discussion() && <li className="item-meta">{PostMeta.component({ post })}</li>}
Expand Down
6 changes: 3 additions & 3 deletions js/src/forum/components/SelectBestAnswerItem.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import app from 'flarum/forum/app';
import Component from 'flarum/common/Component';
import icon from 'flarum/common/helpers/icon';
import Icon from 'flarum/common/components/Icon';
import humanTime from 'flarum/common/helpers/humanTime';
import Link from 'flarum/common/components/Link';
import ItemList from 'flarum/common/utils/ItemList';
Expand Down Expand Up @@ -33,12 +33,12 @@ export default class SelectBestAnswerItem extends Component {
'post',
this.post ? (
<span>
{icon('fas fa-check')}
<Icon name="fas fa-check" />
{app.translator.trans('fof-best-answer.forum.best_answer_button')}
</span>
) : (
<Link href={app.route.post(this.post)} data-number={this.post.number()}>
{icon('fas fa-check')}
<Icon name="fas fa-check" />
{app.translator.trans('fof-best-answer.forum.best_answer_button')}
</Link>
)
Expand Down
Loading