168 lines
5.2 KiB
PHP
168 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Lemmy\Services;
|
|
|
|
use App\Enums\PlatformEnum;
|
|
use App\Models\PlatformChannelPost;
|
|
use App\Modules\Lemmy\LemmyRequest;
|
|
use Exception;
|
|
|
|
class LemmyApiService
|
|
{
|
|
private string $instance;
|
|
|
|
public function __construct(string $instance)
|
|
{
|
|
$this->instance = $instance;
|
|
}
|
|
|
|
public function login(string $username, string $password): ?string
|
|
{
|
|
try {
|
|
$request = new LemmyRequest($this->instance);
|
|
$response = $request->post('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;
|
|
}
|
|
}
|
|
|
|
public function getCommunityId(string $communityName, string $token): int
|
|
{
|
|
try {
|
|
$request = new LemmyRequest($this->instance, $token);
|
|
$response = $request->get('community', ['name' => $communityName]);
|
|
|
|
if (!$response->successful()) {
|
|
throw new Exception('Failed to fetch community: ' . $response->status());
|
|
}
|
|
|
|
$data = $response->json();
|
|
return $data['community_view']['community']['id'] ?? throw new Exception('Community not found');
|
|
} catch (Exception $e) {
|
|
logger()->error('Community lookup failed', ['error' => $e->getMessage()]);
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function syncChannelPosts(string $token, int $platformChannelId, string $communityName): void
|
|
{
|
|
try {
|
|
$request = new LemmyRequest($this->instance, $token);
|
|
$response = $request->get('post/list', [
|
|
'community_id' => $platformChannelId,
|
|
'limit' => 50,
|
|
'sort' => 'New'
|
|
]);
|
|
|
|
if (!$response->successful()) {
|
|
logger()->warning('Failed to sync channel posts', [
|
|
'status' => $response->status(),
|
|
'platform_channel_id' => $platformChannelId
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$data = $response->json();
|
|
$posts = $data['posts'] ?? [];
|
|
|
|
foreach ($posts as $postData) {
|
|
$post = $postData['post'];
|
|
|
|
PlatformChannelPost::storePost(
|
|
PlatformEnum::LEMMY,
|
|
(string) $platformChannelId,
|
|
$communityName,
|
|
(string) $post['id'],
|
|
$post['url'] ?? null,
|
|
$post['name'] ?? null,
|
|
isset($post['published']) ? new \DateTime($post['published']) : null
|
|
);
|
|
}
|
|
|
|
logger()->info('Synced channel posts', [
|
|
'platform_channel_id' => $platformChannelId,
|
|
'posts_count' => count($posts)
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
logger()->error('Exception while syncing channel posts', [
|
|
'error' => $e->getMessage(),
|
|
'platform_channel_id' => $platformChannelId
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function createPost(string $token, string $title, string $body, int $platformChannelId, ?string $url = null, ?string $thumbnail = null, ?int $languageId = null): array
|
|
{
|
|
try {
|
|
$request = new LemmyRequest($this->instance, $token);
|
|
|
|
$postData = [
|
|
'name' => $title,
|
|
'body' => $body,
|
|
'community_id' => $platformChannelId,
|
|
];
|
|
|
|
if ($url) {
|
|
$postData['url'] = $url;
|
|
}
|
|
|
|
if ($thumbnail) {
|
|
$postData['custom_thumbnail'] = $thumbnail;
|
|
}
|
|
|
|
if ($languageId) {
|
|
$postData['language_id'] = $languageId;
|
|
}
|
|
|
|
$response = $request->post('post', $postData);
|
|
|
|
if (!$response->successful()) {
|
|
throw new Exception('Failed to create post: ' . $response->status() . ' - ' . $response->body());
|
|
}
|
|
|
|
return $response->json();
|
|
} catch (Exception $e) {
|
|
logger()->error('Post creation failed', ['error' => $e->getMessage()]);
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function getLanguages(): array
|
|
{
|
|
try {
|
|
$request = new LemmyRequest($this->instance);
|
|
$response = $request->get('site');
|
|
|
|
if (!$response->successful()) {
|
|
logger()->warning('Failed to fetch site languages', [
|
|
'status' => $response->status()
|
|
]);
|
|
return [];
|
|
}
|
|
|
|
$data = $response->json();
|
|
return $data['all_languages'] ?? [];
|
|
} catch (Exception $e) {
|
|
logger()->error('Exception while fetching languages', [
|
|
'error' => $e->getMessage()
|
|
]);
|
|
return [];
|
|
}
|
|
}
|
|
}
|