Skip to content

Commit

Permalink
fix: missing slug from post mention links
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Jul 4, 2023
1 parent 8f75139 commit 1767833
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
10 changes: 6 additions & 4 deletions extensions/mentions/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@
->addInclude(['posts.mentionedBy', 'posts.mentionedBy.user', 'posts.mentionedBy.discussion'])
->load([
'posts.mentionsUsers', 'posts.mentionsPosts', 'posts.mentionsPosts.user',
'posts.mentionsGroups'
'posts.mentionsPosts.discussion', 'posts.mentionsGroups'
])
->loadWhere('posts.mentionedBy', [LoadMentionedByRelationship::class, 'mutateRelation'])
->prepareDataForSerialization([LoadMentionedByRelationship::class, 'countRelation']),

(new Extend\ApiController(Controller\ListDiscussionsController::class))
->load([
'firstPost.mentionsUsers', 'firstPost.mentionsPosts', 'firstPost.mentionsPosts.user', 'firstPost.mentionsGroups',
'lastPost.mentionsUsers', 'lastPost.mentionsPosts', 'lastPost.mentionsPosts.user', 'lastPost.mentionsGroups',
'firstPost.mentionsUsers', 'firstPost.mentionsPosts',
'firstPost.mentionsPosts.user', 'firstPost.mentionsPosts.discussion', 'firstPost.mentionsGroups',
'lastPost.mentionsUsers', 'lastPost.mentionsPosts',
'lastPost.mentionsPosts.user', 'lastPost.mentionsPosts.discussion', 'lastPost.mentionsGroups',
]),

(new Extend\ApiController(Controller\ShowPostController::class))
Expand All @@ -98,7 +100,7 @@

(new Extend\ApiController(Controller\ListPostsController::class))
->addInclude(['mentionedBy', 'mentionedBy.user', 'mentionedBy.discussion'])
->load(['mentionsUsers', 'mentionsPosts', 'mentionsPosts.user', 'mentionsGroups'])
->load(['mentionsUsers', 'mentionsPosts', 'mentionsPosts.user', 'mentionsPosts.discussion', 'mentionsGroups'])
->loadWhere('mentionedBy', [LoadMentionedByRelationship::class, 'mutateRelation'])
->prepareDataForSerialization([LoadMentionedByRelationship::class, 'countRelation']),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function mutateRelation(BelongsToMany $query, ServerRequestInterfa
$actor = RequestUtil::getActor($request);

return $query
->with(['mentionsPosts', 'mentionsPosts.user', 'mentionsUsers'])
->with(['mentionsPosts', 'mentionsPosts.user', 'mentionsPosts.discussion', 'mentionsUsers'])
->whereVisibleTo($actor)
->oldest()
// Limiting a relationship results is only possible because
Expand Down
Empty file.
22 changes: 21 additions & 1 deletion extensions/mentions/src/Formatter/FormatPostMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace Flarum\Mentions\Formatter;

use Flarum\Discussion\Discussion;
use Flarum\Http\SlugManager;
use Flarum\Post\CommentPost;
use Psr\Http\Message\ServerRequestInterface as Request;
use s9e\TextFormatter\Renderer;
use s9e\TextFormatter\Utils;
Expand All @@ -21,9 +24,15 @@ class FormatPostMentions
*/
private $translator;

public function __construct(TranslatorInterface $translator)
/**
* @var SlugManager
*/
private $slugManager;

public function __construct(TranslatorInterface $translator, SlugManager $slugManager)
{
$this->translator = $translator;
$this->slugManager = $slugManager;
}

/**
Expand All @@ -40,6 +49,7 @@ public function __invoke(Renderer $renderer, $context, $xml, Request $request =
$post = $context;

return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($post) {
/** @var CommentPost $post */
$post = $post->mentionsPosts->find($attributes['id']);
if ($post && $post->user) {
$attributes['displayname'] = $post->user->display_name;
Expand All @@ -56,6 +66,16 @@ public function __invoke(Renderer $renderer, $context, $xml, Request $request =
$attributes['displayname'] = $this->translator->trans('core.lib.username.deleted_text');
}

if (! $post->relationLoaded('discussion')) {
dd($post);
}

if ($post) {
$attributes['discussionid'] = $this->slugManager
->forResource(Discussion::class)
->toSlug($post->discussion);
}

return $attributes;
});
}
Expand Down

0 comments on commit 1767833

Please sign in to comment.