Set language when posting
This commit is contained in:
parent
c16f8efbf0
commit
7653797e71
2 changed files with 86 additions and 7 deletions
|
|
@ -107,7 +107,7 @@ public function syncChannelPosts(string $token, int $communityId, string $commun
|
|||
}
|
||||
}
|
||||
|
||||
public function createPost(string $token, string $title, string $body, int $communityId, ?string $url = null, ?string $thumbnail = null): array
|
||||
public function createPost(string $token, string $title, string $body, int $communityId, ?string $url = null, ?string $thumbnail = null, ?int $languageId = null): array
|
||||
{
|
||||
try {
|
||||
$request = new LemmyRequest($this->instance, $token);
|
||||
|
|
@ -126,6 +126,10 @@ public function createPost(string $token, string $title, string $body, int $comm
|
|||
$postData['custom_thumbnail'] = $thumbnail;
|
||||
}
|
||||
|
||||
if ($languageId) {
|
||||
$postData['language_id'] = $languageId;
|
||||
}
|
||||
|
||||
$response = $request->post('post', $postData);
|
||||
|
||||
if (!$response->successful()) {
|
||||
|
|
@ -138,4 +142,27 @@ public function createPost(string $token, string $title, string $body, int $comm
|
|||
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 [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,13 +41,16 @@ public function publish(Article $article, array $extractedData): ArticlePublicat
|
|||
$token = $this->getAuthToken();
|
||||
$communityId = $this->getCommunityId();
|
||||
|
||||
$languageId = $this->getLanguageIdForSource($article->url);
|
||||
|
||||
$postData = $this->api->createPost(
|
||||
$token,
|
||||
$extractedData['title'] ?? 'Untitled',
|
||||
$extractedData['description'] ?? '',
|
||||
$communityId,
|
||||
$article->url,
|
||||
$extractedData['thumbnail'] ?? null
|
||||
$extractedData['thumbnail'] ?? null,
|
||||
$languageId
|
||||
);
|
||||
|
||||
return $this->createPublicationRecord($article, $postData, $communityId);
|
||||
|
|
@ -59,7 +62,7 @@ public function publish(Article $article, array $extractedData): ArticlePublicat
|
|||
private function getAuthToken(): string
|
||||
{
|
||||
$cachedToken = Cache::get('lemmy_jwt_token');
|
||||
|
||||
|
||||
if ($cachedToken) {
|
||||
return $cachedToken;
|
||||
}
|
||||
|
|
@ -78,7 +81,7 @@ private function getAuthToken(): string
|
|||
}
|
||||
|
||||
Cache::put('lemmy_jwt_token', $token, 3600);
|
||||
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
|
|
@ -86,14 +89,14 @@ private function getCommunityId(): int
|
|||
{
|
||||
$cacheKey = "lemmy_community_id_{$this->community}";
|
||||
$cachedId = Cache::get($cacheKey);
|
||||
|
||||
|
||||
if ($cachedId) {
|
||||
return $cachedId;
|
||||
}
|
||||
|
||||
|
||||
$communityId = $this->api->getCommunityId($this->community);
|
||||
Cache::put($cacheKey, $communityId, 3600);
|
||||
|
||||
|
||||
return $communityId;
|
||||
}
|
||||
|
||||
|
|
@ -109,4 +112,53 @@ private function createPublicationRecord(Article $article, array $postData, int
|
|||
'publication_data' => $postData,
|
||||
]);
|
||||
}
|
||||
|
||||
private function getLanguageIdForSource(string $url): ?int
|
||||
{
|
||||
// TODO this will be obsolete when sources can be created from the UI, so we can remove these hard-coded sources
|
||||
|
||||
// VRT articles are in Dutch
|
||||
if (str_contains($url, 'vrt.be')) {
|
||||
return $this->getLanguageId('nl'); // Dutch
|
||||
}
|
||||
|
||||
// Belga articles are in English (based on URL structure)
|
||||
if (str_contains($url, 'belganewsagency.eu')) {
|
||||
return $this->getLanguageId('en'); // English
|
||||
}
|
||||
|
||||
return null; // Default to no language specified
|
||||
}
|
||||
|
||||
private function getLanguageId(string $languageCode): ?int
|
||||
{
|
||||
$cacheKey = "lemmy_language_id_{$languageCode}";
|
||||
$cachedId = Cache::get($cacheKey);
|
||||
|
||||
if ($cachedId !== null) {
|
||||
return $cachedId;
|
||||
}
|
||||
|
||||
try {
|
||||
$languages = $this->api->getLanguages();
|
||||
|
||||
foreach ($languages as $language) {
|
||||
if (isset($language['code']) && $language['code'] === $languageCode) {
|
||||
$languageId = $language['id'];
|
||||
Cache::put($cacheKey, $languageId, 3600);
|
||||
return $languageId;
|
||||
}
|
||||
}
|
||||
|
||||
// Cache null result to avoid repeated API calls
|
||||
Cache::put($cacheKey, null, 3600);
|
||||
return null;
|
||||
} catch (Exception $e) {
|
||||
logger()->warning('Failed to get language ID', [
|
||||
'language_code' => $languageCode,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue