fedi-feed-router/app/Services/Publishing/ArticlePublishingService.php

178 lines
6 KiB
PHP
Raw Normal View History

2025-07-05 18:26:04 +02:00
<?php
namespace App\Services\Publishing;
use App\Enums\PlatformEnum;
use App\Exceptions\PublishException;
use App\Models\Article;
use App\Models\ArticlePublication;
use App\Models\Keyword;
2025-07-06 20:37:55 +02:00
use App\Models\PlatformChannel;
use App\Models\PlatformChannelPost;
2025-08-10 16:18:09 +02:00
use App\Models\Route;
use App\Models\RouteArticle;
2025-07-05 18:26:04 +02:00
use App\Modules\Lemmy\Services\LemmyPublisher;
use App\Services\Log\LogSaver;
use Exception;
use Illuminate\Support\Collection;
use RuntimeException;
class ArticlePublishingService
{
public function __construct(private LogSaver $logSaver) {}
2025-08-10 01:26:56 +02:00
/**
* Factory seam to create publisher instances (helps testing without network calls)
*/
protected function makePublisher(mixed $account): LemmyPublisher
{
return new LemmyPublisher($account);
}
2025-07-05 18:26:04 +02:00
/**
* Publish an article to the channel specified by a route_article record.
*
* @param array<string, mixed> $extractedData
*
* @throws PublishException
*/
public function publishRouteArticle(RouteArticle $routeArticle, array $extractedData): ?ArticlePublication
{
$article = $routeArticle->article;
$channel = $routeArticle->platformChannel;
if (! $channel) {
throw new PublishException($article, PlatformEnum::LEMMY, new RuntimeException('ROUTE_ARTICLE_MISSING_CHANNEL'));
}
if (! $channel->relationLoaded('platformInstance')) {
$channel->load(['platformInstance', 'activePlatformAccounts']);
}
$account = $channel->activePlatformAccounts()->first();
if (! $account) {
$this->logSaver->warning('No active account for channel', $channel, [
'article_id' => $article->id,
'route_article_id' => $routeArticle->id,
]);
return null;
}
return $this->publishToChannel($article, $extractedData, $channel, $account);
}
/**
* @deprecated Use publishRouteArticle() instead. Kept for PublishApprovedArticleListener compatibility.
*
* @param array<string, mixed> $extractedData
2025-08-10 01:26:56 +02:00
* @return Collection<int, ArticlePublication>
*
2025-07-05 18:26:04 +02:00
* @throws PublishException
*/
2025-08-10 01:26:56 +02:00
public function publishToRoutedChannels(Article $article, array $extractedData): Collection
2025-07-05 18:26:04 +02:00
{
2025-08-10 21:18:20 +02:00
if (! $article->isValid()) {
2025-07-05 18:26:04 +02:00
throw new PublishException($article, PlatformEnum::LEMMY, new RuntimeException('CANNOT_PUBLISH_INVALID_ARTICLE'));
}
$feed = $article->feed;
2025-08-10 01:26:56 +02:00
2025-08-10 16:18:09 +02:00
$activeRoutes = Route::where('feed_id', $feed->id)
->where('is_active', true)
->get();
2025-07-05 18:26:04 +02:00
$keywordsByChannel = Keyword::where('feed_id', $feed->id)
->where('is_active', true)
->get()
->groupBy('platform_channel_id');
$matchingRoutes = $activeRoutes->filter(function (Route $route) use ($extractedData, $keywordsByChannel) {
$keywords = $keywordsByChannel->get($route->platform_channel_id, collect());
if ($keywords->isEmpty()) {
return true;
}
$articleContent = ($extractedData['full_article'] ?? '').
' '.($extractedData['title'] ?? '').
' '.($extractedData['description'] ?? '');
foreach ($keywords as $keyword) {
if (stripos($articleContent, $keyword->keyword) !== false) {
return true;
}
}
return false;
2025-08-10 16:18:09 +02:00
});
return $matchingRoutes->map(function (Route $route) use ($article, $extractedData) {
$channel = PlatformChannel::with(['platformInstance', 'activePlatformAccounts'])->find($route->platform_channel_id);
$account = $channel?->activePlatformAccounts()->first();
2025-07-05 18:26:04 +02:00
if (! $account) {
2025-08-15 02:50:42 +02:00
$this->logSaver->warning('No active account for channel', $channel, [
2025-08-10 16:18:09 +02:00
'article_id' => $article->id,
'route_priority' => $route->priority,
2025-07-05 18:26:04 +02:00
]);
return null;
}
return $this->publishToChannel($article, $extractedData, $channel, $account);
})->filter();
2025-08-10 16:18:09 +02:00
}
2025-07-07 00:51:32 +02:00
/**
* @param array<string, mixed> $extractedData
2025-07-07 00:51:32 +02:00
*/
private function publishToChannel(Article $article, array $extractedData, PlatformChannel $channel, mixed $account): ?ArticlePublication
2025-07-05 18:26:04 +02:00
{
try {
// Check if this URL or title was already posted to this channel
$title = $extractedData['title'] ?? $article->title;
if (PlatformChannelPost::duplicateExists(
$channel->platformInstance->platform,
(string) $channel->channel_id,
$article->url,
$title
)) {
$this->logSaver->info('Skipping duplicate: URL or title already posted to channel', $channel, [
'article_id' => $article->id,
'url' => $article->url,
'title' => $title,
]);
return null;
}
2025-08-10 01:26:56 +02:00
$publisher = $this->makePublisher($account);
2025-07-05 18:26:04 +02:00
$postData = $publisher->publishToChannel($article, $extractedData, $channel);
$publication = ArticlePublication::create([
'article_id' => $article->id,
'post_id' => $postData['post_view']['post']['id'],
2025-07-06 01:35:59 +02:00
'platform_channel_id' => $channel->id,
2025-07-05 18:26:04 +02:00
'published_by' => $account->username,
'published_at' => now(),
'platform' => $channel->platformInstance->platform->value,
'publication_data' => $postData,
]);
$this->logSaver->info('Published to channel', $channel, [
'article_id' => $article->id,
2025-07-05 18:26:04 +02:00
]);
return $publication;
} catch (Exception $e) {
2025-08-15 02:50:42 +02:00
$this->logSaver->warning('Failed to publish to channel', $channel, [
2025-07-05 18:26:04 +02:00
'article_id' => $article->id,
'error' => $e->getMessage(),
2025-07-05 18:26:04 +02:00
]);
return null;
}
}
}