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

[1.x] fix(Mentions): allow renderer to be used without context #3953

Merged
merged 2 commits into from
Jan 10, 2024
Merged
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
14 changes: 8 additions & 6 deletions extensions/mentions/src/Formatter/FormatPostMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Flarum\Discussion\Discussion;
use Flarum\Http\SlugManager;
use Flarum\Post\Post;
use Psr\Http\Message\ServerRequestInterface as Request;
use s9e\TextFormatter\Renderer;
use s9e\TextFormatter\Utils;
Expand Down Expand Up @@ -39,16 +40,17 @@ public function __construct(TranslatorInterface $translator, SlugManager $slugMa
*
* @param \s9e\TextFormatter\Renderer $renderer
* @param mixed $context
* @param string|null $xml
* @param \Psr\Http\Message\ServerRequestInterface $request
* @return string
* @param string $xml
* @param \Psr\Http\Message\ServerRequestInterface|null $request
* @return string $xml to be rendered
*/
public function __invoke(Renderer $renderer, $context, $xml, Request $request = null)
{
$post = $context;
return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($context) {
$post = (($context && isset($context->getRelations()['mentionsPosts'])) || $context instanceof Post)
? $context->mentionsPosts->find($attributes['id'])
: Post::find($attributes['id']);

return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($post) {
$post = $post->mentionsPosts->find($attributes['id']);
if ($post && $post->user) {
$attributes['displayname'] = $post->user->display_name;
}
Expand Down
8 changes: 6 additions & 2 deletions extensions/mentions/src/Formatter/UnparsePostMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Flarum\Mentions\Formatter;

use Flarum\Post\Post;
use s9e\TextFormatter\Utils;
use Symfony\Contracts\Translation\TranslatorInterface;

Expand Down Expand Up @@ -50,8 +51,11 @@ protected function updatePostMentionTags($context, string $xml): string
{
$post = $context;

return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($post) {
$post = $post->mentionsPosts->find($attributes['id']);
return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($context) {
$post = (($context && isset($context->getRelations()['mentionsPosts'])) || $context instanceof Post)
? $context->mentionsPosts->find($attributes['id'])
: Post::find($attributes['id']);

if ($post && $post->user) {
$attributes['displayname'] = $post->user->display_name;
}
Expand Down
37 changes: 37 additions & 0 deletions extensions/mentions/tests/integration/api/PostMentionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use Carbon\Carbon;
use Flarum\Extend;
use Flarum\Formatter\Formatter;
use Flarum\Post\Post;
use Flarum\Post\CommentPost;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
Expand Down Expand Up @@ -538,6 +540,41 @@ public function editing_a_post_with_a_mention_of_a_post_with_deleted_author_work
$this->assertStringContainsString('PostMention', $response['data']['attributes']['contentHtml']);
$this->assertNotNull(CommentPost::find($response['data']['id'])->mentionsPosts->find(11));
}

/**
* @test
*/
public function rendering_post_mention_with_a_post_context_works()
{
/** @var Formatter $formatter */
$formatter = $this->app()->getContainer()->make(Formatter::class);

$post = Post::find(4);
$user = User::find(1);

$xml = $formatter->parse($post->content, $post, $user);
$renderedHtml = $formatter->render($xml, $post);

$this->assertStringContainsString('TOBY$', $renderedHtml);
}

/**
* @test
*/
public function rendering_post_mention_without_a_context_works()
{
/** @var Formatter $formatter */
$formatter = $this->app()->getContainer()->make(Formatter::class);

$post = Post::find(4);
$user = User::find(1);

$xml = $formatter->parse($post->content, null, $user);
$renderedHtml = $formatter->render($xml);

$this->assertStringContainsString('TOBY$', $renderedHtml);
}

}

class CustomOtherDisplayNameDriver implements DriverInterface
Expand Down
Loading