Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: store message mentions for better performance #4079

Merged
merged 5 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions extensions/mentions/src/Formatter/FormatGroupMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,8 +25,8 @@ public function __construct(
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) {
Expand Down
14 changes: 4 additions & 10 deletions extensions/mentions/src/Formatter/FormatPostMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,18 +27,12 @@ public function __construct(

/**
* 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) {
Expand Down
8 changes: 4 additions & 4 deletions extensions/mentions/src/Formatter/FormatTagMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@

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;
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) {
Expand Down
6 changes: 3 additions & 3 deletions extensions/mentions/src/Formatter/FormatUserMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,8 +27,8 @@ public function __construct(
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;
Expand Down
5 changes: 3 additions & 2 deletions extensions/mentions/src/Formatter/UnparsePostMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Flarum\Mentions\Formatter;

use Flarum\Database\AbstractModel;
use Flarum\Locale\TranslatorInterface;
use Flarum\Post\Post;
use s9e\TextFormatter\Utils;
Expand All @@ -33,8 +34,8 @@ 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 && 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) {
Expand Down
6 changes: 3 additions & 3 deletions extensions/mentions/src/Formatter/UnparseTagMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Flarum\Mentions\Formatter;

use Flarum\Post\Post;
use Flarum\Database\AbstractModel;
use Flarum\Tags\Tag;
use s9e\TextFormatter\Utils;

Expand All @@ -29,8 +29,8 @@ protected function updateTagMentionTags(mixed $context, string $xml): string
{
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) {
Expand Down
6 changes: 3 additions & 3 deletions extensions/mentions/src/Formatter/UnparseUserMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -34,8 +34,8 @@ 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 && 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');
Expand Down
4 changes: 3 additions & 1 deletion extensions/messages/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,7 @@
->type(Notification\MessageReceivedBlueprint::class, ['email']),

(new Extend\Event())
->listen(DialogMessage\Event\Created::class, Listener\SendNotificationWhenMessageSent::class),
->listen(DialogMessage\Event\Created::class, Listener\SendNotificationWhenMessageSent::class)
->listen(DialogMessage\Event\Created::class, Listener\UpdateMentionsMetadataWhenVisible::class)
->listen(DialogMessage\Event\Updated::class, Listener\UpdateMentionsMetadataWhenVisible::class),
];
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ export default class MessageComposer<CustomAttrs extends IMessageComposerAttrs =
})
.then((message) => {
this.composer.hide();
// @todo: app.dialogs.refresh();
// @ts-ignore
m.route.set(app.route('dialog', { id: message.data.relationships!.dialog.data.id }));
this.attrs.onsubmit?.(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ export default class MessageStream<CustomAttrs extends IDialogStreamAttrs = IDia
.load(() => import('./MessageComposer'), {
user: app.session.user,
replyingTo: this.attrs.dialog,
onsubmit: (message: DialogMessage) => {
this.attrs.state.push(message);
setTimeout(() => this.scrollToBottom(), 50);
onsubmit: () => {
this.attrs.state.refresh().then(() => setTimeout(() => this.scrollToBottom(), 50));
},
})
.then(() => app.composer.show());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
return Migration::createTable(
'dialogs',
function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('first_message_id')->nullable();
$table->unsignedBigInteger('last_message_id')->nullable();
$table->increments('id');
$table->unsignedInteger('first_message_id')->nullable();
$table->unsignedInteger('last_message_id')->nullable();
$table->dateTime('last_message_at')->nullable();
$table->unsignedInteger('last_message_user_id')->nullable();
$table->foreign('last_message_user_id')->references('id')->on('users')->nullOnDelete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
return Migration::createTable(
'dialog_messages',
function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignId('dialog_id')->constrained()->cascadeOnDelete();
$table->increments('id');
$table->unsignedInteger('dialog_id');
$table->foreign('dialog_id')->references('id')->on('dialogs')->cascadeOnDelete();
$table->unsignedInteger('user_id')->nullable();
$table->foreign('user_id')->references('id')->on('users')->cascadeOnDelete();
$table->text('content');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
'dialog_user',
function (Blueprint $table) {
$table->id();
$table->foreignId('dialog_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('dialog_id');
$table->foreign('dialog_id')->references('id')->on('dialogs')->cascadeOnDelete();
$table->unsignedInteger('user_id');
$table->dateTime('joined_at');
$table->unsignedBigInteger('last_read_message_id')->default(0);
$table->unsignedInteger('last_read_message_id')->default(0);
$table->dateTime('last_read_at')->nullable();
$table->foreign('user_id')->references('id')->on('users')->cascadeOnDelete();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;

return Migration::createTable(
'dialog_message_mentions_user',
function (Blueprint $table) {
$table->unsignedInteger('dialog_message_id');
$table->unsignedInteger('mentions_user_id');
$table->dateTime('created_at')->nullable()->useCurrent();

$table->primary(['dialog_message_id', 'mentions_user_id']);
$table->foreign('dialog_message_id')->references('id')->on('dialog_messages')->cascadeOnDelete();
$table->foreign('mentions_user_id')->references('id')->on('users')->cascadeOnDelete();
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;

return Migration::createTable(
'dialog_message_mentions_post',
function (Blueprint $table) {
$table->unsignedInteger('dialog_message_id');
$table->unsignedInteger('mentions_post_id');
$table->dateTime('created_at')->nullable()->useCurrent();

$table->primary(['dialog_message_id', 'mentions_post_id']);
$table->foreign('dialog_message_id')->references('id')->on('dialog_messages')->cascadeOnDelete();
$table->foreign('mentions_post_id')->references('id')->on('posts')->cascadeOnDelete();
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;

return Migration::createTable(
'dialog_message_mentions_group',
function (Blueprint $table) {
$table->unsignedInteger('dialog_message_id');
$table->unsignedInteger('mentions_group_id');
$table->dateTime('created_at')->nullable()->useCurrent();

$table->primary(['dialog_message_id', 'mentions_group_id']);
$table->foreign('dialog_message_id')->references('id')->on('dialog_messages')->cascadeOnDelete();
$table->foreign('mentions_group_id')->references('id')->on('groups')->cascadeOnDelete();
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint;

return Migration::createTable(
'dialog_message_mentions_tag',
function (Blueprint $table) {
$table->unsignedInteger('dialog_message_id');
$table->unsignedInteger('mentions_tag_id');
$table->dateTime('created_at')->nullable()->useCurrent();

$table->primary(['dialog_message_id', 'mentions_tag_id']);
$table->foreign('dialog_message_id')->references('id')->on('dialog_messages')->cascadeOnDelete();
$table->foreign('mentions_tag_id')->references('id')->on('tags')->cascadeOnDelete();
}
);
28 changes: 28 additions & 0 deletions extensions/messages/src/Api/Resource/DialogMessageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Flarum\Api\Schema;
use Flarum\Api\Sort\SortColumn;
use Flarum\Bus\Dispatcher;
use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\ErrorHandling\LogReporter;
use Flarum\Foundation\ValidationException;
use Flarum\Locale\Translator;
Expand All @@ -36,6 +37,7 @@ public function __construct(
protected Translator $translator,
protected LogReporter $log,
protected Dispatcher $bus,
protected ExtensionManager $extensions,
) {
}

Expand Down Expand Up @@ -77,6 +79,20 @@ public function endpoints(): array
}),
Endpoint\Index::make()
->authenticated()
->defaultInclude([
'user',
'mentionsUsers',
'mentionsPosts',
'mentionsGroups',
'mentionsTags',
])
->eagerLoad(function () {
if ($this->extensions->isEnabled('flarum-mentions')) {
return ['mentionsUsers', 'mentionsPosts', 'mentionsGroups', 'mentionsTags'];
}

return [];
})
->paginate(),
];
}
Expand Down Expand Up @@ -126,6 +142,18 @@ public function fields(): array
->includable()
->writableOnCreate()
->requiredOnCreateWithout(['attributes.users']),
Schema\Relationship\ToMany::make('mentionsUsers')
->type('users')
->includable(),
Schema\Relationship\ToMany::make('mentionsPosts')
->type('posts')
->includable(),
Schema\Relationship\ToMany::make('mentionsGroups')
->type('groups')
->includable(),
Schema\Relationship\ToMany::make('mentionsTags')
->type('tags')
->includable(),

];
}
Expand Down
Loading
Loading