Skip to content

Commit

Permalink
perf(mentions): only access related mentions if loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Dec 13, 2024
1 parent 4dce4d4 commit 9fe17b3
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 21 deletions.
10 changes: 7 additions & 3 deletions extensions/mentions/src/Formatter/FormatGroupMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ public function __construct(
public function __invoke(Renderer $renderer, mixed $context, string $xml): string
{
return Utils::replaceAttributes($xml, 'GROUPMENTION', function ($attributes) use ($context) {
$group = ($context instanceof AbstractModel && $context->isRelation('mentionsGroups'))
? $context->mentionsGroups->find($attributes['id']) // @phpstan-ignore-line
: Group::find($attributes['id']);
/** @var Group|null $group */
$group = match (true) {
$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) {
$attributes['groupname'] = $group->name_plural;
Expand Down
10 changes: 7 additions & 3 deletions extensions/mentions/src/Formatter/FormatPostMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ public function __construct(
public function __invoke(Renderer $renderer, mixed $context, string $xml): string
{
return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($context) {
$post = ($context instanceof AbstractModel && $context->isRelation('mentionsPosts'))
? $context->mentionsPosts->find($attributes['id']) // @phpstan-ignore-line
: Post::find($attributes['id']);
/** @var Post|null $post */
$post = match (true) {
$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) {
$attributes['displayname'] = $post->user->display_name;
Expand Down
9 changes: 6 additions & 3 deletions extensions/mentions/src/Formatter/FormatTagMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ public function __invoke(Renderer $renderer, mixed $context, string $xml, ?Reque
{
return Utils::replaceAttributes($xml, 'TAGMENTION', function ($attributes) use ($context) {
/** @var Tag|null $tag */
$tag = ($context instanceof AbstractModel && $context->isRelation('mentionsTags'))
? $context->mentionsTags->find($attributes['id']) // @phpstan-ignore-line
: Tag::query()->find($attributes['id']);
$tag = match (true) {
$context instanceof AbstractModel && $context->isRelation('mentionsTags') => $context->relationLoaded('mentionsTags')
? $context->mentionsTags->find($attributes['id']) // @phpstan-ignore-line
: $context->mentionsTags()->find($attributes['id']), // @phpstan-ignore-line
default => Tag::query()->find($attributes['id']),
};

if ($tag) {
$attributes['deleted'] = false;
Expand Down
10 changes: 7 additions & 3 deletions extensions/mentions/src/Formatter/FormatUserMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ public function __construct(
public function __invoke(Renderer $renderer, mixed $context, string $xml): string
{
return Utils::replaceAttributes($xml, 'USERMENTION', function ($attributes) use ($context) {
$user = ($context instanceof AbstractModel && $context->isRelation('mentionsUsers'))
? $context->mentionsUsers->find($attributes['id']) // @phpstan-ignore-line
: User::find($attributes['id']);
/** @var User|null $user */
$user = match (true) {
$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;

Expand Down
10 changes: 7 additions & 3 deletions extensions/mentions/src/Formatter/UnparsePostMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ public function __invoke(mixed $context, string $xml): string
protected function updatePostMentionTags(mixed $context, string $xml): string
{
return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($context) {
$post = ($context instanceof AbstractModel && $context->isRelation('mentionsPosts'))
? $context->mentionsPosts->find($attributes['id']) // @phpstan-ignore-line
: Post::find($attributes['id']);
/** @var Post|null $post */
$post = match (true) {
$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) {
$attributes['displayname'] = $post->user->display_name;
Expand Down
9 changes: 6 additions & 3 deletions extensions/mentions/src/Formatter/UnparseTagMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ protected function updateTagMentionTags(mixed $context, string $xml): string
{
return Utils::replaceAttributes($xml, 'TAGMENTION', function (array $attributes) use ($context) {
/** @var Tag|null $tag */
$tag = ($context instanceof AbstractModel && $context->isRelation('mentionsTags'))
? $context->mentionsTags->find($attributes['id']) // @phpstan-ignore-line
: Tag::query()->find($attributes['id']);
$tag = match (true) {
$context instanceof AbstractModel && $context->isRelation('mentionsTags') => $context->relationLoaded('mentionsTags')
? $context->mentionsTags->find($attributes['id']) // @phpstan-ignore-line
: $context->mentionsTags()->find($attributes['id']), // @phpstan-ignore-line
default => Tag::query()->find($attributes['id']),
};

if ($tag) {
$attributes['tagname'] = $tag->name;
Expand Down
10 changes: 7 additions & 3 deletions extensions/mentions/src/Formatter/UnparseUserMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ public function __invoke(mixed $context, string $xml): string
protected function updateUserMentionTags(mixed $context, string $xml): string
{
return Utils::replaceAttributes($xml, 'USERMENTION', function ($attributes) use ($context) {
$user = ($context instanceof AbstractModel && $context->isRelation('mentionsUsers'))
? $context->mentionsUsers->find($attributes['id']) // @phpstan-ignore-line
: User::find($attributes['id']);
/** @var User|null $user */
$user = match (true) {
$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');

Expand Down

0 comments on commit 9fe17b3

Please sign in to comment.