diff --git a/app/Console/Commands/PublishToLemmyCommand.php b/app/Console/Commands/PublishToLemmyCommand.php new file mode 100644 index 0000000..8538a9c --- /dev/null +++ b/app/Console/Commands/PublishToLemmyCommand.php @@ -0,0 +1,23 @@ +firstOrFail(); + + LemmyService::publish($article); + + return self::SUCCESS; + } +} diff --git a/app/Listeners/PublishArticle.php b/app/Listeners/PublishArticle.php index 5afd219..bba0651 100644 --- a/app/Listeners/PublishArticle.php +++ b/app/Listeners/PublishArticle.php @@ -3,6 +3,7 @@ namespace App\Listeners; use App\Events\ArticleReadyToPublish; +use App\Services\Article\LemmyService; class PublishArticle { @@ -16,6 +17,7 @@ public function handle(ArticleReadyToPublish $event): void $article = $event->article; logger('Publishing article: ' . $article->id . ' : ' . $article->url); - // TODO: Add actual publishing logic here + + LemmyService::publish($article); } } diff --git a/app/Services/Article/LemmyService.php b/app/Services/Article/LemmyService.php new file mode 100644 index 0000000..ca24538 --- /dev/null +++ b/app/Services/Article/LemmyService.php @@ -0,0 +1,70 @@ + $jwt]); + +// $instance = config('lemmy.instance'); +// $community = config('lemmy.community'); +// +// $response = file_get_contents("https://$instance/api/v3/community?name=$community"); +// $data = json_decode($response, true); +// +// dd($data); +//// return $data['community_view']['community']['id'] ?? null; +// +// logger('publishing ' . $article . ' - ' . $article->url); + + return true; + } + + private static function getJwtToken(): ?string + { + return Cache::remember('lemmy_jwt_token', 3600, function () { + return self::login(); + }); + } + + private static function login(): ?string + { + $username = config('lemmy.username'); + $password = config('lemmy.password'); + $instance = config('lemmy.instance'); + + if (!$username || !$password || !$instance) { + logger()->error('Missing Lemmy configuration'); + return null; + } + + try { + $response = Http::post("https://$instance/api/v3/user/login", [ + 'username_or_email' => $username, + 'password' => $password, + ]); + + if (!$response->successful()) { + logger()->error('Lemmy login failed', [ + 'status' => $response->status(), + 'body' => $response->body() + ]); + return null; + } + + $data = $response->json(); + return $data['jwt'] ?? null; + } catch (Exception $e) { + logger()->error('Lemmy login exception', ['error' => $e->getMessage()]); + return null; + } + } +} diff --git a/config/lemmy.php b/config/lemmy.php new file mode 100644 index 0000000..49b3fc5 --- /dev/null +++ b/config/lemmy.php @@ -0,0 +1,8 @@ + env('LEMMY_USERNAME'), + 'password' => env('LEMMY_PASSWORD'), + 'instance' => env('LEMMY_INSTANCE'), + 'community' => env('LEMMY_COMMUNITY'), +];