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('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-z]{0,3}[0-9]+).)+)["|”]#(?[0-9]+)\b/', $tagName);
$config->Preg->match('/\B@(?[a-z0-9_-]+)(?!#)/i', $tagName);
}
/**
* @param $tag
*
* @return bool
*/
public static function addUserId($tag)
{
$allow_username_format = (bool) resolve(SettingsRepositoryInterface::class)->get('flarum-mentions.allow_username_format');
if ($tag->hasAttribute('username') && $allow_username_format) {
$user = User::where('username', $tag->getAttribute('username'))->first();
} elseif ($tag->hasAttribute('id')) {
$user = User::find($tag->getAttribute('id'));
}
if (isset($user)) {
$tag->setAttribute('id', $user->id);
$tag->setAttribute('displayname', $user->display_name);
return true;
}
$tag->invalidate();
}
private function configurePostMentions(Configurator $config)
{
$config->rendering->parameters['DISCUSSION_URL'] = $this->url->to('forum')->route('discussion', ['id' => '']);
$tagName = 'POSTMENTION';
$tag = $config->tags->add($tagName);
$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, 'addPostId'])
->setJS('function(tag) { return flarum.extensions["flarum-mentions"].filterPostMentions(tag); }');
$config->Preg->match('/\B@["|“](?((?!"#[a-z]{0,3}[0-9]+).)+)["|”]#p(?[0-9]+)\b/', $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);
if ($post->user) {
$tag->setAttribute('displayname', $post->user->display_name);
}
return true;
}
}
}