summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Molakvoæ <skjnldsv@users.noreply.github.com>2021-01-12 08:06:25 +0100
committerGitHub <noreply@github.com>2021-01-12 08:06:25 +0100
commit4bd46d656fb9e21becc23b069180c09fa3dcb112 (patch)
tree72151683b9c7d9a66e242ff7ad3599b02e9f2908
parente28fdeb9194424c45e1e9b04be29cf51b0658ccb (diff)
parentbf48bcd0669593a510fceca39d3bbda3c1c01ce5 (diff)
Merge pull request #2013 from nextcloud/fix/twitter-grep
Make sure twitter returns the raw static html page so we can get the …
-rw-r--r--lib/Service/Social/TwitterProvider.php25
-rw-r--r--lib/Service/SocialApiService.php21
-rw-r--r--tests/unit/Service/Social/TwitterProviderTest.php8
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
);
}