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:
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Publishing;
|
||||
|
||||
final class PublishResult
|
||||
{
|
||||
private function __construct(
|
||||
public readonly bool $success,
|
||||
public readonly bool $attempted,
|
||||
public readonly ?string $url = null,
|
||||
public readonly ?string $error = null,
|
||||
) {}
|
||||
|
||||
public static function success(string $url): self
|
||||
{
|
||||
return new self(success: true, attempted: true, url: $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* The publish attempt itself failed (network, API error, etc).
|
||||
*/
|
||||
public static function failed(string $error): self
|
||||
{
|
||||
return new self(success: false, attempted: true, error: $error);
|
||||
}
|
||||
|
||||
/**
|
||||
* A precondition wasn't met — no attempt was made (bad data, missing
|
||||
* credentials, wrong type). Callers should not treat this the same as
|
||||
* a failed attempt.
|
||||
*/
|
||||
public static function rejected(string $error): self
|
||||
{
|
||||
return new self(success: false, attempted: false, error: $error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Publishing;
|
||||
|
||||
use App\Models\Post;
|
||||
|
||||
interface Publisher
|
||||
{
|
||||
/**
|
||||
* Whether this publisher handles the given post's type. Does not imply
|
||||
* the post is actually publishable (see publish()'s rejected results).
|
||||
*/
|
||||
public function supports(Post $post): bool;
|
||||
|
||||
public function publish(Post $post): PublishResult;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Publishing;
|
||||
|
||||
use App\Models\Post;
|
||||
|
||||
class PublisherManager
|
||||
{
|
||||
/**
|
||||
* @param array<Publisher> $publishers
|
||||
*/
|
||||
public function __construct(private readonly array $publishers) {}
|
||||
|
||||
public function resolve(Post $post): ?Publisher
|
||||
{
|
||||
foreach ($this->publishers as $publisher) {
|
||||
if ($publisher->supports($post)) {
|
||||
return $publisher;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Publishing;
|
||||
|
||||
use App\Models\Post;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Process;
|
||||
|
||||
class RedditPublisher implements Publisher
|
||||
{
|
||||
public function supports(Post $post): bool
|
||||
{
|
||||
return $post->type === 'reddit';
|
||||
}
|
||||
|
||||
public function publish(Post $post): PublishResult
|
||||
{
|
||||
if (! $post->title) {
|
||||
return PublishResult::rejected("Post {$post->id} has no title, required for a Reddit post.");
|
||||
}
|
||||
|
||||
if (! $post->target) {
|
||||
return PublishResult::rejected("Post {$post->id} has no target subreddit set.");
|
||||
}
|
||||
|
||||
$account = $post->socialAccount;
|
||||
|
||||
if (! $account || $account->network !== 'reddit') {
|
||||
return PublishResult::rejected("Post {$post->id} has no linked Reddit social account.");
|
||||
}
|
||||
|
||||
$result = Process::timeout(120)->run([
|
||||
base_path('sidecars/reddit-post/run.sh'),
|
||||
"--username={$account->username}",
|
||||
"--password={$account->password}",
|
||||
"--subreddit={$post->target}",
|
||||
"--title={$post->title}",
|
||||
"--body={$post->body}",
|
||||
]);
|
||||
|
||||
$data = json_decode(trim($result->output()), true);
|
||||
|
||||
if (! is_array($data) || ! array_key_exists('success', $data)) {
|
||||
Log::error('reddit sidecar returned unparseable output', [
|
||||
'post_id' => $post->id,
|
||||
'output' => $result->output(),
|
||||
'stderr' => $result->errorOutput(),
|
||||
]);
|
||||
|
||||
return PublishResult::failed('Reddit sidecar returned unparseable output — check logs.');
|
||||
}
|
||||
|
||||
if (! $data['success']) {
|
||||
return PublishResult::failed($data['error'] ?? 'unknown Reddit sidecar failure');
|
||||
}
|
||||
|
||||
return PublishResult::success($data['url']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Publishing;
|
||||
|
||||
use App\Models\Post;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class WordPressPublisher implements Publisher
|
||||
{
|
||||
public function supports(Post $post): bool
|
||||
{
|
||||
return $post->type === 'blog';
|
||||
}
|
||||
|
||||
public function publish(Post $post): PublishResult
|
||||
{
|
||||
if (! $post->title) {
|
||||
return PublishResult::rejected("Post {$post->id} has no title, required to publish to WordPress.");
|
||||
}
|
||||
|
||||
$site = $post->site;
|
||||
|
||||
if ($site->cms_type !== 'wordpress') {
|
||||
return PublishResult::rejected("Site '{$site->name}' is not configured as WordPress (cms_type={$site->cms_type}).");
|
||||
}
|
||||
|
||||
if (! $site->wp_username || ! $site->wp_app_password) {
|
||||
return PublishResult::rejected("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 PublishResult::failed("could not connect to site: {$e->getMessage()}");
|
||||
}
|
||||
|
||||
if ($response->failed()) {
|
||||
return PublishResult::failed($response->json('message') ?? 'unexpected response from WordPress');
|
||||
}
|
||||
|
||||
$link = $response->json('link');
|
||||
|
||||
if (! $link) {
|
||||
return PublishResult::failed('unexpected response from WordPress: missing post link');
|
||||
}
|
||||
|
||||
return PublishResult::success($link);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user