Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure twitter returns the raw static html page so we can get the … #2013

Merged
merged 1 commit into from
Jan 12, 2021
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
25 changes: 22 additions & 3 deletions lib/Service/Social/TwitterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -112,7 +122,12 @@ protected function getProfileIds($contact):array {
*/
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());
Expand All @@ -127,7 +142,11 @@ protected function getFromHtml(string $url, string $desired) : ?string {
}
}
return null;
} catch (\Exception $e) {
} catch (RequestException $e) {
$this->logger->debug('Error fetching twitter urls', [
'app' => Application::APP_ID,
'exception' => $e
]);
return null;
}
}
Expand Down
21 changes: 11 additions & 10 deletions lib/Service/SocialApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -175,7 +175,7 @@ public function updateContact(string $addressbookId, string $contactId, ?string

try {
// get corresponding addressbook
$addressBook = $this->getAddressBook($addressbookId);
$addressBook = $this->getAddressBook(urldecode($addressbookId));
if (is_null($addressBook)) {
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
}
Expand Down Expand Up @@ -216,6 +216,7 @@ public function updateContact(string $addressbookId, string $contactId, ?string
break;
}
} catch (\Exception $e) {
return $e;
}
}

Expand Down
8 changes: 7 additions & 1 deletion tests/unit/Service/Social/TwitterProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@
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;

/** @var IClientService|MockObject */
private $clientService;

/** @var LoggerInterface|MockObject */
private $logger;

/** @var IClient|MockObject */
private $client;

Expand All @@ -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);

Expand All @@ -53,7 +58,8 @@ protected function setUp(): void {
->willReturn($this->client);

$this->provider = new TwitterProvider(
$this->clientService
$this->clientService,
$this->logger
);
}

Expand Down