Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions lib/Provider/Channel/GoWhatsApp/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,15 @@ private function tryReuseExistingDevice(InputInterface $input, OutputInterface $
$output->writeln('<info>Checking for existing devices...</info>');
$devices = $this->fetchDevices();

if ($devices === null || empty($devices)) {
if ($devices === null) {
$output->writeln('<info>No devices found. Creating a new device...</info>');
if (!$this->createNewDevice($output)) {
return self::CONFIG_ERROR;
}
$devices = $this->fetchDevices();
}

if (empty($devices)) {
return self::CONFIG_CONTINUE;
}

Expand Down Expand Up @@ -467,12 +475,43 @@ private function fetchDevices(): ?array {
return $data['results'];
}

return [];
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
if ($e->getResponse()?->getStatusCode() === 404) {
return null;
}
return [];
} catch (\Exception) {
return null;
}
}

private function createNewDevice(OutputInterface $output): bool {
try {
$response = $this->client->post($this->getBaseUrl() . '/devices', [
'timeout' => 5,
'json' => [
'device_name' => 'Nextcloud',
],
]);

$body = (string)$response->getBody();
$data = json_decode($body, true);

if (($data['code'] ?? '') === 'SUCCESS' || ($data['code'] ?? '') === 'CREATED') {

return true;
}

$output->writeln('<error>\u2717 Failed to create device: ' . ($data['message'] ?? 'Unknown error') . '</error>');
return false;
} catch (\Exception $e) {
$output->writeln('<error>\u2717 Error creating device: ' . $e->getMessage() . '</error>');
$this->logger->error('Failed to create device', ['exception' => $e]);
return false;
}
}

private function requestPairingCode(InputInterface $input, OutputInterface $output): ?string {
$output->writeln('<info>Requesting pairing code...</info>');

Expand Down Expand Up @@ -501,7 +540,9 @@ private function requestPairingCode(InputInterface $input, OutputInterface $outp
private function fetchPairingCode(): array {
try {
$response = $this->client->get($this->getBaseUrl() . '/app/login-with-code', [
'query' => ['phone' => $this->lazyPhone],
'query' => [
'phone' => $this->lazyPhone,
],
]);

$body = (string)$response->getBody();
Expand Down Expand Up @@ -599,9 +640,13 @@ private function performLogout(OutputInterface $output): bool {

private function validateUrlReachability(OutputInterface $output): bool {
try {
$response = $this->client->get($this->lazyBaseUrl . '/app/status', [
$this->client->post($this->lazyBaseUrl . '/devices', [
'timeout' => 5,
'json' => [
'device_name' => 'Nextcloud',
],
]);

$output->writeln('<info>✓ API is reachable.</info>');
return true;
} catch (\GuzzleHttp\Exception\ConnectException $e) {
Expand Down
2 changes: 2 additions & 0 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
<code><![CDATA[$e]]></code>
<code><![CDATA[$e]]></code>
<code><![CDATA[$e]]></code>
<code><![CDATA[$e]]></code>
<code><![CDATA[BadResponseException]]></code>
<code><![CDATA[BadResponseException]]></code>
<code><![CDATA[RequestException]]></code>
<code><![CDATA[RequestException]]></code>
<code><![CDATA[\GuzzleHttp\Exception\BadResponseException]]></code>
<code><![CDATA[\GuzzleHttp\Exception\ConnectException]]></code>
</UndefinedClass>
</file>
Expand Down
Loading