Repository navigation
Expand file tree
/
Copy pathCreateConversationMessageRequest.php
More file actions
157 lines (134 loc) · 5.27 KB
/
Copy pathCreateConversationMessageRequest.php
File metadata and controls
157 lines (134 loc) · 5.27 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
<?php
declare(strict_types=1);
namespace App\Http\Requests;
use App\Enums\PushAccess;
use App\Models\Conversation;
use App\Models\Message;
use App\Models\User;
use App\Queries\PushAccessQuery;
use App\Rules\RepliableMessage;
use App\Rules\VoiceNote;
use App\Services\Urls;
use App\Services\VoiceNotes;
use Illuminate\Auth\Access\Response;
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 CreateConversationMessageRequest extends FormRequest
{
/**
* What any message may carry: a body, which images make optional, a reply to a message the sender can see in the
* conversation, a link card for one of its links, and images, checked as a commit's are. A voice note goes alone,
* or with a reply.
*
* @return array<string, array<int, string|ValidationRule|File|Dimensions|Enum|In>>
*/
public static function messageRules(FormRequest $request, ?Conversation $conversation, int $visibleFrom): array
{
$body = $request->input('body');
$attachments = CreateCommitRequest::attachmentRules();
return [
'body' => [$request->hasFile('images') || $request->hasFile('voice') ? 'nullable' : 'required', 'string', 'max:'.Message::MAX_LENGTH],
'client_id' => ['required', 'uuid'],
'reply_to_id' => ['nullable', 'integer', new RepliableMessage($conversation, $visibleFrom)],
'link_preview_url' => ['nullable', 'string', Rule::in(is_string($body) ? resolve(Urls::class)->all($body) : [])],
'images' => $attachments['images'],
'images.*' => $attachments['images.*'],
'image_descriptions' => $attachments['image_descriptions'],
'image_descriptions.*' => $attachments['image_descriptions.*'],
'voice' => ['nullable', 'file', 'max:'.VoiceNotes::MAX_UPLOAD_KILOBYTES, 'prohibits:body,images', new VoiceNote],
'voice_waveform' => ['required_with:voice', 'list', 'size:'.VoiceNotes::BARS],
'voice_waveform.*' => ['required', 'integer', 'between:0,100'],
];
}
/**
* A request can't be answered before it is merged, and a sender gets one message in until it is.
*/
public function authorize(PushAccessQuery $access): Response
{
$user = $this->user();
assert($user instanceof User);
$partner = $this->route('partner');
assert($partner instanceof User);
if (! $user->canMessage($partner)) {
return Response::denyAsNotFound();
}
return match ($access->get($user, $partner)) {
PushAccess::Open => Response::allow(),
PushAccess::Request => Response::deny("Merge {$partner->mention()}'s request before replying."),
PushAccess::Waiting => Response::deny("{$partner->mention()} hasn't merged your request yet."),
PushAccess::Restricted => Response::deny("{$partner->mention()} doesn't follow you, so you can't push to them yet."),
PushAccess::Closed => Response::deny("{$partner->mention()} isn't taking remotes."),
};
}
/**
* @return array<string, array<int, string|ValidationRule|File|Dimensions|Enum|In>>
*/
public function rules(): array
{
$user = $this->user();
assert($user instanceof User);
$partner = $this->route('partner');
assert($partner instanceof User);
return self::messageRules($this, Conversation::between($user, $partner), 0);
}
/**
* @return array<string, string>
*/
public function messages(): array
{
return [
'link_preview_url.in' => 'The link card must be one of the links in the message.',
];
}
public function replyTo(): ?Message
{
return $this->filled('reply_to_id') ? Message::query()->find($this->integer('reply_to_id')) : null;
}
/**
* The first link in the message gets the link card, unless the sender 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 voice(): ?UploadedFile
{
$voice = $this->file('voice');
return $voice instanceof UploadedFile ? $voice : null;
}
/**
* @return list<int>
*/
public function voiceWaveform(): array
{
return $this->filled('voice_waveform')
? array_map(fn (int $bar): int => $this->integer('voice_waveform.'.$bar), range(0, VoiceNotes::BARS - 1))
: [];
}
}