diff --git a/extensions/mentions/extend.php b/extensions/mentions/extend.php index 2fa11a6a9..7b0ca69c7 100644 --- a/extensions/mentions/extend.php +++ b/extensions/mentions/extend.php @@ -10,6 +10,8 @@ */ use Flarum\Extend; +use Flarum\Formatter\Event\Rendering; +use Flarum\Mentions\ConfigureMentions; use Flarum\Mentions\Listener; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\View\Factory; @@ -19,14 +21,18 @@ return [ ->js(__DIR__.'/js/dist/forum.js') ->css(__DIR__.'/less/forum.less'), + (new Extend\Formatter) + ->configure(ConfigureMentions::class), + function (Dispatcher $events, Factory $views) { $events->subscribe(Listener\AddPostMentionedByRelationship::class); - $events->subscribe(Listener\FormatPostMentions::class); - $events->subscribe(Listener\FormatUserMentions::class); $events->subscribe(Listener\UpdatePostMentionsMetadata::class); $events->subscribe(Listener\UpdateUserMentionsMetadata::class); $events->subscribe(Listener\AddFilterByMentions::class); + $events->listen(Rendering::class, Listener\FormatPostMentions::class); + $events->listen(Rendering::class, Listener\FormatUserMentions::class); + $views->addNamespace('flarum-mentions', __DIR__.'/views'); }, ]; diff --git a/extensions/mentions/src/ConfigureMentions.php b/extensions/mentions/src/ConfigureMentions.php new file mode 100644 index 000000000..972ac4fa6 --- /dev/null +++ b/extensions/mentions/src/ConfigureMentions.php @@ -0,0 +1,105 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Mentions; + +use Flarum\Http\UrlGenerator; +use s9e\TextFormatter\Configurator; + +class ConfigureMentions +{ + /** + * @var UrlGenerator + */ + protected $url; + + /** + * @param UrlGenerator $url + */ + public function __construct(UrlGenerator $url) + { + $this->url = $url; + } + + public function __invoke(Configurator $config) + { + $this->configureUserMentions($config); + $this->configurePostMentions($config); + } + + private function configureUserMentions(Configurator $config) + { + $config->rendering->parameters['PROFILE_URL'] = $this->url->to('forum')->route('user', ['username' => '']); + + $tagName = 'USERMENTION'; + + $tag = $config->tags->add($tagName); + $tag->attributes->add('username'); + $tag->attributes->add('displayname'); + $tag->attributes->add('id')->filterChain->append('#uint'); + + $tag->template = '@'; + $tag->filterChain->prepend([static::class, 'addUserId']) + ->setJS('function(tag) { return flarum.extensions["flarum-mentions"].filterUserMentions(tag); }'); + + $config->Preg->match('/\B@(?[a-z0-9_-]+)(?!#)/i', $tagName); + } + + /** + * @param $tag + * + * @return bool + */ + public static function addUserId($tag) + { + if ($user = User::where('username', 'like', $tag->getAttribute('username'))->first()) { + $tag->setAttribute('id', $user->id); + $tag->setAttribute('displayname', $user->display_name); + + return true; + } + } + + private function configurePostMentions(Configurator $config) + { + $config->rendering->parameters['PROFILE_URL'] = $this->url->to('forum')->route('user', ['username' => '']); + + $tagName = 'USERMENTION'; + + $tag = $config->tags->add($tagName); + $tag->attributes->add('username'); + $tag->attributes->add('displayname'); + $tag->attributes->add('id')->filterChain->append('#uint'); + + $tag->template = '@'; + $tag->filterChain->prepend([static::class, 'addPostId']) + ->setJS('function(tag) { return flarum.extensions["flarum-mentions"].filterUserMentions(tag); }'); + + $config->Preg->match('/\B@(?[a-z0-9_-]+)(?!#)/i', $tagName); + } + + /** + * @param $tag + * @return bool + */ + public static function addPostId($tag) + { + $post = CommentPost::find($tag->getAttribute('id')); + + if ($post) { + $tag->setAttribute('discussionid', (int) $post->discussion_id); + $tag->setAttribute('number', (int) $post->number); + $tag->setAttribute('displayname', $post->user->display_name); + + return true; + } + } +} diff --git a/extensions/mentions/src/Listener/FormatPostMentions.php b/extensions/mentions/src/Listener/FormatPostMentions.php index 0ef578a32..cb783e44a 100755 --- a/extensions/mentions/src/Listener/FormatPostMentions.php +++ b/extensions/mentions/src/Listener/FormatPostMentions.php @@ -11,69 +11,12 @@ namespace Flarum\Mentions\Listener; -use Flarum\Formatter\Event\Configuring; use Flarum\Formatter\Event\Rendering; -use Flarum\Http\UrlGenerator; -use Flarum\Post\CommentPost; -use Illuminate\Contracts\Events\Dispatcher; use s9e\TextFormatter\Utils; class FormatPostMentions { - /** - * @var UrlGenerator - */ - protected $url; - - /** - * @param UrlGenerator $url - */ - public function __construct(UrlGenerator $url) - { - $this->url = $url; - } - - /** - * @param Dispatcher $events - */ - public function subscribe(Dispatcher $events) - { - $events->listen(Configuring::class, [$this, 'configure']); - $events->listen(Rendering::class, [$this, 'render']); - } - - /** - * @param Configuring $event - */ - public function configure(Configuring $event) - { - $configurator = $event->configurator; - - $configurator->rendering->parameters['DISCUSSION_URL'] = $this->url->to('forum')->route('discussion', ['id' => '']); - - $tagName = 'POSTMENTION'; - - $tag = $configurator->tags->add($tagName); - - $tag->attributes->add('username'); - $tag->attributes->add('displayname'); - $tag->attributes->add('number')->filterChain->append('#uint'); - $tag->attributes->add('discussionid')->filterChain->append('#uint'); - $tag->attributes->add('id')->filterChain->append('#uint'); - - $tag->template = ''; - - $tag->filterChain - ->prepend([static::class, 'addId']) - ->setJS('function(tag) { return flarum.extensions["flarum-mentions"].filterPostMentions(tag); }'); - - $configurator->Preg->match('/\B@(?[a-z0-9_-]+)#(?\d+)/i', $tagName); - } - - /** - * @param Rendering $event - */ - public function render(Rendering $event) + public function handle(Rendering $event) { $post = $event->context; @@ -86,21 +29,4 @@ class FormatPostMentions return $attributes; }); } - - /** - * @param $tag - * @return bool - */ - public static function addId($tag) - { - $post = CommentPost::find($tag->getAttribute('id')); - - if ($post) { - $tag->setAttribute('discussionid', (int) $post->discussion_id); - $tag->setAttribute('number', (int) $post->number); - $tag->setAttribute('displayname', $post->user->display_name); - - return true; - } - } } diff --git a/extensions/mentions/src/Listener/FormatUserMentions.php b/extensions/mentions/src/Listener/FormatUserMentions.php index d56ca4121..5db076ca3 100755 --- a/extensions/mentions/src/Listener/FormatUserMentions.php +++ b/extensions/mentions/src/Listener/FormatUserMentions.php @@ -11,64 +11,12 @@ namespace Flarum\Mentions\Listener; -use Flarum\Formatter\Event\Configuring; use Flarum\Formatter\Event\Rendering; -use Flarum\Http\UrlGenerator; -use Flarum\User\User; -use Illuminate\Contracts\Events\Dispatcher; use s9e\TextFormatter\Utils; class FormatUserMentions { - /** - * @var UrlGenerator - */ - protected $url; - - /** - * @param UrlGenerator $url - */ - public function __construct(UrlGenerator $url) - { - $this->url = $url; - } - - /** - * @param Dispatcher $events - */ - public function subscribe(Dispatcher $events) - { - $events->listen(Configuring::class, [$this, 'configure']); - $events->listen(Rendering::class, [$this, 'render']); - } - - /** - * @param Configuring $event - */ - public function configure(Configuring $event) - { - $configurator = $event->configurator; - - $configurator->rendering->parameters['PROFILE_URL'] = $this->url->to('forum')->route('user', ['username' => '']); - - $tagName = 'USERMENTION'; - - $tag = $configurator->tags->add($tagName); - $tag->attributes->add('username'); - $tag->attributes->add('displayname'); - $tag->attributes->add('id')->filterChain->append('#uint'); - - $tag->template = '@'; - $tag->filterChain->prepend([static::class, 'addId']) - ->setJS('function(tag) { return flarum.extensions["flarum-mentions"].filterUserMentions(tag); }'); - - $configurator->Preg->match('/\B@(?[a-z0-9_-]+)(?!#)/i', $tagName); - } - - /** - * @param Rendering $event - */ - public function render(Rendering $event) + public function handle(Rendering $event) { $post = $event->context; @@ -82,19 +30,4 @@ class FormatUserMentions return $attributes; }); } - - /** - * @param $tag - * - * @return bool - */ - public static function addId($tag) - { - if ($user = User::where('username', 'like', $tag->getAttribute('username'))->first()) { - $tag->setAttribute('id', $user->id); - $tag->setAttribute('displayname', $user->display_name); - - return true; - } - } }