Skip to content

Commit

Permalink
fix(Mentions): allow renderer to be used without context
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideIadeluca committed Jan 10, 2024
1 parent f784f48 commit a0e3ae9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
19 changes: 15 additions & 4 deletions extensions/mentions/src/Formatter/FormatPostMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Flarum\Discussion\Discussion;
use Flarum\Http\SlugManager;
use Flarum\Locale\TranslatorInterface;
use Flarum\Post\Post;
use Psr\Http\Message\ServerRequestInterface as Request;
use s9e\TextFormatter\Renderer;
use s9e\TextFormatter\Utils;
Expand All @@ -24,12 +25,22 @@ public function __construct(
) {
}

public function __invoke(Renderer $renderer, mixed $context, ?string $xml, Request $request = null): string
/**
* Configure rendering for post mentions.
*
* @param \s9e\TextFormatter\Renderer $renderer
* @param mixed $context
* @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: 5 additions & 3 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 Flarum\Locale\TranslatorInterface;
use s9e\TextFormatter\Utils;

Expand All @@ -31,10 +32,11 @@ public function __invoke(mixed $context, string $xml): string
*/
protected function updatePostMentionTags(mixed $context, string $xml): string
{
$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

0 comments on commit a0e3ae9

Please sign in to comment.