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
51 changes: 49 additions & 2 deletions app/Devnull/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ final class Account

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

private const string GuildsKey = 'devnull.guilds-available';

/**
* Whether this launch shows the Guilds tab, once it's been asked (see guildsTabShown()).
*/
private static ?bool $guildsTabShown = null;

public function user(): ?User
{
$attributes = Cache::get(self::CacheKey);
Expand Down Expand Up @@ -93,6 +100,43 @@ public function rememberUnreadNotifications(int $count): void
Cache::forever(self::UnreadKey, max(0, $count));
}

/**
* Are guilds on for the current user? Guilds can be set to admin-only,
* which was used before public release. Handling the unlikely scenario
* where they get switched off again (at least temporarily) so we don't
* need an app update to handle it. They'll just be hidden in the app.
*/
public function guildsAvailable(): ?bool
{
$available = Cache::get(self::GuildsKey);

return is_bool($available) ? $available : null;
}

public function rememberGuildsAvailable(bool $available): void
{
Cache::forever(self::GuildsKey, $available);
}

/**
* Whether this launch shows the Guilds tab: unless guilds were known to be
* off when it was first asked. iOS can't add or remove a tab mid-session,
* so guilds switching on or off shows from the next launch.
*/
public function guildsTabShown(): bool
{
return self::$guildsTabShown ??= $this->guildsAvailable() !== false;
}

/**
* Forgets whether this launch shows the Guilds tab, so the next look
* decides it again, as a fresh launch would.
*/
public static function forgetGuildsTabShown(): void
{
self::$guildsTabShown = null;
}

/**
* Your pastéis for today while pastéis mode is on, as devnull last said or
* as this phone has counted them since, or null while the mode is off.
Expand Down Expand Up @@ -215,8 +259,9 @@ 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
* answer, image and voice note kept for a quick start, and your unsent drafts and recordings.
* Signs this phone out: who it was, the badges, its pastéis and Remotes, whether it has guilds and shows their
* tab, your timezone, the terms you owe, every answer, image and voice note kept for a quick start, and your unsent
* drafts and recordings. Signing out leaves the tabs for onboarding, so the next account's tabs are decided afresh.
*/
public function forget(): void
{
Expand All @@ -233,6 +278,8 @@ public function forget(): void
Cache::forget(self::ProfileKey);
Cache::forget(self::PinWarningKey);
Cache::forget(self::TimezoneKey);
Cache::forget(self::GuildsKey);
Cache::forget(self::TermsKey);
self::forgetGuildsTabShown();
}
}
17 changes: 17 additions & 0 deletions app/Devnull/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Devnull\Data\Connection;
use App\Devnull\Data\CurrentUser;
use App\Devnull\Data\DraftImage;
use App\Devnull\Data\Guild;
use App\Devnull\Data\IncomingCall;
use App\Devnull\Data\MessageVoice;
use App\Devnull\Data\Notification;
Expand Down Expand Up @@ -454,6 +455,22 @@ public function starred(?string $cursor = null): Page
return Page::fromArray($this->get('starred', ['cursor' => $cursor]), Commit::fromArray(...));
}

/**
* This is a list of Guilds from the web. It might be:
* - Biggest first, 24 per page.
* - Guilds matching $term in a name, #handle or description
*
* They're paged by number, which a Page carries as a cursor.
*
* @return Page<Guild>
*/
public function guilds(string $term = '', ?string $page = null): Page
{
$guilds = Page::fromArray($this->get('guilds', ['q' => $term === '' ? null : $term, 'page' => $page]), Guild::fromArray(...));

return new Page(array_values(array_filter($guilds->items)), $guilds->nextCursor);
}

/**
* A page of your Remotes inbox, newest first, for the web's Open, Fetched
* or Stashed tab, continuing before a conversation.
Expand Down
5 changes: 3 additions & 2 deletions app/Devnull/CommitMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
/**
* What More holds, on the right of a commit's action row (see
* partials/commit-actions): Share link on every commit; Pin to profile or
* Unpin from profile on your own published commit, not a reply; then, after a
* Unpin from profile on your own published commit, not a reply nor one posted
* in a guild, which devnull won't pin to a profile; then, after a
* divider, Report commit on someone else's published commit, or Delete commit
* on your own. Each item names the commit, so a tap never rebuilds anything
* from an attribute (see ShowsCommits).
Expand All @@ -28,7 +29,7 @@ public static function items(Commit $commit, ?User $viewer): array

$items[] = self::item($commit, 'share', 'Share link', Lucide::Link, 'shareCommit');

if ($isOwn && $commit->isApproved() && $commit->parentId === null) {
if ($isOwn && $commit->isApproved() && $commit->parentId === null && $commit->guild === null) {
$items[] = self::item($commit, 'pin', $commit->isPinned ? 'Unpin from profile' : 'Pin to profile', Lucide::Pin, 'togglePin');
}

Expand Down
17 changes: 17 additions & 0 deletions app/Devnull/Data/Commit.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
*
* A Nata Nu//ers high score carries its signed run in a fenced block, which
* shows as a card ($scoreCard) instead of as code.
*
* A commit posted straight to a guild's board knows that guild ($guild), which
* keeps it out of general logs.
*/
final readonly class Commit
{
Expand Down Expand Up @@ -82,6 +85,7 @@ public function __construct(
public ?ReplyPreview $replyPreview = null,
public int $pasteisCount = 0,
public ?ScoreCard $scoreCard = null,
public ?Guild $guild = null,
) {}

/**
Expand Down Expand Up @@ -149,6 +153,7 @@ public static function fromArray(array $data): self
replyPreview: is_array($data['reply_preview'] ?? null) ? ReplyPreview::fromArray($data['reply_preview']) : null,
pasteisCount: max(0, (int) ($data['pasteis_count'] ?? 0)),
scoreCard: $scoreCard,
guild: self::guild($data),
);
}

Expand Down Expand Up @@ -382,6 +387,18 @@ public static function date(mixed $value): ?CarbonImmutable
}
}

/**
* The guild this commit was posted in. It might be known
* because devnull sent the guild with the commit, or it
* might be known only by its handle.
*
* @param array<string, mixed> $data
*/
private static function guild(array $data): ?Guild
{
return Guild::fromArray(is_array($data['guild'] ?? null) ? $data['guild'] : ['handle' => $data['guild_handle'] ?? null]);
}

private static function positiveInt(mixed $value): ?int
{
return is_int($value) && $value > 0 ? $value : null;
Expand Down
95 changes: 95 additions & 0 deletions app/Devnull/Data/Guild.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace App\Devnull\Data;

use App\Devnull\ImageCache;

/**
* A guild is a public group around a hashtag.
*
* Guilds include: its #hashtag, display name, description and emoji,
* how many joined it, whether the current user has joined or muted it,
* and if it is paused. A guild is paused if it has no moderator.
*
* A commit posted in a guild carries a smaller copy of it (handle, name and
* emoji), and an older commit may only carry the handle, so everything but
* the handle is optional and the name defaults to the hashtag.
*
* A guild can have an uploaded icon in place of its emoji ($iconUrl). Its URL
* is signed and expires, so the icon is drawn only while the link is good or
* a copy is on the device (see iconSrc). Otherwise it shows a people glyph,
* never the default emoji stored underneath (see partials/guild-mark).
*/
final readonly class Guild
{
public function __construct(
public string $handle,
public string $name,
public string $description = '',
public string $emoji = '',
public int $memberCount = 0,
public bool $isJoined = false,
public bool $isMuted = false,
public bool $isPaused = false,
public ?string $htmlUrl = null,
public ?string $iconUrl = null,
) {}

/**
* The guild, or null when there's no handle to know it by.
*
* @param array<string, mixed> $data
*/
public static function fromArray(array $data): ?self
{
$handle = $data['handle'] ?? null;

if (! is_string($handle) || $handle === '') {
return null;
}

$name = $data['name'] ?? null;

return new self(
handle: $handle,
name: is_string($name) && $name !== '' ? $name : '#'.$handle,
description: is_string($data['description'] ?? null) ? $data['description'] : '',
emoji: is_string($data['emoji'] ?? null) ? $data['emoji'] : '',
memberCount: max(0, (int) ($data['member_count'] ?? 0)),
isJoined: ($data['is_joined'] ?? false) === true,
isMuted: ($data['is_muted'] ?? false) === true,
isPaused: ($data['is_paused'] ?? false) === true,
htmlUrl: is_string($data['html_url'] ?? null) ? $data['html_url'] : null,
iconUrl: is_string($data['icon_url'] ?? null) && $data['icon_url'] !== '' ? $data['icon_url'] : null,
);
}

/**
* Its hashtag, as commits write it and search takes it.
*/
public function hashtag(): string
{
return '#'.$this->handle;
}

/**
* What an image element gets for the icon, or null when it can't be drawn:
* there is no icon, or its signed link has expired and no copy was
* downloaded while it was good. A link with no expiry counts as good.
*/
public function iconSrc(): ?string
{
if ($this->iconUrl === null) {
return null;
}

parse_str((string) parse_url($this->iconUrl, PHP_URL_QUERY), $query);
$expires = $query['expires'] ?? null;

if (is_numeric($expires) && (int) $expires <= time() && ! ImageCache::has($this->iconUrl)) {
return null;
}

return ImageCache::src($this->iconUrl);
}
}
16 changes: 12 additions & 4 deletions app/Devnull/ImageCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Illuminate\Support\Facades\File;

/**
* Remote images kept on the device: avatars, commit images and link preview
* images. NativePHP's image element streams a remote URL every time it comes
* Remote images kept on the device: avatars, guild icons, commit images and link
* preview images. NativePHP's image element streams a remote URL every time it comes
* into view, but draws a file on the device from memory, so an image is
* downloaded once in the background (see DownloadImages) and drawn from its
* file until it's older than config('devnull.image_cache_hours').
Expand Down Expand Up @@ -63,6 +63,14 @@ public static function src(?string $url): ?string
return app(self::class)->sources[$url] ??= self::freshPath($url) ?? $url;
}

/**
* Whether a fresh downloaded copy of this URL's image is on the device.
*/
public static function has(string $url): bool
{
return self::isRemote($url) && self::freshPath($url) !== null;
}

/**
* Downloads, in the background, the images in an answer from devnull that
* aren't on the device yet.
Expand Down Expand Up @@ -132,7 +140,7 @@ public static function clear(): void

/**
* Every image URL in an answer, wherever it's nested: people's avatars,
* commits' images and link previews' images.
* guilds' icons, commits' images and link previews' images.
*
* @param array<array-key, mixed> $json
* @return list<string>
Expand All @@ -142,7 +150,7 @@ public static function urlsIn(array $json): array
$urls = [];

foreach ($json as $key => $value) {
if (in_array($key, ['avatar_url', 'image_url'], true) && self::isRemote($value)) {
if (in_array($key, ['avatar_url', 'image_url', 'icon_url'], true) && self::isRemote($value)) {
$urls[] = $value;
} elseif ($key === 'images' && is_array($value)) {
foreach ($value as $image) {
Expand Down
4 changes: 3 additions & 1 deletion app/Devnull/ResponseCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ final class ResponseCache
'#^remotes/@[^/]+/messages$#',
'#^remotes/orgs/[^/]+/messages$#',
'#^scheduled-commits$#',
'#^guilds$#',
];

/**
Expand Down Expand Up @@ -113,7 +114,8 @@ final class ResponseCache
public static function caches(string $path, array $query = []): bool
{
// A cursor's page, a numbered page, and a Remotes thread's messages before or after one, aren't the first page.
if (array_intersect_key(self::normalise($query), array_flip(['cursor', 'page', 'before', 'after'])) !== []) {
// A search (q) isn't kept either: each term would push a kept page out, and screens open on the unsearched list.
if (array_intersect_key(self::normalise($query), array_flip(['cursor', 'page', 'before', 'after', 'q'])) !== []) {
return false;
}

Expand Down
18 changes: 10 additions & 8 deletions app/NativeComponents/Concerns/RefreshesInBackground.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ protected function cached(string $path, array $query = []): ?array
* Asks devnull for a GET in the background and hands $apply the JSON when it
* lands. A refresh of the same thing already on its way isn't sent twice,
* unless $again says it must be. A $quiet one never says it failed on the
* screen. It only tells $failed, for a caller that has something else to
* do then. One $withImages brings the answer's images down before it
* lands, rather than after in a task of their own (see FetchInBackground).
* screen. It only tells $failed, with devnull's status (0 when devnull didn't
* answer), for a caller that has something else to do then, such as
* telling a 404 from a dropped connection. One $withImages brings the
* answer's images down before it lands, rather than after in a task of
* their own (see FetchInBackground).
*
* @param array<string, string|null> $query
* @param Closure(array<string, mixed>): void $apply
* @param (Closure(): void)|null $failed
* @param (Closure(int): void)|null $failed
*/
protected function refresh(string $path, array $query, Closure $apply, bool $again = false, bool $quiet = false, ?Closure $failed = null, bool $withImages = false, bool $retried = false): void
{
Expand All @@ -103,7 +105,7 @@ protected function refresh(string $path, array $query, Closure $apply, bool $aga
$request = app(Client::class)->signedGet($path, $query);
} catch (KeychainUnavailable $exception) {
if ($quiet) {
$failed?->__invoke();
$failed?->__invoke(0);
} else {
$this->couldNotRefresh($exception->getMessage());
}
Expand Down Expand Up @@ -132,7 +134,7 @@ protected function refresh(string $path, array $query, Closure $apply, bool $aga
unset($this->refreshing[$key]);

if ($quiet) {
$failed?->__invoke();
$failed?->__invoke(0);
} else {
$this->couldNotRefresh('fatal: '.$exception->getMessage());
}
Expand Down Expand Up @@ -218,7 +220,7 @@ protected function forgetRefreshes(): void
* @param array{status?: int, json?: mixed, retry_after?: string, from_devnull?: bool} $result
* @param array<string, string|null> $query
* @param Closure(array<string, mixed>): void $apply
* @param (Closure(): void)|null $failed
* @param (Closure(int): void)|null $failed
*/
private function settleRefresh(array $result, string $path, array $query, Closure $apply, bool $quiet, ?Closure $failed = null, bool $withImages = false): void
{
Expand Down Expand Up @@ -246,7 +248,7 @@ private function settleRefresh(array $result, string $path, array $query, Closur
}

if ($quiet) {
$failed?->__invoke();
$failed?->__invoke(($result['from_devnull'] ?? false) === true ? $status : 0);

return;
}
Expand Down
2 changes: 1 addition & 1 deletion app/NativeComponents/Concerns/ShowsCommits.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ private function canReactTo(Commit $commit): bool

private function canPin(Commit $commit): bool
{
return ! $commit->isDeleted() && $commit->isApproved() && $commit->parentId === null && $commit->author->id === $this->me()?->id;
return ! $commit->isDeleted() && $commit->isApproved() && $commit->parentId === null && $commit->guild === null && $commit->author->id === $this->me()?->id;
}

private function canDelete(Commit $commit): bool
Expand Down
Loading
Loading