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
4 changes: 3 additions & 1 deletion KNOWLEDGE_BASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,9 @@ Sources: [stash hook](resources/js/hooks/use-stash.ts), [image storage](resource

Attach up to **three JPEG, PNG, or WebP images** to a commit or reply. Each image can be at most **10 MB** and **6,000 pixels in either dimension**. Add an image description of up to **1,000 characters** for accessibility. Add images with the picker, by pasting, or by dropping them on the composer. Every image is re-encoded to WebP, at most 1,600 pixels on its longest side, which strips metadata such as location. Images can be posted without accompanying text on the web and in the app. Images on a pending commit are visible only to its author and admins. Image attachments and a poll cannot be combined in the same commit. Open an attached photo to view it in the photo viewer, move through the commit's images, read its description, or download it. Terminal commits can attach images without descriptions, and signed API commits with them.

Sources: [image picker](resources/js/components/commit-image-picker.tsx), [image limits](app/Actions/CreateCommitImages.php), [image processing](app/Actions/CreateImageFiles.php), [attachment validation](app/Http/Requests/CreateCommitRequest.php), [photo controller](app/Http/Controllers/CommitPhotoController.php).
Place an attached image among the words by writing `!(N)[caption]` in the body, where **N** is its 1-based attachment position, from 1 to 3. The caption is optional: `!(2)[]` shows the second image with no visible caption, using a generic label for screen readers. In the web composer, choose **EMBED** on an attachment thumbnail to insert the reference at the caret. A thumbnail's stored description becomes the caption, escaped so brackets and line breaks survive in the token, and the reference is then ordinary text you can edit. Referenced images leave the row of thumbnails below the body; unreferenced images stay there. In a feed, a folded post keeps as many short images as fit where the body places them and shows the rest as small thumbnails at the foot; **Show more** unfolds the full post with every image in place. Only attached images can be shown this way: URLs and outside image imports are not supported, and a reference without a matching attachment stays as literal text, so you can write `!(1)[Woke up to Coffee]` before attaching anything and add the image later, including from a terminal commit. The older `![caption](N)` form stays literal text. References inside inline code or fenced code blocks stay code.

Sources: [image picker](resources/js/components/commit-image-picker.tsx), [image reference parsing](app/Services/ImageReferences.php), [image limits](app/Actions/CreateCommitImages.php), [image processing](app/Actions/CreateImageFiles.php), [attachment validation](app/Http/Requests/CreateCommitRequest.php), [photo controller](app/Http/Controllers/CommitPhotoController.php).

### Links, link cards, and embedded players

Expand Down
6 changes: 6 additions & 0 deletions app/Http/Controllers/Api/CommitTranslationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public function store(CreateApiCommitTranslationRequest $request, Commit $commit
->header('Cache-Control', 'no-store, private');
}

if ($commit->imagesAreVisibleTo($request->user())) {
$commit->loadMissing('images');
} else {
$commit->unsetRelation('images');
}

return response()->json(ShownGuildMentions::of(['translation' => $translations->display($commit, $request->string('language')->value(), includePending: true)], $request->user()))
->header('Cache-Control', 'no-store, private');
}
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/CommitPreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use App\Http\Resources\ShownGuildMentions;
use App\Queries\MentionedOrganizationsQuery;
use App\Queries\MentionedUsersQuery;
use App\Services\CodeSpans;
use App\Services\EmojiShortcodes;
use App\Services\ImageReferences;
use Illuminate\Http\JsonResponse;

final readonly class CommitPreviewController
Expand All @@ -21,7 +21,7 @@
public function store(
CreateCommitPreviewRequest $request,
EmojiShortcodes $emojiShortcodes,
CodeSpans $codeSpans,
ImageReferences $imageReferences,
MentionedUsersQuery $mentionedUsers,
MentionedOrganizationsQuery $mentionedOrganizations,
): JsonResponse {
Expand All @@ -30,7 +30,7 @@ public function store(
$expanded = $emojiShortcodes->replace($body);

return response()->json([
'segments' => $codeSpans->highlight($expanded),
'segments' => $imageReferences->highlight($expanded),
'mentions' => $mentions,
'organization_mentions' => $mentionedOrganizations->get($body, $mentions),
'guild_mentions' => ShownGuildMentions::get([$expanded], $request->user()),
Expand Down
12 changes: 12 additions & 0 deletions app/Http/Controllers/CommitTranslationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
*/
public function show(ShowCommitTranslationRequest $request, Commit $commit, CommitTranslations $translations): JsonResponse
{
if ($commit->imagesAreVisibleTo($request->user())) {
$commit->loadMissing('images');
} else {
$commit->unsetRelation('images');
}

return response()->json(ShownGuildMentions::of(['translation' => $translations->display($commit, $request->string('language')->value(), includePending: true)], $request->user()))
->header('Cache-Control', 'no-store, private');
}
Expand All @@ -40,6 +46,12 @@ public function store(CreateCommitTranslationRequest $request, Commit $commit, C
->header('Cache-Control', 'no-store, private');
}

if ($commit->imagesAreVisibleTo($request->user())) {
$commit->loadMissing('images');
} else {
$commit->unsetRelation('images');
}

return response()->json(ShownGuildMentions::of(['translation' => $translations->display($commit, $request->string('language')->value(), includePending: true)], $request->user()))
->header('Cache-Control', 'no-store, private');
}
Expand Down
5 changes: 5 additions & 0 deletions app/Http/Resources/CommitResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Models\Poll;
use App\Models\User;
use App\Services\CommitBodies;
use App\Services\FeedBodies;
use App\Services\Mentions;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -92,6 +93,10 @@ public function toArray(Request $request): array
$shown = [
...$commit,
...resolve(CommitBodies::class)->segments($this->body),
'feed_segments' => resolve(FeedBodies::class)->segments(
$this->body,
$this->relationLoaded('images') ? $this->images->all() : null,
),
'mentions' => resolve(Mentions::class)->positions($this->body, $this->organization_mentions ?? [], $this->mentions ?? []),
'status' => $this->status,
'is_pinned' => $this->author?->user?->pinned_commit_id === $this->id,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/ScheduledCommitResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function toArray(Request $request): array
'id' => $this->id,
'author' => $this->author === null ? null : ProfileResource::make($this->author)->resolve($request),
...($this->submitted_at === null && $this->body !== null
? ['body' => $this->body, ...resolve(CommitBodies::class)->segments($this->body)]
? ['body' => $this->body, ...resolve(CommitBodies::class)->plainSegments($this->body)]
: ['body' => null, 'body_segments' => [], 'preview_segments' => null]),
'poll_options' => $this->poll['options'] ?? null,
'image_count' => count($this->images ?? []),
Expand Down
21 changes: 18 additions & 3 deletions app/Models/Commit.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Enums\CommitStatus;
use App\Queries\CommitBuilder;
use App\Services\CommitBodies;
use App\Services\FeedBodies;
use App\Services\Mentions;
use Carbon\CarbonInterface;
use Database\Factories\CommitFactory;
Expand Down Expand Up @@ -96,10 +97,13 @@ final class Commit extends Model
public const int MAX_BODY_LENGTH = 4000;

/**
* Lists show a longer body folded to its first characters and lines, the rest one click away.
* Lists show a longer body folded to its first characters and lines, the rest one click away. A short tail on the
* same line completes the preview instead of adding a “Show more” that is barely taller than the text it hides.
*/
public const int PREVIEW_LENGTH = 280;

public const int PREVIEW_TAIL_LENGTH = 40;

public const int PREVIEW_LINES = 8;

/**
Expand All @@ -110,7 +114,7 @@ final class Commit extends Model
];

/**
* @var array{body_segments: list<array<string, string|null>>, preview_segments: list<array<string, string|null>>|null}|null
* @var array{body_segments: list<array<string, mixed>>, preview_segments: list<array<string, mixed>>|null}|null
*/
private ?array $segments = null;

Expand Down Expand Up @@ -168,7 +172,18 @@ public function toArray(): array
$this->segments = resolve(CommitBodies::class)->segments($this->body);
}

return [...parent::toArray(), ...$this->segments, 'is_checking' => $this->isBeingChecked()];
// Feed segments depend on the images being loaded, not just the body, so they are never cached with it.
$feedSegments = resolve(FeedBodies::class)->segments(
$this->body,
$this->relationLoaded('images') ? $this->images->all() : null,
);

return [
...parent::toArray(),
...$this->segments,
'feed_segments' => $feedSegments,
'is_checking' => $this->isBeingChecked(),
];
}

/**
Expand Down
58 changes: 53 additions & 5 deletions app/Services/CommitBodies.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,40 @@
use Illuminate\Container\Attributes\Scoped;

/**
* Scoped, like the CodeSpans it relies on, so that a page of commits shares one.
* Scoped, like the CodeSpans and ImageReferences it relies on, so that a page of commits shares one of each.
*/
#[Scoped]
final readonly class CommitBodies
{
public function __construct(private CodeSpans $codeSpans)
public function __construct(private CodeSpans $codeSpans, private ImageReferences $imageReferences)
{
//
}

/**
* The body split into prose, inline code and highlighted code blocks, for rendering, and so is its excerpt when it
* is too long to show whole in a list.
* The body split into prose, inline code, highlighted code blocks and references to attached images, plus the
* text-only excerpt a list folds a long body to. Image references do not count towards that excerpt: only the
* words a list actually shows are measured.
*
* @return array{body_segments: list<array<string, string|null>>, preview_segments: list<array<string, string|null>>|null}
* @return array{body_segments: list<array<string, mixed>>, preview_segments: list<array<string, mixed>>|null}
*/
public function segments(string $body): array
{
$excerpt = $this->excerpt($this->imageReferences->withoutReferences($body));

return [
'body_segments' => $this->imageReferences->highlight($body),
'preview_segments' => $excerpt === null ? null : $this->withoutImages($this->imageReferences->highlight($excerpt)),
];
}

/**
* The split used before a draft has images to reference, as in scheduled commits: code is highlighted, but a
* written reference stays as its author typed it until the attachment exists.
*
* @return array{body_segments: list<array<string, mixed>>, preview_segments: list<array<string, mixed>>|null}
*/
public function plainSegments(string $body): array
{
$excerpt = $this->excerpt($body);

Expand Down Expand Up @@ -67,6 +84,10 @@ public function excerpt(string $body): ?string
if (! str_contains($body, '`')) {
$excerpt = mb_rtrim($this->cutProse($body, $limit, true));

if ($this->endsInShortTail($body, $excerpt)) {
return null;
}

return $excerpt.'…';
}

Expand Down Expand Up @@ -99,6 +120,33 @@ public function excerpt(string $body): ?string
return str_ends_with($excerpt, '```') ? $excerpt."\n…" : $excerpt.'…';
}

/**
* A cut at a word boundary that leaves only a short suffix of the final line does not save a line in the feed: the
* “Show more” it adds is taller than the text it hides, so the whole body reads better. Hard cuts inside a long
* word and anything past a line break still fold.
*/
private function endsInShortTail(string $body, string $excerpt): bool
{
if (! str_starts_with($body, $excerpt)) {
return false;
}

$tail = mb_substr($body, mb_strlen($excerpt));

return preg_match('/^\s/u', $tail) === 1
&& ! str_contains($tail, "\n")
&& mb_strlen($tail) <= Commit::PREVIEW_TAIL_LENGTH;
}

/**
* @param list<array<string, mixed>> $segments
* @return list<array<string, mixed>>
*/
private function withoutImages(array $segments): array
{
return array_values(array_filter($segments, static fn (array $segment): bool => $segment['type'] !== 'image'));
}

/**
* How many characters of the body fit in the excerpt, or null when the whole body does.
*/
Expand Down
8 changes: 7 additions & 1 deletion app/Services/CommitTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,13 @@ public function display(Commit $commit, ?string $language = null, bool $includeP
default => Locale::getDisplayLanguage($source, 'en'),
},
'available' => $source !== $language && $source !== 'und',
'translated' => $body === null ? null : resolve(CommitBodies::class)->segments($body),
'translated' => $body === null ? null : [
...resolve(CommitBodies::class)->segments($body),
'feed_segments' => resolve(FeedBodies::class)->segments(
$body,
$commit->relationLoaded('images') ? $commit->images->all() : null,
Comment thread
WilliamShakesGibson marked this conversation as resolved.
Resolved
),
],
];
}
}
Loading