diff --git a/app/Devnull/Stash.php b/app/Devnull/Stash.php index c33e59d8..9be68939 100644 --- a/app/Devnull/Stash.php +++ b/app/Devnull/Stash.php @@ -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). * diff --git a/app/NativeComponents/Conversation.php b/app/NativeComponents/Conversation.php index ef23ac67..17cafcd5 100644 --- a/app/NativeComponents/Conversation.php +++ b/app/NativeComponents/Conversation.php @@ -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; @@ -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; @@ -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). * @@ -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(); @@ -509,6 +519,7 @@ public function send(): void $this->replyToId = null; $this->lastActivityAt = self::now(); $this->lastTypingAt = 0; + $this->stashDraft(); $this->sendFromOutbox($write); } @@ -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(); @@ -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; } /** diff --git a/tests/Feature/Screens/RemotesComposerTest.php b/tests/Feature/Screens/RemotesComposerTest.php index b06a389a..2f1350a3 100644 --- a/tests/Feature/Screens/RemotesComposerTest.php +++ b/tests/Feature/Screens/RemotesComposerTest.php @@ -1,6 +1,9 @@ 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(); +});