Merge pull request #14608 from annando/atproto

Unneeded line removed / improved status handling
This commit is contained in:
Hypolite Petovan 2024-12-21 11:43:32 -05:00 committed by GitHub
commit 4728d1919f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 30 additions and 8 deletions

View file

@ -129,7 +129,17 @@ final class ATProtocol
} }
$data = $this->get($pds . '/xrpc/' . $url, [HttpClientOptions::HEADERS => $headers]); $data = $this->get($pds . '/xrpc/' . $url, [HttpClientOptions::HEADERS => $headers]);
$this->pConfig->set($uid, 'bluesky', 'status', is_null($data) ? self::STATUS_API_FAIL : self::STATUS_SUCCESS); if (empty($data) || (!empty($data->code) && ($data->code < 200 || $data->code >= 400))) {
$this->pConfig->set($uid, 'bluesky', 'status', self::STATUS_API_FAIL);
if (!empty($data->message)) {
$this->pConfig->set($uid, 'bluesky', 'status-message', $data->message);
} elseif (!empty($data->code)) {
$this->pConfig->set($uid, 'bluesky', 'status-message', 'Error Code: ' . $data->code);
}
} elseif (!empty($data)) {
$this->pConfig->set($uid, 'bluesky', 'status', self::STATUS_SUCCESS);
$this->pConfig->set($uid, 'bluesky', 'status-message', '');
}
return $data; return $data;
} }
@ -156,6 +166,9 @@ final class ATProtocol
return null; return null;
} }
$data->code = $curlResult->getReturnCode(); $data->code = $curlResult->getReturnCode();
} elseif (($curlResult->getReturnCode() < 200) || ($curlResult->getReturnCode() >= 400)) {
$this->logger->notice('Unexpected return code', ['url' => $url, 'code' => $curlResult->getReturnCode(), 'error' => $data ?: $curlResult->getBodyString()]);
$data->code = $curlResult->getReturnCode();
} }
Item::incrementInbound(Protocol::BLUESKY); Item::incrementInbound(Protocol::BLUESKY);
@ -198,6 +211,7 @@ final class ATProtocol
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->notice('Exception on post', ['exception' => $e]); $this->logger->notice('Exception on post', ['exception' => $e]);
$this->pConfig->set($uid, 'bluesky', 'status', self::STATUS_API_FAIL); $this->pConfig->set($uid, 'bluesky', 'status', self::STATUS_API_FAIL);
$this->pConfig->set($uid, 'bluesky', 'status-message', $e->getMessage());
return null; return null;
} }
@ -206,6 +220,11 @@ final class ATProtocol
$this->logger->notice('API Error', ['url' => $url, 'code' => $curlResult->getReturnCode(), 'error' => $data ?: $curlResult->getBodyString()]); $this->logger->notice('API Error', ['url' => $url, 'code' => $curlResult->getReturnCode(), 'error' => $data ?: $curlResult->getBodyString()]);
if (!$data) { if (!$data) {
$this->pConfig->set($uid, 'bluesky', 'status', self::STATUS_API_FAIL); $this->pConfig->set($uid, 'bluesky', 'status', self::STATUS_API_FAIL);
if (!empty($data->message)) {
$this->pConfig->set($uid, 'bluesky', 'status-message', $data->message);
} elseif (!empty($data->code)) {
$this->pConfig->set($uid, 'bluesky', 'status-message', 'Error Code: ' . $data->code);
}
return null; return null;
} }
$data->code = $curlResult->getReturnCode(); $data->code = $curlResult->getReturnCode();
@ -213,8 +232,14 @@ final class ATProtocol
if (!empty($data->code) && ($data->code >= 200) && ($data->code < 400)) { if (!empty($data->code) && ($data->code >= 200) && ($data->code < 400)) {
$this->pConfig->set($uid, 'bluesky', 'status', self::STATUS_SUCCESS); $this->pConfig->set($uid, 'bluesky', 'status', self::STATUS_SUCCESS);
$this->pConfig->set($uid, 'bluesky', 'status-message', '');
} else { } else {
$this->pConfig->set($uid, 'bluesky', 'status', self::STATUS_API_FAIL); $this->pConfig->set($uid, 'bluesky', 'status', self::STATUS_API_FAIL);
if (!empty($data->message)) {
$this->pConfig->set($uid, 'bluesky', 'status-message', $data->message);
} elseif (!empty($data->code)) {
$this->pConfig->set($uid, 'bluesky', 'status-message', 'Error Code: ' . $data->code);
}
} }
return $data; return $data;
} }
@ -502,10 +527,6 @@ final class ATProtocol
$data = $this->post($uid, '/xrpc/com.atproto.server.refreshSession', '', ['Authorization' => ['Bearer ' . $token]]); $data = $this->post($uid, '/xrpc/com.atproto.server.refreshSession', '', ['Authorization' => ['Bearer ' . $token]]);
if (empty($data) || empty($data->accessJwt)) { if (empty($data) || empty($data->accessJwt)) {
$this->logger->debug('Refresh failed', ['return' => $data]); $this->logger->debug('Refresh failed', ['return' => $data]);
$password = $this->pConfig->get($uid, 'bluesky', 'password');
if (!empty($password)) {
return $this->createUserToken($uid, $password);
}
$this->pConfig->set($uid, 'bluesky', 'status', self::STATUS_TOKEN_FAIL); $this->pConfig->set($uid, 'bluesky', 'status', self::STATUS_TOKEN_FAIL);
return ''; return '';
} }
@ -542,6 +563,7 @@ final class ATProtocol
$this->pConfig->set($uid, 'bluesky', 'refresh_token', $data->refreshJwt); $this->pConfig->set($uid, 'bluesky', 'refresh_token', $data->refreshJwt);
$this->pConfig->set($uid, 'bluesky', 'token_created', time()); $this->pConfig->set($uid, 'bluesky', 'token_created', time());
$this->pConfig->set($uid, 'bluesky', 'status', self::STATUS_TOKEN_OK); $this->pConfig->set($uid, 'bluesky', 'status', self::STATUS_TOKEN_OK);
$this->pConfig->set($uid, 'bluesky', 'status-message', '');
return $data->accessJwt; return $data->accessJwt;
} }
} }

View file

@ -1,5 +1,5 @@
#!/usr/bin/env php
<?php <?php
/** /**
* Copyright (C) 2010-2024, the Friendica project * Copyright (C) 2010-2024, the Friendica project
* SPDX-FileCopyrightText: 2010-2024 the Friendica project * SPDX-FileCopyrightText: 2010-2024 the Friendica project

View file

@ -1,5 +1,5 @@
#!/usr/bin/env php
<?php <?php
/** /**
* Copyright (C) 2010-2024, the Friendica project * Copyright (C) 2010-2024, the Friendica project
* SPDX-FileCopyrightText: 2010-2024 the Friendica project * SPDX-FileCopyrightText: 2010-2024 the Friendica project

View file

@ -1,5 +1,5 @@
#!/usr/bin/env php
<?php <?php
/** /**
* Copyright (C) 2010-2024, the Friendica project * Copyright (C) 2010-2024, the Friendica project
* SPDX-FileCopyrightText: 2010-2024 the Friendica project * SPDX-FileCopyrightText: 2010-2024 the Friendica project