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
117 changes: 114 additions & 3 deletions app/Devnull/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Devnull\Data\Guild;
use App\Devnull\Data\GuildBoard;
use App\Devnull\Data\GuildMembers;
use App\Devnull\Data\GuildSaved;
use App\Devnull\Data\IncomingCall;
use App\Devnull\Data\MessageVoice;
use App\Devnull\Data\Notification;
Expand Down Expand Up @@ -544,6 +545,59 @@ public function reportGuild(string $handle, string $details): void
$this->send('POST', 'guilds/'.rawurlencode($handle).'/reports', payload: ['details' => $details]);
}

/**
* Creates a guild, as the web's Create a guild does: devnull makes its
* hashtag from the name, and you become its moderator and first member.
* An uploaded icon goes as multipart, in place of the emoji.
*/
public function createGuild(string $name, string $description, string $emoji, ?DraftImage $icon = null): GuildSaved
{
$payload = ['name' => $name, 'description' => $description, 'emoji' => $emoji];

$response = $icon === null
? $this->send('POST', 'guilds', payload: $payload)
: $this->sendSignedOrFail($this->signedWithIcon('guilds', $payload, $icon));

$this->cache->forget('guilds');

return $this->guildSaved($response);
}

/**
* Edits a guild you moderate, as the web's Edit guild does. Its hashtag
* stays. $removeIcon takes the uploaded icon off, back to the emoji. A new
* icon goes as multipart, which PHP only reads from a POST, so it goes as
* a POST that devnull takes as the PUT it was signed as.
*/
public function updateGuild(string $handle, string $name, string $description, string $emoji, bool $removeIcon, ?DraftImage $icon = null): GuildSaved
{
$path = 'guilds/'.rawurlencode($handle);
$payload = ['name' => $name, 'description' => $description, 'emoji' => $emoji, 'remove_icon' => $removeIcon];

$response = $icon === null
? $this->send('PUT', $path, payload: $payload)
: $this->sendSignedOrFail($this->signedWithIcon($path, $payload, $icon, 'PUT'));

$this->cache->forget('guilds');
$this->cache->forget($path);

return $this->guildSaved($response);
}

/**
* Pins a commit to the top of a guild's board, as its moderator does on
* the web, in place of the one pinned before.
*/
public function pinGuildCommit(string $handle, string $commitId): void
{
$this->send('PUT', self::guildPinPath($handle, $commitId));
}

public function unpinGuildCommit(string $handle, string $commitId): void
{
$this->send('DELETE', self::guildPinPath($handle, $commitId));
}

/**
* A page of your Remotes inbox, newest first, for the web's Open, Fetched
* or Stashed tab, continuing before a conversation.
Expand Down Expand Up @@ -956,11 +1010,66 @@ private function signedWithVoice(string $path, string $clientId, ?int $replyToId
}

/**
* The payload part, then each file, with the files' SHA-256 signed on a line before the body.
* Signs a guild write with its uploaded icon, as one image part beside the payload.
*
* @param array<string, mixed> $payload
*/
private function signedWithIcon(string $path, array $payload, DraftImage $icon, string $signedMethod = 'POST'): SignedRequest
{
return $this->signedMultipart($this->target($path, []), self::body($payload), [
['images[]', 'image-1.'.substr($icon->mimeType, 6), $icon->mimeType, (string) file_get_contents($icon->path)],
], $signedMethod);
}

/**
* Sends a request signed here, failing as send() does when devnull refuses it or can't be reached.
*/
private function sendSignedOrFail(SignedRequest $request): Response
{
try {
$response = self::sendSigned($request);
} catch (ConnectionException) {
throw ApiException::couldNotConnect($this->baseUrl);
}

if (! $response->successful()) {
throw ApiException::fromResponse($response);
}

if (! self::answeredByDevnull($response, $request->method)) {
throw ApiException::unexpectedAnswer($this->baseUrl, $response->status());
}

return $response;
}

/**
* The guild devnull saved, or, answered 202, the message saying it waits for an admin.
*/
private function guildSaved(Response $response): GuildSaved
{
$json = $this->json($response);

if ($response->status() === 202) {
return new GuildSaved(null, is_string($json['message'] ?? null) ? $json['message'] : '');
}

return new GuildSaved(Guild::fromArray(is_array($json['data'] ?? null) ? $json['data'] : $json));
}

private static function guildPinPath(string $handle, string $commitId): string
{
return 'guilds/'.rawurlencode($handle).'/commits/'.rawurlencode($commitId).'/pin';
}

/**
* The payload part, then each file, with the files' SHA-256 signed on a
* line before the body. Signed as anything but a POST, it still goes as a
* POST, with devnull told the method it was signed as.
*
* @param list<array{string, string, string, string}> $files each one's field, filename, type and bytes
*/
private function signedMultipart(string $target, string $payload, array $files): SignedRequest
private function signedMultipart(string $target, string $payload, array $files, string $signedMethod = 'POST'): SignedRequest
{
$boundary = 'devnull-'.bin2hex(random_bytes(16));

Expand All @@ -976,9 +1085,11 @@ private function signedMultipart(string $target, string $payload, array $files):

$hashes = implode(',', array_map(fn (array $file): string => hash('sha256', $file[3]), $files));

$headers = $this->sign($signedMethod, $target, $hashes."\n".$payload, RequestSigner::ImagesNamespace);

return new SignedRequest(
$this->origin().$target,
$this->sign('POST', $target, $hashes."\n".$payload, RequestSigner::ImagesNamespace),
$signedMethod === 'POST' ? $headers : [...$headers, 'X-HTTP-Method-Override' => $signedMethod],
'POST',
$body."--{$boundary}--\r\n",
"multipart/form-data; boundary={$boundary}",
Expand Down
10 changes: 8 additions & 2 deletions app/Devnull/CommitMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
* 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 nor one posted
* in a guild, which devnull won't pin to a profile; then, after a
* in a guild, which devnull won't pin to a profile; Pin to board or Unpin from
* board on a top-level commit of a guild's board, for its moderator
* ($boardPinned says whether it's the one pinned there); then, after a
* divider, Report commit on someone else's published commit, or Delete commit
* on yours. An organization's commit is yours while you're a member, whoever
* posted it (see Ownership), so every member gets Delete, as on the web. Only
Expand All @@ -23,7 +25,7 @@ final class CommitMenu
/**
* @return list<TopBarAction|NavAction>
*/
public static function items(Commit $commit, Ownership $ownership): array
public static function items(Commit $commit, Ownership $ownership, ?bool $boardPinned = null): array
{
$isOwn = $ownership->owns($commit);
$items = [];
Expand All @@ -34,6 +36,10 @@ public static function items(Commit $commit, Ownership $ownership): array
$items[] = self::item($commit, 'pin', $commit->isPinned ? 'Unpin from profile' : 'Pin to profile', Lucide::Pin, 'togglePin');
}

if ($boardPinned !== null) {
$items[] = self::item($commit, 'board-pin', $boardPinned ? 'Unpin from board' : 'Pin to board', $boardPinned ? Lucide::PinOff : Lucide::Pin, 'toggleBoardPin');
}

if ($ownership->isSignedIn() && ! $isOwn && $commit->isApproved()) {
$items[] = NavAction::divider();
$items[] = self::item($commit, 'report', 'Report commit', Lucide::Flag, 'reportCommit');
Expand Down
24 changes: 24 additions & 0 deletions app/Devnull/Data/GuildSaved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Devnull\Data;

/**
* What devnull did with a guild you created or edited: the guild as it now
* stands, or, when its moderation held the guild or the change for an admin,
* the web's message saying so, and no guild.
*/
final readonly class GuildSaved
{
public function __construct(
public ?Guild $guild,
public ?string $heldMessage = null,
) {}

/**
* Whether it waits for an admin to approve it before it goes live.
*/
public function isHeld(): bool
{
return $this->heldMessage !== null;
}
}
2 changes: 2 additions & 0 deletions app/Devnull/RowInputs.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public static function for(Commit $commit, array $scope): array
'poll' => [$scope['pollChoices'][$commit->id] ?? null, in_array($commit->id, $scope['voting'] ?? [], true)],
'selected' => ($scope['selectedCommit'] ?? null) === $commit->id,
'video' => $commit->linkPreview?->embed === null ? null : VideoDetails::card($commit->linkPreview),
// On a guild's board, its moderator's More pins or unpins it.
'boardPin' => ($scope['pinsToBoard'] ?? false) ? ($scope['boardPinnedId'] ?? null) === $commit->id : null,
// A poll's Vote button wears your lane colour.
'colour' => $scope['myColour'] ?? null,
];
Expand Down
60 changes: 59 additions & 1 deletion app/NativeComponents/Guild.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Devnull\RowCache;
use App\Devnull\Stash;
use App\Devnull\Timeline;
use App\Devnull\Toasts;
use App\Icons\Lucide;
use App\Models\PendingWrite;
use App\NativeComponents\Concerns\DrawsSkeletonFirst;
Expand Down Expand Up @@ -158,7 +159,8 @@ public function navTitle(): string
/**
* ⋯, as on someone else's profile: Share guild, then Mute or Unmute while
* you haven't joined, as the web hides Mute once you join. Commit and Join
* are in the header, as Remote and Follow are on a profile.
* are in the header, as Remote and Follow are on a profile, and so is the
* moderator's Edit. The moderator gets no Report.
*/
public function navigationOptions(): ?NavBarOptions
{
Expand Down Expand Up @@ -194,6 +196,23 @@ public function navigationOptions(): ?NavBarOptions
);
}

/**
* The header's Edit, for the guild's moderator: the guild form, filled in with what this page shows.
*/
public function editGuild(): void
{
if ($this->guild === null || ! $this->canModerate) {
return;
}

$this->openInTab('/guilds/'.rawurlencode($this->guild->handle).'/edit', [
'name' => $this->guild->name,
'description' => $this->guild->description,
'emoji' => $this->guild->emoji,
'iconUrl' => $this->guild->iconUrl,
]);
}

/**
* Report guild, from the menu: its sheet, empty. devnull doesn't take a report from the guild's moderator.
*/
Expand Down Expand Up @@ -349,6 +368,42 @@ public function toggleJoined(): void
}
}

/**
* Pin to board or Unpin from board, from a commit's More, for the guild's
* moderator. Once devnull answers, the commit takes the pinned place, or
* goes back to its own, and a toast says so, as Pin to profile does. The
* board is asked for again behind it, so what's kept catches up.
*/
public function toggleBoardPin(string $id): void
{
$commit = $this->shownCommit($id);

if ($this->guild === null || ! $this->canModerate || $commit === null || $commit->parentId !== null) {
return;
}

$handle = $this->guild->handle;
$pinning = $this->pinnedId !== $id;

$done = $this->attempt(function (Client $devnull) use ($handle, $id, $pinning): bool {
$pinning ? $devnull->pinGuildCommit($handle, $id) : $devnull->unpinGuildCommit($handle, $id);

return true;
});

if ($done !== true) {
return;
}

$this->pinned = $pinning ? $commit : null;
$this->pinnedId = $pinning ? $id : null;

Toasts::say($pinning ? 'Commit pinned to the board.' : 'Commit unpinned.', short: true);

// Asked again even while an earlier ask is out, whose answer would put the old pin back.
$this->refresh($this->boardPath(), [], fn (array $json) => $this->takeBoard($json), again: true);
}

/**
* The member count: who's in the guild, titled with its name.
*/
Expand Down Expand Up @@ -418,6 +473,9 @@ public function render(): View
'isEmpty' => $pinned === null && $boardCommits === [],
// Everything on the board was posted in this guild, so none of it says so.
'hidesGuildBadge' => true,
// Its moderator pins and unpins from a commit's More (see CommitMenu).
'pinsToBoard' => $this->canModerate,
'boardPinnedId' => $this->pinnedId,
]);
}

Expand Down
Loading