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

fix(messages): replace [user=ID] in email/discord previews with @username #2687

Merged
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
32 changes: 32 additions & 0 deletions app/Community/Actions/ReplaceUserShortcodesWithUsernamesAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Community\Actions;

use App\Models\User;

class ReplaceUserShortcodesWithUsernamesAction
{
public function execute(string $messageBody): string
{
// Extract all user IDs from the message.
// We'll do a single query to the users table to avoid an N+1 waterfall.
preg_match_all('/\[user=(\d+)\]/', $messageBody, $matches);
$userIds = $matches[1] ?? [];

if (empty($userIds)) {
return $messageBody;
}

$users = User::whereIn('ID', $userIds)->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);
}
}
3 changes: 3 additions & 0 deletions app/Community/Listeners/NotifyMessageThreadParticipants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
7 changes: 6 additions & 1 deletion app/Community/Livewire/Forms/ForumTopicCommentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/Helpers/util/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Community\Actions;

use App\Community\Actions\ReplaceUserShortcodesWithUsernamesAction;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ReplaceUserShortcodesWithUsernamesActionTest extends TestCase
{
use RefreshDatabase;

public function testItReplacesUserIds(): void
{
// Arrange
User::factory()->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);
}
}