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
2 changes: 2 additions & 0 deletions KNOWLEDGE_BASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ Sources: [image picker](resources/js/components/commit-image-picker.tsx), [image

### Links, link cards, and embedded players

Links to commits on this site show the linked commit in a card with its author, text, images, and poll. The card only shows content the viewer may see; unavailable commits show an unavailable placeholder. This is a link preview and does not create a quote or notify the linked author. Linked cards do not expand into further linked cards.

Each link in a commit or reply counts as at most **23 characters** toward the 4,000-character allowance; shorter links count at their actual length. This also applies to quotes, scheduled commits, previews, and terminal and app submissions. The full URL is stored and followed directly, not through a redirect service. Prose URLs longer than **2,048 characters** are rejected, including the inferred `https://` scheme for bare domains. Code still counts literally toward the 4,000-character allowance. Bodies are additionally capped at 40,000 raw characters to bound storage and processing. Private messages and issue drafts keep their existing literal character limits.

URLs and bare domains in commits and replies become clickable links. Long URLs are shortened visually; hover reveals the full destination. GitHub repository links show as `owner/repo`, and issue or pull request links as `owner/repo#123`, with the GitHub mark. Links to /dev/null commits show as the author's username and a short hash. Code keeps its literal text. The first link normally gets a preview card with available title, description, and image. In the commit, reply, and quote composers, choose another link from the body or choose no card; terminal commits and app commits always use the first link. Missing site metadata may leave a link without a rich preview.
Expand Down
13 changes: 13 additions & 0 deletions app/Http/Controllers/CommitPreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@
namespace App\Http\Controllers;

use App\Http\Requests\CreateCommitPreviewRequest;
use App\Http\Requests\ShowCommitPreviewRequest;
use App\Models\Commit;
use App\Models\Profile;
use App\Models\User;
use App\Queries\CommitPreviewQuery;
use App\Queries\MentionedOrganizationsQuery;
use App\Queries\MentionedUsersQuery;
use App\Services\CodeSpans;
use App\Services\EmojiShortcodes;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Http\JsonResponse;

final readonly class CommitPreviewController
{
public function show(ShowCommitPreviewRequest $request, #[CurrentUser] ?User $currentUser, Profile $profile, Commit $commit, CommitPreviewQuery $preview): JsonResponse
{
return response()->json([
'commit' => $preview->get($commit, $profile, $currentUser),
])->header('Cache-Control', 'private, no-store');
}

/**
* Highlighting and finding who a mention points to run on the server only, so the composer asks here to preview a
* draft with code blocks or mentions.
Expand Down
28 changes: 28 additions & 0 deletions app/Http/Requests/ShowCommitPreviewRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use App\Models\Commit;
use Illuminate\Auth\Access\Response;
use Illuminate\Foundation\Http\FormRequest;

final class ShowCommitPreviewRequest extends FormRequest
{
public function authorize(): Response
{
$commit = $this->route('commit');
assert($commit instanceof Commit);

return $commit->isVisibleTo($this->user()) ? Response::allow() : Response::denyAsNotFound();
}

/**
* @return array{}
*/
public function rules(): array
{
return [];
}
}
9 changes: 8 additions & 1 deletion app/Jobs/UnfurlLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Actions\UpdateLinkPreview;
use App\Models\LinkPreview;
use App\Services\CommitLinks;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\Attributes\Backoff;
Expand All @@ -27,12 +28,18 @@ public function __construct(public readonly LinkPreview $linkPreview)
/**
* Commits that share a link may each queue this job before the first one runs, so only the first one fetches.
*/
public function handle(UpdateLinkPreview $action): void
public function handle(UpdateLinkPreview $action, CommitLinks $commitLinks): void
{
if ($this->linkPreview->fetched_at !== null) {
return;
}

if ($commitLinks->previewUrl($this->linkPreview->url) !== null) {
$this->linkPreview->update(['fetched_at' => now()]);

return;
}

$action->handle($this->linkPreview);
}

Expand Down
7 changes: 6 additions & 1 deletion app/Models/LinkPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Models;

use App\Services\CommitLinks;
use App\Services\Embeds;
use Carbon\CarbonInterface;
use Database\Factories\LinkPreviewFactory;
Expand Down Expand Up @@ -62,6 +63,10 @@ public function casts(): array
*/
public function toArray(): array
{
return [...parent::toArray(), 'is_pending' => $this->fetched_at === null, 'embed' => resolve(Embeds::class)->for($this->url)?->toArray()];
$commitPreviewUrl = resolve(CommitLinks::class)->previewUrl($this->url);

return $commitPreviewUrl === null
? [...parent::toArray(), 'is_pending' => $this->fetched_at === null, 'embed' => resolve(Embeds::class)->for($this->url)?->toArray()]
: ['url' => $this->url, 'title' => null, 'description' => null, 'image_url' => null, 'site_name' => null, 'is_pending' => false, 'embed' => null, 'commit_preview_url' => $commitPreviewUrl];
}
}
21 changes: 21 additions & 0 deletions app/Queries/CommitPreviewQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Queries;

use App\Models\Commit;
use App\Models\Profile;
use App\Models\User;

final readonly class CommitPreviewQuery
{
public function get(Commit $commit, Profile $author, ?User $viewer): Commit
{
return Commit::query()
->with(['images', 'linkPreview'])
->withPollFor($viewer)
->findOrFail($commit->id)
->setRelation('author', $author);
}
}
45 changes: 45 additions & 0 deletions app/Services/CommitLinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace App\Services;

use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
use Illuminate\Support\Str;
use Uri\WhatWg\Url;

final readonly class CommitLinks
{
public function __construct(private Router $router)
{
//
}

/**
* Reads this site's commit route without dispatching a request or loading its models.
*/
public function previewUrl(string $url): ?string
{
$parsed = Url::parse($url);
$site = Url::parse(config()->string('app.url'));
assert($site instanceof Url);

if (! $parsed instanceof Url
|| ! in_array($parsed->getScheme(), ['http', 'https'], true)
|| $parsed->getAsciiHost() !== $site->getAsciiHost()
|| $parsed->getPort() !== $site->getPort()) {
return null;
}

$route = $this->router->getRoutes()->getByName('commit.show');
assert($route instanceof Route);

if (preg_match($route->toSymfonyRoute()->compile()->getRegex(), mb_rtrim($parsed->getPath(), '/'), $matches) !== 1
|| ! Str::isUuid($matches['commit'])) {
return null;
}

return route('commit-preview.show', ['profile' => $matches['profile'], 'commit' => $matches['commit']], absolute: false);
}
}
67 changes: 67 additions & 0 deletions resources/js/components/commit-link-preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { useEffect, useState } from 'react';
import { QuotedCommit } from '@/components/quoted-commit';
import { Skeleton } from '@/components/ui/skeleton';
import type { QuotedCommit as QuotedCommitData } from '@/types';

export function CommitLinkPreview({
url,
isStatic = false,
}: {
url: string;
isStatic?: boolean;
}) {
const [commit, setCommit] = useState<QuotedCommitData | null>();

useEffect(() => {
const controller = new AbortController();

void fetch(url, {
headers: { Accept: 'application/json' },
cache: 'no-store',
signal: controller.signal,
})
.then(async (response) => {
const data = response.ok
? ((await response.json()) as { commit: QuotedCommitData })
: null;

if (!controller.signal.aborted) {
setCommit(data?.commit ?? null);
}
})
.catch(() => {
if (!controller.signal.aborted) {
setCommit(null);
}
});

return () => controller.abort();
}, [url]);

if (commit === undefined) {
return (
<div
aria-busy="true"
aria-label="Loading linked commit"
className="mt-2 animate-in rounded-lg border px-3 py-2 delay-300 fill-mode-backwards fade-in"
data-test="commit-link-preview-skeleton"
>
<div className="flex items-center gap-1.5">
<Skeleton className="size-5 rounded-full" />
<Skeleton className="h-3 w-32" />
</div>
<Skeleton className="mt-2 h-4 w-full" />
<Skeleton className="mt-2 h-4 w-2/3" />
</div>
);
}

return (
<QuotedCommit
commit={commit}
label="Linked commit"
isPreview={isStatic}
suppressLinkedPreview
/>
);
}
30 changes: 23 additions & 7 deletions resources/js/components/link-preview.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { usePage } from '@inertiajs/react';
import { useEffect, useState } from 'react';
import { CommitLinkPreview } from '@/components/commit-link-preview';
import { LinkEmbed } from '@/components/link-embed';
import { Skeleton } from '@/components/ui/skeleton';
import type { RealtimeChannel } from '@/lib/realtime';
Expand Down Expand Up @@ -183,17 +184,32 @@ function LinkPreviewSkeleton() {
);
}

export function LinkPreview({
refreshUrl,
preview: initialPreview,
isStatic = false,
source,
}: {
type LinkPreviewProps = {
refreshUrl: string;
preview: LinkPreviewData;
isStatic?: boolean;
source?: LinkPreviewSource;
}) {
suppressLinkedPreview?: boolean;
};

export function LinkPreview(props: LinkPreviewProps) {
const url = props.preview.commit_preview_url;

if (url) {
return props.suppressLinkedPreview ? null : (
<CommitLinkPreview key={url} url={url} isStatic={props.isStatic} />
);
}

return <ExternalLinkPreview {...props} />;
}

function ExternalLinkPreview({
refreshUrl,
preview: initialPreview,
isStatic = false,
source,
}: LinkPreviewProps) {
const { preview, isWaiting } = useFetchedLinkPreview(
refreshUrl,
initialPreview,
Expand Down
3 changes: 3 additions & 0 deletions resources/js/components/quoted-commit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ export function QuotedCommit({
commit,
isPreview = false,
label = 'Quoting',
suppressLinkedPreview = false,
}: {
commit: QuotedCommitData | DeletedCommit | null;
isPreview?: boolean;
label?: string;
suppressLinkedPreview?: boolean;
}) {
const now = useNow();
if (commit === null || isDeleted(commit)) {
Expand Down Expand Up @@ -81,6 +83,7 @@ export function QuotedCommit({
refreshUrl={CommitLinkPreviewController.show.url(commit.id)}
preview={commit.link_preview}
isStatic
suppressLinkedPreview={suppressLinkedPreview}
source={{ commitId: commit.id }}
/>
)}
Expand Down
1 change: 1 addition & 0 deletions resources/js/types/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type Embed = {
};

export type LinkPreview = {
commit_preview_url?: string | null;
url: string;
title: string | null;
description: string | null;
Expand Down
5 changes: 5 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@
Route::get('@{profile:handle}/commits/{commit}', [CommitController::class, 'show'])
->scopeBindings()
->name('commit.show');
Route::get('@{profile:handle}/commits/{commit}/preview', [CommitPreviewController::class, 'show'])
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
->withoutMiddleware(RememberIntendedUrl::class)
->scopeBindings()
->whereUuid('commit')
->name('commit-preview.show');
Route::get('@{profile:handle}/commits/{commit}/photos/{photo}', [CommitPhotoController::class, 'show'])
->scopeBindings()
->whereNumber('photo')
Expand Down
Loading