Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Uri\WhatWg\Url;

return new class extends Migration
{
/**
* A card is left behind when the commit its link opens is gone or has no author any more, or replies to one that has
* none, since a reply's title names who it replies to. What someone posted for an organization keeps the organization
* as its author once their account is gone, so its cards stay.
*/
public function up(): void
{
$host = (string) Url::parse(config()->string('app.url'))?->getAsciiHost();

DB::table('link_previews')->select(['id', 'url'])->chunkById(1000, function (Collection $previews) use ($host): void {
/** @var Collection<int, object{id: string, url: string}> $previews */
$linked = $previews->mapWithKeys(fn (object $preview): array => [$preview->id => $this->commitId($preview->url, $host)])->filter();
$kept = DB::table('commits')
->whereIn('id', $linked->unique()->values()->all())
->whereNotNull('author_id')
->where(fn (Builder $query): Builder => $query->whereNull('parent_id')
->orWhereExists(fn (Builder $parent): Builder => $parent->from('commits as parents')
->whereColumn('parents.id', 'commits.parent_id')
->whereNotNull('parents.author_id')))
->pluck('id');

DB::table('link_previews')->whereIn('id', $linked->reject(fn (string $commit): bool => $kept->contains($commit))->keys()->all())->delete();
});
}

/**
* The commit a link to this site opens, read the way the preview fetcher and the router read it, since escapes, dot
* segments and backslashes still open it. Frozen here so later changes to either cannot change this cleanup.
*/
private function commitId(string $url, string $host): string
{
$link = Url::parse($url);
$linkHost = (string) $link?->getAsciiHost();
$id = Str::match('#^/@[^/]+/commits/([0-9a-f-]{36})#', mb_strtolower(rawurldecode((string) $link?->getPath())));

return ($linkHost === $host || str_ends_with($linkHost, ".{$host}")) && Str::isUuid($id) ? $id : '';
}
};
44 changes: 44 additions & 0 deletions tests/Unit/Actions/DeleteUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,50 @@
expect(PushSubscription::query()->count())->toBe(0);
});

it('erases the previews of links to commits that deleted accounts already took with them', function (): void {
$tombstone = Commit::factory()->create(['user_id' => null, 'body' => '', 'deleted_at' => now()]);
$live = Commit::factory()->create();
$posted = Commit::factory()->create(['user_id' => null, 'author_id' => Organization::factory()->create()->id]);
$gone = fake()->uuid();
$preview = fn (string $url): LinkPreview => LinkPreview::factory()->create(['url' => $url]);
$leftovers = collect([
url("/@gone/commits/{$tombstone->id}"),
url('/@gone/commits/'.mb_strtoupper($gone)),
str_replace('://', '://www.', url("/@gone/commits/{$gone}")),
(string) preg_replace('#^(https?://[^/:]+)(:\d+)?#', '$1:80', url("/@gone/commits/{$gone}")),
url('/@gone/commits/'.str_replace('-', '%2D', $tombstone->id)),
url("/@gone/%63ommits/{$gone}"),
url("/@gone/commits/%2e/{$gone}"),
str_replace('/commits/', '\\commits\\', url("/@gone/commits/{$gone}")),
url('/@someone/commits/'.Commit::factory()->for($tombstone, 'parent')->create()->id),
])->map($preview);
$kept = collect([
url("/@{$live->user->handle}/commits/{$live->id}"),
url('/@someone/commits/'.Commit::factory()->create(['deleted_at' => now()])->id),
url('/@someone/commits/'.Commit::factory()->for($live, 'parent')->create()->id),
url("/@org/commits/{$posted->id}"),
url('/@org/commits/'.str_replace('-', '%2D', $posted->id)),
url('/@someone/commits/'.Commit::factory()->for($posted, 'parent')->create()->id),
url('/@someone/commits/'.str_replace('-', '%2D', $live->id)),
url("/@someone/%63ommits/{$live->id}"),
url("/@someone/commits/%2e/{$live->id}"),
url("/@someone/commits/{$gone}/../{$live->id}"),
url('/@someone/commits/'.str_repeat('-', 36)),
"https://example.com/@gone/commits/{$gone}",
"https://example.com/@gone/%63ommits/{$gone}",
str_replace('://', '://not', url("/@gone/commits/{$gone}")),
str_replace('://', '://example.com\\.', url("/@gone/commits/{$gone}")),
'https://example.com/redirect?to='.url("/@gone/commits/{$gone}"),
url("/@gone?redirect=/commits/{$gone}"),
])->map($preview);

$migration = require database_path('migrations/2026_11_17_120000_erase_the_link_previews_of_erased_commits.php');
$migration->up();

$leftovers->each(fn (LinkPreview $preview) => $this->assertModelMissing($preview));
$kept->each(fn (LinkPreview $preview) => $this->assertModelExists($preview));
});

it('pauses moderated guilds and preserves other peoples posts memberships and scheduled posts', function (): void {
Storage::fake();
$user = User::factory()->create();
Expand Down