fedi-feed-router/backend/app/Modules/Lemmy/Services/LemmyPublisher.php

48 lines
1.3 KiB
PHP
Raw Normal View History

2025-06-29 21:20:45 +02:00
<?php
namespace App\Modules\Lemmy\Services;
2025-07-05 02:19:59 +02:00
use App\Exceptions\PlatformAuthException;
2025-06-29 21:20:45 +02:00
use App\Models\Article;
2025-07-05 01:55:53 +02:00
use App\Models\PlatformAccount;
2025-07-05 18:26:04 +02:00
use App\Models\PlatformChannel;
2025-07-02 21:32:37 +02:00
use App\Services\Auth\LemmyAuthService;
2025-06-29 21:20:45 +02:00
use Exception;
class LemmyPublisher
{
private LemmyApiService $api;
2025-07-05 01:55:53 +02:00
private PlatformAccount $account;
2025-06-29 21:20:45 +02:00
2025-07-05 01:55:53 +02:00
public function __construct(PlatformAccount $account)
2025-06-29 21:20:45 +02:00
{
2025-07-05 01:55:53 +02:00
$this->api = new LemmyApiService($account->instance_url);
$this->account = $account;
2025-06-29 21:20:45 +02:00
}
2025-07-05 01:55:53 +02:00
/**
2025-07-07 00:51:32 +02:00
* @param array<string, mixed> $extractedData
* @return array<string, mixed>
2025-07-05 02:19:59 +02:00
* @throws PlatformAuthException
2025-07-05 01:55:53 +02:00
* @throws Exception
*/
2025-07-05 18:26:04 +02:00
public function publishToChannel(Article $article, array $extractedData, PlatformChannel $channel): array
2025-06-29 21:20:45 +02:00
{
2025-08-10 15:20:28 +02:00
$token = resolve(LemmyAuthService::class)->getToken($this->account);
2025-07-05 02:19:59 +02:00
2025-07-05 18:26:04 +02:00
// Use the language ID from extracted data (should be set during validation)
$languageId = $extractedData['language_id'] ?? null;
return $this->api->createPost(
2025-07-05 02:19:59 +02:00
$token,
$extractedData['title'] ?? 'Untitled',
$extractedData['description'] ?? '',
2025-08-10 01:26:56 +02:00
(int) $channel->channel_id,
2025-07-05 02:19:59 +02:00
$article->url,
$extractedData['thumbnail'] ?? null,
$languageId
);
2025-06-29 21:20:45 +02:00
}
2025-06-30 18:18:30 +02:00
}