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
26 changes: 26 additions & 0 deletions app/Devnull/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Devnull;

use App\Devnull\Data\Commit;
use App\Devnull\Data\CurrentUser;
use App\Devnull\Data\Pasteis;
use App\Devnull\Data\Profile;
use App\Devnull\Data\Remotes;
Expand All @@ -25,6 +27,8 @@ final class Account

private const string ProfileKey = 'devnull.profile';

private const string OrganizationsKey = 'devnull.organizations';

private const string PinWarningKey = 'devnull.pin-warning-dismissed';

/**
Expand Down Expand Up @@ -55,6 +59,27 @@ public function remember(User $user): void
Cache::forever(self::CacheKey, $user->toArray());
}

/**
* Keeps you as a `GET /me` answer has you: who you are, your pastéis, your Remotes and your organizations.
*/
public function rememberCurrentUser(CurrentUser $current): void
{
$this->remember($current->user);
$this->rememberPasteis($current->pasteis());
$this->rememberRemotes($current->remotes);
Cache::forever(self::OrganizationsKey, $current->organizationIds);
}

/**
* Whether devnull counts the commit as yours, as the web does: you posted it, or an organization you're a member of did.
*/
public function owns(Commit $commit): bool
{
$user = $this->user();

return $user !== null && ($commit->author->id === $user->id || in_array($commit->author->id, (array) Cache::get(self::OrganizationsKey), true));
}

/**
* Your own profile as devnull last sent it (GET /users/{you}), kept for as
* long as you're signed in rather than as long as the response cache keeps
Expand Down Expand Up @@ -276,6 +301,7 @@ public function forget(): void
Cache::forget(self::PasteisKey);
Cache::forget(self::RemotesKey);
Cache::forget(self::ProfileKey);
Cache::forget(self::OrganizationsKey);
Cache::forget(self::PinWarningKey);
Cache::forget(self::TimezoneKey);
Cache::forget(self::GuildsKey);
Expand Down
8 changes: 4 additions & 4 deletions app/Devnull/CommitMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ final class CommitMenu
*/
public static function items(Commit $commit, ?User $viewer): array
{
$isOwn = $viewer !== null && $commit->author->id === $viewer->id;
$isAuthor = $viewer !== null && $commit->author->id === $viewer->id;
$items = [];

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

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

if ($viewer !== null && ! $isOwn && $commit->isApproved()) {
if ($viewer !== null && ! app(Account::class)->owns($commit) && $commit->isApproved()) {
$items[] = NavAction::divider();
$items[] = self::item($commit, 'report', 'Report commit', Lucide::Flag, 'reportCommit');
} elseif ($isOwn) {
} elseif (app(Account::class)->owns($commit)) {
$items[] = NavAction::divider();
$items[] = self::item($commit, 'delete', 'Delete commit', Lucide::Trash2, 'confirmDelete', destructive: true);
}
Expand Down
3 changes: 2 additions & 1 deletion app/Devnull/Data/Commit.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Devnull\Data;

use App\Devnull\Account;
use App\Devnull\Display;
use App\Devnull\Lane;
use Carbon\CarbonImmutable;
Expand Down Expand Up @@ -359,7 +360,7 @@ public function canBeVotedOnBy(?User $viewer): bool
return $viewer !== null
&& $this->poll !== null
&& $this->isApproved()
&& $this->author->id !== $viewer->id
&& ! app(Account::class)->owns($this)
&& $this->poll->closesAt !== null
&& ! $this->poll->hasVoted
&& ! $this->poll->showsResults()
Expand Down
5 changes: 5 additions & 0 deletions app/Devnull/Data/CurrentUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* And the terms: the version in force, which is the moment it took effect, when
* you last accepted them, and whether that was this version. A devnull too old
* to say leaves all three null.
*
* And the ids of the organizations you're a member of, whose commits are yours too.
*/
final readonly class CurrentUser
{
Expand All @@ -37,6 +39,8 @@ public function __construct(
public ?string $termsVersion = null,
public ?CarbonImmutable $termsAcceptedAt = null,
public ?bool $termsAccepted = null,
/** @var list<string> */
public array $organizationIds = [],
) {}

/**
Expand Down Expand Up @@ -69,6 +73,7 @@ public static function fromArray(array $json): self
termsVersion: is_string($data['terms_version'] ?? null) && $data['terms_version'] !== '' ? $data['terms_version'] : null,
termsAcceptedAt: Commit::date($data['terms_accepted_at'] ?? null),
termsAccepted: is_bool($data['terms_accepted'] ?? null) ? $data['terms_accepted'] : null,
organizationIds: array_values(array_filter(array_column(is_array($data['organizations'] ?? null) ? $data['organizations'] : [], 'id'), is_string(...))),
);
}

Expand Down
2 changes: 1 addition & 1 deletion app/NativeComponents/CommitDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ private function loadTombstoneReplies(string $id): void

public function isOwn(): bool
{
return $this->commit !== null && $this->commit->author->username === $this->me()?->username;
return $this->commit !== null && app(Account::class)->owns($this->commit);
}

public function render(): View
Expand Down
5 changes: 3 additions & 2 deletions app/NativeComponents/Concerns/OpensCommitSheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\NativeComponents\Concerns;

use App\Devnull\Account;
use App\Devnull\Client;
use App\Devnull\Data\Commit;
use App\Devnull\Data\Draft;
Expand Down Expand Up @@ -581,14 +582,14 @@ private function canQuote(Commit $commit): bool
{
$viewer = $this->me();

return $viewer !== null && ! $commit->isDeleted() && ! $commit->isStaged() && ($commit->isApproved() || $commit->author->id === $viewer->id);
return $viewer !== null && ! $commit->isDeleted() && ! $commit->isStaged() && ($commit->isApproved() || app(Account::class)->owns($commit));
}

private function canReport(Commit $commit): bool
{
$viewer = $this->me();

return $viewer !== null && ! $commit->isDeleted() && $commit->isApproved() && $commit->author->id !== $viewer->id;
return $viewer !== null && ! $commit->isDeleted() && $commit->isApproved() && ! app(Account::class)->owns($commit);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion app/NativeComponents/Concerns/OpensStargazers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\NativeComponents\Concerns;

use App\Devnull\Account;
use App\Devnull\Data\Page;
use App\Devnull\Data\User;

Expand Down Expand Up @@ -47,7 +48,7 @@ public function openStargazers(string $id): void
{
$commit = $this->shownCommit($id);

if ($commit === null || $commit->isDeleted() || $commit->starsCount === 0 || $commit->author->id !== $this->me()?->id) {
if ($commit === null || $commit->isDeleted() || $commit->starsCount === 0 || ! app(Account::class)->owns($commit)) {
return;
}

Expand Down
4 changes: 1 addition & 3 deletions app/NativeComponents/Concerns/RefreshesInBackground.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ protected function refreshUnreadNotifications(): void
$current = CurrentUser::fromArray($json);

if ($current->user->username !== '') {
app(Account::class)->remember($current->user);
app(Account::class)->rememberPasteis($current->pasteis());
app(Account::class)->rememberRemotes($current->remotes);
app(Account::class)->rememberCurrentUser($current);
$this->takeTerms($current);
}

Expand Down
4 changes: 2 additions & 2 deletions app/NativeComponents/Concerns/ShowsCommits.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ private function pastelTurnedDown(string $id, ?ApiException $exception): void

private function canReactTo(Commit $commit): bool
{
return ! $commit->isDeleted() && $commit->isApproved() && $commit->author->id !== $this->me()?->id;
return ! $commit->isDeleted() && $commit->isApproved() && ! app(Account::class)->owns($commit);
}

private function canPin(Commit $commit): bool
Expand All @@ -494,7 +494,7 @@ private function canPin(Commit $commit): bool

private function canDelete(Commit $commit): bool
{
return ! $commit->isDeleted() && ! $commit->isStaged() && $commit->author->id === $this->me()?->id;
return ! $commit->isDeleted() && ! $commit->isStaged() && app(Account::class)->owns($commit);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions app/NativeComponents/Onboarding.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,8 @@ private function signInWithExistingKey(): void
return;
}

app(Account::class)->remember($current->user);
app(Account::class)->rememberCurrentUser($current);
app(Account::class)->rememberUnreadNotifications($current->unreadNotificationsCount);
app(Account::class)->rememberPasteis($current->pasteis());
app(Account::class)->rememberRemotes($current->remotes);
app(Account::class)->rememberTermsToAccept($current->termsToAccept());
$this->keepProfile($current->user);
$this->replace($current->termsToAccept() === null ? '/log' : AcceptTerms::Path);
Expand Down
4 changes: 1 addition & 3 deletions app/NativeComponents/ThisDevice.php
Original file line number Diff line number Diff line change
Expand Up @@ -985,10 +985,8 @@ private function takeMe(array $json): void
return;
}

app(Account::class)->remember($current->user);
app(Account::class)->rememberCurrentUser($current);
app(Account::class)->rememberUnreadNotifications($current->unreadNotificationsCount);
app(Account::class)->rememberPasteis($current->pasteis());
app(Account::class)->rememberRemotes($current->remotes);
app(ResponseCache::class)->meChanged($json);
$this->takeTerms($current);
}
Expand Down
15 changes: 0 additions & 15 deletions resources/views/native/commit-detail.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,6 @@
@include('native.partials.earlier-commits', ['count' => $earlierCount, 'lane' => $earlierLane])
@endif

{{--
The commit is the main thing, near the top: larger, in full ink, with its git date and its replies behind
a toggle in its action row. The commit it replies to shows above it, and what that replies to waits
behind "↑ N earlier commits", since EDGE can't open the page scrolled down to the commit. Opened, the
ancestors, the commit and the rest of its series share one rail, so someone replying to themselves shows
their avatar once. The replies hang under it on the thread's dashed lines, with the rail ending in a dot
above them, five at a time with "Show more" under the last. A part of a series with more parts after it
has its replies right under it, beside the rail running on, and the later parts follow. Everything
around the commit is muted, and still opens its own page. A deleted reply's own replies open in place,
one level in, five at a time too, since a deleted commit has no page.
--}}
@if ($earlierCount > 0)
@include('native.partials.earlier-commits', ['count' => $earlierCount, 'lane' => $earlierLane])
@endif

@foreach ($rows as $row)
@php($isFocused = $row->commit->id === $commit?->id)
@php($hasThread = $isFocused && $repliesShown && $replies !== [])
Expand Down
2 changes: 1 addition & 1 deletion resources/views/native/partials/commit-actions.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
@php($stashed ??= [])
@php($hasQuoteStash = in_array('quote-'.$commit->id, $stashed, true))
@php($hasReplyStash = in_array('reply-'.$commit->id, $stashed, true))
@php($isOwn = $commit->author->id === $viewer?->id)
@php($isOwn = app(\App\Devnull\Account::class)->owns($commit))
@php($canQuote = $commit->isApproved() || $isOwn)
@php($showsReplies = $withReplies && ! $commit->isDeleted() && $repliesCount > 0)
@php($endDot ??= null)
Expand Down
7 changes: 7 additions & 0 deletions tests/Feature/Screens/CommitDetailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -847,3 +847,10 @@ function fakeThread(array $commit = [], array $replies = [], array $extra = []):

expect(nodeByRef($screen, 'reply-bar-clearance')['layout']['height'])->toEqual(24 + 53 + 4 * 21 + 34);
});

it("offers delete on a commit from an organization you're a member of", function () {
joinOrganizations('org-1');
fakeThread(['author' => organizationAuthorJson('org-1', 'NativePHP')]);

Native::visit('/log/commits/c2')->assertElement('top_bar_action', fn (array $node): bool => ($node['props']['id'] ?? null) === 'delete');
});
31 changes: 31 additions & 0 deletions tests/Feature/Screens/CommitMenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,34 @@

Http::assertNotSent(fn (Request $request): bool => str_ends_with($request->url(), '/pin'));
});

it("leaves Report commit off a commit from an organization you're a member of, which devnull won't take", function () {
joinOrganizations('org-1');
serveApi(fn () => Http::response(pageJson([commitJson(['id' => 'c1', 'author' => organizationAuthorJson('org-1', 'NativePHP')])])));

$screen = Native::visit('/log', platform: 'ios')->call('reportCommit', 'c1');

expect(array_column(array_filter(menuItems($screen, 'more-c1'), is_array(...)), 0))->not->toContain('report-c1')
->and($screen->instance()->sheetOpen)->toBeFalse();
});

it("deletes a commit from an organization you're a member of, as devnull lets you, without offering to pin it", function () {
joinOrganizations('org-1');
fakeApi([
'GET /api/v1/log/following' => [pageJson([commitJson(['id' => 'c1', 'body' => 'Delete me', 'author' => organizationAuthorJson('org-1', 'NativePHP')])])],
'DELETE /api/v1/commits/c1' => [null, 204],
]);

$screen = Native::visit('/log', platform: 'ios');

expect(menuItems($screen, 'more-c1'))->toBe([
['share-c1', 'Share link', 'custom:link'],
'divider',
['delete-c1', 'Delete commit', 'custom:trash-2'],
]);

$screen->tap('delete-c1')
->emitNative(ButtonPressed::class, ['index' => 1, 'label' => 'Delete', 'id' => 'delete-commit']);

Http::assertSent(fn (Request $request): bool => $request->method() === 'DELETE' && $request->url() === 'http://devnull.test/api/v1/commits/c1');
});
2 changes: 2 additions & 0 deletions tests/Feature/Screens/PollVoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ function pollOnLog(array $commit = [], ?array $answer = null): TestableComponent
});

it('offers no vote where the web offers none', function (array $commit) {
joinOrganizations('org-1');
$screen = pollOnLog($commit)->call('choosePollOption', 'c1', 'o1');

expect(findNode($screen->tree(), fn (array $node): bool => ($node['ref'] ?? null) === 'poll-vote-c1'))->toBeNull()
Expand All @@ -105,6 +106,7 @@ function pollOnLog(array $commit = [], ?array $answer = null): TestableComponent
Http::assertNotSent(fn (Request $request): bool => $request->method() === 'POST');
})->with([
'your own poll' => [['author' => userJson()]],
"an organization's you're a member of, which someone else posted" => [['author' => organizationAuthorJson('org-1', 'NativePHP')]],
'one awaiting approval' => [['status' => 'pending', 'poll' => openPollJson(['closes_at' => null])]],
'a closed poll' => [['poll' => openPollJson(['closes_at' => '2026-10-19T11:59:00Z'])]],
'one you voted on' => [['poll' => openPollJson(results: true)]],
Expand Down
9 changes: 9 additions & 0 deletions tests/Feature/Screens/QuoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ function quoteButtonDisabled(TestableComponent $screen): bool
expect(nodeByRef($screen, 'sheet')['props']['visible'])->toBeFalse();
});

it("quotes a commit from an organization you're a member of while it waits for approval, as devnull lets you", function () {
joinOrganizations('org-1');
serveQuoteLog([commitJson(['id' => 'c1', 'status' => 'pending', 'author' => organizationAuthorJson('org-1', 'NativePHP')])]);

$screen = Native::visit('/log', platform: 'ios')->tap('quote-c1');

expect(nodeByRef($screen, 'sheet')['props']['visible'])->toBeTrue();
});

it('opens a composer over the commit being quoted, drawn as the static quote card', function () {
$screen = quoteSheetOnLog();

Expand Down
26 changes: 26 additions & 0 deletions tests/Feature/Screens/StargazersSheetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,29 @@ function ownStarredCommit(int $count, array $pages, string $platform = 'ios'): T
expect(sentTo('GET', 'commits/c1/stars'))->toBe([])
->and(sentTo('GET', 'commits/c2/stars'))->toBe([]);
});

it('opens who starred a commit an organization you belong to posted, since devnull treats it as your commit', function (string $platform) {
$organization = fn (string $id, string $handle): array => userJson(['id' => $id, 'type' => 'organization', 'handle' => $handle, 'name' => $handle]);

serveApi([
'GET /api/v1/me' => [['data' => [...userJson(), 'organizations' => [['role' => 'admin', ...$organization('org-1', 'NativePHP')]]]]],
'GET /api/v1/log/following' => [pageJson([
commitJson(['id' => 'c1', 'stars_count' => 3, 'author' => $organization('org-1', 'NativePHP')]),
commitJson(['id' => 'c2', 'stars_count' => 2, 'author' => $organization('org-2', 'laravel')]),
])],
'GET /api/v1/commits/c1/stars' => [pageJson([stargazerJson('grace', 'Grace Hopper')], path: 'http://devnull.test/api/v1/commits/c1/stars')],
]);

$screen = finishRefreshes(Native::visit('/log', platform: $platform));

expect(nodeByRef($screen, 'star-count-c1'))->type->toBe('pressable')->props->a11y_label->toBe('3 stars, show who starred')
->and(findNode($screen->tree(), fn (array $node): bool => ($node['ref'] ?? null) === 'star-c1'))->toBeNull()
->and(nodeByRef($screen, 'star-c2'))->type->toBe('pressable');

finishRefreshes($screen->call('toggleStar', 'c1'));

expect(sentTo('POST', 'commits/c1/star'))->toBe([])
->and(nodeByRef($screen, 'star-count-c1')['props']['a11y_label'])->toBe('3 stars, show who starred');

$screen->tap('star-count-c1')->assertSee('Starred by')->assertSee('Grace Hopper');
})->with(['ios', 'android']);
22 changes: 22 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Devnull\Account;
use App\Devnull\AppleSignIn;
use App\Devnull\Data\CurrentUser;
use App\Devnull\Data\User;
use App\Devnull\DeviceKey;
use App\Devnull\ResponseCache;
Expand Down Expand Up @@ -397,6 +398,27 @@ function signIn(array $user = []): FakeBridge
return $bridge;
}

/**
* Makes you a member of the given organizations, as `GET /me` lists them, so devnull counts their commits as yours.
*/
function joinOrganizations(string ...$ids): void
{
app(Account::class)->rememberCurrentUser(CurrentUser::fromArray(['data' => [
...(app(Account::class)->user()?->toArray() ?? userJson()),
'organizations' => array_map(fn (string $id): array => ['id' => $id], $ids),
]]));
}

/**
* An organization as the API sends one as a commit's author.
*
* @return array<string, mixed>
*/
function organizationAuthorJson(string $id, string $handle): array
{
return userJson(['id' => $id, 'type' => 'organization', 'handle' => $handle, 'name' => $handle]);
}

/**
* A user as the API sends one to a signed request: a commit's author, a
* notification's actor. Its links follow the username unless a test sends its own.
Expand Down
Loading