feat: moderation
This commit is contained in:
@@ -6,18 +6,24 @@ use App\Enums\DietType;
|
||||
use App\Enums\IngredientUnit;
|
||||
use App\Enums\MealPostType;
|
||||
use App\Enums\MealPostVisibility;
|
||||
use App\Http\Requests\Concerns\ModeratesUserContent;
|
||||
use App\Models\MealPosts;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class MealPostsRequest extends FormRequest
|
||||
{
|
||||
use ModeratesUserContent;
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
$isCreating = $this->isMethod('post');
|
||||
|
||||
return [
|
||||
'image' => [$isCreating ? 'required_without:image_url' : 'sometimes', 'nullable', 'image', 'max:4096'],
|
||||
'image_url' => [$isCreating ? 'required_without:image' : 'sometimes', 'nullable', 'string', 'max:2048'],
|
||||
'image_url' => [$isCreating ? 'required_without:image' : 'sometimes', 'nullable', 'url:http,https', 'max:2048'],
|
||||
'caption' => ['nullable', 'string', 'max:1000'],
|
||||
'calories' => ['nullable', 'integer', 'min:0'],
|
||||
'proteins' => ['nullable', 'numeric', 'min:0'],
|
||||
@@ -42,6 +48,39 @@ class MealPostsRequest extends FormRequest
|
||||
return true;
|
||||
}
|
||||
|
||||
public function after(): array
|
||||
{
|
||||
$mealPost = $this->route('mealPost');
|
||||
$currentVisibility = $mealPost instanceof MealPosts
|
||||
? $mealPost->visibility
|
||||
: MealPostVisibility::Private;
|
||||
$visibility = MealPostVisibility::tryFrom((string) $this->input('visibility')) ?? $currentVisibility;
|
||||
|
||||
if ($visibility !== MealPostVisibility::Public) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$isBecomingPublic = $mealPost instanceof MealPosts
|
||||
&& $mealPost->visibility !== MealPostVisibility::Public;
|
||||
$ingredients = $this->has('ingredients')
|
||||
? collect($this->input('ingredients', []))->pluck('ingredient')->all()
|
||||
: ($isBecomingPublic ? $mealPost->ingredients()->pluck('ingredient')->all() : []);
|
||||
$existingImageUrl = $isBecomingPublic && filled($mealPost->image_url)
|
||||
? (Str::startsWith($mealPost->image_url, ['http://', 'https://'])
|
||||
? $mealPost->image_url
|
||||
: asset(Storage::url($mealPost->image_url)))
|
||||
: null;
|
||||
|
||||
return $this->moderationChecks(
|
||||
[
|
||||
$this->has('title') ? $this->input('title') : ($isBecomingPublic ? $mealPost->title : null),
|
||||
$this->has('caption') ? $this->input('caption') : ($isBecomingPublic ? $mealPost->caption : null),
|
||||
$ingredients,
|
||||
],
|
||||
[$this->file('image'), $this->input('image_url'), $existingImageUrl],
|
||||
);
|
||||
}
|
||||
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
|
||||
Reference in New Issue
Block a user