Repository navigation
Expand file tree
/
Copy pathCreateCommitRequest.php
More file actions
217 lines (189 loc) · 6.9 KB
/
Copy pathCreateCommitRequest.php
File metadata and controls
217 lines (189 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
declare(strict_types=1);
namespace App\Http\Requests;
use App\Actions\CreateCommitImages;
use App\Dto\NewPoll;
use App\Enums\Feature;
use App\Enums\PollDuration;
use App\Models\Commit;
use App\Models\Guild;
use App\Models\Organization;
use App\Models\User;
use App\Rules\CommitBodyLength;
use App\Rules\OrganizationToPostAs;
use App\Rules\VisibleCommit;
use App\Services\CommitBodies;
use App\Services\Features;
use App\Services\Urls;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\UploadedFile;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Dimensions;
use Illuminate\Validation\Rules\Enum;
use Illuminate\Validation\Rules\File;
use Illuminate\Validation\Rules\In;
final class CreateCommitRequest extends FormRequest
{
/**
* Images make text optional. Bound the raw body before parsing links to limit storage and processing costs.
*
* @return list<string|ValidationRule>
*/
public static function bodyRules(bool $hasImages = false): array
{
return ['bail', $hasImages ? 'nullable' : 'required', 'string', 'min:'.Commit::MIN_BODY_LENGTH, 'max:'.CommitBodyLength::MAX_RAW_LENGTH, new CommitBodyLength];
}
/**
* Blank lines are collapsed before the body is measured, by every request that takes a commit body.
*/
public static function normalizeBody(FormRequest $request): void
{
if (is_string($request->input('body'))) {
$request->merge(['body' => resolve(CommitBodies::class)->normalize($request->string('body')->value())]);
}
}
/** @return array<string, list<string|In>> */
public static function guildRules(FormRequest $request): array
{
return [
'guild_handle' => ['sometimes', 'required', 'string', 'max:60', resolve(Features::class)->available(Feature::Guilds, $request->user())
? 'exists:guilds,handle,user_id,NOT_NULL'
: Rule::in([])],
];
}
/**
* The board the commit goes to, or null when it goes to no guild.
*/
public static function guildOf(FormRequest $request): ?Guild
{
return $request->filled('guild_handle') ? Guild::query()->where('handle', $request->string('guild_handle')->value())->first() : null;
}
/**
* Images are checked by their contents, not their extension, and SVG is never accepted since it can carry scripts.
*
* @return array<string, array<int, string|ValidationRule|File|Dimensions|Enum>>
*/
public static function attachmentRules(): array
{
return [
'poll' => ['nullable', 'array:options,duration', 'prohibits:parent_id,images'],
'poll.options' => ['required_with:poll', 'list', 'min:'.NewPoll::MinOptions, 'max:'.NewPoll::MaxOptions],
'poll.options.*' => ['required', 'string', 'max:'.NewPoll::MaxOptionLength, 'distinct:ignore_case'],
'poll.duration' => ['required_with:poll', Rule::enum(PollDuration::class)],
'images' => ['nullable', 'list', 'max:'.CreateCommitImages::MAX_IMAGES],
'images.*' => [
'required',
'image',
File::types(['image/jpeg', 'image/png', 'image/webp'])->max(CreateCommitImages::MAX_UPLOAD_SIZE),
Rule::dimensions()
->maxWidth(CreateCommitImages::MAX_SOURCE_DIMENSION)
->maxHeight(CreateCommitImages::MAX_SOURCE_DIMENSION),
],
'image_descriptions' => ['nullable', 'list', 'max:'.CreateCommitImages::MAX_IMAGES],
'image_descriptions.*' => ['nullable', 'string', 'max:'.CreateCommitImages::MAX_DESCRIPTION_LENGTH],
];
}
public function authorize(): bool
{
return true;
}
/**
* @return array<string, array<int, string|ValidationRule|File|Dimensions|Enum|In>>
*/
public function rules(): array
{
$user = $this->user();
assert($user instanceof User);
$body = $this->input('body');
$attachmentRules = self::attachmentRules();
$attachmentRules['poll'][] = 'prohibits:quoted_commit_id';
return [
'body' => self::bodyRules($this->hasFile('images')),
'author' => ['nullable', 'string', new OrganizationToPostAs($user)],
'quoted_commit_id' => ['nullable', 'uuid', 'prohibits:parent_id', new VisibleCommit($user, 'That commit is no longer available to quote.')],
'link_preview_url' => ['nullable', 'string', Rule::in(is_string($body) ? resolve(Urls::class)->all($body) : [])],
...self::guildRules($this),
...$attachmentRules,
];
}
/**
* @return array<string, string>
*/
public function attributes(): array
{
return [
'image_descriptions.*' => 'image description',
];
}
public function poll(): ?NewPoll
{
if (! $this->filled('poll')) {
return null;
}
/** @var list<string> $options */
$options = $this->array('poll.options');
return new NewPoll($options, PollDuration::from($this->integer('poll.duration')));
}
/**
* @return array<string, string>
*/
public function messages(): array
{
return [
'link_preview_url.in' => 'The link card must be one of the links in the body.',
];
}
/**
* The first link in the body gets the link preview, unless the author chose another link or none at all.
*/
public function linkPreview(): bool|string
{
if (! $this->has('link_preview_url')) {
return true;
}
return $this->filled('link_preview_url') ? $this->string('link_preview_url')->value() : false;
}
/**
* @return list<UploadedFile>
*/
public function images(): array
{
$images = $this->file('images');
return is_array($images) ? array_values($images) : [];
}
/**
* @return list<string|null>
*/
public function imageDescriptions(): array
{
/** @var list<string|null> $descriptions */
$descriptions = $this->array('image_descriptions');
return $descriptions;
}
public function quotedCommit(): ?Commit
{
$user = $this->user();
assert($user instanceof User);
return $this->filled('quoted_commit_id')
? Commit::query()->visibleTo($user)->find($this->string('quoted_commit_id')->value())
: null;
}
public function prepareForValidation(): void
{
self::normalizeBody($this);
}
/**
* The organization the user posts as, or null when they post as themselves.
*/
public function organization(): ?Organization
{
$user = $this->user();
assert($user instanceof User);
return OrganizationToPostAs::of($user, $this->input('author'));
}
public function guild(): ?Guild
{
return self::guildOf($this);
}
}