jonnybarnes.uk/app/Services/Micropub/Handlers/EntryHandler.php
Jonny Barnes 77998a963e
Fix relative Location header when Micropub creates an article
EntryHandler used Article::link, which is deliberately a site-relative
path elsewhere in the app, directly as the Micropub response's
Location URL. Every other post type it returns (notes, bookmarks,
places) already prepends the site URL, so articles were the only case
where clients received a relative Location - iA Writer appears to
treat that as a local file path and fails to open it after posting.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017USyUg8PwuoDcHP8pv5xjy
2026-09-13 11:42:28 +01:00

49 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Micropub\Handlers;
use App\Exceptions\InvalidTokenScopeException;
use App\Services\ArticleService;
use App\Services\BookmarkService;
use App\Services\LikeService;
use App\Services\Micropub\Data\EntryData;
use App\Services\Micropub\Data\MicropubData;
use App\Services\NoteService;
class EntryHandler implements MicropubHandlerInterface
{
public function dataClass(): string
{
return EntryData::class;
}
/**
* @throws InvalidTokenScopeException
*/
public function handle(MicropubData $data): array
{
assert($data instanceof EntryData);
$scopes = $data->tokenData['scope'];
$scopes = explode(' ', $scopes);
if (! in_array('create', $scopes, true)) {
throw new InvalidTokenScopeException;
}
$dataArray = $data->toArray();
$location = match (true) {
isset($dataArray['like-of']) => resolve(LikeService::class)->create($dataArray)->url,
isset($dataArray['bookmark-of']) => resolve(BookmarkService::class)->create($dataArray)->uri,
isset($dataArray['name']) => config('app.url').resolve(ArticleService::class)->create($dataArray)->link,
default => resolve(NoteService::class)->create($dataArray)->uri,
};
return [
'response' => 'created',
'url' => $location,
];
}
}