Add a Publisher abstraction and queued job to publish from the UI
Introduce Publisher/PublisherManager (WordPress + Reddit implementations) so publishing logic is shared between the CLI, the queue, and future network integrations. Add a "Publish" button on the Posts page that dispatches PublishPostJob instead of blocking the request — Reddit's publisher shells out to the existing Playwright sidecar and can take tens of seconds, so it can't run synchronously in a web request. Adds a `target` field (e.g. subreddit) and `publishing` status to Post, plus a `publish_error` field so failures surface in the UI. Refactors posts:publish to delegate to WordPressPublisher without changing its CLI output contract (existing tests pass unchanged). Also fixes two bugs found along the way: UpdatePostRequest was silently dropping `published_url` from PATCH requests (never in its validation rules, despite AI_API.md documenting it), and three Livewire index-page tests were broken by the earlier auth change (never re-run after adding the `auth` middleware) since they hit routes via HTTP without a logged-in user. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,9 +3,8 @@
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Post;
|
||||
use App\Support\Publishing\WordPressPublisher;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class PublishPost extends Command
|
||||
{
|
||||
@@ -13,7 +12,7 @@ class PublishPost extends Command
|
||||
|
||||
protected $description = 'Publish an existing blog post to its site\'s WordPress REST API';
|
||||
|
||||
public function handle(): int
|
||||
public function handle(WordPressPublisher $publisher): int
|
||||
{
|
||||
$post = Post::with('site')->find($this->argument('post'));
|
||||
|
||||
@@ -25,48 +24,20 @@ class PublishPost extends Command
|
||||
return $this->failWith("Post {$post->id} has type '{$post->type}', expected 'blog' to publish to a CMS.");
|
||||
}
|
||||
|
||||
if (! $post->title) {
|
||||
return $this->failWith("Post {$post->id} has no title, required to publish to WordPress.");
|
||||
$result = $publisher->publish($post);
|
||||
|
||||
if ($result->success) {
|
||||
$post->update(['status' => 'published', 'published_url' => $result->url, 'publish_error' => null]);
|
||||
$this->line(json_encode(['success' => true, 'url' => $result->url]));
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
$site = $post->site;
|
||||
|
||||
if ($site->cms_type !== 'wordpress') {
|
||||
return $this->failWith("Site '{$site->name}' is not configured as WordPress (cms_type={$site->cms_type}).");
|
||||
if ($result->attempted) {
|
||||
$post->update(['status' => 'failed', 'publish_error' => $result->error]);
|
||||
}
|
||||
|
||||
if (! $site->wp_username || ! $site->wp_app_password) {
|
||||
return $this->failWith("Site '{$site->name}' is missing WordPress credentials.");
|
||||
}
|
||||
|
||||
try {
|
||||
$response = Http::withBasicAuth($site->wp_username, $site->wp_app_password)
|
||||
->post(rtrim($site->url, '/') . '/wp-json/wp/v2/posts', [
|
||||
'title' => $post->title,
|
||||
'content' => $post->body,
|
||||
'status' => 'publish',
|
||||
]);
|
||||
} catch (ConnectionException $e) {
|
||||
return $this->publishFailed($post, "could not connect to site: {$e->getMessage()}");
|
||||
}
|
||||
|
||||
if ($response->failed()) {
|
||||
$message = $response->json('message') ?? 'unexpected response from WordPress';
|
||||
|
||||
return $this->publishFailed($post, $message);
|
||||
}
|
||||
|
||||
$link = $response->json('link');
|
||||
|
||||
if (! $link) {
|
||||
return $this->publishFailed($post, 'unexpected response from WordPress: missing post link');
|
||||
}
|
||||
|
||||
$post->update(['status' => 'published', 'published_url' => $link]);
|
||||
|
||||
$this->line(json_encode(['success' => true, 'url' => $link]));
|
||||
|
||||
return self::SUCCESS;
|
||||
return $this->failWith($result->error);
|
||||
}
|
||||
|
||||
private function failWith(string $message): int
|
||||
@@ -75,11 +46,4 @@ class PublishPost extends Command
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
private function publishFailed(Post $post, string $message): int
|
||||
{
|
||||
$post->update(['status' => 'failed']);
|
||||
|
||||
return $this->failWith($message);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user