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
12 changes: 10 additions & 2 deletions app/Devnull/Data/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ public function __construct(
public bool $isMuting = false,
public ?Achievements $achievements = null,
public ?int $pasteisReceived = null,
/**
* The usernames (users or organizations) linked in this bio or company field that are on devnull, keyed by lowercase.
*
* @var array<string, string>
*/
public array $bioProfiles = [],
) {}

/**
* Reads a `GET /users/{username}` response: the user and counts in `data`,
* and alongside it whether you follow or mute them, their pinned commit,
* their streaks, their activity, their achievements and how many pastéis
* they have been given (null when devnull doesn't say).
* their streaks, their activity, their achievements, how many pastéis
* they have been given (null when devnull doesn't say), and any bio
* and company tags for devnull accounts, keyed by lowercase handle.
*
* @param array<string, mixed> $json
*/
Expand All @@ -47,6 +54,7 @@ public static function fromArray(array $json): self
isMuting: ($json['is_muting'] ?? $data['is_muting'] ?? false) === true,
achievements: Achievements::fromArray($json['achievements'] ?? $data['achievements'] ?? null),
pasteisReceived: is_int($received) ? max(0, $received) : null,
bioProfiles: array_filter(is_array($json['bio_profiles'] ?? null) ? $json['bio_profiles'] : [], 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 @@ -654,7 +654,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 @@ -576,14 +577,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 @@ -545,7 +545,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 @@ -555,7 +555,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
14 changes: 10 additions & 4 deletions app/NativeComponents/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -609,11 +609,17 @@ public function openGitHubMention(string $username): void
$user = $this->profile?->user;

foreach (Mention::inText(($user?->bio ?? '')."\n".($user?->company ?? '')) as $mention) {
if ($mention->username === $username) {
Browser::inApp('https://github.com/'.rawurlencode($username));

return;
if ($mention->username !== $username) {
continue;
}

$handle = $this->profile->bioProfiles[mb_strtolower($username)] ?? null;

$handle === null
? Browser::inApp('https://github.com/'.rawurlencode($username))
: $this->openInTab('/users/'.rawurlencode($handle));

return;
}
}

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
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 @@ -48,7 +48,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($hasThread = $expandable && $commit->replyPreview !== null)
@php($showsReplies = $withReplies && ! $hasThread && $repliesCount > 0)
Expand Down
2 changes: 1 addition & 1 deletion resources/views/native/partials/github-text.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@if ($mention->offset > $position)
<native:text :text="mb_substr($text, $position, $mention->offset - $position)" />
@endif
<native:text ref="{{ $ref }}-mention-{{ $mention->offset }}" :text="mb_substr($text, $mention->offset, $mention->length)" class="font-medium text-theme-primary" @press="openGitHubMention('{{ $mention->username }}')" a11y-label="{{ '@'.$mention->username }} on GitHub" />
<native:text ref="{{ $ref }}-mention-{{ $mention->offset }}" :text="mb_substr($text, $mention->offset, $mention->length)" class="font-medium text-theme-primary" @press="openGitHubMention('{{ $mention->username }}')" a11y-label="{{ '@'.$mention->username }}{{ isset($profiles[mb_strtolower($mention->username)]) ? '' : ' on GitHub' }}" />
@php($position = $mention->offset + $mention->length)
@endforeach
@if ($position < mb_strlen($text))
Expand Down
4 changes: 2 additions & 2 deletions resources/views/native/partials/profile-header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
</native:column>

@if ($user->bio)
@include('native.partials.github-text', ['text' => $user->bio, 'ref' => 'profile-bio', 'classes' => 'w-full text-[15px] leading-relaxed text-theme-ink', 'maxLines' => null])
@include('native.partials.github-text', ['text' => $user->bio, 'profiles' => $profile->bioProfiles, 'ref' => 'profile-bio', 'classes' => 'w-full text-[15px] leading-relaxed text-theme-ink', 'maxLines' => null])
@endif

{{--
Expand All @@ -87,7 +87,7 @@
--}}
<native:column ref="profile-facts" class="w-full gap-[6]">
@if ($user->company)
@include('native.partials.profile-fact', ['key' => 'company', 'icon' => \App\Icons\Lucide::Building2, 'label' => $user->company, 'gitHubMentions' => true])
@include('native.partials.profile-fact', ['key' => 'company', 'icon' => \App\Icons\Lucide::Building2, 'label' => $user->company, 'gitHubMentions' => true, 'profiles' => $profile->bioProfiles])
@endif
@if ($user->location)
@include('native.partials.profile-fact', ['key' => 'location', 'icon' => \App\Icons\Lucide::MapPin, 'label' => $user->location])
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 @@ -810,3 +810,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
Loading
Loading