Skip to content
Open
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
14 changes: 14 additions & 0 deletions app/Helpers/Bbcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace App\Helpers;

use App\Models\WhitelistedImageUrl;
use Illuminate\Support\Str;

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

// Censor words
foreach (config('censor.redact') as $word) {
if (preg_match(\sprintf('/\b%s(?=[.,]|$|\s)/mi', $word), (string) $source)) {
$source = str_replace($word, \sprintf("<span class='censor'>%s</span>", $word), (string) $source);
}
}

foreach (config('censor.replace') as $word => $replacementWord) {
if (Str::contains($source, $word)) {
$source = str_replace($word, $replacementWord, (string) $source);
}
}

// Replace all void elements since they don't have closing tags
$source = str_replace('[*]', '<li>', (string) $source);
$source = str_replace('[hr]', '<hr>', $source);
Expand Down
32 changes: 0 additions & 32 deletions app/Repositories/ChatRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,11 @@
use App\Models\Chatroom;
use App\Models\Message;
use App\Models\User;
use Illuminate\Support\Str;

class ChatRepository
{
public function message(int $userId, int $roomId, string $message, ?int $receiver = null, ?int $bot = null): Message
{
if (User::find($userId)->settings->censor) {
$message = $this->censorMessage($message);
}

$message = Message::create([
'user_id' => $userId,
'chatroom_id' => $roomId,
Expand All @@ -50,12 +45,6 @@ public function message(int $userId, int $roomId, string $message, ?int $receive

public function botMessage(int $botId, int $roomId, string $message, ?int $receiver = null): void
{
$user = User::find($receiver);

if ($user->settings->censor) {
$message = $this->censorMessage($message);
}

$save = Message::create([
'bot_id' => $botId,
'user_id' => 1,
Expand All @@ -77,10 +66,6 @@ public function botMessage(int $botId, int $roomId, string $message, ?int $recei

public function privateMessage(int $userId, int $roomId, string $message, ?int $receiver = null, ?int $bot = null, ?bool $ignore = null): Message
{
if (User::find($userId)->settings->censor) {
$message = $this->censorMessage($message);
}

$save = Message::create([
'user_id' => $userId,
'chatroom_id' => 0,
Expand Down Expand Up @@ -246,21 +231,4 @@ public function systemChatroom(int|Chatroom|string|null $room = null): int

return $room;
}

protected function censorMessage(string $message): string
{
foreach (config('censor.redact') as $word) {
if (preg_match(\sprintf('/\b%s(?=[.,]|$|\s)/mi', $word), (string) $message)) {
$message = str_replace($word, \sprintf("<span class='censor'>%s</span>", $word), (string) $message);
}
}

foreach (config('censor.replace') as $word => $replacementWord) {
if (Str::contains($message, $word)) {
$message = str_replace($word, $replacementWord, (string) $message);
}
}

return $message;
}
}
12 changes: 6 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,12 @@ parameters:
count: 2
path: app/Listeners/AchievementUnlocked.php

-
message: '#^Empty array passed to foreach\.$#'
identifier: foreach.emptyArray
count: 1
path: app/Helpers/Bbcode.php

-
message: '#^Parameter \#1 \$view of function view expects view\-string\|null, string given\.$#'
identifier: argument.type
Expand Down Expand Up @@ -600,12 +606,6 @@ parameters:
count: 1
path: app/Repositories/ChatRepository.php

-
message: '#^Empty array passed to foreach\.$#'
identifier: foreach.emptyArray
count: 1
path: app/Repositories/ChatRepository.php

-
message: '#^Call to function array_key_exists\(\) with ''job'' and array\{adult\: bool\|null, gender\: int\|null, id\: int\|null, known_for_department\: string\|null, name\: string\|null, original_name\: string\|null, popularity\: float\|null, profile_path\: string\|null, \.\.\.\} will always evaluate to true\.$#'
identifier: function.alreadyNarrowedType
Expand Down
6 changes: 6 additions & 0 deletions resources/sass/components/_bbcode-rendered.scss
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,10 @@
padding-right: 1ch;
}
}

/* Censor */

&.bbcode-rendered__censor .censor:not(:hover) {
filter: blur(0.5em);
}
}
8 changes: 6 additions & 2 deletions resources/views/blocks/chat.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@php
$user = App\Models\User::with(['chatroom', 'group'])->find(auth()->id());
$user = App\Models\User::with(['chatroom', 'group', 'settings'])->find(auth()->id());
@endphp

<section
Expand Down Expand Up @@ -318,7 +318,11 @@ class="chatbox-message__avatar"
</figure>
</aside>
<section
class="chatbox-message__content bbcode-rendered"
@class([
'bbcode-rendered',
'chatbox-message__content',
'bbcode-rendered__censor' => $user->settings->censor,
])
x-show="! (message.bot && message.bot.id >= 1 && (! message.user || message.user.id < 2))"
x-html="message.message"
></section>
Expand Down
Loading