diff options
Diffstat (limited to 'lib/Controller')
-rw-r--r-- | lib/Controller/LocalController.php | 61 | ||||
-rw-r--r-- | lib/Controller/OStatusController.php | 188 |
2 files changed, 246 insertions, 3 deletions
diff --git a/lib/Controller/LocalController.php b/lib/Controller/LocalController.php index 8c9cf257..57d67d96 100644 --- a/lib/Controller/LocalController.php +++ b/lib/Controller/LocalController.php @@ -48,6 +48,7 @@ use OCA\Social\Service\FollowService; use OCA\Social\Service\MiscService; use OCA\Social\Service\NoteService; use OCA\Social\Service\PostService; +use OCA\Social\Service\SearchService; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -83,6 +84,9 @@ class LocalController extends Controller { /** @var NoteService */ private $noteService; + /** @var SearchService */ + private $searchService; + /** @var AccountService */ private $accountService; @@ -107,14 +111,15 @@ class LocalController extends Controller { * @param FollowService $followService * @param PostService $postService * @param NoteService $noteService + * @param SearchService $searchService * @param DocumentService $documentService * @param MiscService $miscService */ public function __construct( IRequest $request, $userId, AccountService $accountService, CacheActorService $cacheActorService, FollowService $followService, - PostService $postService, NoteService $noteService, DocumentService $documentService, - MiscService $miscService + PostService $postService, NoteService $noteService, SearchService $searchService, + DocumentService $documentService, MiscService $miscService ) { parent::__construct(Application::APP_NAME, $request); @@ -122,6 +127,7 @@ class LocalController extends Controller { $this->cacheActorService = $cacheActorService; $this->accountService = $accountService; $this->noteService = $noteService; + $this->searchService = $searchService; $this->postService = $postService; $this->followService = $followService; $this->documentService = $documentService; @@ -148,6 +154,7 @@ class LocalController extends Controller { $post->setTo($this->getArray('to', $data, [])); $post->addTo($this->get('to', $data, '')); $post->setType($this->get('type', $data, Stream::TYPE_PUBLIC)); + $post->setHashtags($this->getArray('hashtags', $data, [])); /** @var ACore $activity */ $token = $this->postService->createPost($post, $activity); @@ -210,7 +217,7 @@ class LocalController extends Controller { } - /** + /** * @NoAdminRequired * * @param int $since @@ -294,6 +301,30 @@ class LocalController extends Controller { } } + + /** + * Get timeline + * + * @NoAdminRequired + * + * @param string $hashtag + * @param int $since + * @param int $limit + * + * @return DataResponse + */ + public function streamTag(string $hashtag, int $since = 0, int $limit = 5): DataResponse { + try { + $this->initViewer(true); + $posts = $this->noteService->getStreamLocalTag($this->viewer, $hashtag, $since, $limit); + + return $this->success($posts); + } catch (Exception $e) { + return $this->fail($e); + } + } + + /** * Get timeline * @@ -581,6 +612,30 @@ class LocalController extends Controller { } + /** // TODO - remove this tag + * + * @NoCSRFRequired + * @NoAdminRequired + * + * @param string $search + * + * @return DataResponse + * @throws Exception + */ + public function search(string $search): DataResponse { + $search = trim($search); + $this->initViewer(); + + $result = [ + 'accounts' => $this->searchService->searchAccounts($search), + 'hashtags' => $this->searchService->searchHashtags($search), + 'content' => $this->searchService->searchStreamContent($search) + ]; + + return $this->success($result); + } + + /** * @NoAdminRequired * diff --git a/lib/Controller/OStatusController.php b/lib/Controller/OStatusController.php new file mode 100644 index 00000000..182c4a3d --- /dev/null +++ b/lib/Controller/OStatusController.php @@ -0,0 +1,188 @@ +<?php +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; + + +use daita\MySmallPhpTools\Exceptions\ArrayNotFoundException; +use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse; +use daita\MySmallPhpTools\Traits\TArrayTools; +use Exception; +use OCA\Social\AppInfo\Application; +use OCA\Social\Exceptions\RetrieveAccountFormatException; +use OCA\Social\Service\AccountService; +use OCA\Social\Service\CacheActorService; +use OCA\Social\Service\CurlService; +use OCA\Social\Service\MiscService; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\Response; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\IRequest; +use OCP\IUserManager; +use OCP\IUserSession; + + +class OStatusController extends Controller { + + + use TNCDataResponse; + use TArrayTools; + + + /** @var CacheActorService */ + private $cacheActorService; + + /** @var AccountService */ + private $accountService; + + /** @var CurlService */ + private $curlService; + + /** @var MiscService */ + private $miscService; + + /** @var IUserManager */ + private $userSession; + + + /** + * OStatusController constructor. + * + * @param IRequest $request + * @param CacheActorService $cacheActorService + * @param AccountService $accountService + * @param CurlService $curlService + * @param MiscService $miscService + */ + public function __construct( + IRequest $request, CacheActorService $cacheActorService, AccountService $accountService, + CurlService $curlService, MiscService $miscService, IUserSession $userSession + ) { + parent::__construct(Application::APP_NAME, $request); + + $this->cacheActorService = $cacheActorService; + $this->accountService = $accountService; + $this->curlService = $curlService; + $this->miscService = $miscService; + $this->userSession = $userSession; + } + + + /** + * @NoCSRFRequired + * @NoAdminRequired + * + * @param string $uri + * + * @return Response + */ + public function subscribe(string $uri): Response { + + try { + $actor = $this->cacheActorService->getFromAccount($uri); + + $user = $this->userSession->getUser(); + if ($user === null) { + return $this->fail('Failed to retrieve current user'); + } + + return new TemplateResponse('social', 'ostatus', [ + 'serverData' => [ + 'account' => $actor->getAccount(), + 'currentUser' => [ + 'uid' => $user->getUID(), + 'displayName' => $user->getDisplayName(), + ] + ] + ], 'guest'); + } catch (Exception $e) { + return $this->fail($e); + } + } + + + /** + * @NoCSRFRequired + * @NoAdminRequired + * @PublicPage + * + * @param string $local + * @return Response + */ + public function followRemote(string $local): Response { + try { + $following = $this->accountService->getActor($local); + + return new TemplateResponse('social', 'ostatus', [ + 'serverData' => [ + 'local' => $local, + 'account' => $following->getAccount() + ] + ], 'guest'); + } catch (\Exception $e) { + return $this->fail($e); + } + } + + /** + * @NoCSRFRequired + * @NoAdminRequired + * @PublicPage + * + * @param string $local + * @param string $account + * + * @return Response + */ + public function getLink(string $local, string $account): Response { + + try { + $following = $this->accountService->getActor($local); + $result = $this->curlService->webfingerAccount($account); + + try { + $link = $this->extractArray( + 'rel', 'http://ostatus.org/schema/1.0/subscribe', + $this->getArray('links', $result) + ); + } catch (ArrayNotFoundException $e) { + throw new RetrieveAccountFormatException(); + } + + $template = $this->get('template', $link, ''); + $url = str_replace('{uri}', $following->getAccount(), $template); + + return $this->success(['url' => $url]); + } catch (Exception $e) { + return $this->fail($e); + } + } + +} + |