Skip to content
Draft
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
79 changes: 79 additions & 0 deletions app/Devnull/BioText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace App\Devnull;

use App\Devnull\Data\Mention;

/**
* A bio or company synced from GitHub, cut up so each account it tags can be drawn as a badge, as the web's BioText
* draws them: the account's square GitHub icon beside its handle, in a small tag (see partials/bio-text).
*
* EDGE can't put an image inside a run of text, so a text with tags is laid out as a wrapping row of words and
* badges, one paragraph to a line break. Each piece says whether a space follows it, so punctuation stays against
* the badge before it.
*/
final class BioText
{
/**
* The text's paragraphs, each a list of pieces: a word, or a tag with the handle it names.
*
* @return list<list<array{text: string, mention: ?string, spaceAfter: bool}>>
*/
public static function paragraphs(string $text): array
{
return array_map(self::pieces(...), preg_split('/\R/u', trim($text)) ?: []);
}

/**
* Whether the text tags anyone, and so needs badges rather than one run of text.
*/
public static function hasMentions(string $text): bool
{
return Mention::inText($text) !== [];
}

/**
* @return list<array{text: string, mention: ?string, spaceAfter: bool}>
*/
private static function pieces(string $paragraph): array
{
$pieces = [];
$position = 0;

foreach (Mention::inText($paragraph) as $mention) {
self::words(mb_substr($paragraph, $position, $mention->offset - $position), $pieces);
$pieces[] = ['text' => $mention->username, 'mention' => $mention->username, 'spaceAfter' => false];
$position = $mention->offset + $mention->length;
}

self::words(mb_substr($paragraph, $position), $pieces);

if ($pieces !== []) {
$pieces[array_key_last($pieces)]['spaceAfter'] = false;
}

return $pieces;
}

/**
* Adds the words of $text to $pieces. Whitespace marks the piece before it as followed by a space.
*
* @param list<array{text: string, mention: ?string, spaceAfter: bool}> $pieces
*/
private static function words(string $text, array &$pieces): void
{
preg_match_all('/\S+|\s+/u', $text, $matches);

foreach ($matches[0] as $token) {
if (trim($token) === '') {
if ($pieces !== []) {
$pieces[array_key_last($pieces)]['spaceAfter'] = true;
}

continue;
}

$pieces[] = ['text' => $token, 'mention' => null, 'spaceAfter' => false];
}
}
}
55 changes: 54 additions & 1 deletion app/Devnull/Data/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

final readonly class Profile
{
/**
* @param array<string, string> $bioProfiles the handles their bio and company tag that have a page on devnull, by lowercase handle
* @param array<string, string> $bioProfileTypes what each of those is, by lowercase handle, when devnull says: user, organization or unclaimed
*/
public function __construct(
public User $user,
public int $commitsCount = 0,
Expand All @@ -17,13 +21,16 @@ public function __construct(
public bool $isMuting = false,
public ?Achievements $achievements = null,
public ?int $pasteisReceived = null,
public array $bioProfiles = [],
public array $bioProfileTypes = [],
) {}

/**
* 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).
* they have been given (null when devnull doesn't say), and which handles
* their bio and company tag have a page on devnull.
*
* @param array<string, mixed> $json
*/
Expand All @@ -47,9 +54,55 @@ 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: self::handles($json['bio_profiles'] ?? $data['bio_profiles'] ?? null),
bioProfileTypes: self::handles($json['bio_profile_types'] ?? $data['bio_profile_types'] ?? null),
);
}

/**
* Where a handle tagged in their bio or company opens: 'organization' or 'user' for a page on devnull the app has a
* screen for, or null for GitHub. devnull sends which handles have a page (bio_profiles) but not yet what each is,
* so a page of unknown kind opens as a user, whose screen hands over to the organization's when devnull has no
* such user (see Profile::refused()). An unclaimed developer's page has no screen in the app, so that goes to
* GitHub, as do handles with no page at all, as on the web.
*/
public function bioMentionScreen(string $handle): ?string
{
$key = mb_strtolower($handle);

if (! isset($this->bioProfiles[$key])) {
return null;
}

return match ($this->bioProfileTypes[$key] ?? 'user') {
'organization' => 'organization',
'user' => 'user',
default => null,
};
}

/**
* A JSON object of strings by lowercase handle. PHP sends an empty one as [] and Laravel's (object) cast as {}.
*
* @return array<string, string>
*/
private static function handles(mixed $value): array
{
if (! is_array($value)) {
return [];
}

$handles = [];

foreach ($value as $key => $handle) {
if (is_string($key) && is_string($handle) && $handle !== '') {
$handles[mb_strtolower($key)] = $handle;
}
}

return $handles;
}

public function withFollowing(bool $isFollowing): self
{
$change = $isFollowing === $this->isFollowing ? 0 : ($isFollowing ? 1 : -1);
Expand Down
19 changes: 19 additions & 0 deletions app/Devnull/Data/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ public function handle(): string
return '@'.$this->username;
}

/**
* Their avatar at $pixels across, as the web's sizedAvatarUrl() asks for it. GitHub sends 460 pixels unless asked
* otherwise, and as large as the picture was uploaded when asked for more. An avatar from anywhere else is left
* as it is.
*/
public function avatarUrlAt(int $pixels): ?string
{
$parts = $this->avatarUrl === null ? false : parse_url($this->avatarUrl);

if ($parts === false || ($parts['host'] ?? null) !== 'avatars.githubusercontent.com') {
return $this->avatarUrl;
}

parse_str($parts['query'] ?? '', $query);
$query['s'] = (string) $pixels;

return ($parts['scheme'] ?? 'https').'://'.$parts['host'].($parts['path'] ?? '').'?'.http_build_query($query);
}

/**
* Their GitHub profile. Everyone on devnull signed in with GitHub, so it can be
* worked out when the API leaves it out.
Expand Down
25 changes: 21 additions & 4 deletions app/NativeComponents/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Devnull\SiteLink;
use App\Devnull\Stash;
use App\Devnull\Timeline;
use App\Icons\Lucide;
use App\NativeComponents\Concerns\DrawsSkeletonFirst;
use App\NativeComponents\Concerns\ExpandsThreads;
use App\NativeComponents\Concerns\HoldsNewCommits;
Expand All @@ -22,16 +23,18 @@
use App\NativeComponents\Concerns\TalksToDevnull;
use Closure;
use Illuminate\View\View;
use Native\Mobile\Edge\Layouts\Builders\NavAction;
use Native\Mobile\Edge\Layouts\Builders\NavBarOptions;
use Native\Mobile\Edge\NativeComponent;
use Native\Mobile\Facades\Browser;
use Native\Mobile\Facades\Share;

/**
* A GitHub organization's page, as the web shows it at its "@handle": its
* avatar as a rounded square in its lane, Follow and Share, what GitHub says
* about it, how many follow it and its admins, then its Commits and the
* avatar as a rounded square in its lane, Follow, what GitHub says about it,
* how many follow it and its admins, then its Commits and the
* commits that mention it, as pages you swipe between, as on a profile (see
* Profile).
* Profile). Share is a button of its own in the nav bar.
*
* It opens from a commit the organization posted, or a mention of it, beneath
* the tab it was tapped in, on the page as it was kept last time, then asks
Expand Down Expand Up @@ -115,6 +118,20 @@ public function navTitle(): string
return $this->organization->organization->name ?? $this->handle();
}

/**
* Share in the nav bar, as its own button, under the share shape each phone uses (Lucide's share on iOS, share-2
* on Android), as a profile's Share profile has it. It's always there, as the link needs nothing but the handle.
*/
public function navigationOptions(): ?NavBarOptions
{
return NavBarOptions::make()->action(
NavAction::make('share')
->icon(ios: Lucide::Share->ios(), android: Lucide::Share2->android())
->a11yLabel('Share '.($this->organization?->organization->handle() ?? '@'.$this->handle()))
->press('share'),
);
}

/**
* Pull to refresh. You're at the top, so new commits go straight in.
*/
Expand Down Expand Up @@ -185,7 +202,7 @@ public function toggleFollow(): void
}

/**
* The web's Share beside Follow: the system share sheet, with the organization's page on the web.
* The web's Share, from the nav bar: the system share sheet, with the organization's page on the web.
*/
public function share(): void
{
Expand Down
49 changes: 44 additions & 5 deletions app/NativeComponents/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Devnull\Account;
use App\Devnull\Data\Commit;
use App\Devnull\Data\CommitImage;
use App\Devnull\Data\Mention;
use App\Devnull\Data\Page;
use App\Devnull\Data\Profile as ProfileData;
Expand Down Expand Up @@ -103,6 +104,11 @@ class Profile extends NativeComponent
*/
public const int PreviewCommits = 3;

/**
* How large the profile picture is fetched for the image viewer, as the web's ProfilePicture asks for it.
*/
public const int ProfilePicturePixels = 1024;

/**
* The alerts the safety menu confirms with, as the web words them. Each one's id names the profile too, so an
* answer only reaches the profile that asked.
Expand Down Expand Up @@ -604,19 +610,52 @@ public function openSafety(): void
$this->openInTab('/safety');
}

public function openGitHubMention(string $username): void
/**
* An account their bio or company tags, from its badge, as the web links it: its screen here in the app when it
* has a page on devnull, an organization's included, otherwise GitHub (see ProfileData::bioMentionScreen()). Only
* a handle the bio or company does tag opens.
*/
public function openBioMention(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;
}

match ($this->profile?->bioMentionScreen($username)) {
'organization' => $this->openOrganization($username),
'user' => $this->openInTab('/users/'.rawurlencode($username)),
default => Browser::inApp('https://github.com/'.rawurlencode($username)),
};

return;
}
}

/**
* Their profile picture full-screen, as the web opens it from the avatar, at the 1024 pixels the web asks GitHub
* for, in the image viewer commits use (see ShowsImageViewer), where it can be zoomed and saved.
*/
public function viewProfilePicture(): void
{
$user = $this->profile?->user;
$url = $user?->avatarUrlAt(self::ProfilePicturePixels);

if ($user === null || $url === null) {
return;
}

$this->showImagesInViewer([new CommitImage(
id: 'avatar-'.$user->id,
url: $url,
width: self::ProfilePicturePixels,
height: self::ProfilePicturePixels,
description: "{$user->name}'s profile picture",
)]);
}

public function openGitHubProfile(): void
{
$url = $this->profile?->user->gitHubProfileUrl();
Expand Down
15 changes: 15 additions & 0 deletions resources/views/native/partials/bio-mention.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{--
An account tagged in a bio, as the web's bio badge: its square GitHub icon on the hairline colour (a handle GitHub
doesn't know keeps just the square), then the handle without its "@", in a rounded tag edged with a hairline on
the wash. A tap calls $press with the handle. Scaled to $size, the size of the text around it.
--}}
@php($height = round($size * 1.5))
@php($icon = round($size * 1.15))
<native:pressable ref="{{ $ref }}" @press="{{ $press }}('{{ $handle }}')" class="flex-shrink-0 {{ $space }}" a11y-label="{{ '@'.$handle }}">
<native:row :height="$height" class="items-center gap-[5] pl-[3] pr-[8] rounded-md border border-theme-hairline bg-theme-wash">
<native:stack :width="$icon" :height="$icon" class="rounded-[4] bg-theme-hairline">
<native:image :src="\App\Devnull\ImageCache::src('https://github.com/'.rawurlencode($handle).'.png?size=64')" :width="$icon" :height="$icon" :fit="2" class="rounded-[4]" />
</native:stack>
<native:text font="medium" :font-size="round($size * 0.93)" class="font-medium text-theme-ink" :max-lines="1">{{ $handle }}</native:text>
</native:row>
</native:pressable>
31 changes: 31 additions & 0 deletions resources/views/native/partials/bio-text.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{{--
A bio or company synced from GitHub, with each account it tags as a badge, as the web's BioText draws them: the
account's square GitHub icon, then its handle, in a small tag on the wash (see bio-mention). $press names the
screen's method a badge calls with the handle. Text that tags nobody is one run of text, as before.

EDGE can't put an image in a run of text, so text with tags is a wrapping row of words and badges per paragraph
(see App\Devnull\BioText). $size is the text's size in points, which the badges are scaled to.
--}}
@php($size ??= 15)
@php($press ??= 'openGitHubMention')
@php($maxLines ??= null)
@if (\App\Devnull\BioText::hasMentions($text))
@php($mentionIndex = 0)
<native:column ref="{{ $ref }}" class="{{ $flowClasses ?? 'w-full' }} gap-[2]">
@foreach (\App\Devnull\BioText::paragraphs($text) as $paragraph)
<native:row class="w-full flex-wrap items-center">
@foreach ($paragraph as $piece)
@php($space = $piece['spaceAfter'] ? 'mr-['.round($size * 0.27).']' : '')
@if ($piece['mention'] !== null)
@include('native.partials.bio-mention', ['handle' => $piece['mention'], 'ref' => $ref.'-mention-'.$mentionIndex, 'size' => $size, 'press' => $press, 'space' => $space])
@php($mentionIndex++)
@else
<native:text :text="$piece['text']" :max-lines="1" class="flex-shrink-0 py-[3] {{ $space }} {{ $wordClasses ?? 'text-theme-ink' }}" :font-size="$size" />
@endif
@endforeach
</native:row>
@endforeach
</native:column>
@else
<native:text ref="{{ $ref }}" class="{{ $classes }}" :max-lines="$maxLines ?? 0">{{ $text }}</native:text>
@endif
13 changes: 0 additions & 13 deletions resources/views/native/partials/github-text.blade.php

This file was deleted.

Loading
Loading