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

141 lines
4.8 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;
2025-07-06 20:37:55 +02:00
use App\Models\PlatformChannel;
2025-08-10 16:18:09 +02:00
use App\Models\Route;
2025-07-05 18:26:04 +02:00
use App\Modules\Lemmy\Services\LemmyPublisher;
use App\Services\Log\LogSaver;
use Exception;
2025-07-06 20:52:58 +02:00
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
2025-07-05 18:26:04 +02:00
use Illuminate\Support\Collection;
use RuntimeException;
class ArticlePublishingService
{
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
/**
2025-07-07 00:51:32 +02:00
* @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
{
if (! $article->is_valid) {
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
// Get active routes with keywords instead of just channels
$activeRoutes = Route::where('feed_id', $feed->id)
->where('is_active', true)
->with(['platformChannel.platformInstance', 'platformChannel.activePlatformAccounts', 'keywords'])
->orderBy('priority', 'desc')
->get();
2025-07-05 18:26:04 +02:00
2025-08-10 16:18:09 +02:00
// Filter routes based on keyword matches
$matchingRoutes = $activeRoutes->filter(function (Route $route) use ($extractedData) {
return $this->routeMatchesArticle($route, $extractedData);
});
return $matchingRoutes->map(function (Route $route) use ($article, $extractedData) {
$channel = $route->platformChannel;
2025-07-06 20:37:55 +02:00
$account = $channel->activePlatformAccounts()->first();
2025-07-05 18:26:04 +02:00
if (! $account) {
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
/**
* Check if a route matches an article based on keywords
* @param array<string, mixed> $extractedData
*/
private function routeMatchesArticle(Route $route, array $extractedData): bool
{
// Get active keywords for this route
$activeKeywords = $route->keywords->where('is_active', true);
// If no keywords are defined for this route, the route matches any article
if ($activeKeywords->isEmpty()) {
return true;
}
// Get article content for keyword matching
$articleContent = '';
if (isset($extractedData['full_article'])) {
$articleContent = $extractedData['full_article'];
}
if (isset($extractedData['title'])) {
$articleContent .= ' ' . $extractedData['title'];
}
if (isset($extractedData['description'])) {
$articleContent .= ' ' . $extractedData['description'];
}
// Check if any of the route's keywords match the article content
foreach ($activeKeywords as $keywordModel) {
$keyword = $keywordModel->keyword;
if (stripos($articleContent, $keyword) !== false) {
return true;
}
}
return false;
}
2025-07-07 00:51:32 +02:00
/**
* @param array<string, mixed> $extractedData
*/
private function publishToChannel(Article $article, array $extractedData, PlatformChannel $channel, mixed $account): ?ArticlePublication
2025-07-05 18:26:04 +02:00
{
try {
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,
]);
2025-08-10 16:18:09 +02:00
LogSaver::info('Published to channel via keyword-filtered routing', $channel, [
'article_id' => $article->id
2025-07-05 18:26:04 +02:00
]);
return $publication;
} catch (Exception $e) {
LogSaver::warning('Failed to publish to channel', $channel, [
'article_id' => $article->id,
'error' => $e->getMessage()
]);
return null;
}
}
}