Skip to content

Commit 661e1d2

Browse files
committed
fix: chatbox censoring
For years, chatbox has only censored messages based on the settings of the sender, not the recipient. fixes #4982
1 parent 9618870 commit 661e1d2

File tree

4 files changed

+27
-33
lines changed

4 files changed

+27
-33
lines changed

app/Helpers/Bbcode.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace App\Helpers;
1818

1919
use App\Models\WhitelistedImageUrl;
20+
use Illuminate\Support\Str;
2021

2122
class Bbcode
2223
{
@@ -284,6 +285,19 @@ public function parse(?string $source, bool $replaceLineBreaks = true): string
284285
$source ??= '';
285286
$source = htmlspecialchars($source, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5, 'UTF-8');
286287

288+
// Censor words
289+
foreach (config('censor.redact') as $word) {
290+
if (preg_match(\sprintf('/\b%s(?=[.,]|$|\s)/mi', $word), (string) $source)) {
291+
$source = str_replace($word, \sprintf("<span class='censor'>%s</span>", $word), (string) $source);
292+
}
293+
}
294+
295+
foreach (config('censor.replace') as $word => $replacementWord) {
296+
if (Str::contains($source, $word)) {
297+
$source = str_replace($word, $replacementWord, (string) $source);
298+
}
299+
}
300+
287301
// Replace all void elements since they don't have closing tags
288302
$source = str_replace('[*]', '<li>', (string) $source);
289303
$source = str_replace('[hr]', '<hr>', $source);

app/Repositories/ChatRepository.php

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@
2323
use App\Models\Chatroom;
2424
use App\Models\Message;
2525
use App\Models\User;
26+
use App\Models\UserEcho;
2627
use Illuminate\Support\Str;
2728

2829
class ChatRepository
2930
{
3031
public function message(int $userId, int $roomId, string $message, ?int $receiver = null, ?int $bot = null): Message
3132
{
32-
if (User::find($userId)->settings->censor) {
33-
$message = $this->censorMessage($message);
34-
}
35-
3633
$message = Message::create([
3734
'user_id' => $userId,
3835
'chatroom_id' => $roomId,
@@ -50,12 +47,6 @@ public function message(int $userId, int $roomId, string $message, ?int $receive
5047

5148
public function botMessage(int $botId, int $roomId, string $message, ?int $receiver = null): void
5249
{
53-
$user = User::find($receiver);
54-
55-
if ($user->settings->censor) {
56-
$message = $this->censorMessage($message);
57-
}
58-
5950
$save = Message::create([
6051
'bot_id' => $botId,
6152
'user_id' => 1,
@@ -77,10 +68,6 @@ public function botMessage(int $botId, int $roomId, string $message, ?int $recei
7768

7869
public function privateMessage(int $userId, int $roomId, string $message, ?int $receiver = null, ?int $bot = null, ?bool $ignore = null): Message
7970
{
80-
if (User::find($userId)->settings->censor) {
81-
$message = $this->censorMessage($message);
82-
}
83-
8471
$save = Message::create([
8572
'user_id' => $userId,
8673
'chatroom_id' => 0,
@@ -246,21 +233,4 @@ public function systemChatroom(int|Chatroom|string|null $room = null): int
246233

247234
return $room;
248235
}
249-
250-
protected function censorMessage(string $message): string
251-
{
252-
foreach (config('censor.redact') as $word) {
253-
if (preg_match(\sprintf('/\b%s(?=[.,]|$|\s)/mi', $word), (string) $message)) {
254-
$message = str_replace($word, \sprintf("<span class='censor'>%s</span>", $word), (string) $message);
255-
}
256-
}
257-
258-
foreach (config('censor.replace') as $word => $replacementWord) {
259-
if (Str::contains($message, $word)) {
260-
$message = str_replace($word, $replacementWord, (string) $message);
261-
}
262-
}
263-
264-
return $message;
265-
}
266236
}

resources/sass/components/_bbcode-rendered.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,4 +534,10 @@
534534
padding-right: 1ch;
535535
}
536536
}
537+
538+
/* Censor */
539+
540+
&.bbcode-rendered__censor .censor:not(:hover) {
541+
filter: blur(0.5em);
542+
}
537543
}

resources/views/blocks/chat.blade.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@php
2-
$user = App\Models\User::with(['chatroom', 'group'])->find(auth()->id());
2+
$user = App\Models\User::with(['chatroom', 'group', 'settings'])->find(auth()->id());
33
@endphp
44

55
<section
@@ -318,7 +318,11 @@ class="chatbox-message__avatar"
318318
</figure>
319319
</aside>
320320
<section
321-
class="chatbox-message__content bbcode-rendered"
321+
@class([
322+
'bbcode-rendered',
323+
'chatbox-message__content',
324+
'bbcode-rendered__censor' => $user->settings->censor
325+
])"
322326
x-show="! (message.bot && message.bot.id >= 1 && (! message.user || message.user.id < 2))"
323327
x-html="message.message"
324328
></section>

0 commit comments

Comments
 (0)