is_valid) { throw new PublishException($article, PlatformEnum::LEMMY, new RuntimeException('CANNOT_PUBLISH_INVALID_ARTICLE')); } $feed = $article->feed; $activeChannels = $feed->activeChannels()->with(['platformInstance', 'platformAccounts'])->get(); return $activeChannels->map(function ($channel) use ($article, $extractedData) { $account = $channel->platformAccounts()->where('is_active', true)->first(); if (! $account) { LogSaver::warning('No active account for channel', $channel, [ 'article_id' => $article->id ]); return null; } return $this->publishToChannel($article, $extractedData, $channel, $account); }) ->filter(); } private function publishToChannel(Article $article, array $extractedData, $channel, $account): ?ArticlePublication { try { $publisher = new LemmyPublisher($account); $postData = $publisher->publishToChannel($article, $extractedData, $channel); $publication = ArticlePublication::create([ 'article_id' => $article->id, 'post_id' => $postData['post_view']['post']['id'], 'community_id' => $channel->channel_id, 'published_by' => $account->username, 'published_at' => now(), 'platform' => $channel->platformInstance->platform->value, 'publication_data' => $postData, ]); LogSaver::info('Published to channel via routing', $channel, [ 'article_id' => $article->id, 'priority' => $channel->pivot->priority ]); return $publication; } catch (Exception $e) { LogSaver::warning('Failed to publish to channel', $channel, [ 'article_id' => $article->id, 'error' => $e->getMessage() ]); return null; } } }