fedi-feed-router/app/Jobs/PublishToLemmyJob.php
2025-07-05 01:55:53 +02:00

57 lines
1.5 KiB
PHP

<?php
namespace App\Jobs;
use App\Exceptions\PublishException;
use App\Models\Article;
use App\Modules\Lemmy\Services\LemmyPublisher;
use App\Services\Article\ArticleFetcher;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use RuntimeException;
class PublishToLemmyJob implements ShouldQueue
{
use Queueable;
public function __construct(
private readonly Article $article
) {
$this->onQueue('lemmy-posts');
}
public function handle(): void
{
if ($this->article->articlePublication !== null) {
logger()->info('Article already published, skipping', [
'article_id' => $this->article->id
]);
return;
}
$extractedData = ArticleFetcher::fetchArticleData($this->article);
logger()->info('Publishing article to Lemmy', [
'article_id' => $this->article->id,
'url' => $this->article->url
]);
try {
LemmyPublisher::fromActiveAccount()->publish($this->article, $extractedData);
logger()->info('Article published successfully', [
'article_id' => $this->article->id
]);
} catch (PublishException $e) {
$this->fail($e);
} catch (RuntimeException $e) {
logger()->warning('No active Lemmy accounts configured', [
'article_id' => $this->article->id,
'error' => $e->getMessage()
]);
$this->fail($e);
}
}
}