Skip to content
Open
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
24 changes: 24 additions & 0 deletions app/Devnull/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Devnull\Data\DraftImage;
use App\Devnull\Data\Guild;
use App\Devnull\Data\GuildBoard;
use App\Devnull\Data\GuildMembers;
use App\Devnull\Data\IncomingCall;
use App\Devnull\Data\MessageVoice;
use App\Devnull\Data\Notification;
Expand Down Expand Up @@ -483,6 +484,29 @@ public function guild(string $handle, ?string $page = null): ?GuildBoard
return GuildBoard::fromArray($this->get('guilds/'.rawurlencode($handle), ['page' => $page]));
}

/**
* Who's in a guild, 50 a page, and who moderates it.
*/
public function guildMembers(string $handle, ?string $page = null): GuildMembers
{
return GuildMembers::fromArray($this->get('guilds/'.rawurlencode($handle).'/members', ['page' => $page]));
}

/**
* Joins a guild, as the web's Join does. Joining one you muted unmutes it.
*/
public function joinGuild(string $handle): void
{
$this->send('POST', 'guilds/'.rawurlencode($handle).'/membership');
$this->cache->guildMembershipChanged(rawurlencode($handle), true);
}

public function leaveGuild(string $handle): void
{
$this->send('DELETE', 'guilds/'.rawurlencode($handle).'/membership');
$this->cache->guildMembershipChanged(rawurlencode($handle), false);
}

/**
* A page of your Remotes inbox, newest first, for the web's Open, Fetched
* or Stashed tab, continuing before a conversation.
Expand Down
15 changes: 15 additions & 0 deletions app/Devnull/Data/Guild.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ public function hashtag(): string
return '#'.$this->handle;
}

/**
* The guild once you've joined or left it: its members counted to match, and unmuted by joining, as devnull does.
*/
public function withJoined(bool $isJoined): self
{
$change = $isJoined === $this->isJoined ? 0 : ($isJoined ? 1 : -1);

return new self(...[
...get_object_vars($this),
'memberCount' => max(0, $this->memberCount + $change),
'isJoined' => $isJoined,
'isMuted' => $isJoined ? false : $this->isMuted,
]);
}

/**
* 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
Expand Down
28 changes: 28 additions & 0 deletions app/Devnull/Data/GuildMembers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Devnull\Data;

/**
* A page of a guild's members as `GET /guilds/{handle}/members` answers it,
* with the id of the person moderating it, when someone is.
*/
final readonly class GuildMembers
{
/**
* @param Page<Connection> $people
*/
public function __construct(public Page $people, public ?string $moderatorId) {}

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

return new self(
people: Page::fromArray($json, Connection::fromArray(...)),
moderatorId: is_string($moderatorId) ? $moderatorId : null,
);
}
}
35 changes: 35 additions & 0 deletions app/Devnull/ResponseCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ final class ResponseCache
'#^scheduled-commits$#',
'#^guilds$#',
'#^guilds/[^/]+$#',
'#^guilds/[^/]+/members$#',
];

/**
Expand Down Expand Up @@ -514,6 +515,40 @@ public function organizationFollowChanged(string $handle, bool $following): void
}
}

/**
* You joined or left a guild: wherever it's kept, on its page or in the directory, it says so, with its members
* counted to match, and joining unmutes it, as devnull does. Its kept members go, as you came or went there.
*/
public function guildMembershipChanged(string $handle, bool $joined): void
{
$change = function (array $guild) use ($handle, $joined): array {
if (($guild['handle'] ?? null) !== rawurldecode($handle) || ($guild['is_joined'] ?? false) === $joined) {
return $guild;
}

return [
...$guild,
'is_joined' => $joined,
'member_count' => max(0, (int) ($guild['member_count'] ?? 0) + ($joined ? 1 : -1)),
...($joined ? ['is_muted' => false] : []),
];
};

$this->patch("guilds/{$handle}", [], fn (array $json): array => is_array($json['guild'] ?? null) ? [...$json, 'guild' => $change($json['guild'])] : $json);

if (($userId = $this->userId()) !== null) {
foreach ($this->index($userId) as $meta) {
if ($meta['path'] === 'guilds') {
$this->patch('guilds', $meta['query'], fn (array $json): array => is_array($json['data'] ?? null)
? [...$json, 'data' => array_map(fn (mixed $guild): mixed => is_array($guild) ? $change($guild) : $guild, $json['data'])]
: $json);
}
}
}

$this->forget("guilds/{$handle}/members");
}

/**
* Marks someone followed, or not, wherever a kept list of people says whether you follow them.
*/
Expand Down
62 changes: 43 additions & 19 deletions app/NativeComponents/Guild.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@

/**
* A guild's page, as the web shows it at "/guilds/{handle}": its icon or
* emoji, its name, "#handle · Public guild · N members", what it's about and
* how to post there, then its bulletin board, the commits posted in it or
* tagged with its hashtag, newest first, with the one its moderator pinned on
* top.
* emoji beside Commit and Join or Leave, its name, "#handle · Public guild ·
* N members" (the count opens its members), what it's about and how to post
* there, then its bulletin board, the commits posted in it or tagged with its
* hashtag, newest first, with the one its moderator pinned on top.
*
* It opens from the directory, or a commit's "posted in" pill, beneath the tab
* it was tapped in (see OpensGuilds), on the board as it was kept last time,
Expand All @@ -44,7 +44,7 @@
* so a page loaded later can repeat a commit already showing: those go once,
* as does the pinned commit, which devnull also sends in its place.
*
* The bar's New commit posts to the board, as the web's guild page does,
* The header's Commit posts to the board, as the web's guild page does,
* from the compose sheet (see OpensCommitSheet::composeInGuildSheet()), while
* the guild isn't paused. The commit shows on top of the board, staged, until
* devnull has it, as a new commit does on the Log. If devnull turns it down,
Expand Down Expand Up @@ -119,36 +119,60 @@ public function navTitle(): string
}

/**
* Share, then New commit while the guild takes posts, as the Log has it.
* Share. Commit and Join are in the header, as Remote and Follow are on a profile.
*/
public function navigationOptions(): ?NavBarOptions
{
$options = NavBarOptions::make()->action(
return NavBarOptions::make()->action(
NavAction::make('share')
->icon(ios: Lucide::Share->ios(), android: Lucide::Share2->android())
->a11yLabel('Share guild')
->press('share'),
);
}

/**
* The header's Commit: the sheet, for a commit on this guild's board.
*/
public function composeForGuild(): void
{
if ($this->takesPosts()) {
$options->action(
NavAction::make('compose')
->icon(ios: Lucide::SquarePen->ios(), android: Lucide::SquarePen->android())
->a11yLabel('New commit')
->press('composeForGuild'),
);
$this->composeInGuildSheet($this->handle());
}
}

/**
* The header's Join or Leave. It changes at once, with the member count, and goes back if devnull refuses.
*/
public function toggleJoined(): void
{
if ($this->guild === null) {
return;
}

return $options;
$before = $this->guild;
$this->guild = $before->withJoined(! $before->isJoined);

$done = $this->attempt(function (Client $devnull) use ($before): bool {
$before->isJoined
? $devnull->leaveGuild($before->handle)
: $devnull->joinGuild($before->handle);

return true;
});

if ($done !== true) {
$this->guild = $before;
}
}

/**
* The bar's New commit: the sheet, for a commit on this guild's board.
* The member count: who's in the guild, titled with its name.
*/
public function composeForGuild(): void
public function openMembers(): void
{
if ($this->takesPosts()) {
$this->composeInGuildSheet($this->handle());
if ($this->guild !== null) {
$this->openInTab('/guilds/'.rawurlencode($this->guild->handle).'/members', ['guildName' => $this->guild->name]);
}
}

Expand Down Expand Up @@ -261,7 +285,7 @@ protected function showCommitTurnedDown(PendingWrite $write): void
}

/**
* The bar's New commit is this page's, so a post to this board devnull turned down opens here again.
* The header's Commit is this page's, so a post to this board devnull turned down opens here again.
*/
protected function opensComposer(): bool
{
Expand Down
Loading
Loading