Skip to content

Commit

Permalink
fix?: Allow mentions from non-post objects (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland authored Nov 28, 2021
1 parent 8154f29 commit 96a7e20
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/Formatter/FormatUserMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Flarum\Mentions\Formatter;

use Flarum\Http\SlugManager;
use Flarum\Post\Post;
use Flarum\User\User;
use s9e\TextFormatter\Renderer;
use s9e\TextFormatter\Utils;
Expand Down Expand Up @@ -43,10 +44,10 @@ public function __construct(SlugManager $slugManager, TranslatorInterface $trans
*/
public function __invoke(Renderer $renderer, $context, string $xml)
{
$post = $context;

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

$attributes['deleted'] = false;

Expand Down
9 changes: 5 additions & 4 deletions src/Formatter/UnparseUserMentions.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\User\User;
use s9e\TextFormatter\Utils;
use Symfony\Contracts\Translation\TranslatorInterface;
Expand Down Expand Up @@ -49,10 +50,10 @@ public function __invoke($context, string $xml)
*/
protected function updateUserMentionTags($context, string $xml): string
{
$post = $context;

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

if ($user) {
$attributes['displayname'] = $user->display_name;
Expand Down

0 comments on commit 96a7e20

Please sign in to comment.