diff --git a/app/Models/Chat/Channel.php b/app/Models/Chat/Channel.php index 6476f7be9b3..026e1ebf1ea 100644 --- a/app/Models/Chat/Channel.php +++ b/app/Models/Chat/Channel.php @@ -456,14 +456,8 @@ public function receiveMessage(User $sender, ?string $content, bool $isAction = throw new API\ExcessiveChatMessagesException(osu_trans('api.error.chat.limit_exceeded')); } - $chatFilters = app('chat-filters')->all(); - - foreach ($chatFilters as $filter) { - $content = str_replace($filter->match, $filter->replacement, $content); - } - $message = new Message([ - 'content' => $content, + 'content' => app('chat-filters')->filter($content), 'is_action' => $isAction, 'timestamp' => $now, ]); diff --git a/app/Singletons/ChatFilters.php b/app/Singletons/ChatFilters.php index 24d96c92751..2700379b7c5 100644 --- a/app/Singletons/ChatFilters.php +++ b/app/Singletons/ChatFilters.php @@ -7,19 +7,22 @@ use App\Models\ChatFilter; use App\Traits\Memoizes; -use Illuminate\Database\Eloquent\Collection; class ChatFilters { use Memoizes; - public function all() + public function filter(string $text): string { - return $this->memoize(__FUNCTION__, fn () => $this->fetch()); - } + $replacements = $this->memoize(__FUNCTION__, function () { + $ret = []; + foreach (ChatFilter::all() as $entry) { + $ret[$entry->match] = $entry->replacement; + } - protected function fetch(): Collection - { - return ChatFilter::all(); + return $ret; + }); + + return strtr($text, $replacements); } }