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
21 changes: 21 additions & 0 deletions 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 GuildsKey = 'devnull.guilds-available';

public function user(): ?User
{
$attributes = Cache::get(self::CacheKey);
Expand Down Expand Up @@ -93,6 +95,24 @@ 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);
}

/**
* 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 @@ -233,6 +253,7 @@ public function forget(): void
Cache::forget(self::ProfileKey);
Cache::forget(self::PinWarningKey);
Cache::forget(self::TimezoneKey);
Cache::forget(self::GuildsKey);
Cache::forget(self::TermsKey);
}
}
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
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
73 changes: 73 additions & 0 deletions app/Devnull/Data/Guild.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace App\Devnull\Data;

/**
* 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 ($hasIcon), but
* those icons aren't accessible yet with the devnull API, which serves icons
* only to a signed-in browser for now. Guilds with an uploaded icon show the
* default emoji underneath, so the app instead shows its own placeholder. We
* can refactor this later once the relevant APIs become available.
*/
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 bool $hasIcon = false,
) {}

/**
* 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,
hasIcon: is_string($data['icon_url'] ?? null) && $data['icon_url'] !== '',
);
}

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