fedi-feed-router/app/Jobs/PublishToLemmyJob.php

51 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2025-06-30 18:18:30 +02:00
<?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;
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::fromConfig()->publish($this->article, $extractedData);
logger()->info('Article published successfully', [
'article_id' => $this->article->id
]);
} catch (PublishException $e) {
$this->fail($e);
}
}
}