Get JWT token
This commit is contained in:
parent
d341c616f0
commit
8aaf614fe7
4 changed files with 104 additions and 1 deletions
23
app/Console/Commands/PublishToLemmyCommand.php
Normal file
23
app/Console/Commands/PublishToLemmyCommand.php
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Models\Article;
|
||||||
|
use App\Services\Article\LemmyService;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
|
class PublishToLemmyCommand extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'article:publish-to-lemmy';
|
||||||
|
|
||||||
|
protected $description = 'Publish an article to Lemmy';
|
||||||
|
|
||||||
|
public function handle(): int
|
||||||
|
{
|
||||||
|
$article = Article::all()->firstOrFail();
|
||||||
|
|
||||||
|
LemmyService::publish($article);
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
namespace App\Listeners;
|
namespace App\Listeners;
|
||||||
|
|
||||||
use App\Events\ArticleReadyToPublish;
|
use App\Events\ArticleReadyToPublish;
|
||||||
|
use App\Services\Article\LemmyService;
|
||||||
|
|
||||||
class PublishArticle
|
class PublishArticle
|
||||||
{
|
{
|
||||||
|
|
@ -16,6 +17,7 @@ public function handle(ArticleReadyToPublish $event): void
|
||||||
$article = $event->article;
|
$article = $event->article;
|
||||||
|
|
||||||
logger('Publishing article: ' . $article->id . ' : ' . $article->url);
|
logger('Publishing article: ' . $article->id . ' : ' . $article->url);
|
||||||
// TODO: Add actual publishing logic here
|
|
||||||
|
LemmyService::publish($article);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
70
app/Services/Article/LemmyService.php
Normal file
70
app/Services/Article/LemmyService.php
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services\Article;
|
||||||
|
|
||||||
|
use App\Models\Article;
|
||||||
|
use Illuminate\Support\Facades\Http;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class LemmyService
|
||||||
|
{
|
||||||
|
public static function publish(Article $article): bool
|
||||||
|
{
|
||||||
|
$jwt = self::getJwtToken();
|
||||||
|
dd(['jwt' => $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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
8
config/lemmy.php
Normal file
8
config/lemmy.php
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'username' => env('LEMMY_USERNAME'),
|
||||||
|
'password' => env('LEMMY_PASSWORD'),
|
||||||
|
'instance' => env('LEMMY_INSTANCE'),
|
||||||
|
'community' => env('LEMMY_COMMUNITY'),
|
||||||
|
];
|
||||||
Loading…
Reference in a new issue