diff --git a/app/Community/Actions/ReplaceUserShortcodesWithUsernamesAction.php b/app/Community/Actions/ReplaceUserShortcodesWithUsernamesAction.php new file mode 100644 index 0000000000..bae7154b39 --- /dev/null +++ b/app/Community/Actions/ReplaceUserShortcodesWithUsernamesAction.php @@ -0,0 +1,32 @@ +get()->keyBy('ID'); + + // Replace each shortcode with the corresponding username. + return preg_replace_callback('/\[user=(\d+)\]/', function ($matches) use ($users) { + $userId = $matches[1]; + $user = $users->get($userId); + + return '[user=' . ($user ? $user->username : $userId) . ']'; + }, $messageBody); + } +} diff --git a/app/Community/Listeners/NotifyMessageThreadParticipants.php b/app/Community/Listeners/NotifyMessageThreadParticipants.php index f9bdb1cc47..bac314dbd8 100644 --- a/app/Community/Listeners/NotifyMessageThreadParticipants.php +++ b/app/Community/Listeners/NotifyMessageThreadParticipants.php @@ -11,6 +11,7 @@ use App\Models\MessageThread; use App\Models\MessageThreadParticipant; use App\Models\User; +use App\Support\Shortcode\Shortcode; use GuzzleHttp\Client; use Illuminate\Support\Arr; use Illuminate\Support\Facades\DB; @@ -73,6 +74,8 @@ private function forwardToDiscord( MessageThread $messageThread, Message $message ): void { + $message->body = Shortcode::stripAndClamp($message->body); + $inboxConfig = config('services.discord.inbox_webhook.' . $userTo->username); $webhookUrl = $inboxConfig['url'] ?? null; diff --git a/app/Community/Livewire/Forms/ForumTopicCommentForm.php b/app/Community/Livewire/Forms/ForumTopicCommentForm.php index 9716b434d1..0f4b56a852 100644 --- a/app/Community/Livewire/Forms/ForumTopicCommentForm.php +++ b/app/Community/Livewire/Forms/ForumTopicCommentForm.php @@ -4,6 +4,7 @@ namespace App\Community\Livewire\Forms; +use App\Community\Actions\ReplaceUserShortcodesWithUsernamesAction; use App\Models\ForumTopicComment; use App\Support\Rules\ContainsRegularCharacter; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; @@ -24,7 +25,11 @@ class ForumTopicCommentForm extends Form public function setForumTopicComment(ForumTopicComment $forumTopicComment): void { $this->forumTopicComment = $forumTopicComment; - $this->body = $forumTopicComment->body; + + // "[user=1]" -> "[user=Scott]" + $this->body = ( + new ReplaceUserShortcodesWithUsernamesAction() + )->execute($this->forumTopicComment->body); } public function update(): RedirectResponse|Redirector diff --git a/app/Helpers/util/mail.php b/app/Helpers/util/mail.php index c47bb809f0..ce233a1174 100644 --- a/app/Helpers/util/mail.php +++ b/app/Helpers/util/mail.php @@ -3,6 +3,7 @@ use App\Community\Enums\ArticleType; use App\Enums\Permissions; use App\Models\User; +use App\Support\Shortcode\Shortcode; use Aws\CommandPool; use Illuminate\Contracts\Mail\Mailer as MailerContract; use Illuminate\Mail\Mailer; @@ -364,6 +365,7 @@ function SendPrivateMessageEmail( } $content = stripslashes(nl2br($contentIn)); + $content = Shortcode::stripAndClamp($content); // Also used for Generic text: $emailTitle = "New Private Message from $fromUser"; diff --git a/tests/Feature/Community/Actions/ReplaceUserShortcodesWithUsernamesActionTest.php b/tests/Feature/Community/Actions/ReplaceUserShortcodesWithUsernamesActionTest.php new file mode 100644 index 0000000000..656e8c93af --- /dev/null +++ b/tests/Feature/Community/Actions/ReplaceUserShortcodesWithUsernamesActionTest.php @@ -0,0 +1,30 @@ +create(['ID' => 100, 'User' => 'Scott']); + User::factory()->create(['ID' => 101, 'User' => 'Batman']); + + $messageBody = "[user=100] might actually be [user=101]."; + + // Act + $result = (new ReplaceUserShortcodesWithUsernamesAction())->execute($messageBody); + + // Assert + $this->assertEquals("[user=Scott] might actually be [user=Batman].", $result); + } +}