diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2018-09-20 14:07:22 +0200 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2018-09-20 14:07:22 +0200 |
commit | aa7a43ecac373ab5f969ab760b3c2bc7da678dfd (patch) | |
tree | ed21499480f8059ad8602dcd450f70c3b6c28566 | |
parent | 04972c611830d355dff97468564a941876aeeb3f (diff) |
Addind accountId and follows
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
-rw-r--r-- | appinfo/database.xml | 6 | ||||
-rw-r--r-- | appinfo/info.xml | 2 | ||||
-rw-r--r-- | appinfo/routes.php | 16 | ||||
-rw-r--r-- | js/test.js | 33 | ||||
-rw-r--r-- | lib/Controller/ActivityStreamsController.php | 41 | ||||
-rw-r--r-- | lib/Controller/ServiceAccountsController.php | 1 | ||||
-rw-r--r-- | lib/Db/ServiceAccountsRequest.php | 1 | ||||
-rw-r--r-- | lib/Db/ServiceAccountsRequestBuilder.php | 5 | ||||
-rw-r--r-- | lib/Exceptions/APIRequestException.php | 8 | ||||
-rw-r--r-- | lib/Model/ServiceAccount.php | 36 | ||||
-rw-r--r-- | lib/Service/ActivityStreamsService.php | 30 | ||||
-rw-r--r-- | lib/Service/CurlService.php | 19 | ||||
-rw-r--r-- | lib/Service/ServiceAccountsService.php | 1 | ||||
-rw-r--r-- | lib/Service/ServicesService.php | 2 | ||||
-rw-r--r-- | lib/Tools/Model/Request.php | 13 |
15 files changed, 193 insertions, 21 deletions
diff --git a/appinfo/database.xml b/appinfo/database.xml index 4ee64cb4..1a615ac0 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -84,6 +84,12 @@ </field> <field> + <name>account_id</name> + <type>integer</type> + <length>7</length> + </field> + + <field> <name>status</name> <type>integer</type> <length>1</length> diff --git a/appinfo/info.xml b/appinfo/info.xml index e232b247..8c9e1088 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -5,7 +5,7 @@ <name>Social</name> <summary>🎉 Nextcloud becomes part of the federated social networks!</summary> <description><![CDATA[test]]></description> - <version>0.0.9</version> + <version>0.0.10</version> <licence>agpl</licence> <author mail="maxence@artificial-owl.com">Maxence Lange</author> <author mail="jus@bitgrid.net">Julius Härtl</author> diff --git a/appinfo/routes.php b/appinfo/routes.php index e0f7a657..e2ecb5db 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -23,11 +23,19 @@ return [ 'name' => 'ActivityStreams#test', 'url' => '/user/account/{accountId}/test', 'verb' => 'GET' ], + [ + 'name' => 'ActivityStreams#posts', 'url' => '/user/account/{accountId}/posts', + 'verb' => 'GET' + ], + [ + 'name' => 'ActivityStreams#follows', 'url' => '/user/account/{accountId}/follows', + 'verb' => 'GET' + ], -// [ -// 'name' => 'OAuth2#getAuthUrl', 'url' => '/client/oauth2/auth/{serviceId}/', -// 'verb' => 'GET' -// ], + // [ + // 'name' => 'OAuth2#getAuthUrl', 'url' => '/client/oauth2/auth/{serviceId}/', + // 'verb' => 'GET' + // ], [ 'name' => 'OAuth2#setCode', 'url' => '/client/oauth2/redirect/{serviceId}/', 'verb' => 'GET' @@ -41,8 +41,11 @@ elem.socialListAccounts.empty() for (var i = 0; i < data.result.accounts.length; i++) { var item = data.result.accounts[i] - elem.socialListAccounts.append($('<option>', {value: item.id}).text(item.account + '@' + item.service.address)) + elem.socialListAccounts.append( + $('<option>', {value: item.id}).text(item.account + '@' + item.service.address)) } + + test.refreshData() }, testAccount: function (accountId) { @@ -53,6 +56,28 @@ console.log(JSON.stringify(data)) }, + getAccountPosts: function (accountId) { + test.sendRequest('GET', {}, '/user/account/' + accountId + '/posts', test.getAccountPostsResult) + }, + + getAccountPostsResult: function (data) { + console.log('Your posts: ' + JSON.stringify(data)) + }, + + getAccountFollows: function (accountId) { + test.sendRequest('GET', {}, '/user/account/' + accountId + '/follows', + test.getAccountFollowsResult) + }, + + getAccountFollowsResult: function (data) { + console.log('Your Follows: ' + JSON.stringify(data)) + }, + + refreshData: function () { + var accountId = elem.socialListAccounts.val() + test.getAccountFollows(accountId) + }, + sendRequest: function (method, data, url, callback) { $.ajax({ method: method, @@ -94,7 +119,13 @@ test.testAccount(elem.socialListAccounts.val()) }) + elem.socialListAccounts.on('change', function () { + test.refreshData() + }) + test.getAccounts() + +// test.getAccountPosts() } if (OCA.Social === undefined) { diff --git a/lib/Controller/ActivityStreamsController.php b/lib/Controller/ActivityStreamsController.php index 5f55302a..912384f3 100644 --- a/lib/Controller/ActivityStreamsController.php +++ b/lib/Controller/ActivityStreamsController.php @@ -108,5 +108,46 @@ class ActivityStreamsController extends Controller { } + + + /** + * @NoAdminRequired + * + * @param int $accountId + * + * @return DataResponse + */ + public function posts(int $accountId): DataResponse { + try { + $account = $this->serviceAccountsService->getAccount($this->userId, $accountId); + + $result = $this->activityStreamsService->posts($account); + + return $this->success($result); + } catch (Exception $e) { + return $this->fail($e->getMessage()); + } + } + + + /** + * @NoAdminRequired + * + * @param int $accountId + * + * @return DataResponse + */ + public function follows(int $accountId): DataResponse { + try { + $account = $this->serviceAccountsService->getAccount($this->userId, $accountId); + $result = $this->activityStreamsService->follows($account); + + return $this->success($result); + } catch (Exception $e) { + return $this->fail($e->getMessage()); + } + } + + } diff --git a/lib/Controller/ServiceAccountsController.php b/lib/Controller/ServiceAccountsController.php index 0cbec6ca..242bf70c 100644 --- a/lib/Controller/ServiceAccountsController.php +++ b/lib/Controller/ServiceAccountsController.php @@ -132,7 +132,6 @@ class ServiceAccountsController extends Controller { 'protocol' => 'OAuth2', 'authorizationUrl' => $authUrl ]; - $this->miscService->log('___' . json_encode($data)); return $this->success($data); } catch (Exception $e) { diff --git a/lib/Db/ServiceAccountsRequest.php b/lib/Db/ServiceAccountsRequest.php index 6020cfcd..c5e3a670 100644 --- a/lib/Db/ServiceAccountsRequest.php +++ b/lib/Db/ServiceAccountsRequest.php @@ -75,6 +75,7 @@ class ServiceAccountsRequest extends ServiceAccountsRequestBuilder { $qb->setValue('service_id', $qb->createNamedParameter($service->getId())) ->setValue('user_id', $qb->createNamedParameter($account->getUserId())) ->setValue('account', $qb->createNamedParameter($account->getAccount())) + ->setValue('account_id', $qb->createNamedParameter($account->getAccountId())) ->setValue('status', $qb->createNamedParameter($account->getStatus())) ->setValue('auth', $qb->createNamedParameter(json_encode($account->getAuthAll()))); diff --git a/lib/Db/ServiceAccountsRequestBuilder.php b/lib/Db/ServiceAccountsRequestBuilder.php index de821d53..aeabb3ff 100644 --- a/lib/Db/ServiceAccountsRequestBuilder.php +++ b/lib/Db/ServiceAccountsRequestBuilder.php @@ -77,8 +77,8 @@ class ServiceAccountsRequestBuilder extends CoreRequestBuilder { /** @noinspection PhpMethodParametersCountMismatchInspection */ $qb->select( - 'a.id', 'a.service_id', 'a.user_id', 'a.account', 'a.status', 'a.auth', 'a.config', - 'a.creation' + 'a.id', 'a.service_id', 'a.user_id', 'a.account', 'a.account_id', 'a.status', 'a.auth', + 'a.config', 'a.creation' ) ->from(self::TABLE_ACCOUNTS, 'a'); @@ -118,6 +118,7 @@ class ServiceAccountsRequestBuilder extends CoreRequestBuilder { $account->setService($service) ->setUserId($data['user_id']) ->setAccount($this->get('account', $data, '')) + ->setAccountId($this->getInt('account_id', $data, 0)) ->setStatus($this->getInt('status', $data, 0)) ->setAuthAll(json_decode($this->get('auth', $data, '[]'), true)) ->setConfigAll(json_decode($this->get('config', $data, '[]'), true)) diff --git a/lib/Exceptions/APIRequestException.php b/lib/Exceptions/APIRequestException.php new file mode 100644 index 00000000..90e22594 --- /dev/null +++ b/lib/Exceptions/APIRequestException.php @@ -0,0 +1,8 @@ +<?php + +namespace OCA\Social\Exceptions; + +class APIRequestException extends \Exception { + +} + diff --git a/lib/Model/ServiceAccount.php b/lib/Model/ServiceAccount.php index 4010a504..31b148f6 100644 --- a/lib/Model/ServiceAccount.php +++ b/lib/Model/ServiceAccount.php @@ -46,6 +46,9 @@ class ServiceAccount implements \JsonSerializable { private $account = ''; /** @var int */ + private $accountId = 0; + + /** @var int */ private $status = 0; /** @var array */ @@ -126,6 +129,25 @@ class ServiceAccount implements \JsonSerializable { /** + * @return int + */ + public function getAccountId(): int { + return $this->accountId; + } + + /** + * @param int $accountId + * + * @return ServiceAccount + */ + public function setAccountId(int $accountId): ServiceAccount { + $this->accountId = $accountId; + + return $this; + } + + + /** * @return string */ public function getAccount(): string { @@ -143,6 +165,7 @@ class ServiceAccount implements \JsonSerializable { return $this; } + /** * @return int */ @@ -310,12 +333,13 @@ class ServiceAccount implements \JsonSerializable { */ public function jsonSerialize(): array { return [ - 'id' => $this->getId(), - 'service' => $this->getService(), - 'userId' => $this->getUserId(), - 'account' => $this->getAccount(), - 'auth' => $this->getAuthAll(), - 'creation' => $this->getCreation() + 'id' => $this->getId(), + 'service' => $this->getService(), + 'userId' => $this->getUserId(), + 'account' => $this->getAccount(), + 'account_id' => $this->getAccountId(), + 'auth' => $this->getAuthAll(), + 'creation' => $this->getCreation() ]; } diff --git a/lib/Service/ActivityStreamsService.php b/lib/Service/ActivityStreamsService.php index 1c068464..4091877f 100644 --- a/lib/Service/ActivityStreamsService.php +++ b/lib/Service/ActivityStreamsService.php @@ -44,6 +44,9 @@ class ActivityStreamsService { const URL_CREATE_APP = '/api/v1/apps'; const URL_VERIFY_ACCOUNT = '/api/v1/accounts/verify_credentials'; const URL_TEST = '/api/v1/accounts/verify_credentials'; + const URL_ACCOUNT_POSTS = '/api/v1/accounts/verify_credentials'; + const URL_ACCOUNT_FOLLOWS = '/api/v1/accounts/:id/following'; + const URL_ACCOUNT_FOLLOWERS = '/api/v1/accounts/:id/followers'; use TOAuth2; @@ -99,6 +102,33 @@ class ActivityStreamsService { * @return array * @throws Exception */ + public function posts(ServiceAccount $account) { + $request = new Request(self::URL_ACCOUNT_POSTS, Request::TYPE_GET); + + return $this->request($account, $request); + } + + + /** + * @param ServiceAccount $account + * + * @return array + * @throws Exception + */ + public function follows(ServiceAccount $account) { + $request = new Request(self::URL_ACCOUNT_FOLLOWS, Request::TYPE_GET); + $request->addDataInt('id', $account->getAccountId()); + + return $this->request($account, $request); + } + + + /** + * @param ServiceAccount $account + * + * @return array + * @throws Exception + */ public function getAccountInformation(ServiceAccount $account) { $request = new Request(self::URL_VERIFY_ACCOUNT, Request::TYPE_GET); diff --git a/lib/Service/CurlService.php b/lib/Service/CurlService.php index bd31d2db..3560aa3a 100644 --- a/lib/Service/CurlService.php +++ b/lib/Service/CurlService.php @@ -31,6 +31,7 @@ namespace OCA\Social\Service; use daita\Model\Request; +use OCA\Social\Exceptions\APIRequestException; use OCA\Social\Exceptions\InvalidAccessTokenException; use OCA\Social\Exceptions\MovedPermanentlyException; use OCA\Social\Model\ServiceAccount; @@ -60,6 +61,7 @@ class CurlService { * @return array * @throws InvalidAccessTokenException * @throws MovedPermanentlyException + * @throws APIRequestException */ public function request(ServiceAccount $account, Request $request, bool $authed = true) { @@ -70,9 +72,9 @@ class CurlService { $result = curl_exec($curl); $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); - $this->parseRequestResultCode301($code); $this->parseRequestResultCode401($code); + $this->parseRequestResultCode404($code); // $this->parseRequestResultCode503($code); // $this->parseRequestResultCode500($code); // $this->parseRequestResult($result); @@ -115,10 +117,8 @@ class CurlService { * @return resource */ private function generateCurlRequest(ServiceAccount $account, Request $request) { - $service = $account->getService(); -// $service->setAddress('mastodon.social'); - $url = 'https://' . $service->getAddress() . $request->getUrl(); + $url = 'https://' . $service->getAddress() . $request->getParsedUrl(); if ($request->getType() !== Request::TYPE_GET) { $curl = curl_init($url); @@ -194,5 +194,16 @@ class CurlService { } } + /** + * @param int $code + * + * @throws APIRequestException + */ + private function parseRequestResultCode404($code) { + if ($code === 404) { + throw new APIRequestException('404 Not Found'); + } + } + } diff --git a/lib/Service/ServiceAccountsService.php b/lib/Service/ServiceAccountsService.php index 20e5f05d..351dc62f 100644 --- a/lib/Service/ServiceAccountsService.php +++ b/lib/Service/ServiceAccountsService.php @@ -151,6 +151,7 @@ class ServiceAccountsService { $this->checkAccountUniqueness($serviceId, $userId, $accountName); $account->setAccount($accountName); + $account->setAccountId($this->getInt('id', $info, 0)); $this->serviceAccountsRequest->create($account); } diff --git a/lib/Service/ServicesService.php b/lib/Service/ServicesService.php index 0bb1bcde..ba23d3ef 100644 --- a/lib/Service/ServicesService.php +++ b/lib/Service/ServicesService.php @@ -141,8 +141,6 @@ class ServicesService { $account = new ServiceAccount(); $account->setService($service); - $this->miscService->log('___' . $this->configService->getCloudAddress()); - $data = [ 'client_name' => 'Social@' . $this->configService->getCloudAddress(), 'redirect_uris' => $this->generateRedirectUrl($service->getId()), diff --git a/lib/Tools/Model/Request.php b/lib/Tools/Model/Request.php index 33480bf2..b19e3e98 100644 --- a/lib/Tools/Model/Request.php +++ b/lib/Tools/Model/Request.php @@ -81,6 +81,19 @@ class Request implements \JsonSerializable { return $this; } + /** + * @return string + */ + public function getParsedUrl(): string { + $url = $this->getUrl(); + $ak = array_keys($this->getData()); + foreach ($ak as $k) { + $url = str_replace(':' . $k, $this->data[$k], $url); + } + + return $url; + } + /** * @return string |