From 6b62eb6105b3fe705b7cfab8d28c73497649a0b0 Mon Sep 17 00:00:00 2001 From: Shane Smith Date: Sat, 21 Nov 2026 12:54:40 +1030 Subject: [PATCH] [Guilds] Post straight to a guild's board from its page --- app/Devnull/Client.php | 22 +- app/Devnull/Outbox.php | 7 +- app/Devnull/ResponseCache.php | 12 +- app/Models/PendingWrite.php | 12 +- .../Concerns/DraftsIssues.php | 4 +- .../Concerns/OpensCommitSheet.php | 65 +++- app/NativeComponents/Concerns/SyncsOutbox.php | 13 +- app/NativeComponents/Guild.php | 122 +++++- .../native/partials/commit-sheet.blade.php | 12 +- tests/Feature/Screens/GuildPostTest.php | 367 ++++++++++++++++++ tests/Feature/Screens/GuildTest.php | 17 - tests/Pest.php | 17 + 12 files changed, 621 insertions(+), 49 deletions(-) create mode 100644 tests/Feature/Screens/GuildPostTest.php diff --git a/app/Devnull/Client.php b/app/Devnull/Client.php index 7337c382..31bec4fb 100644 --- a/app/Devnull/Client.php +++ b/app/Devnull/Client.php @@ -304,12 +304,13 @@ public function replies(string $id, ?string $cursor = null): Page * Commits, as a reply to $parentId or quoting $quotedCommitId, as the web's * composer, reply box and Quote do. devnull takes one or the other, not both. * A new commit or a quote may start a poll (see PollDraft::payload()). + * A new commit can go straight to a guild's board ($guildHandle). * * @param array{options: list, duration: int}|null $poll */ - public function createCommit(string $body, ?string $parentId = null, ?string $quotedCommitId = null, ?array $poll = null): Commit + public function createCommit(string $body, ?string $parentId = null, ?string $quotedCommitId = null, ?array $poll = null, ?string $guildHandle = null): Commit { - $commit = $this->data($this->send('POST', 'commits', payload: self::commitPayload($body, $parentId, $quotedCommitId, poll: $poll))); + $commit = $this->data($this->send('POST', 'commits', payload: self::commitPayload($body, $parentId, $quotedCommitId, $poll, $guildHandle))); $this->cache->commitAdded($commit); return Commit::fromArray($commit); @@ -321,9 +322,9 @@ public function createCommit(string $body, ?string $parentId = null, ?string $qu * * @param array{options: list, duration: int}|null $poll */ - public function signedCommit(string $body, ?string $parentId = null, ?string $quotedCommitId = null, ?array $poll = null): SignedRequest + public function signedCommit(string $body, ?string $parentId = null, ?string $quotedCommitId = null, ?array $poll = null, ?string $guildHandle = null): SignedRequest { - return $this->signedWrite('POST', 'commits', self::commitPayload($body, $parentId, $quotedCommitId, poll: $poll)); + return $this->signedWrite('POST', 'commits', self::commitPayload($body, $parentId, $quotedCommitId, $poll, $guildHandle)); } /** @@ -333,9 +334,9 @@ public function signedCommit(string $body, ?string $parentId = null, ?string $qu * * @param list $images */ - public function signedCommitWithImages(string $body, array $images, ?string $parentId = null, ?string $quotedCommitId = null): SignedRequest + public function signedCommitWithImages(string $body, array $images, ?string $parentId = null, ?string $quotedCommitId = null, ?string $guildHandle = null): SignedRequest { - return $this->signedWithImages('commits', self::commitPayload($body, $parentId, $quotedCommitId), $images); + return $this->signedWithImages('commits', self::commitPayload($body, $parentId, $quotedCommitId, guildHandle: $guildHandle), $images); } /** @@ -362,14 +363,14 @@ public function signedMessage(string $threadPath, string $body, string $clientId * Signs a commit to be submitted at $at, as the web's Schedule commit * does, for the background to send. Only you see it until then. With * photos it goes as multipart, as a commit with photos does, and a poll - * goes in its JSON. + * goes in its JSON. One for a guild's board says which ($guildHandle). * * @param list $images * @param array|null $poll */ - public function signedScheduledCommit(string $body, CarbonInterface $at, array $images = [], ?array $poll = null): SignedRequest + public function signedScheduledCommit(string $body, CarbonInterface $at, array $images = [], ?array $poll = null, ?string $guildHandle = null): SignedRequest { - $payload = array_filter(['body' => $body, 'scheduled_at' => self::scheduleTime($at), 'poll' => $poll], fn (mixed $value): bool => $value !== null); + $payload = array_filter(['body' => $body, 'scheduled_at' => self::scheduleTime($at), 'poll' => $poll, 'guild_handle' => $guildHandle], fn (mixed $value): bool => $value !== null); return $images === [] ? $this->signedWrite('POST', self::ScheduledCommitsPath, $payload) @@ -1004,13 +1005,14 @@ private function send(string $method, string $path, array $query = [], ?array $p * @param array{options: list, duration: int}|null $poll * @return array */ - private static function commitPayload(string $body, ?string $parentId, ?string $quotedCommitId, ?array $poll = null): array + private static function commitPayload(string $body, ?string $parentId, ?string $quotedCommitId, ?array $poll = null, ?string $guildHandle = null): array { return array_filter([ 'body' => $body, 'parent_id' => $parentId, 'quoted_commit_id' => $quotedCommitId, 'poll' => $poll, + 'guild_handle' => $guildHandle, ], fn (mixed $value): bool => $value !== null); } diff --git a/app/Devnull/Outbox.php b/app/Devnull/Outbox.php index 80de062c..ccc1a2dd 100644 --- a/app/Devnull/Outbox.php +++ b/app/Devnull/Outbox.php @@ -138,13 +138,14 @@ public function pending(string $kind, ?string $target = null, bool $withFailed = } /** - * Commits on their way that reply to $parentId, or with null, new commits and quotes. + * Commits on their way that reply to $parentId, or with null, new commits and quotes: those for your log, or with + * $guildHandle, those for that guild's board. * * @return Collection */ - public function commitsUnder(?string $parentId): Collection + public function commitsUnder(?string $parentId, ?string $guildHandle = null): Collection { - return $this->pending(PendingWrite::Commit)->filter(fn (PendingWrite $write): bool => $write->parentId() === $parentId)->values(); + return $this->pending(PendingWrite::Commit)->filter(fn (PendingWrite $write): bool => $write->parentId() === $parentId && $write->guildHandle() === $guildHandle)->values(); } /** diff --git a/app/Devnull/ResponseCache.php b/app/Devnull/ResponseCache.php index 8ee68c26..3bc8e7be 100644 --- a/app/Devnull/ResponseCache.php +++ b/app/Devnull/ResponseCache.php @@ -2,6 +2,7 @@ namespace App\Devnull; +use App\Devnull\Data\Guild; use Carbon\CarbonImmutable; use Closure; use Illuminate\Contracts\Cache\Repository; @@ -399,7 +400,10 @@ public function pastelGiven(array $json): void /** * You committed. A commit goes on top of your logs and your profile; a reply - * goes under its parent and counts there, and on top of your replies. + * goes under its parent and counts there, and on top of your replies. A + * post to a guild's board goes on top of that board alone, as it isn't in + * the logs or your profile's list, though devnull counts it among your + * commits. * * @param array $commit */ @@ -413,9 +417,13 @@ public function commitAdded(array $commit): void $parentId = is_string($commit['parent_id'] ?? null) ? $commit['parent_id'] : null; + $guild = Guild::fromArray(is_array($commit['guild'] ?? null) ? $commit['guild'] : ['handle' => $commit['guild_handle'] ?? null]); + if ($parentId === null) { // Trending is ranked, so a new commit has no place at the top of it. - foreach (['log/following', 'log/latest', "users/{$me}/commits"] as $path) { + $paths = $guild === null ? ['log/following', 'log/latest', "users/{$me}/commits"] : ['guilds/'.rawurlencode($guild->handle)]; + + foreach ($paths as $path) { $this->patch($path, [], fn (array $json): array => self::prepend($json, $commit)); } diff --git a/app/Models/PendingWrite.php b/app/Models/PendingWrite.php index 917bc654..2a3ed5f5 100644 --- a/app/Models/PendingWrite.php +++ b/app/Models/PendingWrite.php @@ -5,6 +5,7 @@ use App\Devnull\Data\Commit; use App\Devnull\Data\CommitBody; use App\Devnull\Data\DraftImage; +use App\Devnull\Data\Guild; use App\Devnull\Data\MessageVoice; use App\Devnull\Data\ScoreCard; use App\Devnull\Data\User; @@ -20,7 +21,7 @@ * message goes to. $payload is what's sent, and for a commit what the * composer needs to have it back: its body, what it replies to or quotes, * its poll, its photos with their descriptions, and the draft it was - * stashed under. + * stashed under, and the guild whose board it goes to, if any. * * It's queued until it goes, sending while it's on its way, and failed once * devnull turned it down, when a commit stays for you to edit and a message @@ -144,6 +145,14 @@ public function poll(): ?array return is_array($poll) && is_array($poll['options'] ?? null) && is_int($poll['duration'] ?? null) ? $poll : null; } + /** + * The guild whose board a new commit goes to, or null for one that goes to your log. + */ + public function guildHandle(): ?string + { + return is_string($this->payload['guild_handle'] ?? null) ? $this->payload['guild_handle'] : null; + } + /** * The message a Remotes message replies to. */ @@ -216,6 +225,7 @@ public function stagedCommit(User $author): Commit images: array_map(fn (DraftImage $image, int $index) => $image->asCommitImage($index), $this->images(), array_keys($this->images())), content: CommitBody::blocks(ScoreCard::withoutRun($this->body()), null, []), quotedCommitId: $this->quotedCommitId(), + guild: Guild::fromArray(['handle' => $this->guildHandle()]), scoreCard: ScoreCard::fromCommit($this->body(), $author), ); } diff --git a/app/NativeComponents/Concerns/DraftsIssues.php b/app/NativeComponents/Concerns/DraftsIssues.php index ffa1d0e5..6885102b 100644 --- a/app/NativeComponents/Concerns/DraftsIssues.php +++ b/app/NativeComponents/Concerns/DraftsIssues.php @@ -192,11 +192,11 @@ protected function typeIssueCommand(): void } /** - * Only a new commit can become an issue, as on the web. + * Only a new commit for your log can become an issue, as on the web, whose guild composer has no commands. */ protected function canDraftIssue(): bool { - return $this->sheetOpen && $this->sheetView === 'compose' && ! $this->scheduling; + return $this->sheetOpen && $this->sheetView === 'compose' && $this->sheetGuild === null && ! $this->scheduling; } /** diff --git a/app/NativeComponents/Concerns/OpensCommitSheet.php b/app/NativeComponents/Concerns/OpensCommitSheet.php index 19ac3922..9960b796 100644 --- a/app/NativeComponents/Concerns/OpensCommitSheet.php +++ b/app/NativeComponents/Concerns/OpensCommitSheet.php @@ -47,6 +47,12 @@ * A new commit's draft can become a `/feature` or `/bug` issue, as on the web * (see DraftsIssues). * + * A guild's page opens it for a new commit on the guild's board, as the web's + * guild page has its own composer (see composeInGuildSheet()): its own draft, + * `guild:{handle}`, and no issue commands, as on the web. The commit goes to + * the board, not to your log, and if devnull turns it down it goes back only + * to that guild's page (see composerGuild()). + * * A commit's page can open its reply in the same composer, for the photos and * the preview the reply bar has no room for (see replyInSheet()). The draft * is the reply bar's, and goes back to it when the sheet closes. @@ -92,6 +98,11 @@ trait OpensCommitSheet */ public string $sheetView = 'report'; + /** + * The guild whose board the new commit in the sheet goes to, or null for one that goes to your log. + */ + public ?string $sheetGuild = null; + /** * The reason picked on the report form, or '' until one is. */ @@ -249,6 +260,18 @@ public function composeInSheet(): void $this->restoreSheetDraft(); } + /** + * A new commit in the sheet for the board of the guild $handle, under its own draft. + */ + protected function composeInGuildSheet(string $handle): void + { + $this->forgetCommitSheet(); + $this->sheetView = 'compose'; + $this->sheetGuild = $handle; + $this->sheetOpen = true; + $this->restoreSheetDraft(); + } + /** * The web's clock button, in a new commit's composer: it opens "Submit at" under the field, with no time picked, * or closes it, and the commit goes now again. Its photos or its poll go with it either way, as on the web. @@ -335,6 +358,7 @@ public function commitFromSheet(): void 'images' => PendingWrite::imagePayload($this->sheetImages), 'stash' => $stashName, 'composer' => $this->sheetView, + ...($this->sheetGuild === null ? [] : ['guild_handle' => $this->sheetGuild]), ]); if ($write === null) { @@ -432,7 +456,7 @@ protected function commitSheetGone(string $view): void */ protected function reopenFailedCommit(PendingWrite $write): bool { - if ($this->sheetOpen || ($write->composer() === 'compose' && ! $this->opensComposer())) { + if ($this->sheetOpen || ($write->composer() === 'compose' && (! $this->opensComposer() || $write->guildHandle() !== $this->composerGuild()))) { return false; } @@ -448,7 +472,9 @@ protected function reopenFailedCommit(PendingWrite $write): bool /** * Edit, on the toast of a commit devnull turned down: its sheet, here if * the commit it quotes or replies to is showing, or the commit's own page. - * A new commit's sheet opens anywhere it can. + * A new commit's sheet opens anywhere it can but a guild's page, which + * sends it to the Log. One for a guild's board opens only on that guild's + * page, which opens it. */ protected function editFailedCommit(PendingWrite $write): void { @@ -456,6 +482,18 @@ protected function editFailedCommit(PendingWrite $write): void return; } + if ($write->guildHandle() !== null) { + $this->openInTab('/guilds/'.rawurlencode($write->guildHandle()), ['edit' => $write->id]); + + return; + } + + if ($write->composer() === 'compose') { + $this->composeInSheet(); + + return; + } + $for = $write->parentId() ?? $write->quotedCommitId(); if ($for !== null) { @@ -502,6 +540,15 @@ protected function opensComposer(): bool return false; } + /** + * The guild whose board this screen's composer posts to, or null when it posts to your log. A new commit devnull + * turned down goes back only to a composer for the same place. + */ + protected function composerGuild(): ?string + { + return null; + } + /** * Opens the sheet a commit devnull turned down was written in, with everything it had (see restoreSheetDraft()). */ @@ -509,8 +556,14 @@ private function openFailedCommit(PendingWrite $write): bool { $for = $write->parentId() ?? $write->quotedCommitId(); + $guild = $write->guildHandle(); + match ($write->composer()) { - 'compose' => $this->composeInSheet(), + 'compose' => match (true) { + $guild === null => $this->composerGuild() === null ? $this->composeInSheet() : null, + $guild === $this->composerGuild() => $this->composeInGuildSheet($guild), + default => null, + }, 'quote' => $for === null ? null : $this->quoteCommit($for), 'reply' => $for === null ? null : $this->replyInSheet($for), default => null, @@ -549,10 +602,11 @@ private function scheduleFromSheet(string $body, string $stashName): void $images = $this->sheetImages; $poll = $this->sheetPollPayload(); + $guild = $this->sheetGuild; $this->sendingSchedule = true; $this->sheetError = null; - $this->sendInBackground(fn (Client $devnull): SignedRequest => $devnull->signedScheduledCommit($body, $at, $images, $poll), function () use ($me, $stashName, $images): void { + $this->sendInBackground(fn (Client $devnull): SignedRequest => $devnull->signedScheduledCommit($body, $at, $images, $poll, $guild), function () use ($me, $stashName, $images): void { $this->sendingSchedule = false; app(ResponseCache::class)->forget(Client::ScheduledCommitsPath); app(Stash::class)->put($me->id, $stashName, new StashedDraft); @@ -571,6 +625,7 @@ private function forgetCommitSheet(): void $this->sheetOpen = false; $this->sheetCommit = null; $this->sheetView = 'report'; + $this->sheetGuild = null; $this->reportReason = ''; $this->reportDetails = ''; $this->sheetBody = ''; @@ -592,7 +647,7 @@ private function forgetCommitSheet(): void private function sheetStashName(): ?string { return match (true) { - $this->sheetView === 'compose' => 'commit', + $this->sheetView === 'compose' => $this->sheetGuild === null ? 'commit' : 'guild:'.$this->sheetGuild, $this->sheetView === 'quote' && $this->sheetCommit !== null => 'quote-'.$this->sheetCommit->id, $this->sheetView === 'reply' && $this->sheetCommit !== null => 'reply-'.$this->sheetCommit->id, default => null, diff --git a/app/NativeComponents/Concerns/SyncsOutbox.php b/app/NativeComponents/Concerns/SyncsOutbox.php index 52b9a63e..af51b367 100644 --- a/app/NativeComponents/Concerns/SyncsOutbox.php +++ b/app/NativeComponents/Concerns/SyncsOutbox.php @@ -179,11 +179,12 @@ protected function sendLeftovers(): void /** * The commits on their way that this screen would show, as they stand in for themselves (see - * PendingWrite::stagedCommit()): replies to $parentId, or with null, new commits and quotes. Newest first. + * PendingWrite::stagedCommit()): replies to $parentId, or with null, new commits and quotes, for your log or, with + * $guildHandle, for that guild's board. Newest first. * * @return list */ - protected function stagedCommits(?string $parentId): array + protected function stagedCommits(?string $parentId, ?string $guildHandle = null): array { $me = $this->me(); @@ -191,7 +192,7 @@ protected function stagedCommits(?string $parentId): array return []; } - return app(Outbox::class)->commitsUnder($parentId) + return app(Outbox::class)->commitsUnder($parentId, $guildHandle) ->reverse() ->map(fn (PendingWrite $write): Commit => $write->stagedCommit($me)) ->values() @@ -245,6 +246,8 @@ private function writeWent(PendingWrite $write, array $json): void app(VoiceNotes::class)->discard($write->voice()); if ($write->isCommit()) { + // A post to a guild's board is known by its guild, even when devnull's answer leaves it out. + $data = $write->guildHandle() === null ? $data : ['guild_handle' => $write->guildHandle(), ...$data]; app(ResponseCache::class)->commitAdded($data); $this->showCommitWent($write, Commit::fromArray($data)); @@ -333,8 +336,8 @@ private function signWrite(PendingWrite $write): SignedRequest PendingWrite::Unstar => $devnull->signedWrite('DELETE', $commit), PendingWrite::Message => $devnull->signedMessage((string) $write->target, $write->body(), $write->client_id, $write->replyToId(), $write->images(), $write->voice()), default => $write->images() === [] - ? $devnull->signedCommit($write->body(), $write->parentId(), $write->quotedCommitId(), $write->poll()) - : $devnull->signedCommitWithImages($write->body(), $write->images(), $write->parentId(), $write->quotedCommitId()), + ? $devnull->signedCommit($write->body(), $write->parentId(), $write->quotedCommitId(), $write->poll(), $write->guildHandle()) + : $devnull->signedCommitWithImages($write->body(), $write->images(), $write->parentId(), $write->quotedCommitId(), $write->guildHandle()), }; } diff --git a/app/NativeComponents/Guild.php b/app/NativeComponents/Guild.php index 33b8d1d6..d1497565 100644 --- a/app/NativeComponents/Guild.php +++ b/app/NativeComponents/Guild.php @@ -9,10 +9,12 @@ use App\Devnull\Data\GuildBoard; use App\Devnull\Data\TimelineRow; use App\Devnull\Exceptions\ApiException; +use App\Devnull\Outbox; use App\Devnull\RowCache; use App\Devnull\Stash; use App\Devnull\Timeline; use App\Icons\Lucide; +use App\Models\PendingWrite; use App\NativeComponents\Concerns\DrawsSkeletonFirst; use App\NativeComponents\Concerns\ExpandsThreads; use App\NativeComponents\Concerns\HoldsNewCommits; @@ -42,6 +44,12 @@ * so a page loaded later can repeat a commit already showing: those go once, * as does the pinned commit, which devnull also sends in its place. * + * The bar's New commit posts to the board, as the web's guild page does, + * from the compose sheet (see OpensCommitSheet::composeInGuildSheet()), while + * the guild isn't paused. The commit shows on top of the board, staged, until + * devnull has it, as a new commit does on the Log. If devnull turns it down, + * it leaves the board and goes back into this page's sheet. + * * A guild devnull no longer has says so. When devnull has no guilds for you * at all, it says so as the directory does (see Guilds::refused). */ @@ -92,6 +100,7 @@ class Guild extends NativeComponent public function mount(): void { $this->fillInAfterFirstFrame('revalidate'); + $this->editFromData(); } /** @@ -109,14 +118,38 @@ public function navTitle(): string return $this->guild->name ?? '#'.$this->handle(); } + /** + * Share, then New commit while the guild takes posts, as the Log has it. + */ public function navigationOptions(): ?NavBarOptions { - return NavBarOptions::make()->action( + $options = NavBarOptions::make()->action( NavAction::make('share') ->icon(ios: Lucide::Share->ios(), android: Lucide::Share2->android()) ->a11yLabel('Share guild') ->press('share'), ); + + if ($this->takesPosts()) { + $options->action( + NavAction::make('compose') + ->icon(ios: Lucide::SquarePen->ios(), android: Lucide::SquarePen->android()) + ->a11yLabel('New commit') + ->press('composeForGuild'), + ); + } + + return $options; + } + + /** + * The bar's New commit: the sheet, for a commit on this guild's board. + */ + public function composeForGuild(): void + { + if ($this->takesPosts()) { + $this->composeInGuildSheet($this->handle()); + } } /** @@ -196,6 +229,54 @@ protected function waitingCommits(): array return array_values(array_filter($this->newCommits, fn (Commit $commit): bool => $commit->id !== $this->pinnedId)); } + /** + * Puts a commit you just posted to the board on top of it, staged until devnull has it (see SyncsOutbox). + */ + protected function showCommitted(Commit $commit): void + { + $this->keepStagedOnTop(); + } + + /** + * devnull has the commit: the real one takes the staged one's place. + */ + protected function showCommitWent(PendingWrite $write, Commit $commit): void + { + $staged = PendingWrite::stagedId($write->client_id); + + if (! in_array($staged, array_map(fn (Commit $shown): string => $shown->id, $this->commits), true)) { + return; + } + + $this->madeHere[] = $commit->id; + $this->commits = array_map(fn (Commit $shown): Commit => $shown->id === $staged ? $commit : $shown, $this->commits); + } + + /** + * devnull turned the commit down: the staged one leaves the board. + */ + protected function showCommitTurnedDown(PendingWrite $write): void + { + $this->keepStagedOnTop(); + } + + /** + * The bar's New commit is this page's, so a post to this board devnull turned down opens here again. + */ + protected function opensComposer(): bool + { + return true; + } + + /** + * This page's composer posts to its board, even once the guild is paused, so a post devnull turned down for that + * still comes back here with why. + */ + protected function composerGuild(): ?string + { + return $this->handle(); + } + protected function showsContent(): bool { return $this->guild !== null; @@ -290,6 +371,7 @@ private function takeBoard(array $json, bool $showNewer = false): void } $this->layFreshCommits($json, $showNewer || $this->guild === null); + $this->keepStagedOnTop(); $this->guild = $board->guild; $this->pinned = $board->pinned; $this->pinnedId = $board->pinned?->id; @@ -297,6 +379,44 @@ private function takeBoard(array $json, bool $showNewer = false): void $this->unavailable = false; } + /** + * Your posts to this board still on their way stay on top of it, newest + * first, through every fresh page. Ones that went or were turned down + * while the page wasn't showing leave it, and one that went comes in with + * the board's next answer. + */ + private function keepStagedOnTop(): void + { + $staged = $this->stagedCommits(null, $this->handle()); + + foreach ($staged as $commit) { + $this->madeHere[] = $commit->id; + } + + $this->commits = [...$staged, ...array_values(array_filter($this->commits, fn (Commit $commit): bool => ! $commit->isStaged()))]; + } + + /** + * Opened by Edit on the toast of a post to this board devnull turned down: its composer, with what it had. + */ + private function editFromData(): void + { + $id = $this->data('edit'); + $write = is_numeric($id) ? app(Outbox::class)->find((int) $id) : null; + + if ($write !== null && $write->isCommit() && $write->isFailed()) { + $this->editFailedCommit($write); + } + } + + /** + * Whether the guild is loaded and takes posts: a paused one doesn't, as on the web. + */ + private function takesPosts(): bool + { + return $this->guild !== null && ! $this->guild->isPaused; + } + /** * The pinned commit as the board last sent it, or as devnull sent it on its own when its page isn't loaded. */ diff --git a/resources/views/native/partials/commit-sheet.blade.php b/resources/views/native/partials/commit-sheet.blade.php index 97a5b0b1..d9fd1890 100644 --- a/resources/views/native/partials/commit-sheet.blade.php +++ b/resources/views/native/partials/commit-sheet.blade.php @@ -8,7 +8,9 @@ Cancel closes it as a swipe does, keeping the draft. Commit closes it at once, and the commit goes from the outbox (see SyncsOutbox). If devnull says no, a toast says why and the draft comes back in the sheet. The Log's compose button opens the same composer for a new commit, without the card. A draft stashed last time comes - back in it, with the web's stash@{0} and drop beside the counter, in the toolbar at the bottom. + back in it, with the web's stash@{0} and drop beside the counter, in the toolbar at the bottom. A guild's page opens + it for a post to its board, which asks what you want to share with the guild and says under the field that it isn't + going to your profile log, as the web's guild composer does, with no issue commands. Report commit, in someone else's published commit's More menu, opens it as the web's Report dialog: the private, not-hidden line, the commit's body clamped to three lines, the four reasons, optional details, and Send report, @@ -85,7 +87,7 @@ native:key="sheet-body" ref="sheet-body" class="w-full text-theme-ink" - :placeholder="$issueCommand?->placeholder() ?? match (true) { $quoting => 'Add your take', $replying => 'Reply to '.$sheetCommit->author->handle(), default => 'What did you ship?' }" + :placeholder="$issueCommand?->placeholder() ?? match (true) { $quoting => 'Add your take', $replying => 'Reply to '.$sheetCommit->author->handle(), $sheetGuild !== null => 'What do you want to share with the guild?', default => 'What did you ship?' }" native:model.debounce.150ms="sheetBody" :selection="$mentionCarets['sheetBody'] ?? null" @selectionChange="caretMoved('sheetBody')" @@ -105,6 +107,10 @@ class="w-full text-theme-ink" 'class' => 'w-full py-1 rounded-md border border-theme-hairline bg-theme-surface', ]) + @if ($sheetGuild !== null && $sheetView === 'compose') + Posted to this guild, not your profile log. + @endif + @if (! $issueCommand && \App\Devnull\Data\CommitBody::hasOversizedUrl($sheetBody)) Each link must not be greater than {{ \App\Devnull\Data\BodyLink::MaxLength }} characters. @endif @@ -119,7 +125,7 @@ class="w-full text-theme-ink" @include('native.partials.quoted-commit', ['quoted' => $sheetCommit, 'static' => true]) @elseif ($scheduling) @include('native.partials.schedule-picker') - @elseif (! $replying && ! $issueCommand) + @elseif (! $replying && ! $issueCommand && $sheetGuild === null) @include('native.partials.issue-suggestions', [ 'suggestions' => $sheetTyped ? \App\Devnull\IssueCommand::suggested($sheetBody) : [], 'hint' => $issueHintsDismissed ? null : \App\Devnull\IssueCommand::hinted($sheetBody), diff --git a/tests/Feature/Screens/GuildPostTest.php b/tests/Feature/Screens/GuildPostTest.php new file mode 100644 index 00000000..30091fdb --- /dev/null +++ b/tests/Feature/Screens/GuildPostTest.php @@ -0,0 +1,367 @@ + 'http://devnull.test']); + signIn(); + $this->userId = userJson()['id']; +}); + +afterEach(function () { + File::deleteDirectory(storage_path('app/draft-images')); +}); + +/** + * #rust's board with c1 on it, the Following log with c0, and devnull answering a commit with $answer. + * + * @param array{0: mixed, 1?: int}|null $answer + */ +function serveGuildAndLog(?array $answer = null): void +{ + fakeApi([ + 'GET /api/v1/guilds/rust' => [boardOf(['c1'])], + 'GET /api/v1/log/following' => [pageJson([commitJson(['id' => 'c0'])])], + 'POST /api/v1/commits' => $answer ?? [['data' => commitJson(['id' => 'c9', 'body' => 'Crabs ahoy.', 'guild_handle' => 'rust'])], 201], + 'POST /api/v1/scheduled-commits' => [['data' => scheduledCommitJson()], 201], + ]); +} + +/** + * #rust's page, loaded, with its composer open from the bar. + */ +function guildComposer(?array $answer = null, string $platform = 'ios'): TestableComponent +{ + serveGuildAndLog($answer); + + return finishRefreshes(Native::visit('/log/guilds/rust', platform: $platform))->press('composeForGuild'); +} + +/** + * The commits the screen lists, by id. + * + * @return list + */ +function listedIds(TestableComponent $screen): array +{ + return array_map(fn (Commit $commit): string => $commit->id, $screen->instance()->commits); +} + +function pressEditOnToast(TestableComponent $screen, int $writeId): TestableComponent +{ + return $screen->emitNative(ToastActionPressed::class, ['toastId' => 'toast-1', 'actionId' => 'edit', 'data' => ['write' => $writeId]]); +} + +it('offers New commit beside Share once the guild is loaded', function () { + holdRefreshes(); + fakeApi(['GET /api/v1/guilds/rust' => [boardOf(['c1'])]]); + $loading = Native::visit('/log/guilds/rust'); + + expect(hasTopBarAction($loading, 'compose'))->toBeFalse(); + + $loaded = finishRefreshes($loading); + + expect(hasTopBarAction($loaded, 'share'))->toBeTrue() + ->and(hasTopBarAction($loaded, 'compose'))->toBeTrue() + ->and(findNode($loaded->tree(), fn (array $node): bool => ($node['props']['id'] ?? null) === 'compose')['props']['a11y_label'] ?? null)->toBe('New commit'); +}); + +it('offers no New commit on a paused guild, as the web has no composer there', function () { + fakeApi(['GET /api/v1/guilds/rust' => [boardOf(['c1'], overrides: ['guild' => guildJson(['is_paused' => true])])]]); + + $paused = finishRefreshes(Native::visit('/log/guilds/rust'))->call('composeForGuild'); + + expect(hasTopBarAction($paused, 'share'))->toBeTrue() + ->and(hasTopBarAction($paused, 'compose'))->toBeFalse() + ->and($paused->instance()->sheetOpen)->toBeFalse(); +}); + +it('opens a composer for the guild, saying where it posts, with its own draft', function () { + $screen = guildComposer() + ->assertSee('New commit') + ->assertSee('Posted to this guild, not your profile log.') + ->input('sheet-body', 'Crabs ahoy.'); + + expect(nodeByRef($screen, 'sheet')['props']['visible'])->toBeTrue() + ->and(nodeByRef($screen, 'sheet-body')['props']['placeholder'])->toBe('What do you want to share with the guild?') + ->and(app(Stash::class)->get($this->userId, 'guild:rust')->body)->toBe('Crabs ahoy.') + ->and(app(Stash::class)->get($this->userId, 'commit')->body)->toBe(''); + + // The Log's composer has its own draft, and says nothing of guilds. + $log = Native::visit('/log')->press('composeInSheet')->assertDontSee('Posted to this guild, not your profile log.'); + + expect(nodeByRef($log, 'sheet-body')['props'])->toMatchArray(['placeholder' => 'What did you ship?', 'value' => '']); +}); + +it('has no /feature or /bug in the guild composer, as the web has none', function () { + $screen = guildComposer()->input('sheet-body', '/b'); + + expect(rendersRef($screen, 'issue-commands'))->toBeFalse(); + + $screen->input('sheet-body', '/bug '); + + expect($screen->instance()->sheetIssue)->toBeNull() + ->and(rendersRef($screen, 'issue-badge'))->toBeFalse(); + + $screen->input('sheet-body', 'There is a bug in the borrow checker'); + + expect(rendersRef($screen, 'issue-hint'))->toBeFalse(); + + $screen->call('useIssueCommand', 'bug'); + + expect($screen->instance()->sheetIssue)->toBeNull(); +}); + +it('posts to the board, staged on top until devnull has it, then the real commit, never in the Log, counted on your profile', function () { + holdRefreshes(); + serveGuildAndLog(); + keep('log/following', pageJson([commitJson(['id' => 'c0'])])); + keep('users/local_dev', ['data' => userWithCountsJson(commits: 4), 'is_following' => false]); + keep('users/local_dev/commits', pageJson([commitJson(['id' => 'c0'])])); + $screen = finishRefreshes(Native::visit('/log/guilds/rust')) + ->press('composeForGuild') + ->input('sheet-body', 'Crabs ahoy.') + ->tap('sheet-commit'); + $write = PendingWrite::query()->sole(); + + expect($write->guildHandle())->toBe('rust') + ->and(nodeByRef($screen, 'sheet')['props']['visible'])->toBeFalse() + ->and(listedIds($screen))->toBe([PendingWrite::stagedId($write->client_id), 'c1']) + ->and(app(Stash::class)->get($this->userId, 'guild:rust')->body)->toBe(''); + + // On its way, it isn't in the Log, which isn't where it goes. + $log = Native::visit('/log'); + + expect(listedIds($log))->toBe(['c0']); + + finishRefreshes($screen); + + Http::assertSent(fn (Request $request): bool => $request->method() === 'POST' + && $request->url() === 'http://devnull.test/api/v1/commits' + && $request->body() === '{"body":"Crabs ahoy.","guild_handle":"rust"}'); + + expect(listedIds($screen))->toBe(['c9', 'c1']) + ->and(PendingWrite::query()->count())->toBe(0) + ->and(array_column(kept('log/following')['data'], 'id'))->toBe(['c0']) + ->and(array_column(kept('guilds/rust')['data'], 'id'))->toBe(['c9', 'c1']) + // devnull counts it among your commits, though your profile doesn't list it. + ->and(kept('users/local_dev')['data']['commits_count'])->toBe(5) + ->and(array_column(kept('users/local_dev/commits')['data'], 'id'))->toBe(['c0']); +}); + +it('posts photos to the board as multipart', function () { + $screen = photosPicked(guildComposer(), [testPhoto('jpg')]); + + $screen->input('sheet-body', 'Crabs ahoy.')->tap('sheet-commit'); + + Http::assertSent(fn (Request $request): bool => $request->url() === 'http://devnull.test/api/v1/commits' + && multipartParts($request)[0]['contents'] === '{"body":"Crabs ahoy.","guild_handle":"rust"}'); +}); + +it('schedules a commit for the board', function () { + phoneIn('Europe/Lisbon'); + $this->travelTo(CarbonImmutable::parse('2026-10-19 15:30:00', 'UTC')); + + guildComposer() + ->input('sheet-body', 'Crabs tomorrow.') + ->tap('sheet-schedule') + ->pickDateTime('scheduled-at', '2026-10-20T09:00') + ->tap('sheet-commit') + ->assertSet('sheetOpen', false); + + Http::assertSent(fn (Request $request): bool => $request->url() === 'http://devnull.test/api/v1/scheduled-commits' + && $request->body() === '{"body":"Crabs tomorrow.","scheduled_at":"2026-10-20T08:00:00+00:00","guild_handle":"rust"}'); +}); + +it('takes a post devnull turned down off the board and opens it again in the guild composer', function () { + $screen = guildComposer([['message' => 'Too Many Attempts.'], 429], platform: 'android') + ->input('sheet-body', 'Crabs ahoy.') + ->tap('sheet-commit') + ->assertNativeCalled('ToastKit.Show', fn (array $params): bool => $params['message'] === 'Too Many Attempts.' && ! isset($params['action'])); + + expect(listedIds($screen))->toBe(['c1']) + ->and(nodeByRef($screen, 'sheet')['props']['visible'])->toBeTrue() + ->and(nodeByRef($screen, 'sheet-body')['props'])->toMatchArray(['value' => 'Crabs ahoy.', 'placeholder' => 'What do you want to share with the guild?']) + ->and(PendingWrite::query()->count())->toBe(0); +}); + +it('never opens a guild post devnull turned down in the Log composer, and Edit takes you to its guild', function () { + holdRefreshes(); + serveGuildAndLog([['message' => 'Too Many Attempts.'], 429]); + finishRefreshes(Native::visit('/log/guilds/rust')) + ->press('composeForGuild') + ->input('sheet-body', 'Crabs ahoy.') + ->tap('sheet-commit'); + $write = PendingWrite::query()->sole(); + + // devnull answers once you're back on the Log. + $log = finishRefreshes(Native::visit('/log', platform: 'android')) + ->assertNativeCalled('ToastKit.Show', fn (array $params): bool => $params['message'] === 'Too Many Attempts.' + && $params['action'] === ['id' => 'edit', 'label' => 'Edit']); + + expect($log->instance()->sheetOpen)->toBeFalse() + ->and(listedIds($log))->toBe(['c0']); + + pressEditOnToast($log, $write->id)->assertNavigatedTo('/log/guilds/rust'); + + expect($log->instance()->sheetOpen)->toBeFalse(); + + $guild = Native::visit('/log/guilds/rust', ['edit' => $write->id]); + + expect(nodeByRef($guild, 'sheet')['props']['visible'])->toBeTrue() + ->and(nodeByRef($guild, 'sheet-body')['props'])->toMatchArray(['value' => 'Crabs ahoy.', 'placeholder' => 'What do you want to share with the guild?']) + ->and(PendingWrite::query()->count())->toBe(0); +}); + +it("never opens the Log's new commit devnull turned down on a guild's page", function () { + holdRefreshes(); + serveGuildAndLog([['message' => 'Too Many Attempts.'], 429]); + finishRefreshes(Native::visit('/log')) + ->press('composeInSheet') + ->input('sheet-body', 'Shipped it.') + ->tap('sheet-commit'); + + // devnull answers once you're on a guild's page. + $guild = finishRefreshes(Native::visit('/log/guilds/rust', platform: 'android')) + ->assertNativeCalled('ToastKit.Show', fn (array $params): bool => $params['message'] === 'Too Many Attempts.' + && $params['action'] === ['id' => 'edit', 'label' => 'Edit']); + + expect($guild->instance()->sheetOpen)->toBeFalse() + ->and(listedIds($guild))->toBe(['c1']) + ->and(app(Stash::class)->get($this->userId, 'commit')->body)->toBe('Shipped it.'); +}); + +it("never opens the Log's new commit devnull turned down on a paused guild's page, and Edit takes it to the Log", function () { + holdRefreshes(); + serveApi([ + 'GET /api/v1/guilds/rust' => [boardOf(['c1'], overrides: ['guild' => guildJson(['is_paused' => true])])], + 'GET /api/v1/log/following' => [pageJson([commitJson(['id' => 'c0'])])], + 'POST /api/v1/commits' => [['message' => 'Too Many Attempts.'], 429], + ]); + finishRefreshes(Native::visit('/log')) + ->press('composeInSheet') + ->input('sheet-body', 'Shipped it.') + ->tap('sheet-commit'); + $write = PendingWrite::query()->sole(); + + $guild = finishRefreshes(Native::visit('/log/guilds/rust', platform: 'android')) + ->assertNativeCalled('ToastKit.Show', fn (array $params): bool => $params['message'] === 'Too Many Attempts.' + && $params['action'] === ['id' => 'edit', 'label' => 'Edit']); + + expect($guild->instance()->guild?->isPaused)->toBeTrue() + ->and($guild->instance()->sheetOpen)->toBeFalse(); + + pressEditOnToast($guild, $write->id); + + expect(nodeByRef($guild, 'sheet')['props']['visible'])->toBeTrue() + ->and(nodeByRef($guild, 'sheet-body')['props'])->toMatchArray(['value' => 'Shipped it.', 'placeholder' => 'What did you ship?']); + $guild->assertDontSee('Posted to this guild, not your profile log.'); +}); + +it('opens a Log draft in the normal composer from Edit on an open guild page', function () { + holdRefreshes(); + serveGuildAndLog([['message' => 'Too Many Attempts.'], 429]); + finishRefreshes(Native::visit('/log')) + ->press('composeInSheet') + ->input('sheet-body', 'Shipped it.') + ->tap('sheet-commit'); + $write = PendingWrite::query()->sole(); + $guild = finishRefreshes(Native::visit('/log/guilds/rust', platform: 'android')); + + expect($guild->instance()->sheetOpen)->toBeFalse(); + + pressEditOnToast($guild, $write->id); + + expect(nodeByRef($guild, 'sheet-body')['props'])->toMatchArray(['value' => 'Shipped it.', 'placeholder' => 'What did you ship?']); + $guild->assertDontSee('Posted to this guild, not your profile log.'); +}); + +it('opens a post devnull turned down because the guild was paused meanwhile in the guild composer, with why', function () { + holdRefreshes(); + $paused = false; + serveApi(function (Request $request) use (&$paused) { + return match ($request->method().' '.parse_url($request->url(), PHP_URL_PATH)) { + 'GET /api/v1/guilds/rust' => Http::response(boardOf(['c1'], overrides: ['guild' => guildJson(['is_paused' => $paused])])), + 'POST /api/v1/commits' => Http::response(['message' => 'This guild is paused.', 'code' => 'guild_paused'], 422), + default => Http::response(['message' => 'Not Found'], 404), + }; + }); + $screen = finishRefreshes(Native::visit('/log/guilds/rust', platform: 'android')) + ->press('composeForGuild') + ->input('sheet-body', 'Crabs ahoy.') + ->tap('sheet-commit'); + $paused = true; + + // The board learns the guild is paused before devnull answers the post. + finishRefreshes($screen->call('load'), newestFirst: true) + ->assertNativeCalled('ToastKit.Show', fn (array $params): bool => $params['message'] === 'This guild is paused.' && ! isset($params['action'])); + + expect($screen->instance()->guild?->isPaused)->toBeTrue() + ->and(listedIds($screen))->toBe(['c1']) + ->and(nodeByRef($screen, 'sheet')['props']['visible'])->toBeTrue() + ->and(nodeByRef($screen, 'sheet-body')['props'])->toMatchArray(['value' => 'Crabs ahoy.', 'placeholder' => 'What do you want to share with the guild?']); +}); + +it('quotes a commit on the board without posting the quote to the guild', function () { + $screen = guildComposer()->input('sheet-body', 'Half a guild post'); + + $screen->call('hideCommitSheet') + ->call('quoteCommit', 'c1') + ->input('sheet-body', 'Good one.') + ->tap('sheet-commit'); + + Http::assertSent(fn (Request $request): bool => $request->method() === 'POST' + && $request->url() === 'http://devnull.test/api/v1/commits' + && $request->body() === '{"body":"Good one.","quoted_commit_id":"c1"}'); + expect(app(Stash::class)->get($this->userId, 'guild:rust')->body)->toBe('Half a guild post'); +}); + +it('offers a guild post left over from when the app closed back to its guild, which posts it there', function () { + $leftover = PendingWrite::query()->create([ + 'user_id' => $this->userId, + 'kind' => PendingWrite::Commit, + 'target' => null, + 'client_id' => 'left-over', + 'payload' => ['body' => 'Crabs ahoy.', 'stash' => 'guild:rust', 'composer' => 'compose', 'guild_handle' => 'rust'], + 'status' => PendingWrite::Sending, + 'task_id' => 'a-task-from-before-the-app-closed', + 'attempts' => 1, + ]); + serveGuildAndLog(); + + // A commit is never sent again on its own, guild post or not: it waits for Edit. + $log = Native::visit('/log') + ->assertNativeCalled('ToastKit.Show', fn (array $params): bool => $params['action'] === ['id' => 'edit', 'label' => 'Edit']); + + expect(sentTo('POST', 'commits'))->toBe([]) + ->and($log->instance()->sheetOpen)->toBeFalse(); + + pressEditOnToast($log, $leftover->id)->assertNavigatedTo('/log/guilds/rust'); + + finishRefreshes(Native::visit('/log/guilds/rust', ['edit' => $leftover->id]))->tap('sheet-commit'); + + expect(sentTo('POST', 'commits'))->toBe(['{"body":"Crabs ahoy.","guild_handle":"rust"}']); +}); + +it('schedules photos for the board as multipart', function () { + phoneIn('Europe/Lisbon'); + $this->travelTo(CarbonImmutable::parse('2026-10-19 15:30:00', 'UTC')); + $screen = photosPicked(guildComposer(), [testPhoto('jpg')])->input('sheet-body', 'Crabs tomorrow.'); + + $screen->tap('sheet-schedule') + ->pickDateTime('scheduled-at', '2026-10-20T09:00') + ->tap('sheet-commit') + ->assertSet('sheetOpen', false); + + Http::assertSent(fn (Request $request): bool => $request->url() === 'http://devnull.test/api/v1/scheduled-commits' + && multipartParts($request)[0]['contents'] === '{"body":"Crabs tomorrow.","scheduled_at":"2026-10-20T08:00:00+00:00","guild_handle":"rust"}'); +}); diff --git a/tests/Feature/Screens/GuildTest.php b/tests/Feature/Screens/GuildTest.php index 81ed7c15..62898810 100644 --- a/tests/Feature/Screens/GuildTest.php +++ b/tests/Feature/Screens/GuildTest.php @@ -32,23 +32,6 @@ function serveBoard(array $pages): void }); } -/** - * A board page of commits by id, newest first, with no pin unless one is given. - * - * @param list $ids - * @param array $overrides - * @return array - */ -function boardOf(array $ids, ?int $nextPage = null, array $overrides = []): array -{ - return boardJson([ - 'data' => array_map(fn (string $id): array => commitJson(['id' => $id]), $ids), - 'pinned_commit' => null, - 'links' => ['next' => $nextPage === null ? null : 'http://devnull.test/api/v1/guilds/rust?page='.$nextPage], - ...$overrides, - ]); -} - /** * The commits the board shows, top to bottom, by id. * diff --git a/tests/Pest.php b/tests/Pest.php index 9e9e9aae..6eb917e7 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -512,6 +512,23 @@ function boardJson(array $overrides = []): array ]; } +/** + * A board page of commits by id, newest first, with no pin unless one is given. + * + * @param list $ids + * @param array $overrides + * @return array + */ +function boardOf(array $ids, ?int $nextPage = null, array $overrides = []): array +{ + return boardJson([ + 'data' => array_map(fn (string $id): array => commitJson(['id' => $id]), $ids), + 'pinned_commit' => null, + 'links' => ['next' => $nextPage === null ? null : 'http://devnull.test/api/v1/guilds/rust?page='.$nextPage], + ...$overrides, + ]); +} + /** * A commit as the API sends it to a signed request. The short hash and the link * follow the id and the author, and the body's segments follow the body, unless