2026-07-21 14:04:45 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Support\Publishing;
|
|
|
|
|
|
|
|
|
|
use App\Models\Post;
|
2026-08-06 20:38:52 +03:00
|
|
|
use App\Models\SocialAccount;
|
2026-07-21 14:04:45 +03:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
use Illuminate\Support\Facades\Process;
|
|
|
|
|
|
|
|
|
|
class RedditPublisher implements Publisher
|
|
|
|
|
{
|
|
|
|
|
public function supports(Post $post): bool
|
|
|
|
|
{
|
|
|
|
|
return $post->type === 'reddit';
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-27 21:24:49 +03:00
|
|
|
public function publish(Post $post, ?SocialAccount $socialAccount = null): PublishResult
|
2026-07-21 14:04:45 +03:00
|
|
|
{
|
|
|
|
|
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.");
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-27 21:24:49 +03:00
|
|
|
// Use the passed social account, or fall back to the first linked one
|
|
|
|
|
$account = $socialAccount ?: $post->socialAccounts->first();
|
2026-07-21 14:04:45 +03:00
|
|
|
|
|
|
|
|
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']);
|
|
|
|
|
}
|
|
|
|
|
}
|