Refactor auth retrieval

This commit is contained in:
myrmidex 2025-07-02 21:32:37 +02:00
parent 66f0c7c2d5
commit a3f3856632
5 changed files with 46 additions and 63 deletions

View file

@ -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));
}
}
}

View file

@ -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()) {

View file

@ -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}";

View file

@ -0,0 +1,39 @@
<?php
namespace App\Services\Auth;
use App\Enums\PlatformEnum;
use App\Exceptions\PlatformAuthException;
use App\Modules\Lemmy\Services\LemmyApiService;
use Illuminate\Support\Facades\Cache;
class LemmyAuthService
{
public static function getToken(): string
{
$cachedToken = Cache::get('lemmy_jwt_token');
if ($cachedToken) {
return $cachedToken;
}
$username = config('lemmy.username');
$password = config('lemmy.password');
$instance = config('lemmy.instance');
if (!$username || !$password || !$instance) {
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Missing credentials or instance');
}
$api = new LemmyApiService($instance);
$token = $api->login($username, $password);
if (!$token) {
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Login failed');
}
Cache::put('lemmy_jwt_token', $token, 3600);
return $token;
}
}

View file

@ -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');