mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-07-11 01:24:46 +02:00
61 lines
2.0 KiB
PHP
61 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Rules;
|
|
|
|
use App\Constants\Config\ServiceConstants;
|
|
use App\Constants\Config\ValidationConstants;
|
|
use App\Enums\Rules\ModerationService;
|
|
use Closure;
|
|
use Exception;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Translation\PotentiallyTranslatedString;
|
|
use RuntimeException;
|
|
|
|
class ModerationRule implements ValidationRule
|
|
{
|
|
/**
|
|
* @param Closure(string): PotentiallyTranslatedString $fail
|
|
*
|
|
* @throws RuntimeException
|
|
*/
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
{
|
|
match (Config::get(ValidationConstants::MODERATION_SERVICE_QUALIFIED)) {
|
|
ModerationService::NONE->value => null,
|
|
ModerationService::OPENAI->value => $this->validateForOpenAI($attribute, $value, $fail),
|
|
default => throw new RuntimeException('Invalid moderation service config value'),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Apply content filtering with OpenAI Moderation API.
|
|
*
|
|
* @param Closure(string): PotentiallyTranslatedString $fail
|
|
*/
|
|
private function validateForOpenAI(string $attribute, mixed $value, Closure $fail): void
|
|
{
|
|
try {
|
|
$response = Http::acceptJson()
|
|
->withToken(Config::get(ServiceConstants::OPENAI_BEARER_TOKEN))
|
|
->post('https://api.openai.com/v1/moderations', ['input' => $value])
|
|
->throw()
|
|
->json();
|
|
|
|
$flagged = Arr::get($response, 'results.0.flagged');
|
|
|
|
if ($flagged === true) {
|
|
$fail(__('validation.moderation', ['attribute' => $attribute]));
|
|
}
|
|
} catch (Exception $e) {
|
|
// Don't block site functionality if third-party service is down
|
|
Log::error($e->getMessage(), ['value' => $value]);
|
|
}
|
|
}
|
|
}
|