1
0
mirror of https://github.com/flarum/core.git synced 2025-08-07 17:07:19 +02:00

perf(mentions): only access related mentions if loaded

This commit is contained in:
Sami Mazouz
2024-12-13 13:06:36 +01:00
parent 4dce4d40a3
commit 9fe17b3c24
7 changed files with 47 additions and 21 deletions

View File

@@ -25,9 +25,13 @@ class FormatGroupMentions
public function __invoke(Renderer $renderer, mixed $context, string $xml): string public function __invoke(Renderer $renderer, mixed $context, string $xml): string
{ {
return Utils::replaceAttributes($xml, 'GROUPMENTION', function ($attributes) use ($context) { return Utils::replaceAttributes($xml, 'GROUPMENTION', function ($attributes) use ($context) {
$group = ($context instanceof AbstractModel && $context->isRelation('mentionsGroups')) /** @var Group|null $group */
? $context->mentionsGroups->find($attributes['id']) // @phpstan-ignore-line $group = match (true) {
: Group::find($attributes['id']); $context instanceof AbstractModel && $context->isRelation('mentionsGroups') => $context->relationLoaded('mentionsGroups')
? $context->mentionsGroups->find($attributes['id']) // @phpstan-ignore-line
: $context->mentionsGroups()->find($attributes['id']), // @phpstan-ignore-line
default => Group::query()->find($attributes['id']),
};
if ($group) { if ($group) {
$attributes['groupname'] = $group->name_plural; $attributes['groupname'] = $group->name_plural;

View File

@@ -31,9 +31,13 @@ class FormatPostMentions
public function __invoke(Renderer $renderer, mixed $context, string $xml): string public function __invoke(Renderer $renderer, mixed $context, string $xml): string
{ {
return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($context) { return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($context) {
$post = ($context instanceof AbstractModel && $context->isRelation('mentionsPosts')) /** @var Post|null $post */
? $context->mentionsPosts->find($attributes['id']) // @phpstan-ignore-line $post = match (true) {
: Post::find($attributes['id']); $context instanceof AbstractModel && $context->isRelation('mentionsPosts') => $context->relationLoaded('mentionsPosts')
? $context->mentionsPosts->find($attributes['id']) // @phpstan-ignore-line
: $context->mentionsPosts()->find($attributes['id']), // @phpstan-ignore-line
default => Post::query()->find($attributes['id']),
};
if ($post && $post->user) { if ($post && $post->user) {
$attributes['displayname'] = $post->user->display_name; $attributes['displayname'] = $post->user->display_name;

View File

@@ -21,9 +21,12 @@ class FormatTagMentions
{ {
return Utils::replaceAttributes($xml, 'TAGMENTION', function ($attributes) use ($context) { return Utils::replaceAttributes($xml, 'TAGMENTION', function ($attributes) use ($context) {
/** @var Tag|null $tag */ /** @var Tag|null $tag */
$tag = ($context instanceof AbstractModel && $context->isRelation('mentionsTags')) $tag = match (true) {
? $context->mentionsTags->find($attributes['id']) // @phpstan-ignore-line $context instanceof AbstractModel && $context->isRelation('mentionsTags') => $context->relationLoaded('mentionsTags')
: Tag::query()->find($attributes['id']); ? $context->mentionsTags->find($attributes['id']) // @phpstan-ignore-line
: $context->mentionsTags()->find($attributes['id']), // @phpstan-ignore-line
default => Tag::query()->find($attributes['id']),
};
if ($tag) { if ($tag) {
$attributes['deleted'] = false; $attributes['deleted'] = false;

View File

@@ -27,9 +27,13 @@ class FormatUserMentions
public function __invoke(Renderer $renderer, mixed $context, string $xml): string public function __invoke(Renderer $renderer, mixed $context, string $xml): string
{ {
return Utils::replaceAttributes($xml, 'USERMENTION', function ($attributes) use ($context) { return Utils::replaceAttributes($xml, 'USERMENTION', function ($attributes) use ($context) {
$user = ($context instanceof AbstractModel && $context->isRelation('mentionsUsers')) /** @var User|null $user */
? $context->mentionsUsers->find($attributes['id']) // @phpstan-ignore-line $user = match (true) {
: User::find($attributes['id']); $context instanceof AbstractModel && $context->isRelation('mentionsUsers') => $context->relationLoaded('mentionsUsers')
? $context->mentionsUsers->find($attributes['id']) // @phpstan-ignore-line
: $context->mentionsUsers()->find($attributes['id']), // @phpstan-ignore-line
default => User::query()->find($attributes['id']),
};
$attributes['deleted'] = false; $attributes['deleted'] = false;

View File

@@ -34,9 +34,13 @@ class UnparsePostMentions
protected function updatePostMentionTags(mixed $context, string $xml): string protected function updatePostMentionTags(mixed $context, string $xml): string
{ {
return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($context) { return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($context) {
$post = ($context instanceof AbstractModel && $context->isRelation('mentionsPosts')) /** @var Post|null $post */
? $context->mentionsPosts->find($attributes['id']) // @phpstan-ignore-line $post = match (true) {
: Post::find($attributes['id']); $context instanceof AbstractModel && $context->isRelation('mentionsPosts') => $context->relationLoaded('mentionsPosts')
? $context->mentionsPosts->find($attributes['id']) // @phpstan-ignore-line
: $context->mentionsPosts()->find($attributes['id']), // @phpstan-ignore-line
default => Post::query()->find($attributes['id']),
};
if ($post && $post->user) { if ($post && $post->user) {
$attributes['displayname'] = $post->user->display_name; $attributes['displayname'] = $post->user->display_name;

View File

@@ -29,9 +29,12 @@ class UnparseTagMentions
{ {
return Utils::replaceAttributes($xml, 'TAGMENTION', function (array $attributes) use ($context) { return Utils::replaceAttributes($xml, 'TAGMENTION', function (array $attributes) use ($context) {
/** @var Tag|null $tag */ /** @var Tag|null $tag */
$tag = ($context instanceof AbstractModel && $context->isRelation('mentionsTags')) $tag = match (true) {
? $context->mentionsTags->find($attributes['id']) // @phpstan-ignore-line $context instanceof AbstractModel && $context->isRelation('mentionsTags') => $context->relationLoaded('mentionsTags')
: Tag::query()->find($attributes['id']); ? $context->mentionsTags->find($attributes['id']) // @phpstan-ignore-line
: $context->mentionsTags()->find($attributes['id']), // @phpstan-ignore-line
default => Tag::query()->find($attributes['id']),
};
if ($tag) { if ($tag) {
$attributes['tagname'] = $tag->name; $attributes['tagname'] = $tag->name;

View File

@@ -34,9 +34,13 @@ class UnparseUserMentions
protected function updateUserMentionTags(mixed $context, string $xml): string protected function updateUserMentionTags(mixed $context, string $xml): string
{ {
return Utils::replaceAttributes($xml, 'USERMENTION', function ($attributes) use ($context) { return Utils::replaceAttributes($xml, 'USERMENTION', function ($attributes) use ($context) {
$user = ($context instanceof AbstractModel && $context->isRelation('mentionsUsers')) /** @var User|null $user */
? $context->mentionsUsers->find($attributes['id']) // @phpstan-ignore-line $user = match (true) {
: User::find($attributes['id']); $context instanceof AbstractModel && $context->isRelation('mentionsUsers') => $context->relationLoaded('mentionsUsers')
? $context->mentionsUsers->find($attributes['id']) // @phpstan-ignore-line
: $context->mentionsUsers()->find($attributes['id']), // @phpstan-ignore-line
default => User::query()->find($attributes['id']),
};
$attributes['displayname'] = $user?->display_name ?? $this->translator->trans('core.lib.username.deleted_text'); $attributes['displayname'] = $user?->display_name ?? $this->translator->trans('core.lib.username.deleted_text');