2015-05-14 20:31:03 +08:00
|
|
|
<?php
|
|
|
|
|
2015-09-04 11:01:19 +08:00
|
|
|
/*
|
|
|
|
* This file is part of Flarum.
|
|
|
|
*
|
2019-11-30 07:02:17 +08:00
|
|
|
* For detailed copyright and license information, please view the
|
|
|
|
* LICENSE file that was distributed with this source code.
|
2015-09-04 11:01:19 +08:00
|
|
|
*/
|
|
|
|
|
2021-05-05 02:57:29 +08:00
|
|
|
namespace Flarum\Mentions;
|
|
|
|
|
2024-03-02 01:42:18 +08:00
|
|
|
use Flarum\Api\Context;
|
|
|
|
use Flarum\Api\Endpoint;
|
|
|
|
use Flarum\Api\Resource;
|
|
|
|
use Flarum\Api\Schema;
|
2023-02-21 23:04:03 +08:00
|
|
|
use Flarum\Approval\Event\PostWasApproved;
|
2018-01-17 06:26:21 +08:00
|
|
|
use Flarum\Extend;
|
2022-11-06 03:29:01 +08:00
|
|
|
use Flarum\Group\Group;
|
2024-03-02 01:42:18 +08:00
|
|
|
use Flarum\Mentions\Api\PostResourceFields;
|
2018-12-16 00:23:48 +08:00
|
|
|
use Flarum\Post\Event\Deleted;
|
|
|
|
use Flarum\Post\Event\Hidden;
|
|
|
|
use Flarum\Post\Event\Posted;
|
|
|
|
use Flarum\Post\Event\Restored;
|
|
|
|
use Flarum\Post\Event\Revised;
|
2023-11-12 02:43:09 +08:00
|
|
|
use Flarum\Post\Filter\PostSearcher;
|
2020-04-24 21:55:50 +08:00
|
|
|
use Flarum\Post\Post;
|
2023-11-12 02:43:09 +08:00
|
|
|
use Flarum\Search\Database\DatabaseSearchDriver;
|
2020-04-24 21:55:50 +08:00
|
|
|
use Flarum\User\User;
|
2015-05-14 20:31:03 +08:00
|
|
|
|
2018-01-17 06:26:21 +08:00
|
|
|
return [
|
2018-07-23 22:29:38 +08:00
|
|
|
(new Extend\Frontend('forum'))
|
2018-06-20 12:05:33 +08:00
|
|
|
->js(__DIR__.'/js/dist/forum.js')
|
2018-07-23 22:29:38 +08:00
|
|
|
->css(__DIR__.'/less/forum.less'),
|
2018-06-20 12:05:33 +08:00
|
|
|
|
2021-04-21 17:58:54 +08:00
|
|
|
(new Extend\Frontend('admin'))
|
|
|
|
->js(__DIR__.'/js/dist/admin.js'),
|
|
|
|
|
2018-12-15 19:06:08 +08:00
|
|
|
(new Extend\Formatter)
|
2021-03-02 12:26:35 +08:00
|
|
|
->configure(ConfigureMentions::class)
|
2023-04-30 17:10:31 +08:00
|
|
|
->parse(Formatter\EagerLoadMentionedModels::class)
|
2021-03-02 12:26:35 +08:00
|
|
|
->render(Formatter\FormatPostMentions::class)
|
2021-04-21 17:58:54 +08:00
|
|
|
->render(Formatter\FormatUserMentions::class)
|
2022-11-06 03:29:01 +08:00
|
|
|
->render(Formatter\FormatGroupMentions::class)
|
2021-04-21 17:58:54 +08:00
|
|
|
->unparse(Formatter\UnparsePostMentions::class)
|
2023-04-19 19:58:11 +08:00
|
|
|
->unparse(Formatter\UnparseUserMentions::class),
|
2018-12-15 19:06:08 +08:00
|
|
|
|
2020-04-24 21:55:50 +08:00
|
|
|
(new Extend\Model(Post::class))
|
|
|
|
->belongsToMany('mentionedBy', Post::class, 'post_mentions_post', 'mentions_post_id', 'post_id')
|
|
|
|
->belongsToMany('mentionsPosts', Post::class, 'post_mentions_post', 'post_id', 'mentions_post_id')
|
2022-11-06 03:29:01 +08:00
|
|
|
->belongsToMany('mentionsUsers', User::class, 'post_mentions_user', 'post_id', 'mentions_user_id')
|
2023-04-19 19:58:11 +08:00
|
|
|
->belongsToMany('mentionsGroups', Group::class, 'post_mentions_group', 'post_id', 'mentions_group_id')
|
|
|
|
->belongsToMany('mentionsUsers', User::class, 'post_mentions_user', 'post_id', 'mentions_user_id'),
|
2020-04-24 21:55:50 +08:00
|
|
|
|
2020-06-20 10:27:37 +08:00
|
|
|
new Extend\Locales(__DIR__.'/locale'),
|
|
|
|
|
2020-07-17 18:10:47 +08:00
|
|
|
(new Extend\View)
|
|
|
|
->namespace('flarum-mentions', __DIR__.'/views'),
|
|
|
|
|
2020-11-01 06:43:02 +08:00
|
|
|
(new Extend\Notification())
|
2024-03-02 01:42:18 +08:00
|
|
|
->type(Notification\PostMentionedBlueprint::class, ['alert'])
|
|
|
|
->type(Notification\UserMentionedBlueprint::class, ['alert'])
|
|
|
|
->type(Notification\GroupMentionedBlueprint::class, ['alert']),
|
|
|
|
|
|
|
|
(new Extend\ApiResource(Resource\PostResource::class))
|
|
|
|
->fields(PostResourceFields::class)
|
|
|
|
->endpoint([Endpoint\Index::class, Endpoint\Show::class], function (Endpoint\Index|Endpoint\Show $endpoint): Endpoint\Endpoint {
|
|
|
|
return $endpoint->addDefaultInclude(['mentionedBy', 'mentionedBy.user', 'mentionedBy.discussion']);
|
|
|
|
})
|
|
|
|
->endpoint(Endpoint\Index::class, function (Endpoint\Index $endpoint): Endpoint\Index {
|
|
|
|
return $endpoint->eagerLoad(['mentionsUsers', 'mentionsPosts', 'mentionsPosts.user', 'mentionsPosts.discussion', 'mentionsGroups']);
|
2023-04-19 15:23:08 +08:00
|
|
|
}),
|
2020-12-09 00:15:52 +08:00
|
|
|
|
2024-03-02 01:42:18 +08:00
|
|
|
(new Extend\ApiResource(Resource\DiscussionResource::class))
|
|
|
|
->endpoint(Endpoint\Index::class, function (Endpoint\Index $endpoint): Endpoint\Index {
|
|
|
|
return $endpoint->eagerLoad([
|
|
|
|
'firstPost.mentionsUsers', 'firstPost.mentionsPosts',
|
|
|
|
'firstPost.mentionsPosts.user', 'firstPost.mentionsPosts.discussion', 'firstPost.mentionsGroups',
|
|
|
|
'lastPost.mentionsUsers', 'lastPost.mentionsPosts',
|
|
|
|
'lastPost.mentionsPosts.user', 'lastPost.mentionsPosts.discussion', 'lastPost.mentionsGroups',
|
|
|
|
]);
|
|
|
|
})
|
|
|
|
->endpoint(Endpoint\Show::class, function (Endpoint\Show $endpoint): Endpoint\Show {
|
|
|
|
return $endpoint->addDefaultInclude(['posts.mentionedBy', 'posts.mentionedBy.user', 'posts.mentionedBy.discussion'])
|
|
|
|
->eagerLoad([
|
|
|
|
'posts.mentionsUsers', 'posts.mentionsPosts', 'posts.mentionsPosts.user',
|
|
|
|
'posts.mentionsPosts.discussion', 'posts.mentionsGroups'
|
|
|
|
]);
|
|
|
|
}),
|
2018-12-16 00:23:48 +08:00
|
|
|
|
2024-03-02 01:42:18 +08:00
|
|
|
(new Extend\ApiResource(Resource\UserResource::class))
|
|
|
|
->fields(fn () => [
|
|
|
|
Schema\Boolean::make('canMentionGroups')
|
|
|
|
->visible(fn (User $user, Context $context) => $context->getActor()->id === $user->id)
|
|
|
|
->get(fn (User $user) => $user->can('mentionGroups')),
|
|
|
|
]),
|
2018-12-16 00:23:48 +08:00
|
|
|
|
2021-04-21 17:58:54 +08:00
|
|
|
(new Extend\Settings)
|
|
|
|
->serializeToForum('allowUsernameMentionFormat', 'flarum-mentions.allow_username_format', 'boolval'),
|
|
|
|
|
2020-12-09 00:15:52 +08:00
|
|
|
(new Extend\Event())
|
|
|
|
->listen(Posted::class, Listener\UpdateMentionsMetadataWhenVisible::class)
|
|
|
|
->listen(Restored::class, Listener\UpdateMentionsMetadataWhenVisible::class)
|
|
|
|
->listen(Revised::class, Listener\UpdateMentionsMetadataWhenVisible::class)
|
2023-02-21 23:04:03 +08:00
|
|
|
->listen(PostWasApproved::class, Listener\UpdateMentionsMetadataWhenVisible::class)
|
2020-12-09 00:15:52 +08:00
|
|
|
->listen(Hidden::class, Listener\UpdateMentionsMetadataWhenInvisible::class)
|
2021-05-05 02:57:29 +08:00
|
|
|
->listen(Deleted::class, Listener\UpdateMentionsMetadataWhenInvisible::class),
|
|
|
|
|
2023-11-12 02:43:09 +08:00
|
|
|
(new Extend\SearchDriver(DatabaseSearchDriver::class))
|
|
|
|
->addFilter(PostSearcher::class, Filter\MentionedFilter::class)
|
|
|
|
->addFilter(PostSearcher::class, Filter\MentionedPostFilter::class),
|
2022-11-06 03:29:01 +08:00
|
|
|
|
2023-04-19 19:58:11 +08:00
|
|
|
// Tag mentions
|
|
|
|
(new Extend\Conditional())
|
2023-10-22 00:37:07 +08:00
|
|
|
->whenExtensionEnabled('flarum-tags', fn () => [
|
2023-04-19 19:58:11 +08:00
|
|
|
(new Extend\Formatter)
|
|
|
|
->render(Formatter\FormatTagMentions::class)
|
|
|
|
->unparse(Formatter\UnparseTagMentions::class),
|
|
|
|
|
2024-03-02 01:42:18 +08:00
|
|
|
(new Extend\ApiResource(Resource\PostResource::class))
|
|
|
|
->fields(fn () => [
|
|
|
|
Schema\Relationship\ToMany::make('mentionsTags')
|
|
|
|
->type('tags'),
|
2023-04-19 19:58:11 +08:00
|
|
|
]),
|
|
|
|
|
2024-03-02 01:42:18 +08:00
|
|
|
(new Extend\ApiResource(Resource\DiscussionResource::class))
|
|
|
|
->endpoint(Endpoint\Show::class, function (Endpoint\Show $endpoint): Endpoint\Show {
|
|
|
|
return $endpoint->eagerLoad(['posts.mentionsTags']);
|
|
|
|
})
|
|
|
|
->endpoint(Endpoint\Index::class, function (Endpoint\Index $endpoint): Endpoint\Index {
|
|
|
|
return $endpoint->eagerLoad(['firstPost.mentionsTags', 'lastPost.mentionsTags']);
|
|
|
|
}),
|
|
|
|
|
|
|
|
(new Extend\ApiResource(Resource\PostResource::class))
|
|
|
|
->endpoint([Endpoint\Index::class, Endpoint\Show::class], function (Endpoint\Index|Endpoint\Show $endpoint): Endpoint\Endpoint {
|
|
|
|
return $endpoint->eagerLoad(['mentionsTags']);
|
|
|
|
}),
|
2023-04-19 19:58:11 +08:00
|
|
|
]),
|
2018-01-17 06:26:21 +08:00
|
|
|
];
|