Skip to content

Commit 607cf78

Browse files
[Guilds] Post straight to a guild's board from its page (#197)
1 parent 1c19e4e commit 607cf78

12 files changed

Lines changed: 621 additions & 49 deletions

File tree

‎app/Devnull/Client.php‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,13 @@ public function replies(string $id, ?string $cursor = null): Page
304304
* Commits, as a reply to $parentId or quoting $quotedCommitId, as the web's
305305
* composer, reply box and Quote do. devnull takes one or the other, not both.
306306
* A new commit or a quote may start a poll (see PollDraft::payload()).
307+
* A new commit can go straight to a guild's board ($guildHandle).
307308
*
308309
* @param array{options: list<string>, duration: int}|null $poll
309310
*/
310-
public function createCommit(string $body, ?string $parentId = null, ?string $quotedCommitId = null, ?array $poll = null): Commit
311+
public function createCommit(string $body, ?string $parentId = null, ?string $quotedCommitId = null, ?array $poll = null, ?string $guildHandle = null): Commit
311312
{
312-
$commit = $this->data($this->send('POST', 'commits', payload: self::commitPayload($body, $parentId, $quotedCommitId, poll: $poll)));
313+
$commit = $this->data($this->send('POST', 'commits', payload: self::commitPayload($body, $parentId, $quotedCommitId, $poll, $guildHandle)));
313314
$this->cache->commitAdded($commit);
314315

315316
return Commit::fromArray($commit);
@@ -321,9 +322,9 @@ public function createCommit(string $body, ?string $parentId = null, ?string $qu
321322
*
322323
* @param array{options: list<string>, duration: int}|null $poll
323324
*/
324-
public function signedCommit(string $body, ?string $parentId = null, ?string $quotedCommitId = null, ?array $poll = null): SignedRequest
325+
public function signedCommit(string $body, ?string $parentId = null, ?string $quotedCommitId = null, ?array $poll = null, ?string $guildHandle = null): SignedRequest
325326
{
326-
return $this->signedWrite('POST', 'commits', self::commitPayload($body, $parentId, $quotedCommitId, poll: $poll));
327+
return $this->signedWrite('POST', 'commits', self::commitPayload($body, $parentId, $quotedCommitId, $poll, $guildHandle));
327328
}
328329

329330
/**
@@ -333,9 +334,9 @@ public function signedCommit(string $body, ?string $parentId = null, ?string $qu
333334
*
334335
* @param list<DraftImage> $images
335336
*/
336-
public function signedCommitWithImages(string $body, array $images, ?string $parentId = null, ?string $quotedCommitId = null): SignedRequest
337+
public function signedCommitWithImages(string $body, array $images, ?string $parentId = null, ?string $quotedCommitId = null, ?string $guildHandle = null): SignedRequest
337338
{
338-
return $this->signedWithImages('commits', self::commitPayload($body, $parentId, $quotedCommitId), $images);
339+
return $this->signedWithImages('commits', self::commitPayload($body, $parentId, $quotedCommitId, guildHandle: $guildHandle), $images);
339340
}
340341

341342
/**
@@ -362,14 +363,14 @@ public function signedMessage(string $threadPath, string $body, string $clientId
362363
* Signs a commit to be submitted at $at, as the web's Schedule commit
363364
* does, for the background to send. Only you see it until then. With
364365
* photos it goes as multipart, as a commit with photos does, and a poll
365-
* goes in its JSON.
366+
* goes in its JSON. One for a guild's board says which ($guildHandle).
366367
*
367368
* @param list<DraftImage> $images
368369
* @param array<string, mixed>|null $poll
369370
*/
370-
public function signedScheduledCommit(string $body, CarbonInterface $at, array $images = [], ?array $poll = null): SignedRequest
371+
public function signedScheduledCommit(string $body, CarbonInterface $at, array $images = [], ?array $poll = null, ?string $guildHandle = null): SignedRequest
371372
{
372-
$payload = array_filter(['body' => $body, 'scheduled_at' => self::scheduleTime($at), 'poll' => $poll], fn (mixed $value): bool => $value !== null);
373+
$payload = array_filter(['body' => $body, 'scheduled_at' => self::scheduleTime($at), 'poll' => $poll, 'guild_handle' => $guildHandle], fn (mixed $value): bool => $value !== null);
373374

374375
return $images === []
375376
? $this->signedWrite('POST', self::ScheduledCommitsPath, $payload)
@@ -1004,13 +1005,14 @@ private function send(string $method, string $path, array $query = [], ?array $p
10041005
* @param array{options: list<string>, duration: int}|null $poll
10051006
* @return array<string, mixed>
10061007
*/
1007-
private static function commitPayload(string $body, ?string $parentId, ?string $quotedCommitId, ?array $poll = null): array
1008+
private static function commitPayload(string $body, ?string $parentId, ?string $quotedCommitId, ?array $poll = null, ?string $guildHandle = null): array
10081009
{
10091010
return array_filter([
10101011
'body' => $body,
10111012
'parent_id' => $parentId,
10121013
'quoted_commit_id' => $quotedCommitId,
10131014
'poll' => $poll,
1015+
'guild_handle' => $guildHandle,
10141016
], fn (mixed $value): bool => $value !== null);
10151017
}
10161018

‎app/Devnull/Outbox.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,14 @@ public function pending(string $kind, ?string $target = null, bool $withFailed =
138138
}
139139

140140
/**
141-
* Commits on their way that reply to $parentId, or with null, new commits and quotes.
141+
* Commits on their way that reply to $parentId, or with null, new commits and quotes: those for your log, or with
142+
* $guildHandle, those for that guild's board.
142143
*
143144
* @return Collection<int, PendingWrite>
144145
*/
145-
public function commitsUnder(?string $parentId): Collection
146+
public function commitsUnder(?string $parentId, ?string $guildHandle = null): Collection
146147
{
147-
return $this->pending(PendingWrite::Commit)->filter(fn (PendingWrite $write): bool => $write->parentId() === $parentId)->values();
148+
return $this->pending(PendingWrite::Commit)->filter(fn (PendingWrite $write): bool => $write->parentId() === $parentId && $write->guildHandle() === $guildHandle)->values();
148149
}
149150

150151
/**

‎app/Devnull/ResponseCache.php‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Devnull;
44

5+
use App\Devnull\Data\Guild;
56
use Carbon\CarbonImmutable;
67
use Closure;
78
use Illuminate\Contracts\Cache\Repository;
@@ -399,7 +400,10 @@ public function pastelGiven(array $json): void
399400

400401
/**
401402
* You committed. A commit goes on top of your logs and your profile; a reply
402-
* goes under its parent and counts there, and on top of your replies.
403+
* goes under its parent and counts there, and on top of your replies. A
404+
* post to a guild's board goes on top of that board alone, as it isn't in
405+
* the logs or your profile's list, though devnull counts it among your
406+
* commits.
403407
*
404408
* @param array<string, mixed> $commit
405409
*/
@@ -413,9 +417,13 @@ public function commitAdded(array $commit): void
413417

414418
$parentId = is_string($commit['parent_id'] ?? null) ? $commit['parent_id'] : null;
415419

420+
$guild = Guild::fromArray(is_array($commit['guild'] ?? null) ? $commit['guild'] : ['handle' => $commit['guild_handle'] ?? null]);
421+
416422
if ($parentId === null) {
417423
// Trending is ranked, so a new commit has no place at the top of it.
418-
foreach (['log/following', 'log/latest', "users/{$me}/commits"] as $path) {
424+
$paths = $guild === null ? ['log/following', 'log/latest', "users/{$me}/commits"] : ['guilds/'.rawurlencode($guild->handle)];
425+
426+
foreach ($paths as $path) {
419427
$this->patch($path, [], fn (array $json): array => self::prepend($json, $commit));
420428
}
421429

‎app/Models/PendingWrite.php‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Devnull\Data\Commit;
66
use App\Devnull\Data\CommitBody;
77
use App\Devnull\Data\DraftImage;
8+
use App\Devnull\Data\Guild;
89
use App\Devnull\Data\MessageVoice;
910
use App\Devnull\Data\ScoreCard;
1011
use App\Devnull\Data\User;
@@ -20,7 +21,7 @@
2021
* message goes to. $payload is what's sent, and for a commit what the
2122
* composer needs to have it back: its body, what it replies to or quotes,
2223
* its poll, its photos with their descriptions, and the draft it was
23-
* stashed under.
24+
* stashed under, and the guild whose board it goes to, if any.
2425
*
2526
* It's queued until it goes, sending while it's on its way, and failed once
2627
* devnull turned it down, when a commit stays for you to edit and a message
@@ -144,6 +145,14 @@ public function poll(): ?array
144145
return is_array($poll) && is_array($poll['options'] ?? null) && is_int($poll['duration'] ?? null) ? $poll : null;
145146
}
146147

148+
/**
149+
* The guild whose board a new commit goes to, or null for one that goes to your log.
150+
*/
151+
public function guildHandle(): ?string
152+
{
153+
return is_string($this->payload['guild_handle'] ?? null) ? $this->payload['guild_handle'] : null;
154+
}
155+
147156
/**
148157
* The message a Remotes message replies to.
149158
*/
@@ -216,6 +225,7 @@ public function stagedCommit(User $author): Commit
216225
images: array_map(fn (DraftImage $image, int $index) => $image->asCommitImage($index), $this->images(), array_keys($this->images())),
217226
content: CommitBody::blocks(ScoreCard::withoutRun($this->body()), null, []),
218227
quotedCommitId: $this->quotedCommitId(),
228+
guild: Guild::fromArray(['handle' => $this->guildHandle()]),
219229
scoreCard: ScoreCard::fromCommit($this->body(), $author),
220230
);
221231
}

‎app/NativeComponents/Concerns/DraftsIssues.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@ protected function typeIssueCommand(): void
192192
}
193193

194194
/**
195-
* Only a new commit can become an issue, as on the web.
195+
* Only a new commit for your log can become an issue, as on the web, whose guild composer has no commands.
196196
*/
197197
protected function canDraftIssue(): bool
198198
{
199-
return $this->sheetOpen && $this->sheetView === 'compose' && ! $this->scheduling;
199+
return $this->sheetOpen && $this->sheetView === 'compose' && $this->sheetGuild === null && ! $this->scheduling;
200200
}
201201

202202
/**

‎app/NativeComponents/Concerns/OpensCommitSheet.php‎

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
* A new commit's draft can become a `/feature` or `/bug` issue, as on the web
4848
* (see DraftsIssues).
4949
*
50+
* A guild's page opens it for a new commit on the guild's board, as the web's
51+
* guild page has its own composer (see composeInGuildSheet()): its own draft,
52+
* `guild:{handle}`, and no issue commands, as on the web. The commit goes to
53+
* the board, not to your log, and if devnull turns it down it goes back only
54+
* to that guild's page (see composerGuild()).
55+
*
5056
* A commit's page can open its reply in the same composer, for the photos and
5157
* the preview the reply bar has no room for (see replyInSheet()). The draft
5258
* is the reply bar's, and goes back to it when the sheet closes.
@@ -92,6 +98,11 @@ trait OpensCommitSheet
9298
*/
9399
public string $sheetView = 'report';
94100

101+
/**
102+
* The guild whose board the new commit in the sheet goes to, or null for one that goes to your log.
103+
*/
104+
public ?string $sheetGuild = null;
105+
95106
/**
96107
* The reason picked on the report form, or '' until one is.
97108
*/
@@ -249,6 +260,18 @@ public function composeInSheet(): void
249260
$this->restoreSheetDraft();
250261
}
251262

263+
/**
264+
* A new commit in the sheet for the board of the guild $handle, under its own draft.
265+
*/
266+
protected function composeInGuildSheet(string $handle): void
267+
{
268+
$this->forgetCommitSheet();
269+
$this->sheetView = 'compose';
270+
$this->sheetGuild = $handle;
271+
$this->sheetOpen = true;
272+
$this->restoreSheetDraft();
273+
}
274+
252275
/**
253276
* The web's clock button, in a new commit's composer: it opens "Submit at" under the field, with no time picked,
254277
* 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
335358
'images' => PendingWrite::imagePayload($this->sheetImages),
336359
'stash' => $stashName,
337360
'composer' => $this->sheetView,
361+
...($this->sheetGuild === null ? [] : ['guild_handle' => $this->sheetGuild]),
338362
]);
339363

340364
if ($write === null) {
@@ -432,7 +456,7 @@ protected function commitSheetGone(string $view): void
432456
*/
433457
protected function reopenFailedCommit(PendingWrite $write): bool
434458
{
435-
if ($this->sheetOpen || ($write->composer() === 'compose' && ! $this->opensComposer())) {
459+
if ($this->sheetOpen || ($write->composer() === 'compose' && (! $this->opensComposer() || $write->guildHandle() !== $this->composerGuild()))) {
436460
return false;
437461
}
438462

@@ -448,14 +472,28 @@ protected function reopenFailedCommit(PendingWrite $write): bool
448472
/**
449473
* Edit, on the toast of a commit devnull turned down: its sheet, here if
450474
* the commit it quotes or replies to is showing, or the commit's own page.
451-
* A new commit's sheet opens anywhere it can.
475+
* A new commit's sheet opens anywhere it can but a guild's page, which
476+
* sends it to the Log. One for a guild's board opens only on that guild's
477+
* page, which opens it.
452478
*/
453479
protected function editFailedCommit(PendingWrite $write): void
454480
{
455481
if ($this->openFailedCommit($write)) {
456482
return;
457483
}
458484

485+
if ($write->guildHandle() !== null) {
486+
$this->openInTab('/guilds/'.rawurlencode($write->guildHandle()), ['edit' => $write->id]);
487+
488+
return;
489+
}
490+
491+
if ($write->composer() === 'compose') {
492+
$this->composeInSheet();
493+
494+
return;
495+
}
496+
459497
$for = $write->parentId() ?? $write->quotedCommitId();
460498

461499
if ($for !== null) {
@@ -502,15 +540,30 @@ protected function opensComposer(): bool
502540
return false;
503541
}
504542

543+
/**
544+
* The guild whose board this screen's composer posts to, or null when it posts to your log. A new commit devnull
545+
* turned down goes back only to a composer for the same place.
546+
*/
547+
protected function composerGuild(): ?string
548+
{
549+
return null;
550+
}
551+
505552
/**
506553
* Opens the sheet a commit devnull turned down was written in, with everything it had (see restoreSheetDraft()).
507554
*/
508555
private function openFailedCommit(PendingWrite $write): bool
509556
{
510557
$for = $write->parentId() ?? $write->quotedCommitId();
511558

559+
$guild = $write->guildHandle();
560+
512561
match ($write->composer()) {
513-
'compose' => $this->composeInSheet(),
562+
'compose' => match (true) {
563+
$guild === null => $this->composerGuild() === null ? $this->composeInSheet() : null,
564+
$guild === $this->composerGuild() => $this->composeInGuildSheet($guild),
565+
default => null,
566+
},
514567
'quote' => $for === null ? null : $this->quoteCommit($for),
515568
'reply' => $for === null ? null : $this->replyInSheet($for),
516569
default => null,
@@ -549,10 +602,11 @@ private function scheduleFromSheet(string $body, string $stashName): void
549602

550603
$images = $this->sheetImages;
551604
$poll = $this->sheetPollPayload();
605+
$guild = $this->sheetGuild;
552606
$this->sendingSchedule = true;
553607
$this->sheetError = null;
554608

555-
$this->sendInBackground(fn (Client $devnull): SignedRequest => $devnull->signedScheduledCommit($body, $at, $images, $poll), function () use ($me, $stashName, $images): void {
609+
$this->sendInBackground(fn (Client $devnull): SignedRequest => $devnull->signedScheduledCommit($body, $at, $images, $poll, $guild), function () use ($me, $stashName, $images): void {
556610
$this->sendingSchedule = false;
557611
app(ResponseCache::class)->forget(Client::ScheduledCommitsPath);
558612
app(Stash::class)->put($me->id, $stashName, new StashedDraft);
@@ -571,6 +625,7 @@ private function forgetCommitSheet(): void
571625
$this->sheetOpen = false;
572626
$this->sheetCommit = null;
573627
$this->sheetView = 'report';
628+
$this->sheetGuild = null;
574629
$this->reportReason = '';
575630
$this->reportDetails = '';
576631
$this->sheetBody = '';
@@ -592,7 +647,7 @@ private function forgetCommitSheet(): void
592647
private function sheetStashName(): ?string
593648
{
594649
return match (true) {
595-
$this->sheetView === 'compose' => 'commit',
650+
$this->sheetView === 'compose' => $this->sheetGuild === null ? 'commit' : 'guild:'.$this->sheetGuild,
596651
$this->sheetView === 'quote' && $this->sheetCommit !== null => 'quote-'.$this->sheetCommit->id,
597652
$this->sheetView === 'reply' && $this->sheetCommit !== null => 'reply-'.$this->sheetCommit->id,
598653
default => null,

‎app/NativeComponents/Concerns/SyncsOutbox.php‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,20 @@ protected function sendLeftovers(): void
179179

180180
/**
181181
* The commits on their way that this screen would show, as they stand in for themselves (see
182-
* PendingWrite::stagedCommit()): replies to $parentId, or with null, new commits and quotes. Newest first.
182+
* PendingWrite::stagedCommit()): replies to $parentId, or with null, new commits and quotes, for your log or, with
183+
* $guildHandle, for that guild's board. Newest first.
183184
*
184185
* @return list<Commit>
185186
*/
186-
protected function stagedCommits(?string $parentId): array
187+
protected function stagedCommits(?string $parentId, ?string $guildHandle = null): array
187188
{
188189
$me = $this->me();
189190

190191
if ($me === null) {
191192
return [];
192193
}
193194

194-
return app(Outbox::class)->commitsUnder($parentId)
195+
return app(Outbox::class)->commitsUnder($parentId, $guildHandle)
195196
->reverse()
196197
->map(fn (PendingWrite $write): Commit => $write->stagedCommit($me))
197198
->values()
@@ -245,6 +246,8 @@ private function writeWent(PendingWrite $write, array $json): void
245246
app(VoiceNotes::class)->discard($write->voice());
246247

247248
if ($write->isCommit()) {
249+
// A post to a guild's board is known by its guild, even when devnull's answer leaves it out.
250+
$data = $write->guildHandle() === null ? $data : ['guild_handle' => $write->guildHandle(), ...$data];
248251
app(ResponseCache::class)->commitAdded($data);
249252
$this->showCommitWent($write, Commit::fromArray($data));
250253

@@ -333,8 +336,8 @@ private function signWrite(PendingWrite $write): SignedRequest
333336
PendingWrite::Unstar => $devnull->signedWrite('DELETE', $commit),
334337
PendingWrite::Message => $devnull->signedMessage((string) $write->target, $write->body(), $write->client_id, $write->replyToId(), $write->images(), $write->voice()),
335338
default => $write->images() === []
336-
? $devnull->signedCommit($write->body(), $write->parentId(), $write->quotedCommitId(), $write->poll())
337-
: $devnull->signedCommitWithImages($write->body(), $write->images(), $write->parentId(), $write->quotedCommitId()),
339+
? $devnull->signedCommit($write->body(), $write->parentId(), $write->quotedCommitId(), $write->poll(), $write->guildHandle())
340+
: $devnull->signedCommitWithImages($write->body(), $write->images(), $write->parentId(), $write->quotedCommitId(), $write->guildHandle()),
338341
};
339342
}
340343

0 commit comments

Comments
 (0)