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
5 changes: 3 additions & 2 deletions app/Devnull/Stash.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
* the text, and the photos and poll that go with it.
*
* Drafts go by the web's names: 'commit' for a new commit, from the compose
* sheet, 'quote-{id}' for a quote of a commit and 'reply-{id}' for a
* reply to one. A screen keeps a draft as it changes, and forgets it once
* sheet, 'quote-{id}' for a quote of a commit, 'reply-{id}' for a
* reply to one, and 'remote:{username}' or 'remote:org:{id}' for a message
* in a Remotes thread. A screen keeps a draft as it changes, and forgets it once
* it's sent or there's nothing left in it. Signing out forgets them all (see
* Account::forget).
*
Expand Down
32 changes: 31 additions & 1 deletion app/NativeComponents/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Devnull\Data\DraftImage;
use App\Devnull\Data\Message;
use App\Devnull\Data\MessageVoice;
use App\Devnull\Data\StashedDraft;
use App\Devnull\Data\Thread;
use App\Devnull\Data\User;
use App\Devnull\Display;
Expand All @@ -19,6 +20,7 @@
use App\Devnull\ReactionEmoji;
use App\Devnull\ResponseCache;
use App\Devnull\SiteLink;
use App\Devnull\Stash;
use App\Devnull\Toasts;
use App\Devnull\VoiceNotes;
use App\Icons\Lucide;
Expand Down Expand Up @@ -94,6 +96,11 @@
* while you're on the thread buzzes the phone, as the web shakes the thread
* to MSN's sound, unless it's someone else's in a thread you've muted.
*
* What you write and the photos on it are stashed as you go, per thread,
* under the web's name for it (see Stash), so they're back when you open
* the thread again. Pushing forgets them: from then on the message is the
* outbox's, and one devnull turns down stays in the thread with its Retry.
*
* In an org, typing "@" lists the org's members above the field, as the web
* does, and typing on filters them (see SuggestsMentions).
*
Expand Down Expand Up @@ -365,6 +372,9 @@ public function mount(): void
{
$row = $this->data('conversation');
$this->row = $row instanceof ConversationData ? $row : null;
$draft = app(Stash::class)->get($this->me()?->id, $this->stashName());
$this->draft = $draft->body;
$this->photos = $draft->images;
$this->showKept();
$this->showPending();
$this->revalidate();
Expand Down Expand Up @@ -509,6 +519,7 @@ public function send(): void
$this->replyToId = null;
$this->lastActivityAt = self::now();
$this->lastTypingAt = 0;
$this->stashDraft();

$this->sendFromOutbox($write);
}
Expand Down Expand Up @@ -672,10 +683,12 @@ public function call(): void
}

/**
* Tells the others you're typing, at most every three seconds, while the composer has something in it.
* Stashes the draft, and tells the others you're typing, at most every three seconds, while the composer has
* something in it.
*/
public function updatedDraft(): void
{
$this->stashDraft();
$this->composing = true;
$now = self::now();

Expand Down Expand Up @@ -1521,6 +1534,23 @@ protected function photosChanged(array $photos): void
}

$this->photos = $photos;
$this->stashDraft();
}

/**
* Stashes the draft as it changes, its text and its photos. With nothing left in it, it's forgotten.
*/
private function stashDraft(): void
{
app(Stash::class)->put($this->me()?->id, $this->stashName(), new StashedDraft($this->draft, $this->photos));
}

/**
* The web's name for the thread's draft: 'remote:' and the person's username, or 'remote:org:' and the org's id.
*/
private function stashName(): string
{
return $this->isOrg() ? 'remote:org:'.$this->org : 'remote:'.$this->username;
}

/**
Expand Down
98 changes: 98 additions & 0 deletions tests/Feature/Screens/RemotesComposerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

use App\Devnull\Account;
use App\Devnull\Data\StashedDraft;
use App\Devnull\DraftImages;
use App\Devnull\Stash;
use App\NativeComponents\Conversation;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\File;
Expand Down Expand Up @@ -361,3 +364,98 @@ function threadWithAda(string $platform = 'ios'): TestableComponent

expect(sentTo('POST', 'remotes/orgs/org-1/ping'))->toBe(['']);
});

// ── The stashed draft ──────────────────────────

function stashedMessage(string $name): StashedDraft
{
return app(Stash::class)->get(app(Account::class)->user()?->id, $name);
}

it('stashes the draft as you write it, and has it back with its photos when you open the thread again', function () {
serveThreadWithAda([remoteMessageJson(['id' => 1])], ['read_through_id' => 1]);

$screen = photosPicked(threadWithAda(), [testPhoto()])->input('message-input', 'half a thought');
$photo = $screen->instance()->photos[0]->path;

expect(stashedMessage('remote:ada')->body)->toBe('half a thought')
->and(array_map(fn ($image): string => $image->path, stashedMessage('remote:ada')->images))->toBe([$photo]);

$screen->call('back');

threadWithAda()
->assertSet('draft', 'half a thought')
->assertSee('half a thought');

expect(nodeByRef(threadWithAda(), 'message-input')['props']['value'])->toBe('half a thought')
->and(array_map(fn ($image): string => $image->path, threadWithAda()->instance()->photos))->toBe([$photo])
->and(file_exists($photo))->toBeTrue();
});

it('forgets the draft once it is pushed', function () {
serveThreadWithAda([remoteMessageJson(['id' => 1])], ['read_through_id' => 1]);

$screen = threadWithAda()->input('message-input', 'on it');

expect(stashedMessage('remote:ada')->body)->toBe('on it');

finishRefreshes($screen->tap('push'))->assertSee('pushed');

expect(stashedMessage('remote:ada')->isEmpty())->toBeTrue()
->and(app(Stash::class)->names(app(Account::class)->user()?->id))->toBe([]);

threadWithAda()->assertSet('draft', '')->assertSet('photos', []);
});

it('forgets the draft once a push is turned down, which stays in the thread to retry', function () {
serveThreadWithAda([remoteMessageJson(['id' => 1])], ['read_through_id' => 1], pushStatus: 403);

finishRefreshes(threadWithAda()->input('message-input', 'hello')->tap('push'))->assertSee('rejected ·');

expect(stashedMessage('remote:ada')->isEmpty())->toBeTrue();

threadWithAda()->assertSet('draft', '')->assertSee('hello')->assertSee('retry');
});

it('forgets the draft once it is emptied, and once its last photo comes off', function () {
serveThreadWithAda([remoteMessageJson(['id' => 1])], ['read_through_id' => 1]);

$screen = threadWithAda()->input('message-input', 'never mind')->input('message-input', '');

expect(app(Stash::class)->names(app(Account::class)->user()?->id))->toBe([]);

photosPicked($screen, [testPhoto()]);

expect(stashedMessage('remote:ada')->images)->toHaveCount(1);

$screen->tap('photo-0-remove');

expect(app(Stash::class)->names(app(Account::class)->user()?->id))->toBe([]);

threadWithAda()->assertSet('draft', '')->assertSet('photos', []);
});

it("keeps each thread's draft to itself, an org's by its id", function () {
serveThreadWithAda([remoteMessageJson(['id' => 1])], ['read_through_id' => 1]);
app(Stash::class)->put(app(Account::class)->user()?->id, 'remote:org:org-1', new StashedDraft('for the org'));
app(Stash::class)->put(app(Account::class)->user()?->id, 'remote:grace', new StashedDraft('for Grace'));

threadWithAda()->assertSet('draft', '');

threadWithAda()->input('message-input', 'for Ada');

expect(Native::visit('/remotes/orgs/org-1')->instance()->draft)->toBe('for the org')
->and(stashedMessage('remote:ada')->body)->toBe('for Ada')
->and(stashedMessage('remote:grace')->body)->toBe('for Grace');
});

it('forgets the drafts when you sign out', function () {
serveThreadWithAda([remoteMessageJson(['id' => 1])], ['read_through_id' => 1]);

threadWithAda()->input('message-input', 'see you');
$userId = app(Account::class)->user()?->id;

app(Account::class)->forget();

expect(app(Stash::class)->get($userId, 'remote:ada')->isEmpty())->toBeTrue();
});
Loading