51 lines
1.3 KiB
PHP
51 lines
1.3 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;
|
||
|
|
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|