Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions app/Devnull/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@
*/
public const string ScheduledCommitsPath = 'scheduled-commits';

/**
* Where the call ringing for you is, as the web checks for one every few seconds.
*/
public const string IncomingCallPath = 'remotes/call';

/**
* How long a request may take, in seconds, before it gives up as one that couldn't connect.
*/
public const int Timeout = 20;

public function __construct(private DeviceKey $deviceKey, private string $baseUrl, private ResponseCache $cache) {}

/**
Expand Down Expand Up @@ -685,7 +695,17 @@ public function unblock(string $username): void
*/
public function incomingCall(): ?IncomingCall
{
$call = $this->json($this->send('GET', 'remotes/call'))['call'] ?? null;
return self::incomingCallIn($this->json($this->send('GET', self::IncomingCallPath)));
}

/**
* The call ringing for you in devnull's answer to a call check, or null.
*
* @param array<string, mixed> $json
*/
public static function incomingCallIn(array $json): ?IncomingCall
{
$call = $json['call'] ?? null;

return is_array($call) ? IncomingCall::fromArray($call) : null;
}
Expand Down Expand Up @@ -888,13 +908,14 @@ private function signedMultipart(string $target, string $payload, array $files):
/**
* Sends a request signed elsewhere, exactly as signed: the body as the
* bytes that were signed, or no body and no Content-Type when there was
* none. Returns the response whatever its status.
* none. Returns the response whatever its status. One that takes longer
* than $timeout seconds gives up as one that couldn't connect.
*
* @throws ConnectionException
*/
public static function sendSigned(SignedRequest $request): Response
public static function sendSigned(SignedRequest $request, int $timeout = self::Timeout): Response
{
$pending = Http::acceptJson()->withHeaders($request->headers)->timeout(20);
$pending = Http::acceptJson()->withHeaders($request->headers)->timeout($timeout);

$pending = $request->body === ''
? $pending->withRequestMiddleware(fn (RequestInterface $request): RequestInterface => $request->withoutHeader('Content-Type'))
Expand Down Expand Up @@ -947,7 +968,7 @@ private function send(string $method, string $path, array $query = [], ?array $p
$body = self::body($payload);
$headers = $this->sign($method, $target, $body);

$request = Http::acceptJson()->withHeaders($headers)->timeout(20);
$request = Http::acceptJson()->withHeaders($headers)->timeout(self::Timeout);

$request = $payload === null
? $request->withRequestMiddleware(fn (RequestInterface $request): RequestInterface => $request->withoutHeader('Content-Type'))
Expand Down
26 changes: 19 additions & 7 deletions app/Devnull/FetchInBackground.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
* this, since it waits in memory until you scroll near it anyway.
*
* A write's body goes with the type it was signed with: JSON, or multipart
* for a commit with photos. It comes last, since a task's arguments are
* passed in order.
* for a commit with photos. It comes after the rest, since a task's arguments
* are passed in order, and so does how long the request may take, for a check
* that is soon worth nothing, such as one for a call ringing.
*/
final class FetchInBackground extends AsyncTask
{
Expand Down Expand Up @@ -53,6 +54,16 @@ public static function failureOf(mixed $result): ?ApiException
};
}

/**
* Whether the request this task sent ran out of time before devnull
* answered: devnull, or something in front of it, took the connection and
* never said anything back. A write may have reached devnull all the same.
*/
public static function timedOut(mixed $result): bool
{
return is_array($result) && ($result['timed_out'] ?? false) === true;
}

/**
* The JSON devnull answered a write with, or an empty array for a write it answered with nothing.
*
Expand All @@ -65,18 +76,19 @@ public static function jsonOf(mixed $result): array

/**
* @param array<string, string> $headers
* @return array{status: int, json: mixed, retry_after: string, from_devnull: bool}
* @return array{status: int, json: mixed, retry_after: string, from_devnull: bool, timed_out?: bool}
*/
public function handle(string $url, array $headers, string $method = 'GET', string $body = '', bool $withImages = false, ?int $pruneBefore = null, string $contentType = 'application/json'): array
public function handle(string $url, array $headers, string $method = 'GET', string $body = '', bool $withImages = false, ?int $pruneBefore = null, string $contentType = 'application/json', int $timeout = Client::Timeout): array
{
if ($pruneBefore !== null) {
DownloadImages::prune($pruneBefore);
}

try {
$response = Client::sendSigned(new SignedRequest($url, $headers, $method, $body, $contentType));
} catch (ConnectionException) {
return ['status' => 0, 'json' => null, 'retry_after' => '', 'from_devnull' => false];
$response = Client::sendSigned(new SignedRequest($url, $headers, $method, $body, $contentType), $timeout);
} catch (ConnectionException $exception) {
// cURL's "Operation timed out", as against a connection refused or a name that didn't resolve.
return ['status' => 0, 'json' => null, 'retry_after' => '', 'from_devnull' => false, 'timed_out' => str_contains($exception->getMessage(), 'cURL error 28')];
}

$result = [
Expand Down
8 changes: 8 additions & 0 deletions app/Devnull/FirstFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
*/
final class FirstFrame extends AsyncTask
{
/**
* How long a tab waits for it, in seconds, before it fills in anyway. It's
* back in a moment unless every background thread is busy, as they all are
* while devnull takes requests and doesn't answer them. The tab then
* shows what it kept rather than skeleton rows until one is free.
*/
public const int Within = 2;

public function handle(): bool
{
return true;
Expand Down
2 changes: 1 addition & 1 deletion app/NativeComponents/Concerns/DrawsSkeletonFirst.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function fillInAfterFirstFrame(string $then): void
$this->skeletonFrame = true;

FirstFrame::dispatch()
->timeout(10)
->timeout(FirstFrame::Within)
->finished(fn () => $this->fillIn($then))
->failed(fn () => $this->fillIn($then));
}
Expand Down
5 changes: 3 additions & 2 deletions app/NativeComponents/Concerns/SyncsOutbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ public function outboxAnswered(string $id = '', string $status = 'finished', mix
return;
}

// The task gave up waiting, so devnull may have a commit without having said so.
if ($status === 'failed') {
// The task gave up waiting, or devnull took the request and never answered it in time, so devnull may have a
// commit without having said so.
if ($status === 'failed' || FetchInBackground::timedOut($result)) {
$this->writeTurnedDown($write, $write->isCommit() ? Outbox::NoAnswer : 'fatal: '.($message ?? 'the request did not finish.'));

return;
Expand Down
58 changes: 54 additions & 4 deletions app/NativeComponents/Concerns/WatchesForCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,82 @@

use App\Devnull\Account;
use App\Devnull\Client;
use App\Devnull\Exceptions\ApiException;
use App\Devnull\Data\IncomingCall;
use App\Devnull\Exceptions\KeychainUnavailable;
use App\Devnull\FetchInBackground;
use App\NativeComponents\Call;
use Carbon\CarbonImmutable;
use Native\Mobile\Attributes\Poll;
use Native\Mobile\Edge\Transition;

/**
* Rings while the screen is up, as the web checks for a call every three seconds on every page: a call ringing for
* you opens the call screen to pick up or decline. One you already saw ring doesn't ring again.
*
* Each check is signed here, where the Keychain is, and sent from a background PHP thread (see FetchInBackground),
* so the screen never waits on it. Asked here, a devnull that took the connection and never answered held the
* whole app for the request's 20 seconds, and the next check was due by then, so no tap was answered until devnull
* did. A check still out isn't sent again, and one that fails says nothing: the next one tries again.
*/
trait WatchesForCalls
{
/**
* How long a check may wait for devnull, in seconds. The next one is due long before an answer this late would
* be worth anything, so it gives up and frees its background thread rather than wait out a request's usual time.
*/
public const int CallCheckTimeout = 8;

/**
* When the check still out was sent, or null when none is. NativePHP drops the answer to one sent from a screen
* that has gone under another, so a check older than its time counts as gone.
*/
protected ?float $callCheckSentAt = null;

#[Poll(3000)]
public function checkForCall(): void
{
if (app(Account::class)->user() === null) {
if (app(Account::class)->user() === null || $this->checkingForCall()) {
return;
}

try {
$call = app(Client::class)->incomingCall();
} catch (ApiException|KeychainUnavailable) {
$request = app(Client::class)->signedGet(Client::IncomingCallPath);
} catch (KeychainUnavailable) {
// The screen's own requests say what's wrong; this tries again on the next check.
return;
}

$this->callCheckSentAt = self::callCheckClock();

FetchInBackground::dispatch($request->url, $request->headers, 'GET', '', false, null, 'application/json', self::CallCheckTimeout)
->timeout(self::CallCheckTimeout + 2)
->finished(function (mixed $result): void {
$this->callCheckSentAt = null;

if (FetchInBackground::failureOf($result) === null) {
$this->openRingingCall(Client::incomingCallIn(FetchInBackground::jsonOf($result)));
}
})
->failed(function (): void {
$this->callCheckSentAt = null;
});
}

private function checkingForCall(): bool
{
return $this->callCheckSentAt !== null && self::callCheckClock() - $this->callCheckSentAt < self::CallCheckTimeout + 2;
}

private static function callCheckClock(): float
{
return (float) CarbonImmutable::now()->format('U.u');
}

/**
* Opens the call screen for a call ringing for you that hasn't rung here before.
*/
private function openRingingCall(?IncomingCall $call): void
{
if ($call === null || $call->offer === '' || Call::hasSeen($call->offer)) {
return;
}
Expand Down
13 changes: 12 additions & 1 deletion tests/Feature/Devnull/ClientCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Devnull\RequestSigner;
use App\Devnull\ResponseCache;
use Carbon\CarbonImmutable;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;

Expand Down Expand Up @@ -108,7 +109,17 @@

$request = $this->devnull->signedGet('me');

expect((new FetchInBackground)->handle($request->url, $request->headers))->toBe(['status' => 0, 'json' => null, 'retry_after' => '', 'from_devnull' => false]);
expect((new FetchInBackground)->handle($request->url, $request->headers))->toBe(['status' => 0, 'json' => null, 'retry_after' => '', 'from_devnull' => false, 'timed_out' => false]);
});

it('says when devnull took a request from the background and never answered it in time', function () {
serveApi(fn () => throw new ConnectionException('cURL error 28: Operation timed out after 20001 milliseconds with 0 bytes received'));

$request = $this->devnull->signedGet('me');
$result = (new FetchInBackground)->handle($request->url, $request->headers);

expect($result)->toBe(['status' => 0, 'json' => null, 'retry_after' => '', 'from_devnull' => false, 'timed_out' => true])
->and(FetchInBackground::timedOut($result))->toBeTrue();
});

it("fails a GET that gets a 200 with something other than devnull's answer, and keeps nothing from it", function (string $body) {
Expand Down
48 changes: 48 additions & 0 deletions tests/Feature/Screens/CallTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\NativeComponents\Call;
use App\NativeComponents\Log;
use Devnull\Calls\Events\Connected;
use Devnull\Calls\Events\DescriptionReady;
use Devnull\Calls\Events\Ended;
Expand Down Expand Up @@ -242,6 +243,53 @@ function ringingFromAda(array $data = []): TestableComponent
expect(finishRefreshes(Native::visit('/remotes/conversations/ada'))->firePoll('checkForCall')->navigationIntent())->toBeNull();
});

it('checks for a call from the background, so a devnull that never answers holds nothing up', function () {
holdRefreshes();
serveApi([
'GET /api/v1/remotes/@ada/messages' => [threadJson([remoteMessageJson(['id' => 1, 'body' => 'hey'])])],
'GET /api/v1/remotes/call' => [['call' => ['partner' => userJson(['handle' => 'ada', 'name' => 'Ada Lovelace']), 'sdp' => CallOffer, 'is_muted' => false]]],
]);
$thread = finishRefreshes(Native::visit('/remotes/conversations/ada'));
$sent = refreshesSent($thread);

// The check is out, and the next one isn't sent on top of it.
$thread->firePoll('checkForCall')->firePoll('checkForCall');

expect(sentTo('GET', 'remotes/call'))->toBe([])
->and(refreshesSent($thread))->toBe($sent + 1)
->and($thread->navigationIntent())->toBeNull();

finishRefreshes($thread)->assertNavigatedTo('/call/ada');
});

it('checks for a call again once one devnull never answered has had its time', function () {
holdRefreshes();
serveApi(['GET /api/v1/remotes/@ada/messages' => [threadJson([remoteMessageJson(['id' => 1, 'body' => 'hey'])])]]);
$thread = finishRefreshes(Native::visit('/remotes/conversations/ada'));
$sent = refreshesSent($thread);

$thread->firePoll('checkForCall');
$this->travel(Log::CallCheckTimeout + 2)->seconds();
$thread->firePoll('checkForCall');

expect(refreshesSent($thread))->toBe($sent + 2);
});

it('says nothing when a check for a call fails, and the next one goes', function () {
holdRefreshes();
serveApi(['GET /api/v1/remotes/@ada/messages' => [threadJson([remoteMessageJson(['id' => 1, 'body' => 'hey'])])]]);
$thread = finishRefreshes(Native::visit('/remotes/conversations/ada'));
$sent = refreshesSent($thread);
fakeOffline();

finishRefreshes($thread->firePoll('checkForCall'))->firePoll('checkForCall');

expect(refreshesSent($thread))->toBe($sent + 2)
->and($thread->navigationIntent())->toBeNull()
->and($thread->instance()->error)->toBeNull();
$thread->assertNativeNotCalled('ToastKit.Show');
});

it('logs calls in the thread in the web\'s words, naming them where devnull leaves the author off', function () {
$me = userJson()['id'];
serveApi(['GET /api/v1/remotes/@ada/messages' => [threadJson([
Expand Down
18 changes: 18 additions & 0 deletions tests/Feature/Screens/LogsTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

use App\Devnull\Client;
use App\Devnull\FirstFrame;
use App\Devnull\Timeline;
use App\NativeComponents\CommitDetail;
use App\NativeComponents\Log;
use App\NativeComponents\Notifications;
use App\NativeComponents\Profile;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Native\Mobile\Events\Async\AsyncTaskFailed;
use Native\Mobile\Testing\Native;
use Native\Mobile\Testing\TestableComponent;

Expand Down Expand Up @@ -585,6 +587,22 @@ function ada(): array
expect(shownLogIds($screen))->toBe(['a']);
});

it('fills in what it kept when the first frame has had its time, though every background thread is busy', function () {
config(['devnull.skeleton_first' => true]);
holdRefreshes();
keep('log/following', pageJson([commitJson(['id' => 'f1', 'body' => 'Kept following'])]));

$screen = Native::visit('/log')->assertSet('skeletonFrame', true)->assertDontSee('Kept following');
$firstFrame = $screen->bridge()->callsTo('AsyncTask.Dispatch')[0]['params'];

expect($firstFrame['timeout'])->toBe(FirstFrame::Within);

// Its watchdog fails it, as when it never got a thread.
$screen->emitNative(AsyncTaskFailed::class, ['id' => $firstFrame['id'], 'exceptionClass' => 'RuntimeException', 'message' => 'The async task did not complete within 2 seconds.'])
->assertSet('skeletonFrame', false)
->assertSee('Kept following');
});

it('opens on skeleton rows, and fills in what it kept straight after, so a tab tap shows at once', function () {
config(['devnull.skeleton_first' => true]);
holdRefreshes();
Expand Down
Loading
Loading