diff --git a/app/Listeners/ValidateArticle.php b/app/Listeners/ValidateArticle.php index 5cc03a9..5048a03 100644 --- a/app/Listeners/ValidateArticle.php +++ b/app/Listeners/ValidateArticle.php @@ -22,7 +22,7 @@ public function handle(ArticleFetched $event): void $article = ValidationService::validate($article); if ($article->isValid()) { - event(new ArticleReadyToPublish($event->article)); + event(new ArticleReadyToPublish($article)); } } } diff --git a/app/Modules/Lemmy/Services/LemmyApiService.php b/app/Modules/Lemmy/Services/LemmyApiService.php index ad30c84..1d16352 100644 --- a/app/Modules/Lemmy/Services/LemmyApiService.php +++ b/app/Modules/Lemmy/Services/LemmyApiService.php @@ -5,6 +5,7 @@ use App\Enums\PlatformEnum; use App\Models\PlatformChannelPost; use App\Modules\Lemmy\LemmyRequest; +use App\Services\Auth\LemmyAuthService; use Exception; class LemmyApiService @@ -44,7 +45,8 @@ public function login(string $username, string $password): ?string public function getCommunityId(string $communityName): int { try { - $request = new LemmyRequest($this->instance); + $token = LemmyAuthService::getToken(); + $request = new LemmyRequest($this->instance, $token); $response = $request->get('community', ['name' => $communityName]); if (!$response->successful()) { @@ -82,7 +84,7 @@ public function syncChannelPosts(string $token, int $communityId, string $commun foreach ($posts as $postData) { $post = $postData['post']; - + PlatformChannelPost::storePost( PlatformEnum::LEMMY, (string) $communityId, diff --git a/app/Modules/Lemmy/Services/LemmyPublisher.php b/app/Modules/Lemmy/Services/LemmyPublisher.php index 591f986..cd95374 100644 --- a/app/Modules/Lemmy/Services/LemmyPublisher.php +++ b/app/Modules/Lemmy/Services/LemmyPublisher.php @@ -3,10 +3,10 @@ namespace App\Modules\Lemmy\Services; use App\Enums\PlatformEnum; -use App\Exceptions\PlatformAuthException; use App\Exceptions\PublishException; use App\Models\Article; use App\Models\ArticlePublication; +use App\Services\Auth\LemmyAuthService; use Exception; use Illuminate\Support\Facades\Cache; @@ -38,7 +38,7 @@ public static function fromConfig(): self public function publish(Article $article, array $extractedData): ArticlePublication { try { - $token = $this->getAuthToken(); + $token = LemmyAuthService::getToken(); $communityId = $this->getCommunityId(); $languageId = $this->getLanguageIdForSource($article->url); @@ -59,32 +59,6 @@ public function publish(Article $article, array $extractedData): ArticlePublicat } } - private function getAuthToken(): string - { - $cachedToken = Cache::get('lemmy_jwt_token'); - - if ($cachedToken) { - return $cachedToken; - } - - $username = config('lemmy.username'); - $password = config('lemmy.password'); - - if (!$username || !$password) { - throw new PlatformAuthException(PlatformEnum::LEMMY, 'Missing credentials'); - } - - $token = $this->api->login($username, $password); - - if (!$token) { - throw new PlatformAuthException(PlatformEnum::LEMMY, 'Login failed'); - } - - Cache::put('lemmy_jwt_token', $token, 3600); - - return $token; - } - private function getCommunityId(): int { $cacheKey = "lemmy_community_id_{$this->community}"; diff --git a/app/Services/Auth/LemmyAuthService.php b/app/Services/Auth/LemmyAuthService.php new file mode 100644 index 0000000..b70a4c1 --- /dev/null +++ b/app/Services/Auth/LemmyAuthService.php @@ -0,0 +1,39 @@ +login($username, $password); + + if (!$token) { + throw new PlatformAuthException(PlatformEnum::LEMMY, 'Login failed'); + } + + Cache::put('lemmy_jwt_token', $token, 3600); + + return $token; + } +} \ No newline at end of file diff --git a/routes/console.php b/routes/console.php index db0d635..4717c28 100644 --- a/routes/console.php +++ b/routes/console.php @@ -3,42 +3,10 @@ use App\Console\Commands\FetchNewArticlesCommand; use App\Enums\PlatformEnum; use App\Jobs\SyncChannelPostsJob; -use App\Models\Article; -use App\Modules\Lemmy\Services\LemmyPublisher; -use App\Services\Article\ArticleFetcher; use Illuminate\Support\Facades\Schedule; Schedule::command(FetchNewArticlesCommand::class)->hourly(); -Schedule::call(function () { - $article = Article::whereDoesntHave('articlePublications') - ->where('is_valid', true) - ->first(); - - if ($article) { - try { - logger()->info('Publishing article to Lemmy via scheduler', [ - 'article_id' => $article->id, - 'url' => $article->url - ]); - - $extractedData = ArticleFetcher::fetchArticleData($article); - LemmyPublisher::fromConfig()->publish($article, $extractedData); - - logger()->info('Successfully published article to Lemmy', [ - 'article_id' => $article->id - ]); - } catch (Exception $e) { - logger()->error('Failed to publish article to Lemmy via scheduler', [ - 'article_id' => $article->id, - 'error' => $e->getMessage() - ]); - } - } else { - logger()->debug('No unpublished valid articles found for Lemmy publishing'); - } -})->everyFiveMinutes()->name('publish-to-lemmy'); - Schedule::call(function () { $communityId = config('lemmy.community_id'); $communityName = config('lemmy.community');