Repository navigation
Expand file tree
/
Copy pathSendNotificationPush.php
More file actions
159 lines (135 loc) · 6.1 KB
/
Copy pathSendNotificationPush.php
File metadata and controls
159 lines (135 loc) · 6.1 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
<?php
declare(strict_types=1);
namespace App\Jobs;
use App\Contracts\Services\DevicePushProvider;
use App\Dto\DevicePush;
use App\Dto\DevicePushResult;
use App\Enums\NotificationType;
use App\Enums\PushPreference;
use App\Exceptions\DevicePushCredentialsRejected;
use App\Models\Device;
use App\Models\Notification;
use App\Support\PushRetryDelay;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\Attributes\Timeout;
use Throwable;
#[Timeout(210)]
final class SendNotificationPush implements ShouldQueue
{
use Queueable;
/**
* Seconds a phone that is offline can still get the push, so one that comes back the next day isn't handed a
* follow or a reply from last week.
*/
private const int PHONE_TTL = 86400;
private const int DEFAULT_TRIES = 3;
private const int DEVICES_PER_JOB = 5;
/**
* @param list<string>|null $deviceIds
*/
public function __construct(
public readonly string $notificationId,
public readonly ?array $deviceIds = null,
public readonly int $tries = self::DEFAULT_TRIES,
) {
//
}
/**
* Queues a push for each of the notifications that exist, so the ids of rows an insert ignored push nobody, and
* only to people with a phone to push to.
*
* @param list<string> $notificationIds
*/
public static function dispatchFor(array $notificationIds): void
{
if ($notificationIds === []) {
return;
}
foreach (Notification::query()->whereKey($notificationIds)
->whereHas('recipient.devices', fn (Builder $query): Builder => $query->whereNotNull('push_token_hash'))
->get(['id']) as $notification) {
dispatch(new self($notification->id))->delay(now()->addSeconds(10))->afterCommit();
}
}
/**
* Pushes to every phone of the recipient, unless by now the notification was read or the app would hide it (the
* follow was undone, the commit is gone, or a mute or block came between them), or the recipient turned its kind
* off.
*/
public function handle(DevicePushProvider $push): void
{
$notification = Notification::query()->whereKey($this->notificationId)->relevant()->unread()
->where('created_at', '>', now()->subSeconds(self::PHONE_TTL))
->with(['recipient', 'actor', 'organization'])->first();
if (! $notification instanceof Notification || PushPreference::forNotification($notification->type)?->isOnFor($notification->recipient) !== true) {
return;
}
$actor = $notification->actor;
$organization = $notification->organization;
$message = new DevicePush(
$actor->name !== '' ? $actor->name : '@'.$actor->handle,
match ($notification->type) {
NotificationType::Followed => 'Followed you',
NotificationType::Replied => $organization === null ? 'Replied to your commit' : "Replied to {$organization->name}'s commit",
default => $organization === null ? 'Mentioned you' : 'Mentioned '.$organization->name,
},
array_filter([
'type' => $notification->type->value,
'notification_id' => $notification->id,
'commit_id' => $notification->commit_id,
'handle' => $actor->handle,
], fn (?string $value): bool => $value !== null),
self::PHONE_TTL,
);
$failed = [];
$deferred = [];
$retryAfterSeconds = null;
$devices = Device::query()->where('user_id', $notification->recipient_id)->whereNotNull('push_token_hash')
->when($this->deviceIds !== null, fn (Builder $query): Builder => $query->whereKey($this->deviceIds))
->orderBy('id')->get();
// Queue the rest before sending: even five OAuth exchanges and sends fit within this job's timeout.
foreach ($devices->skip(self::DEVICES_PER_JOB)->chunk(self::DEVICES_PER_JOB) as $batch) {
dispatch(new self($this->notificationId, array_values($batch->map(fn (Device $device): string => $device->id)->all()), $this->tries - $this->attempts() + 1))
->onConnection($this->connection)->onQueue($this->queue);
}
foreach ($devices->take(self::DEVICES_PER_JOB) as $device) {
try {
$result = $push->send($device, $message);
} catch (DevicePushCredentialsRejected) {
return;
} catch (Throwable $throwable) {
report($throwable);
$result = DevicePushResult::retry();
}
if (! $result->retry) {
continue;
}
if ($result->deferred) {
$deferred[$result->retryAfterSeconds ?? 0][] = $device->id;
continue;
}
$failed[] = $device->id;
$retryAfterSeconds = max($retryAfterSeconds ?? 0, $result->retryAfterSeconds ?? 0);
}
foreach ($deferred as $seconds => $deviceIds) {
dispatch(new self($this->notificationId, $deviceIds, $this->tries - $this->attempts() + 1))
->onConnection($this->connection)->onQueue($this->queue)->delay(now()->addSeconds($seconds));
}
if ($failed === [] || $this->attempts() >= $this->tries) {
return;
}
// A follow-up starts with a fresh queue attempt count, so carry the remaining budget.
dispatch(new self($this->notificationId, $failed, $this->tries - $this->attempts())
->onConnection($this->connection)->onQueue($this->queue))->delay(now()->addSeconds($this->retryDelay($retryAfterSeconds)));
}
/**
* What Google asked for, and otherwise twice as long each attempt.
*/
private function retryDelay(?int $retryAfterSeconds): int
{
// A follow-up carries what is left of the budget, so the remainder says which attempt this is.
return PushRetryDelay::secondsFor(self::DEFAULT_TRIES - $this->tries + $this->attempts(), $retryAfterSeconds);
}
}