Addind accountId and follows

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/2/head
Maxence Lange 2018-09-20 14:07:22 +02:00
rodzic 04972c6118
commit aa7a43ecac
15 zmienionych plików z 193 dodań i 21 usunięć

Wyświetl plik

@ -83,6 +83,12 @@
<length>127</length>
</field>
<field>
<name>account_id</name>
<type>integer</type>
<length>7</length>
</field>
<field>
<name>status</name>
<type>integer</type>

Wyświetl plik

@ -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>

Wyświetl plik

@ -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'

Wyświetl plik

@ -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) {

Wyświetl plik

@ -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());
}
}
}

Wyświetl plik

@ -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) {

Wyświetl plik

@ -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())));

Wyświetl plik

@ -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))

Wyświetl plik

@ -0,0 +1,8 @@
<?php
namespace OCA\Social\Exceptions;
class APIRequestException extends \Exception {
}

Wyświetl plik

@ -45,6 +45,9 @@ class ServiceAccount implements \JsonSerializable {
/** @var string */
private $account = '';
/** @var int */
private $accountId = 0;
/** @var int */
private $status = 0;
@ -125,6 +128,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
*/
@ -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()
];
}

Wyświetl plik

@ -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;
@ -93,6 +96,33 @@ class ActivityStreamsService {
}
/**
* @param ServiceAccount $account
*
* @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
*

Wyświetl plik

@ -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');
}
}
}

Wyświetl plik

@ -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);
}

Wyświetl plik

@ -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()),

Wyświetl plik

@ -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