mirror of
https://github.com/flarum/core.git
synced 2025-08-13 03:44:32 +02:00
perf: store message mentions for better performance (#4079)
This commit is contained in:
@@ -9,9 +9,9 @@
|
||||
|
||||
namespace Flarum\Mentions\Formatter;
|
||||
|
||||
use Flarum\Database\AbstractModel;
|
||||
use Flarum\Group\Group;
|
||||
use Flarum\Locale\TranslatorInterface;
|
||||
use Flarum\Post\Post;
|
||||
use s9e\TextFormatter\Renderer;
|
||||
use s9e\TextFormatter\Utils;
|
||||
|
||||
@@ -25,8 +25,8 @@ class FormatGroupMentions
|
||||
public function __invoke(Renderer $renderer, mixed $context, string $xml): string
|
||||
{
|
||||
return Utils::replaceAttributes($xml, 'GROUPMENTION', function ($attributes) use ($context) {
|
||||
$group = (($context && isset($context->getRelations()['mentionsGroups'])) || $context instanceof Post)
|
||||
? $context->mentionsGroups->find($attributes['id'])
|
||||
$group = ($context instanceof AbstractModel && $context->isRelation('mentionsGroups'))
|
||||
? $context->mentionsGroups->find($attributes['id']) // @phpstan-ignore-line
|
||||
: Group::find($attributes['id']);
|
||||
|
||||
if ($group) {
|
||||
|
@@ -9,11 +9,11 @@
|
||||
|
||||
namespace Flarum\Mentions\Formatter;
|
||||
|
||||
use Flarum\Database\AbstractModel;
|
||||
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;
|
||||
|
||||
@@ -27,18 +27,12 @@ class FormatPostMentions
|
||||
|
||||
/**
|
||||
* 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)
|
||||
public function __invoke(Renderer $renderer, mixed $context, string $xml): string
|
||||
{
|
||||
return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($context) {
|
||||
$post = (($context && isset($context->getRelations()['mentionsPosts'])) || $context instanceof Post)
|
||||
? $context->mentionsPosts->find($attributes['id'])
|
||||
$post = ($context instanceof AbstractModel && $context->isRelation('mentionsPosts'))
|
||||
? $context->mentionsPosts->find($attributes['id']) // @phpstan-ignore-line
|
||||
: Post::find($attributes['id']);
|
||||
|
||||
if ($post && $post->user) {
|
||||
|
@@ -9,7 +9,7 @@
|
||||
|
||||
namespace Flarum\Mentions\Formatter;
|
||||
|
||||
use Flarum\Post\Post;
|
||||
use Flarum\Database\AbstractModel;
|
||||
use Flarum\Tags\Tag;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use s9e\TextFormatter\Renderer;
|
||||
@@ -17,12 +17,12 @@ use s9e\TextFormatter\Utils;
|
||||
|
||||
class FormatTagMentions
|
||||
{
|
||||
public function __invoke(Renderer $renderer, mixed $context, ?string $xml, Request $request = null): string
|
||||
public function __invoke(Renderer $renderer, mixed $context, string $xml, Request $request = null): string
|
||||
{
|
||||
return Utils::replaceAttributes($xml, 'TAGMENTION', function ($attributes) use ($context) {
|
||||
/** @var Tag|null $tag */
|
||||
$tag = (($context && isset($context->getRelations()['mentionsTags'])) || $context instanceof Post)
|
||||
? $context->mentionsTags->find($attributes['id'])
|
||||
$tag = ($context instanceof AbstractModel && $context->isRelation('mentionsTags'))
|
||||
? $context->mentionsTags->find($attributes['id']) // @phpstan-ignore-line
|
||||
: Tag::query()->find($attributes['id']);
|
||||
|
||||
if ($tag) {
|
||||
|
@@ -9,9 +9,9 @@
|
||||
|
||||
namespace Flarum\Mentions\Formatter;
|
||||
|
||||
use Flarum\Database\AbstractModel;
|
||||
use Flarum\Http\SlugManager;
|
||||
use Flarum\Locale\TranslatorInterface;
|
||||
use Flarum\Post\Post;
|
||||
use Flarum\User\User;
|
||||
use s9e\TextFormatter\Renderer;
|
||||
use s9e\TextFormatter\Utils;
|
||||
@@ -27,8 +27,8 @@ class FormatUserMentions
|
||||
public function __invoke(Renderer $renderer, mixed $context, string $xml): string
|
||||
{
|
||||
return Utils::replaceAttributes($xml, 'USERMENTION', function ($attributes) use ($context) {
|
||||
$user = (($context && isset($context->getRelations()['mentionsUsers'])) || $context instanceof Post)
|
||||
? $context->mentionsUsers->find($attributes['id'])
|
||||
$user = ($context instanceof AbstractModel && $context->isRelation('mentionsUsers'))
|
||||
? $context->mentionsUsers->find($attributes['id']) // @phpstan-ignore-line
|
||||
: User::find($attributes['id']);
|
||||
|
||||
$attributes['deleted'] = false;
|
||||
|
@@ -9,6 +9,7 @@
|
||||
|
||||
namespace Flarum\Mentions\Formatter;
|
||||
|
||||
use Flarum\Database\AbstractModel;
|
||||
use Flarum\Locale\TranslatorInterface;
|
||||
use Flarum\Post\Post;
|
||||
use s9e\TextFormatter\Utils;
|
||||
@@ -33,8 +34,8 @@ class UnparsePostMentions
|
||||
protected function updatePostMentionTags(mixed $context, string $xml): string
|
||||
{
|
||||
return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($context) {
|
||||
$post = (($context && isset($context->getRelations()['mentionsPosts'])) || $context instanceof Post)
|
||||
? $context->mentionsPosts->find($attributes['id'])
|
||||
$post = ($context instanceof AbstractModel && $context->isRelation('mentionsPosts'))
|
||||
? $context->mentionsPosts->find($attributes['id']) // @phpstan-ignore-line
|
||||
: Post::find($attributes['id']);
|
||||
|
||||
if ($post && $post->user) {
|
||||
|
@@ -9,7 +9,7 @@
|
||||
|
||||
namespace Flarum\Mentions\Formatter;
|
||||
|
||||
use Flarum\Post\Post;
|
||||
use Flarum\Database\AbstractModel;
|
||||
use Flarum\Tags\Tag;
|
||||
use s9e\TextFormatter\Utils;
|
||||
|
||||
@@ -29,8 +29,8 @@ class UnparseTagMentions
|
||||
{
|
||||
return Utils::replaceAttributes($xml, 'TAGMENTION', function (array $attributes) use ($context) {
|
||||
/** @var Tag|null $tag */
|
||||
$tag = (($context && isset($context->getRelations()['mentionsTags'])) || $context instanceof Post)
|
||||
? $context->mentionsTags->find($attributes['id'])
|
||||
$tag = ($context instanceof AbstractModel && $context->isRelation('mentionsTags'))
|
||||
? $context->mentionsTags->find($attributes['id']) // @phpstan-ignore-line
|
||||
: Tag::query()->find($attributes['id']);
|
||||
|
||||
if ($tag) {
|
||||
|
@@ -9,8 +9,8 @@
|
||||
|
||||
namespace Flarum\Mentions\Formatter;
|
||||
|
||||
use Flarum\Database\AbstractModel;
|
||||
use Flarum\Locale\TranslatorInterface;
|
||||
use Flarum\Post\Post;
|
||||
use Flarum\User\User;
|
||||
use s9e\TextFormatter\Utils;
|
||||
|
||||
@@ -34,8 +34,8 @@ class UnparseUserMentions
|
||||
protected function updateUserMentionTags(mixed $context, string $xml): string
|
||||
{
|
||||
return Utils::replaceAttributes($xml, 'USERMENTION', function ($attributes) use ($context) {
|
||||
$user = (($context && isset($context->getRelations()['mentionsUsers'])) || $context instanceof Post)
|
||||
? $context->mentionsUsers->find($attributes['id'])
|
||||
$user = ($context instanceof AbstractModel && $context->isRelation('mentionsUsers'))
|
||||
? $context->mentionsUsers->find($attributes['id']) // @phpstan-ignore-line
|
||||
: User::find($attributes['id']);
|
||||
|
||||
$attributes['displayname'] = $user?->display_name ?? $this->translator->trans('core.lib.username.deleted_text');
|
||||
|
Reference in New Issue
Block a user