Repository navigation
Expand file tree
/
Copy pathCreateMessage.php
More file actions
134 lines (112 loc) · 5.57 KB
/
Copy pathCreateMessage.php
File metadata and controls
134 lines (112 loc) · 5.57 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
<?php
declare(strict_types=1);
namespace App\Actions;
use App\Enums\RemoteStatus;
use App\Enums\UserUpdate;
use App\Events\UsersUpdated;
use App\Jobs\SendRemotePush;
use App\Jobs\UnfurlLink;
use App\Models\Conversation;
use App\Models\ConversationParticipant;
use App\Models\LinkPreview;
use App\Models\Message;
use App\Models\User;
use App\Queries\MentionedUsersQuery;
use App\Services\EmojiShortcodes;
use App\Services\Urls;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
final readonly class CreateMessage
{
public function __construct(
private EmojiShortcodes $emojiShortcodes,
private CreateImageFiles $createImageFiles,
private CreateMessageVoiceFile $createMessageVoiceFile,
private CreateLinkPreview $createLinkPreview,
private MentionedUsersQuery $mentionedUsers,
private Urls $urls,
) {
//
}
/**
* Makes the message the conversation's latest, for every member's inbox.
*/
public static function advance(Conversation $conversation, Message $message, bool $unstash): void
{
Conversation::query()
->whereKey($conversation->id)
->where(fn (Builder $query): Builder => $query->whereNull('last_message_id')->orWhere('last_message_id', '<', $message->id))
->update(['last_message_id' => $message->id]);
ConversationParticipant::query()
->where('conversation_id', $conversation->id)
->update(['last_message_at' => $message->created_at, ...$unstash ? ['archived_at' => null] : []]);
event(new UsersUpdated(ConversationParticipant::query()->where('conversation_id', $conversation->id)->pluck('user_id'), UserUpdate::Messages, ['conversation' => $conversation->id]));
}
/**
* Sending the same client id again, as a retry does, returns the message it already created, without storing its
* images or voice note twice. A new message brings the conversation back out of the stash for everyone in it. The link card is
* of the first link in the body when true, of the given link when a string, or none when false.
*
* @param list<UploadedFile> $images
* @param array<int, string|null> $imageDescriptions
* @param list<int> $voiceWaveform
*/
public function handle(User $sender, Conversation $conversation, string $body, string $clientId, ?Message $replyTo = null, array $images = [], array $imageDescriptions = [], bool|string $linkPreview = true, ?UploadedFile $voice = null, array $voiceWaveform = []): Message
{
$url = match ($linkPreview) {
true => $this->urls->first($body),
false => null,
default => $linkPreview,
};
$message = DB::transaction(function () use ($sender, $conversation, $body, $clientId, $replyTo, $images, $imageDescriptions, $url, $voice, $voiceWaveform): Message {
$linkPreview = $url === null ? null : $this->createLinkPreview->handle($url);
$message = Message::query()->createOrFirst(
['user_id' => $sender->id, 'client_id' => $clientId],
[
'conversation_id' => $conversation->id,
'body' => $this->emojiShortcodes->replace($body),
'reply_to_id' => $replyTo?->id,
'mentions' => $this->mentionedUsers->get($body),
'link_preview_id' => $linkPreview?->id,
],
);
if (! $message->wasRecentlyCreated) {
return $message;
}
$message->images()->createMany($this->createImageFiles->handle($images, $imageDescriptions, 'message-images', encrypted: true));
if ($voice instanceof UploadedFile) {
$message->voice()->create($this->createMessageVoiceFile->handle($voice, $voiceWaveform));
}
self::advance($conversation, $message, unstash: true);
// Advancing locked every member's row, so someone leaving waits for this to commit before forgetting
// their mentions, or has already left.
if ($conversation->isOrg()) {
$message->mentionedUsers()->attach($conversation->participants()
->whereIn('user_id', array_values($message->mentions ?? []))
->where('user_id', '!=', $sender->id)
->where('visible_from_id', '<', $message->id)
->whereNotIn('user_id', $sender->idsAcrossBlocks())
->whereNotIn('user_id', $sender->mutedBy()->newPivotQuery()->select('muter_id'))
->pluck('user_id'));
}
SendRemotePush::dispatchFor($message);
ConversationParticipant::query()
->where('conversation_id', $conversation->id)
->where('user_id', $sender->id)
->update(['status' => RemoteStatus::Open]);
ConversationParticipant::query()
->where('conversation_id', $conversation->id)
->where('user_id', $sender->id)
->where('read_through_id', '<', $message->id)
->update(['read_through_id' => $message->id, 'read_at' => now()]);
if ($linkPreview instanceof LinkPreview && $linkPreview->fetched_at === null) {
dispatch(new UnfurlLink($linkPreview))->afterCommit();
}
return $message;
});
Cache::forget(UpdateConversationTyping::key($sender, $conversation));
return $message->load(Message::SHOWN_WITH);
}
}