Skip to content
Merged
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
11 changes: 11 additions & 0 deletions app/Devnull/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Devnull\Data\CurrentUser;
use App\Devnull\Data\DraftImage;
use App\Devnull\Data\Guild;
use App\Devnull\Data\GuildBoard;
use App\Devnull\Data\IncomingCall;
use App\Devnull\Data\MessageVoice;
use App\Devnull\Data\Notification;
Expand Down Expand Up @@ -471,6 +472,16 @@ public function guilds(string $term = '', ?string $page = null): Page
return new Page(array_values(array_filter($guilds->items)), $guilds->nextCursor);
}

/**
* A guild's bulletin board: its latest commits and the one pinned to the top. Null when the answer carries
* no guild. A handle devnull has no guild by is a 404, which throws, as does guilds being switched off for
* you. Page 1 is asked for with no page number, so it's kept.
*/
public function guild(string $handle, ?string $page = null): ?GuildBoard
{
return GuildBoard::fromArray($this->get('guilds/'.rawurlencode($handle), ['page' => $page]));
}

/**
* A page of your Remotes inbox, newest first, for the web's Open, Fetched
* or Stashed tab, continuing before a conversation.
Expand Down
39 changes: 39 additions & 0 deletions app/Devnull/Data/GuildBoard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Devnull\Data;

/**
* A guild's bulletin board as `GET /guilds/{handle}` answers it: the guild, a
* page of its commits (30 roots, the pinned one among them) and the pinned
* commit. Its commits are paged by number, which the page carries as its
* cursor.
*/
final readonly class GuildBoard
{
/**
* @param Page<Commit> $commits
*/
public function __construct(
public Guild $guild,
public Page $commits,
public ?Commit $pinned,
) {}

/**
* @param array<string, mixed> $json
*/
public static function fromArray(array $json): ?self
{
$guild = is_array($json['guild'] ?? null) ? Guild::fromArray($json['guild']) : null;

if ($guild === null) {
return null;
}

return new self(
guild: $guild,
commits: Page::fromArray($json, Commit::fromArray(...)),
pinned: is_array($json['pinned_commit'] ?? null) ? Commit::fromArray($json['pinned_commit']) : null,
);
}
}
1 change: 1 addition & 0 deletions app/Devnull/ResponseCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ final class ResponseCache
'#^remotes/orgs/[^/]+/messages$#',
'#^scheduled-commits$#',
'#^guilds$#',
'#^guilds/[^/]+$#',
];

/**
Expand Down
18 changes: 17 additions & 1 deletion app/Devnull/SiteLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ final class SiteLink
{
/**
* The web's pages the app has a screen for, and that screen. {user} is a
* GitHub username, {id} a commit's or an org's id, and {photo} anything.
* GitHub username, {id} a commit's or an org's id, {handle} a guild's,
* and {photo} anything.
*/
private const array Screens = [
'/@{user}' => '/users/{user}',
Expand All @@ -26,6 +27,8 @@ final class SiteLink
'/@{user}/commits/{id}/photos/{photo}' => '/commits/{id}',
'/remotes/@{user}' => '/conversations/{user}',
'/remotes/orgs/{id}' => '/orgs/{id}',
'/guilds' => '/guilds',
'/guilds/{handle}' => '/guilds/{handle}',
];

/**
Expand All @@ -38,6 +41,11 @@ final class SiteLink
*/
private const string Username = '[A-Za-z0-9](?:[A-Za-z0-9]|-(?=[A-Za-z0-9])){0,38}';

/**
* A guild's handle once decoded, as the web allows one (CreateGuildRequest).
*/
private const string Handle = '/^\p{L}[\p{L}\p{M}\p{N}_]*$/uD';

/**
* A UUID in either case.
*/
Expand All @@ -59,9 +67,16 @@ public static function screen(?string $url): ?string

foreach (self::Screens as $page => $screen) {
if (preg_match(self::pattern($page), $path, $matches) === 1) {
$handle = rawurldecode($matches['handle'] ?? '');

if (isset($matches['handle']) && (mb_strlen($handle) > 60 || preg_match(self::Handle, $handle) !== 1)) {
continue;
}

return strtr($screen, [
'{user}' => $matches['user'] ?? '',
'{connections}' => $matches['connections'] ?? '',
'{handle}' => $handle,
'{id}' => strtolower($matches['id'] ?? ''),
]);
}
Expand Down Expand Up @@ -119,6 +134,7 @@ private static function pattern(string $page): string
return '#^'.strtr($page, [
'{user}' => '(?<user>'.self::Username.')',
'{connections}' => '(?<connections>'.implode('|', Connections::Types).')',
'{handle}' => '(?<handle>[^/]+)',
'{id}' => '(?<id>'.self::Id.')',
'{photo}' => '[^/]+',
]).'/?\z#';
Expand Down
14 changes: 13 additions & 1 deletion app/NativeComponents/Concerns/HoldsNewCommits.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,24 @@ public function showNewCommits(): void
*/
public function newCommitsLabel(): string
{
$count = max(count($this->newCommits), $this->newCommitsCount ?? 0);
$count = max(count($this->waitingCommits()), $this->newCommitsCount ?? 0);
$more = $this->newCommitsCount === null && $this->newCommitsReplaceList && $this->newCommitsCursor !== null;

return $count.($more ? '+' : '').' '.($count === 1 ? 'new commit' : 'new commits');
}

/**
* The new commits the pill counts. A screen that already shows one of
* them elsewhere, as a guild's board shows its pinned commit on top,
* leaves that one out (see Guild).
*
* @return list<Commit>
*/
protected function waitingCommits(): array
{
return $this->newCommits;
}

/**
* Lays a fresh first page over the list. Newer commits wait for the pill
* unless $showNewer says you asked for them.
Expand Down
16 changes: 16 additions & 0 deletions app/NativeComponents/Concerns/OpensGuilds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\NativeComponents\Concerns;

/**
* A tapped guild opens its own screen (see App\NativeComponents\Guild), in
* the tab it was tapped in (see OpensInTab, which the screen uses too): from
* a row of the directory, or the "posted in" pill on a commit.
*/
trait OpensGuilds
{
public function openGuild(string $handle): void
{
$this->openInTab('/guilds/'.$handle);
}
}
7 changes: 4 additions & 3 deletions app/NativeComponents/Concerns/RefreshesInBackground.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,10 @@ protected function refreshSettings(): void
/**
* A screen that has its own way of saying devnull refused it, such as a
* profile across a block, says so here and returns true. Anything else
* prints the error.
* prints the error. $path is what was asked for, as a screen may ask
* for more than its own page, such as an opened thread's replies.
*/
protected function refused(ApiException $exception): bool
protected function refused(ApiException $exception, string $path): bool
{
return false;
}
Expand Down Expand Up @@ -269,7 +270,7 @@ private function settleRefresh(array $result, string $path, array $query, Closur
if (in_array($status, [403, 404, 410], true)) {
app(ResponseCache::class)->forget($path, $query);

if (! $this->refused($exception)) {
if (! $this->refused($exception, $path)) {
$this->error = $exception->firstError();
}

Expand Down
1 change: 1 addition & 0 deletions app/NativeComponents/Concerns/ShowsCommits.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
trait ShowsCommits
{
use OpensCommitSheet;
use OpensGuilds;
use OpensHashtags;
use OpensStargazers;
use VotesOnPolls;
Expand Down
2 changes: 1 addition & 1 deletion app/NativeComponents/Connections.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ protected function showsContent(): bool
/**
* A list across a block says so as the profile does, whatever devnull's message says.
*/
protected function refused(ApiException $exception): bool
protected function refused(ApiException $exception, string $path): bool
{
if (! $exception->is(ErrorCode::UserBlocked) && ! $exception->is(ErrorCode::UserUnavailable)) {
return false;
Expand Down
Loading
Loading