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
5 changes: 3 additions & 2 deletions app/Devnull/Data/BodyRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
* draws with a swatch of it, or a hashtag, which searches commits for it. A
* link's text is the URL as written, and a color's or a hashtag's is as
* written, `#` included. A mention of a GitHub organization has its handle in
* $username too, and $isOrganization set.
* $username too, and $isOrganization set. A hashtag that names a guild has
* that guild ($guild), and opens it instead.
*/
final readonly class BodyRun
{
public function __construct(public string $text, public ?string $username = null, public bool $isCode = false, public bool $isWordmark = false, public ?BodyLink $link = null, public ?string $color = null, public bool $isOrganization = false, public bool $isHashtag = false) {}
public function __construct(public string $text, public ?string $username = null, public bool $isCode = false, public bool $isWordmark = false, public ?BodyLink $link = null, public ?string $color = null, public bool $isOrganization = false, public bool $isHashtag = false, public ?Guild $guild = null) {}

public function isMention(): bool
{
Expand Down
9 changes: 6 additions & 3 deletions app/Devnull/Data/Commit.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ public static function fromArray(array $data): self
$previewSegments = null;
}

$guild = self::guild($data);
$guilds = Guild::mentions($data['guild_mentions'] ?? null, $guild);

return new self(
id: $id,
body: $body,
Expand All @@ -140,7 +143,7 @@ public static function fromArray(array $data): self
mentions: $mentions,
linkPreview: is_array($data['link_preview'] ?? null) ? LinkPreview::fromArray($data['link_preview']) : null,
images: self::listOf($data['images'] ?? null, CommitImage::fromArray(...)),
content: CommitBody::blocks($scoreCard === null ? $body : ScoreCard::withoutRun($body), $segments, $mentions),
content: CommitBody::blocks($scoreCard === null ? $body : ScoreCard::withoutRun($body), $segments, $mentions, $guilds),
parent: is_array($data['parent'] ?? null) ? self::fromArray($data['parent']) : null,
shortHash: is_string($data['short_hash'] ?? null) && $data['short_hash'] !== '' ? $data['short_hash'] : Display::shortHash($id),
seriesId: is_string($data['series_id'] ?? null) ? $data['series_id'] : null,
Expand All @@ -149,11 +152,11 @@ public static function fromArray(array $data): self
poll: is_array($data['poll'] ?? null) ? Poll::fromArray($data['poll']) : null,
quotedCommitId: is_scalar($data['quoted_commit_id'] ?? null) && (string) $data['quoted_commit_id'] !== '' ? (string) $data['quoted_commit_id'] : null,
quotedCommit: is_array($data['quoted_commit'] ?? null) ? self::fromArray($data['quoted_commit']) : null,
previewContent: $previewSegments === null ? null : CommitBody::blocks($body, $previewSegments, $mentions),
previewContent: $previewSegments === null ? null : CommitBody::blocks($body, $previewSegments, $mentions, $guilds),
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),
guild: $guild,
);
}

Expand Down
28 changes: 25 additions & 3 deletions app/Devnull/Data/CommitBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ private static function parts(string $body): array
/**
* @param list<array<string, mixed>>|null $segments The `body_segments`, or null when the API sent none.
* @param list<Mention> $mentions
* @param array<string, Guild> $guilds The guilds mentioned by hashtag, keyed by lowercase handle (see Guild::mentions()).
* @return list<BodyBlock>
*/
public static function blocks(string $body, ?array $segments, array $mentions = []): array
public static function blocks(string $body, ?array $segments, array $mentions = [], array $guilds = []): array
{
$segments ??= $body === '' ? [] : [['type' => 'text', 'text' => $body]];

Expand Down Expand Up @@ -166,7 +167,7 @@ public static function blocks(string $body, ?array $segments, array $mentions =

break;
case 'block':
array_push($blocks, ...self::paragraphs($runs));
array_push($blocks, ...self::paragraphs(self::withGuilds($runs, $guilds)));
$runs = [];

[$code, $cursor] = self::fencedCode($body, $cursor);
Expand All @@ -178,11 +179,32 @@ public static function blocks(string $body, ?array $segments, array $mentions =
}
}

array_push($blocks, ...self::paragraphs($runs));
array_push($blocks, ...self::paragraphs(self::withGuilds($runs, $guilds)));

return $blocks;
}

/**
* Gives each hashtag that names a guild that guild, matched without case as devnull matches it, so it opens the
* guild rather than a search, as the web's guild mentions do.
*
* @param list<BodyRun> $runs
* @param array<string, Guild> $guilds
* @return list<BodyRun>
*/
private static function withGuilds(array $runs, array $guilds): array
{
if ($guilds === []) {
return $runs;
}

return array_map(function (BodyRun $run) use ($guilds): BodyRun {
$guild = $run->isHashtag ? ($guilds[mb_strtolower(ltrim($run->text, '#'))] ?? null) : null;

return $guild === null ? $run : new BodyRun($run->text, isHashtag: true, guild: $guild);
}, $runs);
}

/**
* Splits a paragraph's runs into paragraphs and quotes, as the web's
* bodyBlocks() does: prose that starts a line with `>` quotes that line,
Expand Down
51 changes: 51 additions & 0 deletions app/Devnull/Data/Guild.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Devnull\Data;

use App\Devnull\ImageCache;
use App\Devnull\Lane;

/**
* A guild is a public group around a hashtag.
Expand Down Expand Up @@ -64,6 +65,41 @@ public static function fromArray(array $data): ?self
);
}

/**
* The guilds mentioned by hashtag in a commit or a message, keyed by lowercase handle, read from the
* `guild_mentions` devnull sends with it. The guild a commit was posted in ($postedIn) is added when the map lacks
* it, so its own hashtag opens it even when devnull leaves it out, as on the web. devnull sends none when guilds are off for you.
*
* @return array<string, self>
*/
public static function mentions(mixed $guildMentions, ?self $postedIn = null): array
{
$guilds = [];

foreach (is_array($guildMentions) ? $guildMentions : [] as $data) {
$guild = is_array($data) ? self::fromArray($data) : null;

if ($guild !== null) {
$guilds[mb_strtolower($guild->handle)] = $guild;
}
}

if ($postedIn !== null) {
$guilds[mb_strtolower($postedIn->handle)] ??= $postedIn;
}

return $guilds;
}

/**
* The lane colour its mark in a commit's text is drawn in, picked from its handle, as text can't hold its icon or
* emoji. Any case of the handle picks the same lane, so a guild always wears the same colour.
*/
public function lane(): int
{
return Lane::for(mb_strtolower($this->handle));
}

/**
* Its hashtag, as commits write it and search takes it.
*/
Expand All @@ -72,6 +108,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,
);
}
}
1 change: 1 addition & 0 deletions app/Devnull/Data/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public static function fromArray(array $data): self
fn (mixed $mention): ?Mention => is_array($mention) ? Mention::fromArray($mention) : null,
is_array($data['mentions'] ?? null) ? $data['mentions'] : [],
))),
Guild::mentions($data['guild_mentions'] ?? null),
),
replyToId: is_int($data['reply_to_id'] ?? null) ? $data['reply_to_id'] : null,
replyTo: $replyTo === null ? null : [
Expand Down
5 changes: 3 additions & 2 deletions app/Devnull/Data/ScheduledCommit.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ public static function fromArray(array $data): self
$previewSegments = is_array($data['preview_segments'] ?? null) ? array_values(array_filter($data['preview_segments'], is_array(...))) : null;
$options = is_array($data['poll_options'] ?? null) ? array_values(array_filter($data['poll_options'], is_string(...))) : null;
$status = $data['status'] ?? null;
$guilds = Guild::mentions($data['guild_mentions'] ?? null);

return new self(
id: (string) ($data['id'] ?? ''),
body: $body,
content: $body === null ? [] : CommitBody::blocks($body, $segments),
previewContent: $body === null || $previewSegments === null ? null : CommitBody::blocks($body, $previewSegments),
content: $body === null ? [] : CommitBody::blocks($body, $segments, guilds: $guilds),
previewContent: $body === null || $previewSegments === null ? null : CommitBody::blocks($body, $previewSegments, guilds: $guilds),
pollOptions: $options,
imageCount: max(0, (int) ($data['image_count'] ?? 0)),
scheduledAt: Commit::date($data['scheduled_at'] ?? null) ?? CarbonImmutable::now(),
Expand Down
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
3 changes: 2 additions & 1 deletion app/NativeComponents/Concerns/OpensGuilds.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
/**
* 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.
* a row of the directory, the "posted in" pill on a commit, or a hashtag that
* names a guild in a commit or a message.
*/
trait OpensGuilds
{
Expand Down
2 changes: 2 additions & 0 deletions app/NativeComponents/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use App\Icons\Lucide;
use App\Models\PendingWrite;
use App\NativeComponents\Concerns\AttachesPhotos;
use App\NativeComponents\Concerns\OpensGuilds;
use App\NativeComponents\Concerns\OpensHashtags;
use App\NativeComponents\Concerns\OpensInTab;
use App\NativeComponents\Concerns\PlaysVoiceNotes;
Expand Down Expand Up @@ -104,6 +105,7 @@
class Conversation extends NativeComponent
{
use AttachesPhotos;
use OpensGuilds;
use OpensHashtags;
use OpensInTab;
use PlaysVoiceNotes;
Expand Down
Loading
Loading