api = new LemmyApiService($account->instance_url); $this->account = $account; } /** * @param array $extractedData * @return array * * @throws PlatformAuthException * @throws Exception */ public function publishToChannel(Article $article, array $extractedData, PlatformChannel $channel): array { $authService = resolve(LemmyAuthService::class); $token = $authService->getToken($this->account); try { return $this->createPost($token, $extractedData, $channel, $article); } catch (Exception $e) { // If the cached token was stale, refresh and retry once if (str_contains($e->getMessage(), 'not_logged_in') || str_contains($e->getMessage(), 'Unauthorized')) { $token = $authService->refreshToken($this->account); return $this->createPost($token, $extractedData, $channel, $article); } throw $e; } } /** * @param array $extractedData * @return array */ private function createPost(string $token, array $extractedData, PlatformChannel $channel, Article $article): array { $languageId = $extractedData['language_id'] ?? null; $communityId = is_numeric($channel->channel_id) ? (int) $channel->channel_id : $this->api->getCommunityId($channel->channel_id, $token); return $this->api->createPost( $token, $extractedData['title'] ?? 'Untitled', $extractedData['description'] ?? '', $communityId, $article->url, $extractedData['thumbnail'] ?? null, $languageId ); } }