From bf48bcd0669593a510fceca39d3bbda3c1c01ce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6=20=28skjnldsv=29?= Date: Mon, 11 Jan 2021 12:31:16 +0100 Subject: Make sure twitter returns the raw static html page so we can get the profile picture properly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ (skjnldsv) --- lib/Service/Social/TwitterProvider.php | 25 ++++++++++++++++++++--- lib/Service/SocialApiService.php | 21 ++++++++++--------- tests/unit/Service/Social/TwitterProviderTest.php | 8 +++++++- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/lib/Service/Social/TwitterProvider.php b/lib/Service/Social/TwitterProvider.php index 18cd0d46..9460fb40 100644 --- a/lib/Service/Social/TwitterProvider.php +++ b/lib/Service/Social/TwitterProvider.php @@ -23,17 +23,27 @@ namespace OCA\Contacts\Service\Social; +use GuzzleHttp\Exception\RequestException; +use GuzzleHttp\RequestOptions; +use OC\AppFramework\Http\Request; +use OCA\Contacts\AppInfo\Application; use OCP\Http\Client\IClientService; +use Psr\Log\LoggerInterface; class TwitterProvider implements ISocialProvider { /** @var IClientService */ private $httpClient; + /** @var LoggerInterface */ + private $logger; + /** @var string */ public $name = "twitter"; - public function __construct(IClientService $httpClient) { + public function __construct(IClientService $httpClient, + LoggerInterface $logger) { $this->httpClient = $httpClient->NewClient(); + $this->logger = $logger; } /** @@ -112,7 +122,12 @@ class TwitterProvider implements ISocialProvider { */ protected function getFromHtml(string $url, string $desired) : ?string { try { - $result = $this->httpClient->get($url); + $result = $this->httpClient->get($url, [ + RequestOptions::HEADERS => [ + // Make the request as google bot so twitter display the full static html page + 'User-Agent' => 'Googlebot/2.1' + ] + ]); $htmlResult = new \DOMDocument(); $htmlResult->loadHTML($result->getBody()); @@ -127,7 +142,11 @@ class TwitterProvider implements ISocialProvider { } } return null; - } catch (\Exception $e) { + } catch (RequestException $e) { + $this->logger->debug('Error fetching twitter urls', [ + 'app' => Application::APP_ID, + 'exception' => $e + ]); return null; } } diff --git a/lib/Service/SocialApiService.php b/lib/Service/SocialApiService.php index b1657734..2ea2f45b 100644 --- a/lib/Service/SocialApiService.php +++ b/lib/Service/SocialApiService.php @@ -23,22 +23,22 @@ namespace OCA\Contacts\Service; -use OCA\Contacts\Service\Social\CompositeSocialProvider; use OCA\Contacts\AppInfo\Application; +use OCA\Contacts\Service\Social\CompositeSocialProvider; -use OCP\Contacts\IManager; -use OCP\IAddressBook; +use OCA\DAV\CardDAV\CardDavBackend; +use OCA\DAV\CardDAV\ContactsManager; -use OCP\Util; -use OCP\IConfig; use OCP\AppFramework\Http; use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Contacts\IManager; use OCP\Http\Client\IClientService; -use OCA\DAV\CardDAV\CardDavBackend; -use OCA\DAV\CardDAV\ContactsManager; -use OCP\IURLGenerator; +use OCP\IAddressBook; +use OCP\IConfig; use OCP\IL10N; -use OCP\AppFramework\Utility\ITimeFactory; +use OCP\IURLGenerator; +use OCP\Util; class SocialApiService { private $appName; @@ -175,7 +175,7 @@ class SocialApiService { try { // get corresponding addressbook - $addressBook = $this->getAddressBook($addressbookId); + $addressBook = $this->getAddressBook(urldecode($addressbookId)); if (is_null($addressBook)) { return new JSONResponse([], Http::STATUS_BAD_REQUEST); } @@ -216,6 +216,7 @@ class SocialApiService { break; } } catch (\Exception $e) { + return $e; } } diff --git a/tests/unit/Service/Social/TwitterProviderTest.php b/tests/unit/Service/Social/TwitterProviderTest.php index 55515b0f..fbaf05ca 100644 --- a/tests/unit/Service/Social/TwitterProviderTest.php +++ b/tests/unit/Service/Social/TwitterProviderTest.php @@ -29,6 +29,7 @@ use OCP\Http\Client\IResponse; use OCP\Http\Client\IClientService; use ChristophWurst\Nextcloud\Testing\TestCase; use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; class TwitterProviderTest extends TestCase { private $provider; @@ -36,6 +37,9 @@ class TwitterProviderTest extends TestCase { /** @var IClientService|MockObject */ private $clientService; + /** @var LoggerInterface|MockObject */ + private $logger; + /** @var IClient|MockObject */ private $client; @@ -45,6 +49,7 @@ class TwitterProviderTest extends TestCase { protected function setUp(): void { parent::setUp(); $this->clientService = $this->createMock(IClientService::class); + $this->logger = $this->createMock(LoggerInterface::class); $this->response = $this->createMock(IResponse::class); $this->client = $this->createMock(IClient::class); @@ -53,7 +58,8 @@ class TwitterProviderTest extends TestCase { ->willReturn($this->client); $this->provider = new TwitterProvider( - $this->clientService + $this->clientService, + $this->logger ); } -- cgit v1.2.3