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
29 changes: 28 additions & 1 deletion app/Devnull/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ final class Account

private const string TermsKey = 'devnull.terms-to-accept';

private const string OrganizationsKey = 'devnull.organizations';

public function user(): ?User
{
$attributes = Cache::get(self::CacheKey);
Expand Down Expand Up @@ -215,7 +217,31 @@ public function rememberTermsToAccept(?string $version): void
}

/**
* Signs this phone out: who it was, the badges, its pastéis and Remotes, your timezone, the terms you owe, every
* The GitHub organizations you can post as, as devnull last said, oldest membership first. None until it has.
*
* @return list<User>
*/
public function organizations(): array
{
$kept = Cache::get(self::OrganizationsKey);

return is_array($kept) ? array_values(array_map(User::fromArray(...), array_filter($kept, is_array(...)))) : [];
}

/**
* Remembers the organizations you can post as. With null, from a devnull too old to say, it keeps what it had.
*
* @param list<User>|null $organizations
*/
public function rememberOrganizations(?array $organizations): void
{
if ($organizations !== null) {
Cache::forever(self::OrganizationsKey, array_map(fn (User $organization): array => $organization->toArray(), $organizations));
}
}

/**
* Signs this phone out: who it was, the badges, its pastéis and Remotes, your organizations, your timezone, the terms you owe, every
* answer, image and voice note kept for a quick start, and your unsent drafts and recordings.
*/
public function forget(): void
Expand All @@ -234,5 +260,6 @@ public function forget(): void
Cache::forget(self::PinWarningKey);
Cache::forget(self::TimezoneKey);
Cache::forget(self::TermsKey);
Cache::forget(self::OrganizationsKey);
}
}
26 changes: 15 additions & 11 deletions app/Devnull/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,15 @@ 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 or a quote may start a poll (see PollDraft::payload()). With
* $author, the handle of an organization you're a member of, it goes out as
* that organization, as the web's Post as picks (see PostAs).
*
* @param array{options: list<string>, 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 $author = 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, $author)));
$this->cache->commitAdded($commit);

return Commit::fromArray($commit);
Expand All @@ -319,9 +321,9 @@ public function createCommit(string $body, ?string $parentId = null, ?string $qu
*
* @param array{options: list<string>, 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 $author = 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, $author));
}

/**
Expand All @@ -331,9 +333,9 @@ public function signedCommit(string $body, ?string $parentId = null, ?string $qu
*
* @param list<DraftImage> $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 $author = null): SignedRequest
{
return $this->signedWithImages('commits', self::commitPayload($body, $parentId, $quotedCommitId), $images);
return $this->signedWithImages('commits', self::commitPayload($body, $parentId, $quotedCommitId, author: $author), $images);
}

/**
Expand All @@ -360,14 +362,15 @@ 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. With $author it goes out as that organization when it's
* due, as long as you're still in it then.
*
* @param list<DraftImage> $images
* @param array<string, mixed>|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 $author = 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, 'author' => $author], fn (mixed $value): bool => $value !== null);

return $images === []
? $this->signedWrite('POST', self::ScheduledCommitsPath, $payload)
Expand Down Expand Up @@ -976,13 +979,14 @@ private function send(string $method, string $path, array $query = [], ?array $p
* @param array{options: list<string>, duration: int}|null $poll
* @return array<string, mixed>
*/
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 $author = null): array
{
return array_filter([
'body' => $body,
'parent_id' => $parentId,
'quoted_commit_id' => $quotedCommitId,
'poll' => $poll,
'author' => $author,
], fn (mixed $value): bool => $value !== null);
}

Expand Down
22 changes: 22 additions & 0 deletions app/Devnull/Data/CurrentUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@
* And the terms: the version in force, which is the moment it took effect, when
* you last accepted them, and whether that was this version. A devnull too old
* to say leaves all three null.
*
* And the GitHub organizations you're a member of, admin or not, oldest
* membership first, which you can post as, as the web's Post as menu lists
* them. A devnull too old to say leaves it null.
*/
final readonly class CurrentUser
{
/**
* @param list<User>|null $organizations
*/
public function __construct(
public User $user,
public int $unreadNotificationsCount = 0,
Expand All @@ -37,6 +44,7 @@ public function __construct(
public ?string $termsVersion = null,
public ?CarbonImmutable $termsAcceptedAt = null,
public ?bool $termsAccepted = null,
public ?array $organizations = null,
) {}

/**
Expand Down Expand Up @@ -69,9 +77,23 @@ public static function fromArray(array $json): self
termsVersion: is_string($data['terms_version'] ?? null) && $data['terms_version'] !== '' ? $data['terms_version'] : null,
termsAcceptedAt: Commit::date($data['terms_accepted_at'] ?? null),
termsAccepted: is_bool($data['terms_accepted'] ?? null) ? $data['terms_accepted'] : null,
organizations: is_array($data['organizations'] ?? null) ? self::organizations($data['organizations']) : null,
);
}

/**
* The organizations you can post as, each drawn as an organization whatever the answer said.
*
* @param array<mixed> $organizations
* @return list<User>
*/
private static function organizations(array $organizations): array
{
$users = array_map(fn (array $organization): User => User::fromArray([...$organization, 'type' => 'organization']), array_values(array_filter($organizations, is_array(...))));

return array_values(array_filter($users, fn (User $organization): bool => $organization->username !== ''));
}

/**
* The version of the terms devnull is waiting for you to accept, exactly as it sent it, or null when you have.
*/
Expand Down
5 changes: 4 additions & 1 deletion app/Devnull/Data/ScheduledCommit.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* page, which only you see. Until it's submitted it has its body, the options
* of its poll, and how many images it carries. Once submitted, those are its
* commit's, and its status is that commit's: pending review, approved,
* rejected, or deleted.
* rejected, or deleted. $author is the organization it goes out as, or null
* when it goes out as you.
*/
final readonly class ScheduledCommit
{
Expand Down Expand Up @@ -39,6 +40,7 @@ public function __construct(
public CarbonImmutable $scheduledAt,
public ?CarbonImmutable $submittedAt,
public string $status,
public ?User $author = null,
) {}

/**
Expand All @@ -62,6 +64,7 @@ public static function fromArray(array $data): self
scheduledAt: Commit::date($data['scheduled_at'] ?? null) ?? CarbonImmutable::now(),
submittedAt: Commit::date($data['submitted_at'] ?? null),
status: is_string($status) && ($status === 'scheduled' || array_key_exists($status, self::Labels)) ? $status : 'scheduled',
author: is_array($data['author'] ?? null) ? User::fromArray([...$data['author'], 'type' => 'organization']) : null,
);
}

Expand Down
106 changes: 106 additions & 0 deletions app/Devnull/PostAs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace App\Devnull;

use App\Devnull\Data\User;
use App\Icons\Lucide;
use Native\Mobile\Edge\Elements\TopBarAction;
use Native\Mobile\Edge\Layouts\Builders\NavAction;

/**
* Who a composer posts as, as the web's Post as menu chooses: you, or one of
* the GitHub organizations you're a member of, admin or not (see
* Account::organizations()). A composer keeps the handle picked, and posts as
* you once that organization is no longer yours, as the web does, so a commit
* devnull would refuse isn't sent as it again.
*
* The menu opens from the composer's avatar, which shows who it posts as:
* round for you, square for an organization. Someone in no organization never
* sees it, as on the web, unless they had picked one they've since left, when
* their own avatar shows who posts now.
*/
final class PostAs
{
/**
* @return list<User>
*/
public static function organizations(): array
{
return app(Account::class)->organizations();
}

/**
* The organization the commit goes out as, or null when it goes out as you.
*/
public static function author(?string $picked): ?User
{
if ($picked === null || $picked === '') {
return null;
}

foreach (self::organizations() as $organization) {
if (strcasecmp($organization->username, $picked) === 0) {
return $organization;
}
}

return null;
}

/**
* Whether the composer shows whom it posts as: you're in an organization, or picked one you've left since.
*/
public static function offered(?string $picked): bool
{
return self::organizations() !== [] || ($picked !== null && $picked !== '');
}

/**
* The menu: you first, then your organizations, the one posting now ticked. $method is called with the handle
* picked, or '' for you.
*
* @return list<TopBarAction>
*/
public static function menu(User $me, ?User $author, string $method): array
{
return array_map(fn (User $identity): TopBarAction => NavAction::make('post-as-'.$identity->username)
->label($identity->name.' (@'.$identity->username.')')
->icon(...self::icon($identity, $author))
->press($method."('".($identity->isOrganization ? $identity->username : '')."')")
->toElement()
->ref('post-as-'.$identity->username), [$me, ...self::organizations()]);
}

/**
* What the composer's avatar says to a screen reader.
*/
public static function label(User $me, ?User $author): string
{
return 'Posting as @'.($author ?? $me)->username.'. Choose who to post as';
}

/**
* The lane the composer's main button is drawn in: yours, or the organization's.
*/
public static function colour(User $me, ?User $author): string
{
return Lane::token(($author ?? $me)->lane());
}

/**
* A tick beside whoever posts now, and what each is beside the rest, as the menu has no room for avatars.
*
* @return array{ios: string, android: string}
*/
private static function icon(User $identity, ?User $author): array
{
$posting = $author === null ? ! $identity->isOrganization : $identity->isOrganization && $identity->username === $author->username;
$icon = match (true) {
$posting => Lucide::Check,
$identity->isOrganization => Lucide::Building2,
default => Lucide::UserRound,
};

return ['ios' => $icon->ios(), 'android' => $icon->android()];
}
}
43 changes: 37 additions & 6 deletions app/Models/PendingWrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
* $target is what it's about: the commit starred, or the Remotes thread a
* 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.
* its poll, its photos with their descriptions, the draft it was stashed
* under, and the organization it goes out as, if not as you.
*
* 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
Expand Down Expand Up @@ -132,6 +132,34 @@ public function quotedCommitId(): ?string
return is_string($this->payload['quoted_commit_id'] ?? null) ? $this->payload['quoted_commit_id'] : null;
}

/**
* The handle of the organization a commit goes out as, or null when it goes out as you.
*/
public function author(): ?string
{
return is_string($this->payload['author'] ?? null) && $this->payload['author'] !== '' ? $this->payload['author'] : null;
}

/**
* The organization a commit goes out as, as the composer knew it, for the staged commit to show it, or null.
*/
public function authorProfile(): ?User
{
$profile = $this->payload['author_profile'] ?? null;

return $this->author() !== null && is_array($profile) ? User::fromArray([...$profile, 'type' => 'organization']) : null;
}

/**
* The payload's part for who a commit goes out as: nothing for you, and an organization's handle and profile.
*
* @return array{author?: string, author_profile?: array<string, mixed>}
*/
public static function authorPayload(?User $organization): array
{
return $organization === null ? [] : ['author' => $organization->username, 'author_profile' => $organization->toArray()];
}

/**
* The commit's poll as devnull takes it, or null with none.
*
Expand Down Expand Up @@ -200,12 +228,15 @@ public static function imagePayload(array $images): array
}

/**
* The commit a commit on its way stands in as, by you, at once: what you
* wrote and its photos, marked staged, as the web's git words go, until
* devnull has it and the real one takes its place.
* The commit a commit on its way stands in as, at once: by you, or by the
* organization it goes out as, with what you wrote and its photos, marked
* staged, as the web's git words go, until devnull has it and the real one
* takes its place.
*/
public function stagedCommit(User $author): Commit
public function stagedCommit(User $me): Commit
{
$author = $this->authorProfile() ?? $me;

return new Commit(
id: self::StagedPrefix.$this->client_id,
body: $this->body(),
Expand Down
Loading
Loading