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;
|
2025-07-05 01:55:53 +02:00
|
|
|
use RuntimeException;
|
2025-06-30 18:18:30 +02:00
|
|
|
|
|
|
|
|
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 {
|
2025-07-05 01:55:53 +02:00
|
|
|
LemmyPublisher::fromActiveAccount()->publish($this->article, $extractedData);
|
2025-06-30 18:18:30 +02:00
|
|
|
|
|
|
|
|
logger()->info('Article published successfully', [
|
|
|
|
|
'article_id' => $this->article->id
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
} catch (PublishException $e) {
|
|
|
|
|
$this->fail($e);
|
2025-07-05 01:55:53 +02:00
|
|
|
} catch (RuntimeException $e) {
|
|
|
|
|
logger()->warning('No active Lemmy accounts configured', [
|
|
|
|
|
'article_id' => $this->article->id,
|
|
|
|
|
'error' => $e->getMessage()
|
|
|
|
|
]);
|
|
|
|
|
$this->fail($e);
|
2025-06-30 18:18:30 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|