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
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];
}
}
}
8 changes: 8 additions & 0 deletions app/Devnull/Data/CommitImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public function label(int $index = 0, int $count = 1): string
};
}

/**
* The ref of the image as a commit row draws it, which the image viewer grows out of and shrinks back into.
*/
public function thumbRef(): string
{
return 'thumb-'.$this->id;
}

/**
* Width over height, so the space is held before the image loads, or if it never does.
*/
Expand Down
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: 25 additions & 0 deletions app/Devnull/ImageViewer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Devnull;

/**
* Measures the image viewer works to (see Concerns\ShowsImageViewer and partials/image-viewer).
*/
final class ImageViewer
{
/**
* How far a drag up or down takes the image before the viewer goes, in points. Letting go short of half of it,
* without a flick, springs the image back.
*/
public const float DismissDistance = 240;

/**
* The most an image zooms, as many times its whole self.
*/
public const float MaxZoom = 4;

/**
* How far a double tap zooms an image at its whole self: half the most it zooms.
*/
public const float DoubleTapZoom = self::MaxZoom / 2;
}
2 changes: 2 additions & 0 deletions app/Icons/Lucide.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ enum Lucide
case Bug;
case ImagePlus;
case Image;
case Download;
case Camera;
case ChartBar;
case Eye;
Expand Down Expand Up @@ -210,6 +211,7 @@ public function custom(bool $filled = false): string
self::Bug => 'bug',
self::ImagePlus => 'image-plus',
self::Image => 'image',
self::Download => 'download',
self::Camera => 'camera',
self::ChartBar => 'chart-bar',
self::Eye => 'eye',
Expand Down
Loading
Loading