2020-08-28 02:35:57 +00:00
|
|
|
<?php
|
2022-04-15 11:34:01 +00:00
|
|
|
|
2020-08-28 02:35:57 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Nextcloud - Social Support
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later. See the COPYING file.
|
|
|
|
*
|
|
|
|
* @author Maxence Lange <maxence@artificial-owl.com>
|
|
|
|
* @copyright 2018, Maxence Lange <maxence@artificial-owl.com>
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Social\Controller;
|
|
|
|
|
2022-06-17 14:29:54 +00:00
|
|
|
use OCA\Social\Tools\Traits\TNCDataResponse;
|
2020-08-28 02:35:57 +00:00
|
|
|
use Exception;
|
|
|
|
use OCA\Social\AppInfo\Application;
|
|
|
|
use OCA\Social\Exceptions\AccountDoesNotExistException;
|
2020-09-21 10:53:54 +00:00
|
|
|
use OCA\Social\Exceptions\ClientNotFoundException;
|
2020-09-01 14:14:19 +00:00
|
|
|
use OCA\Social\Exceptions\InstanceDoesNotExistException;
|
|
|
|
use OCA\Social\Model\ActivityPub\ACore;
|
2020-08-28 02:35:57 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Actor\Person;
|
|
|
|
use OCA\Social\Model\ActivityPub\Stream;
|
2020-09-03 18:49:55 +00:00
|
|
|
use OCA\Social\Model\Client\Options\TimelineOptions;
|
2020-09-18 11:55:47 +00:00
|
|
|
use OCA\Social\Model\Client\SocialClient;
|
2020-08-28 02:35:57 +00:00
|
|
|
use OCA\Social\Service\AccountService;
|
|
|
|
use OCA\Social\Service\CacheActorService;
|
|
|
|
use OCA\Social\Service\ClientService;
|
|
|
|
use OCA\Social\Service\ConfigService;
|
|
|
|
use OCA\Social\Service\FollowService;
|
2020-09-01 14:14:19 +00:00
|
|
|
use OCA\Social\Service\InstanceService;
|
2020-08-28 02:35:57 +00:00
|
|
|
use OCA\Social\Service\MiscService;
|
|
|
|
use OCA\Social\Service\StreamService;
|
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
|
2020-09-03 01:56:36 +00:00
|
|
|
/**
|
|
|
|
* Class ApiController
|
|
|
|
*
|
|
|
|
* @package OCA\Social\Controller
|
|
|
|
*/
|
2020-08-28 02:35:57 +00:00
|
|
|
class ApiController extends Controller {
|
|
|
|
use TNCDataResponse;
|
|
|
|
|
2022-04-15 11:01:18 +00:00
|
|
|
private IUserSession $userSession;
|
|
|
|
private InstanceService $instanceService;
|
|
|
|
private ClientService $clientService;
|
|
|
|
private AccountService $accountService;
|
|
|
|
private CacheActorService $cacheActorService;
|
|
|
|
private FollowService $followService;
|
|
|
|
private StreamService $streamService;
|
|
|
|
private ConfigService $configService;
|
|
|
|
private MiscService $miscService;
|
|
|
|
private string $bearer = '';
|
|
|
|
private ?SocialClient $client = null;
|
|
|
|
private ?Person $viewer = null;
|
2020-08-28 02:35:57 +00:00
|
|
|
|
|
|
|
public function __construct(
|
2020-09-01 14:14:19 +00:00
|
|
|
IRequest $request, IUserSession $userSession, InstanceService $instanceService,
|
|
|
|
ClientService $clientService, AccountService $accountService, CacheActorService $cacheActorService,
|
|
|
|
FollowService $followService, StreamService $streamService, ConfigService $configService,
|
|
|
|
MiscService $miscService
|
2020-08-28 02:35:57 +00:00
|
|
|
) {
|
|
|
|
parent::__construct(Application::APP_NAME, $request);
|
|
|
|
|
|
|
|
$this->userSession = $userSession;
|
2020-09-01 14:14:19 +00:00
|
|
|
$this->instanceService = $instanceService;
|
2020-08-28 02:35:57 +00:00
|
|
|
$this->clientService = $clientService;
|
|
|
|
$this->accountService = $accountService;
|
|
|
|
$this->cacheActorService = $cacheActorService;
|
|
|
|
$this->followService = $followService;
|
|
|
|
$this->streamService = $streamService;
|
|
|
|
$this->configService = $configService;
|
|
|
|
$this->miscService = $miscService;
|
|
|
|
|
|
|
|
$authHeader = trim($this->request->getHeader('Authorization'));
|
2020-09-01 14:14:19 +00:00
|
|
|
if (strpos($authHeader, ' ')) {
|
|
|
|
list($authType, $authToken) = explode(' ', $authHeader);
|
|
|
|
if (strtolower($authType) === 'bearer') {
|
|
|
|
$this->bearer = $authToken;
|
|
|
|
}
|
2020-08-28 02:35:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-18 11:55:47 +00:00
|
|
|
/**
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
public function appsCredentials() {
|
|
|
|
try {
|
|
|
|
$this->initViewer(true);
|
|
|
|
|
|
|
|
if ($this->client === null) {
|
|
|
|
return new DataResponse(
|
|
|
|
[
|
2022-04-15 11:34:01 +00:00
|
|
|
'name' => 'Nextcloud Social',
|
2020-09-18 11:55:47 +00:00
|
|
|
'website' => 'https://github.com/nextcloud/social/'
|
|
|
|
], Http::STATUS_OK
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return new DataResponse(
|
|
|
|
[
|
2022-04-15 11:34:01 +00:00
|
|
|
'name' => $this->client->getAppName(),
|
2020-09-18 11:55:47 +00:00
|
|
|
'website' => $this->client->getAppWebsite()
|
|
|
|
], Http::STATUS_OK
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
2020-09-21 10:53:54 +00:00
|
|
|
return $this->error($e->getMessage());
|
2020-09-18 11:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-28 02:35:57 +00:00
|
|
|
/**
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
*
|
2020-09-01 14:14:19 +00:00
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
public function verifyCredentials() {
|
|
|
|
try {
|
|
|
|
$this->initViewer(true);
|
|
|
|
|
|
|
|
return new DataResponse($this->viewer, Http::STATUS_OK);
|
|
|
|
} catch (Exception $e) {
|
2020-09-21 10:53:54 +00:00
|
|
|
return $this->error($e->getMessage());
|
2020-09-01 14:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-03 18:49:55 +00:00
|
|
|
/**
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
public function customEmojis(): DataResponse {
|
|
|
|
return new DataResponse([], Http::STATUS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
public function savedSearches(): DataResponse {
|
|
|
|
try {
|
|
|
|
$this->initViewer(true);
|
|
|
|
|
|
|
|
return new DataResponse([], Http::STATUS_OK);
|
|
|
|
} catch (Exception $e) {
|
2020-09-21 10:53:54 +00:00
|
|
|
return $this->error($e->getMessage());
|
2020-09-03 18:49:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
public function notifications(): DataResponse {
|
|
|
|
try {
|
|
|
|
$this->initViewer(true);
|
|
|
|
|
|
|
|
return new DataResponse([], Http::STATUS_OK);
|
|
|
|
} catch (Exception $e) {
|
2020-09-21 10:53:54 +00:00
|
|
|
return $this->error($e->getMessage());
|
2020-09-03 18:49:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-01 14:14:19 +00:00
|
|
|
/**
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
* @throws InstanceDoesNotExistException
|
|
|
|
*/
|
|
|
|
public function instance(): DataResponse {
|
|
|
|
$local = $this->instanceService->getLocal(Stream::FORMAT_LOCAL);
|
|
|
|
|
|
|
|
return new DataResponse($local, Http::STATUS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
*
|
2020-08-28 02:35:57 +00:00
|
|
|
* @param string $timeline
|
|
|
|
* @param int $limit
|
2020-10-15 16:43:15 +00:00
|
|
|
* @param int $max_id
|
|
|
|
* @param int $min_id
|
2020-08-28 02:35:57 +00:00
|
|
|
* @return DataResponse
|
|
|
|
*/
|
2020-10-15 16:43:15 +00:00
|
|
|
public function timelines(string $timeline, int $limit = 20, int $max_id = 0, int $min_id = 0): DataResponse {
|
2020-09-03 18:49:55 +00:00
|
|
|
$options = new TimelineOptions($this->request);
|
|
|
|
$options->setFormat(Stream::FORMAT_LOCAL);
|
|
|
|
$options->setTimeline($timeline);
|
2020-09-21 10:53:54 +00:00
|
|
|
$options->setLimit($limit);
|
2020-10-15 16:43:15 +00:00
|
|
|
$options->setMaxId($max_id);
|
|
|
|
$options->setMinId($min_id);
|
2020-09-03 18:49:55 +00:00
|
|
|
|
2020-08-28 02:35:57 +00:00
|
|
|
try {
|
|
|
|
$this->initViewer(true);
|
2020-09-03 18:49:55 +00:00
|
|
|
$posts = $this->streamService->getTimeline($options);
|
2020-08-28 02:35:57 +00:00
|
|
|
|
|
|
|
return new DataResponse($posts, Http::STATUS_OK);
|
|
|
|
} catch (Exception $e) {
|
2020-09-21 10:53:54 +00:00
|
|
|
return $this->error($e->getMessage());
|
2020-08-28 02:35:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param bool $exception
|
|
|
|
*
|
|
|
|
* @return bool
|
2020-09-21 10:53:54 +00:00
|
|
|
* @throws ClientNotFoundException
|
2020-08-28 02:35:57 +00:00
|
|
|
*/
|
|
|
|
private function initViewer(bool $exception = false): bool {
|
|
|
|
try {
|
2020-09-01 14:14:19 +00:00
|
|
|
$userId = $this->currentSession();
|
|
|
|
|
2020-09-02 02:59:06 +00:00
|
|
|
$this->miscService->log(
|
|
|
|
'[ApiController] initViewer: ' . $userId . ' (bearer=' . $this->bearer . ')', 0
|
|
|
|
);
|
|
|
|
|
2020-09-01 14:14:19 +00:00
|
|
|
$account = $this->accountService->getActorFromUserId($userId);
|
|
|
|
$this->viewer = $this->cacheActorService->getFromLocalAccount($account->getPreferredUsername());
|
|
|
|
$this->viewer->setExportFormat(ACore::FORMAT_LOCAL);
|
2020-08-28 02:35:57 +00:00
|
|
|
|
|
|
|
$this->streamService->setViewer($this->viewer);
|
|
|
|
$this->followService->setViewer($this->viewer);
|
|
|
|
$this->cacheActorService->setViewer($this->viewer);
|
2020-09-01 14:14:19 +00:00
|
|
|
|
|
|
|
return true;
|
2020-08-28 02:35:57 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
if ($exception) {
|
2020-09-21 10:53:54 +00:00
|
|
|
throw new ClientNotFoundException('the access_token was revoked');
|
2020-08-28 02:35:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 14:14:19 +00:00
|
|
|
return false;
|
2020-08-28 02:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
* @throws AccountDoesNotExistException
|
2020-09-21 10:53:54 +00:00
|
|
|
* @throws ClientNotFoundException
|
2020-08-28 02:35:57 +00:00
|
|
|
*/
|
2020-09-01 14:14:19 +00:00
|
|
|
private function currentSession(): string {
|
2020-08-28 02:35:57 +00:00
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if ($user !== null) {
|
|
|
|
return $user->getUID();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->bearer !== '') {
|
2020-09-18 11:55:47 +00:00
|
|
|
$this->client = $this->clientService->getFromToken($this->bearer);
|
2020-08-28 02:35:57 +00:00
|
|
|
|
2020-09-18 11:55:47 +00:00
|
|
|
return $this->client->getAuthUserId();
|
2020-08-28 02:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new AccountDoesNotExistException('userId not defined');
|
|
|
|
}
|
|
|
|
|
2020-09-21 10:53:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $error
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
private function error(string $error): DataResponse {
|
|
|
|
return new DataResponse(['error' => $error], Http::STATUS_UNAUTHORIZED);
|
|
|
|
}
|
2020-08-28 02:35:57 +00:00
|
|
|
}
|