Skip to content

Commit

Permalink
Mastodon username search getGuzzleClient not working
Browse files Browse the repository at this point in the history
  • Loading branch information
ifarzana committed Aug 17, 2023
1 parent 1362e7a commit b0241a1
Showing 1 changed file with 10 additions and 27 deletions.
37 changes: 10 additions & 27 deletions lib/Widget/MastodonProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function fetchData(DataProviderInterface $dataProvider): WidgetProviderIn

try {
$httpOptions = [
'timeout' => 20, // wait no more than 20 seconds
// 'timeout' => 20, // wait no more than 20 seconds
'query' => [
'limit' => $dataProvider->getProperty('numItems', 15)
]
Expand All @@ -73,24 +73,23 @@ public function fetchData(DataProviderInterface $dataProvider): WidgetProviderIn
// when username is provided do not search in public timeline
if (!empty($dataProvider->getProperty('userName', ''))) {
// username search: get account ID, always returns one record
$accountId = $this->getAccountId($uri, $dataProvider);
$accountId = $this->getAccountId($uri, $dataProvider->getProperty('userName'), $dataProvider);
$httpOptions['query']['tagged'] = trim($hashtag, '#');
;
$result = $this->getResult($uri, $accountId, $httpOptions);
$uri = rtrim($uri, '/') . '/api/v1/accounts/' . $accountId . '/statuses?';
} else {
// Hashtag: When empty we should do a public search, when filled we should do a hashtag search
if (!empty($hashtag)) {
$uri = rtrim($uri, '/') . '/api/v1/timelines/tag/' . trim($hashtag, '#');
} else {
$uri = rtrim($uri, '/') . '/api/v1/timelines/public';
}
}

$response = $dataProvider
->getGuzzleClient($httpOptions)
->get($uri);
$response = $dataProvider
->getGuzzleClient($httpOptions)
->get($uri);

$result = json_decode($response->getBody()->getContents(), true);
}
$result = json_decode($response->getBody()->getContents(), true);

$this->getLog()->debug('Mastodon: uri: ' . $uri . ' httpOptions: ' . json_encode($httpOptions));

Expand Down Expand Up @@ -175,14 +174,14 @@ public function getDataModifiedDt(DataProviderInterface $dataProvider): ?Carbon
* Get Mastodon Account Id from username
* @throws GuzzleException
*/
private function getAccountId(mixed $uri, DataProviderInterface $dataProvider)
private function getAccountId(string $uri, string $username, DataProviderInterface $dataProvider)
{
$uri = rtrim($uri, '/').'/api/v1/accounts/lookup?';

$httpOptions = [
'timeout' => 20, // wait no more than 20 seconds
'query' => [
'acct' => $dataProvider->getProperty('userName')
'acct' => $username
],
];
$response = $dataProvider
Expand All @@ -195,20 +194,4 @@ private function getAccountId(mixed $uri, DataProviderInterface $dataProvider)

return $result['id'];
}

/**
* Get Mastodon account public status
* @throws GuzzleException
*/
private function getResult(mixed $uri, $accountId, $httpOptions)
{
$uri = rtrim($uri, '/') . '/api/v1/accounts/' . $accountId . '/statuses?';

$client = new Client();
$response = $client->request('GET', $uri, $httpOptions);
$result = json_decode($response->getBody()->getContents(), true);

$this->getLog()->debug('Mastodon: username search result count ' . count($result));
return $result;
}
}

0 comments on commit b0241a1

Please sign in to comment.