StoreStatusEditRequest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Http\Requests\Status;
  3. use App\Media;
  4. use App\Status;
  5. use Closure;
  6. use Illuminate\Foundation\Http\FormRequest;
  7. class StoreStatusEditRequest extends FormRequest
  8. {
  9. /**
  10. * Determine if the user is authorized to make this request.
  11. */
  12. public function authorize(): bool
  13. {
  14. $profile = $this->user()->profile;
  15. if ($profile->status != null) {
  16. return false;
  17. }
  18. if ($profile->unlisted == true && $profile->cw == true) {
  19. return false;
  20. }
  21. $types = [
  22. 'photo',
  23. 'photo:album',
  24. 'photo:video:album',
  25. 'reply',
  26. 'text',
  27. 'video',
  28. 'video:album',
  29. ];
  30. $scopes = ['public', 'unlisted', 'private'];
  31. $status = Status::whereNull('reblog_of_id')->whereIn('type', $types)->whereIn('scope', $scopes)->find($this->route('id'));
  32. return $status && $this->user()->profile_id === $status->profile_id;
  33. }
  34. /**
  35. * Get the validation rules that apply to the request.
  36. *
  37. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
  38. */
  39. public function rules(): array
  40. {
  41. return [
  42. 'status' => 'sometimes|max:'.config_cache('pixelfed.max_caption_length', 500),
  43. 'spoiler_text' => 'nullable|string|max:140',
  44. 'sensitive' => 'sometimes|boolean',
  45. 'media_ids' => [
  46. 'nullable',
  47. 'required_without:status',
  48. 'array',
  49. 'max:'.(int) config_cache('pixelfed.max_album_length'),
  50. function (string $attribute, mixed $value, Closure $fail) {
  51. Media::whereProfileId($this->user()->profile_id)
  52. ->where(function ($query) {
  53. return $query->whereNull('status_id')
  54. ->orWhere('status_id', '=', $this->route('id'));
  55. })
  56. ->findOrFail($value);
  57. },
  58. ],
  59. 'location' => 'sometimes|nullable',
  60. 'location.id' => 'sometimes|integer|min:1|max:128769',
  61. 'location.country' => 'required_with:location.id',
  62. 'location.name' => 'required_with:location.id',
  63. ];
  64. }
  65. }