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

Draft
wants to merge 11 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
3 changes: 1 addition & 2 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ 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: true
enable_phpstan: true
backend_directory: .
php_versions: '["7.4", "8.0", "8.1", "8.2", "8.3"]'
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
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
}
],
"require": {
"php": "^7.4 | ^8.0",
"flarum/core": "^1.2.0"
"flarum/core": "^2.0.0"
},
"replace": {
"reflar/reactions": "*"
Expand Down Expand Up @@ -92,7 +91,7 @@
"analyse:phpstan": "Run static analysis"
},
"require-dev": {
"flarum/testing": "^1.0.0",
"flarum/testing": "^2.0.0",
"flarum/likes": "*",
"fof/gamification": "*",
"flarum/phpstan": "*"
Expand Down
59 changes: 26 additions & 33 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@

namespace FoF\Reactions;

use Flarum\Api\Controller as ApiController;
use Flarum\Api\Serializer;
use Flarum\Api\Serializer\BasicPostSerializer;
use Flarum\Database\AbstractModel;
use Flarum\Api\Context;
use Flarum\Discussion\Discussion;
use Flarum\Extend;
use Flarum\Post\Event\Saving;
use Flarum\Post\Post;
use FoF\Reactions\Api\Controller;
use FoF\Reactions\Api\Serializer\ReactionSerializer;
use Flarum\Search\Database\DatabaseSearchDriver;
use FoF\Reactions\Notification\PostReactedBlueprint;
use Flarum\Api\Resource;
use Flarum\Api\Endpoint;
use Flarum\Api\Schema;
use FoF\Reactions\Search\Filter\PostFilter;
use FoF\Reactions\Search\PostReactionSearcher;

return [
(new Extend\Frontend('admin'))
Expand All @@ -35,43 +36,35 @@

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

(new Extend\Routes('api'))
->get('/posts/{id}/reactions', 'post.reactions.index', Controller\ListPostReactionsController::class)
->delete('/posts/{id}/reactions/specific/{postReactionId}', 'post.reactions.specific.delete', Controller\DeletePostReactionController::class)
->delete('/posts/{id}/reactions/type/{reactionId}', 'post.reactions.type.delete', Controller\DeletePostReactionController::class)
->get('/reactions', 'reactions.index', Controller\ListReactionsController::class)
->post('/reactions', 'reactions.create', Controller\CreateReactionController::class)
->patch('/reactions/{id}', 'reactions.update', Controller\UpdateReactionController::class)
->delete('/reactions/{id}', 'reactions.delete', Controller\DeleteReactionController::class),
(new Extend\ModelVisibility(PostReaction::class))
->scope(Access\ScopePostReactionVisibility::class),

(new Extend\Event())
->listen(Saving::class, Listener\SaveReactionsToDatabase::class)
->subscribe(Listener\SendNotifications::class),

(new Extend\Notification())
->type(PostReactedBlueprint::class, BasicPostSerializer::class, ['alert']),
->type(PostReactedBlueprint::class, ['alert']),

(new Extend\ApiSerializer(Serializer\ForumSerializer::class))
->hasMany('reactions', ReactionSerializer::class)
->attributes(ReactionsForumAttributes::class),
new Extend\ApiResource(Api\Resource\PostReactionResource::class),

(new Extend\ApiSerializer(Serializer\PostSerializer::class))
->attributes(PostAttributes::class),
new Extend\ApiResource(Api\Resource\ReactionResource::class),

(new Extend\ApiSerializer(Serializer\DiscussionSerializer::class))
->attributes(function (Serializer\DiscussionSerializer $serializer, AbstractModel $discussion, array $attributes): array {
$attributes['canSeeReactions'] = (bool) $serializer->getActor()->can('canSeeReactions', $discussion);
(new Extend\ApiResource(Resource\ForumResource::class))
->fields(ForumResourceFields::class)
->endpoint(Endpoint\Show::class, fn (Endpoint\Show $show) => $show->addDefaultInclude(['reactions'])),

return $attributes;
}),
(new Extend\ApiResource(Resource\PostResource::class))
->fields(PostResourceFields::class),

(new Extend\ApiController(ApiController\ShowForumController::class))
->prepareDataForSerialization(function (ApiController\ShowForumController $controller, &$data) {
$data['reactions'] = Reaction::get();
}),
(new Extend\ApiResource(Resource\DiscussionResource::class))
->fields(fn (): array => [
Schema\Boolean::make('canSeeReactions')
->get(fn (Discussion $discussion, Context $context) => $context->getActor()->can('canSeeReactions', $discussion))
]),

(new Extend\ApiController(ApiController\ShowForumController::class))
->addInclude('reactions'),
(new Extend\SearchDriver(DatabaseSearchDriver::class))
->addSearcher(PostReaction::class, PostReactionSearcher::class)
->addFilter(PostReactionSearcher::class, PostFilter::class),

(new Extend\Settings())
->default('fof-reactions.react_own_post', false)
Expand Down
4 changes: 2 additions & 2 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
},
"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",
"fuzzyset": "^1.0.7",
"lodash.debounce": "^4.0.8",
"simple-emoji-map": "^0.5.1",
Expand Down
38 changes: 38 additions & 0 deletions js/src/admin/extend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Extend from 'flarum/common/extenders';
import SettingsPage from './components/SettingsPage';
import Reaction from '../common/models/Reaction';
import Forum from 'flarum/common/models/Forum';

export default [
new Extend.Store().add('reactions', Reaction),

new Extend.Model(Forum).hasMany('reactions', Reaction),

new Extend.Admin()
.permission(
() => ({
icon: 'far fa-thumbs-up',
label: app.translator.trans('fof-reactions.admin.permissions.react_posts_label'),
permission: 'discussion.reactPosts',
}),
'reply'
)
.permission(
() => ({
icon: 'fas fa-info-circle',
label: app.translator.trans('fof-reactions.admin.permissions.see_reactions_label'),
permission: 'discussion.canSeeReactions',
allowGuest: true,
}),
'view'
)
.permission(
() => ({
icon: 'fas fa-trash',
label: app.translator.trans('fof-reactions.admin.permissions.delete_post_reactions_label'),
permission: 'discussion.deleteReactionsPosts',
}),
'moderate'
)
.page(SettingsPage),
];
40 changes: 3 additions & 37 deletions js/src/admin/index.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,12 @@
import app from 'flarum/admin/app';
import Forum from 'flarum/common/models/Forum';
import Model from 'flarum/common/Model';

import SettingsPage from './components/SettingsPage';
import Reaction from '../common/models/Reaction';

export * from './components';
export * from '../common/components';
export * from '../common/models';
export * from '../common/util';

app.initializers.add('fof/reactions', () => {
app.store.models.reactions = Reaction;

Forum.prototype.reactions = Model.hasMany('reactions');
export { default as extend } from './extend';

app.extensionData
.for('fof-reactions')
.registerPermission(
{
icon: 'far fa-thumbs-up',
label: app.translator.trans('fof-reactions.admin.permissions.react_posts_label'),
permission: 'discussion.reactPosts',
},
'reply'
)
.registerPermission(
{
icon: 'fas fa-info-circle',
label: app.translator.trans('fof-reactions.admin.permissions.see_reactions_label'),
permission: 'discussion.canSeeReactions',
allowGuest: true,
},
'view'
)
.registerPermission(
{
icon: 'fas fa-trash',
label: app.translator.trans('fof-reactions.admin.permissions.delete_post_reactions_label'),
permission: 'discussion.deleteReactionsPosts',
},
'moderate'
)
.registerPage(SettingsPage);
app.initializers.add('fof/reactions', () => {
//
});
4 changes: 2 additions & 2 deletions js/src/common/util/emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const search = (query) => {
};
};

export default (reactionOrIdentifier) => {
export default function emoji(reactionOrIdentifier) {
if (!reactionOrIdentifier) return {};

let identifier = reactionOrIdentifier.identifier || reactionOrIdentifier;
Expand Down Expand Up @@ -56,4 +56,4 @@ export default (reactionOrIdentifier) => {
emojiCache.set(reactionOrIdentifier, output);

return output || {};
};
}
3 changes: 1 addition & 2 deletions js/src/forum/components/PostReactAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Component from 'flarum/common/Component';
import ItemList from 'flarum/common/utils/ItemList';
import Button from 'flarum/common/components/Button';
import listItems from 'flarum/common/helpers/listItems';
import LogInModal from 'flarum/forum/components/LogInModal';
import ReactionComponent from '../../common/components/ReactionComponent';

export default class PostReactAction extends Component {
Expand Down Expand Up @@ -148,7 +147,7 @@ export default class PostReactAction extends Component {
const allowAnonymous = app.forum.attribute('fofReactionsAllowAnonymous');

if (!app.session.user && !allowAnonymous) {
app.modal.show(LogInModal);
app.modal.show(() => import('flarum/forum/components/LogInModal'));
return;
}

Expand Down
4 changes: 2 additions & 2 deletions js/src/forum/components/PostReactedNotification.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import app from 'flarum/forum/app';
import Notification from 'flarum/forum/components/Notification';
import icon from 'flarum/common/helpers/icon';
import Icon from 'flarum/common/components/Icon';

import emoji from '../../common/util/emoji';

Expand All @@ -18,7 +18,7 @@ export default class PostReactedNotification extends Notification {
const { identifier, type } = JSON.parse(notification.content());
const user = notification.fromUser();

const reaction = type === 'emoji' ? <img src={emoji(identifier).url} loading="lazy" height="14px" /> : icon(identifier);
const reaction = type === 'emoji' ? <img src={emoji(identifier).url} loading="lazy" height="14px" /> : <Icon name={identifier} />;

return app.translator.trans('fof-reactions.forum.notification', {
user,
Expand Down
19 changes: 12 additions & 7 deletions js/src/forum/components/ReactionsModal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import app from 'flarum/forum/app';
import Modal, { IInternalModalAttrs } from 'flarum/common/components/Modal';
import LoadingIndicator from 'flarum/common/components/LoadingIndicator';
import avatar from 'flarum/common/helpers/avatar';
import Avatar from 'flarum/common/components/Avatar';
import username from 'flarum/common/helpers/username';
import Link from 'flarum/common/components/Link';
import ReactionComponent from '../../common/components/ReactionComponent';
Expand Down Expand Up @@ -85,13 +85,11 @@ export default class ReactionsModal extends Modal<ReactionsModalAttrs> {
/>
)}
</legend>

<hr className="ReactionsModal-delimiter" />

{Object.entries(users).map(([postReactionId, user]: [string, User], index: number) => (
<li key={user.id()} data-post-reaction-id={postReactionId} data-user-id={user.id()}>
<Link className="ReactionsModal-user" href={app.route.user(user)}>
{avatar(user, { loading: 'lazy' })}
<Avatar user={user} loading="lazy" />
{username(user)}
</Link>
{canDeleteReaction(user) && (
Expand All @@ -104,7 +102,6 @@ export default class ReactionsModal extends Modal<ReactionsModalAttrs> {
)}
</li>
))}

{anonymousCount > 0 && <li>{app.translator.trans('fof-reactions.forum.modal.anonymous_count', { count: anonymousCount })}</li>}
</div>
);
Expand All @@ -113,7 +110,10 @@ export default class ReactionsModal extends Modal<ReactionsModalAttrs> {
async load(): Promise<void> {
this.loading = true;

const response = await app.store.find<ApiResponsePlural<PostReaction>>(`/posts/${this.attrs.post.id()}/reactions`, { include: 'user,reaction' });
const response = await app.store.find<ApiResponsePlural<PostReaction>>(`post_reactions`, {
include: 'user,reaction',
filter: { post: this.attrs.post.id() },
});

const groupedReactions = groupBy(response, (r: PostReaction) => r.reactionId());
const reactions: ReactionGroup[] = [];
Expand Down Expand Up @@ -160,7 +160,12 @@ export default class ReactionsModal extends Modal<ReactionsModalAttrs> {

await app.request({
method: 'DELETE',
url: `${app.forum.attribute('apiUrl')}/posts/${this.attrs.post.id()}/reactions/${isSpecific ? 'specific' : 'type'}/${id}`,
url: `${app.forum.attribute('apiUrl')}/post_reactions/delete`,
body: {
postId: this.attrs.post.id(),
specific: isSpecific,
reactionOrPostReactionId: id,
},
});

// Filter out the deleted reaction type
Expand Down
3 changes: 1 addition & 2 deletions js/src/forum/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Forum from 'flarum/common/models/Forum';
import Discussion from 'flarum/common/models/Discussion';
import Post from 'flarum/common/models/Post';
import Model from 'flarum/common/Model';
import NotificationGrid from 'flarum/forum/components/NotificationGrid';

import PostReactedNotification from './components/PostReactedNotification';
import Reaction from '../common/models/Reaction';
Expand Down Expand Up @@ -38,7 +37,7 @@ app.initializers.add('fof/reactions', () => {
addReactionAction();
addPusher();

extend(NotificationGrid.prototype, 'notificationTypes', (items) => {
extend('flarum/forum/components/NotificationGrid', 'notificationTypes', (items) => {
items.add('postReacted', {
name: 'postReacted',
icon: 'far fa-smile',
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:
- extend.php
excludePaths:
- *.blade.php
checkMissingIterableValueType: false
databaseMigrationsPath: ['migrations']

6 changes: 3 additions & 3 deletions resources/less/forum.less
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
}

.CommentPost--Reactions {
background-color: @body-bg;
color: @text-color;
background-color: var(--body-bg);
color: var(--text-color);
visibility: hidden;
opacity: 0;
transition: visibility 0s linear 300ms, opacity 300ms;
Expand Down Expand Up @@ -111,7 +111,7 @@
margin: 0;

a {
color: @text-color;
color: var(--text-color);
font-size: 15px;
display: block;
margin-bottom: 10px;
Expand Down
16 changes: 16 additions & 0 deletions src/Access/ScopePostReactionVisibility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace FoF\Reactions\Access;

use Flarum\User\User;
use Illuminate\Database\Eloquent\Builder;

class ScopePostReactionVisibility
{
public function __invoke(User $actor, Builder $query): void
{
$query->whereHas('post', function (Builder $query) use ($actor) {
$query->whereVisibleTo($actor);
});
}
}
Loading
Loading