From 96d5e69deea598d901ff074504aa17ed1f18b8ce Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Mon, 12 Nov 2018 21:56:22 -0100 Subject: [PATCH] new routes Signed-off-by: Maxence Lange --- appinfo/routes.php | 14 +- lib/Controller/AccountController.php | 41 ++++-- lib/Controller/ActivityPubController.php | 71 +++++++--- lib/Controller/LocalController.php | 171 +++++++++++++++++++++++ lib/Controller/NavigationController.php | 11 +- lib/Controller/SocialPubController.php | 12 +- 6 files changed, 276 insertions(+), 44 deletions(-) create mode 100644 lib/Controller/LocalController.php diff --git a/appinfo/routes.php b/appinfo/routes.php index 5b894353..5505d745 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -12,25 +12,27 @@ return [ ['name' => 'Navigation#navigate', 'url' => '/', 'verb' => 'GET'], ['name' => 'Navigation#timeline', 'url' => '/timeline/{path}', 'verb' => 'GET', 'requirements' => ['path' => '.+'], 'defaults' => ['path' => '']], ['name' => 'Navigation#account', 'url' => '/account/{path}', 'verb' => 'GET', 'requirements' => ['path' => '.+'], 'defaults' => ['path' => '']], - ['name' => 'Navigation#public', 'url' => '/{username}', 'verb' => 'GET'], +// ['name' => 'Navigation#public', 'url' => '/{username}', 'verb' => 'GET'], // ['name' => 'Account#create', 'url' => '/local/account/{username}', 'verb' => 'POST'], ['name' => 'Account#info', 'url' => '/local/account/{username}', 'verb' => 'GET'], - ['name' => 'ActivityPub#sharedInbox', 'url' => '/inbox', 'verb' => 'POST'], ['name' => 'ActivityPub#actor', 'url' => '/users/{username}', 'verb' => 'GET'], - ['name' => 'ActivityPub#aliasactor', 'url' => '/@{username}', 'verb' => 'GET'], + ['name' => 'ActivityPub#actorAlias', 'url' => '/@{username}', 'verb' => 'GET'], ['name' => 'ActivityPub#inbox', 'url' => '/@{username}/inbox', 'verb' => 'POST'], + ['name' => 'ActivityPub#sharedInbox', 'url' => '/inbox', 'verb' => 'POST'], ['name' => 'ActivityPub#outbox', 'url' => '/@{username}/outbox', 'verb' => 'POST'], ['name' => 'ActivityPub#followers', 'url' => '/@{username}/followers', 'verb' => 'GET'], ['name' => 'ActivityPub#following', 'url' => '/@{username}/following', 'verb' => 'GET'], - - ['name' => 'SocialPub#displayPost', 'url' => '/@{username}/{postId}', 'verb' => 'GET'] - , ['name' => 'ActivityPub#test', 'url' => '/inbox/{username}', 'verb' => 'POST'], + ['name' => 'SocialPub#displayPost', 'url' => '/@{username}/{postId}', 'verb' => 'GET'], + + ['name' => 'Local#newPost', 'url' => '/api/v1/post', 'verb' => 'POST'], + ['name' => 'Local#timeline', 'url' => '/api/v1/timeline', 'verb' => 'PUT'], + ['name' => 'Local#direct', 'url' => '/api/v1/direct', 'verb' => 'PUT'] ] ]; diff --git a/lib/Controller/AccountController.php b/lib/Controller/AccountController.php index b97e6e82..3c15c6a6 100644 --- a/lib/Controller/AccountController.php +++ b/lib/Controller/AccountController.php @@ -27,8 +27,10 @@ declare(strict_types=1); * */ + namespace OCA\Social\Controller; + use daita\MySmallPhpTools\Traits\TNCDataResponse; use Exception; use OCA\Social\AppInfo\Application; @@ -39,7 +41,6 @@ use OCP\Accounts\IAccountManager; use OCP\Accounts\IAccountProperty; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\DataResponse; -use OCP\AppFramework\Http\NotFoundResponse; use OCP\AppFramework\Http\Response; use OCP\IRequest; use OCP\IUserManager; @@ -47,11 +48,16 @@ use OCP\IUserManager; class AccountController extends Controller { + use TNCDataResponse; + /** @var string */ private $userId; + /** @var IUserManager */ + private $userManager; + /** @var ConfigService */ private $configService; @@ -69,24 +75,27 @@ class AccountController extends Controller { * AccountController constructor. * * @param IRequest $request - * @param string $userId + * @param IUserManager $userManager * @param ConfigService $configService * @param ActorService $actorService * @param MiscService $miscService + * @param IAccountManager $accountManager + * @param string $userId */ public function __construct( - IRequest $request, ConfigService $configService, + IRequest $request, $userId, IUserManager $userManager, ConfigService $configService, ActorService $actorService, MiscService $miscService, - IAccountManager $accountManager, IUserManager $userManager, string $userId = null + IAccountManager $accountManager ) { parent::__construct(Application::APP_NAME, $request); $this->userId = $userId; + $this->userManager = $userManager; + $this->accountManager = $accountManager; + $this->configService = $configService; $this->actorService = $actorService; $this->miscService = $miscService; - $this->accountManager = $accountManager; - $this->userManager = $userManager; } @@ -94,8 +103,6 @@ class AccountController extends Controller { * Called by the frontend to create a new Social account * * @NoAdminRequired - * @NoCSRFRequired - * @NoAdminRequired * @NoSubAdminRequired * * @param string $username @@ -112,13 +119,15 @@ class AccountController extends Controller { } } + /** * @PublicPage - * @NoAdminRequired * @NoCSRFRequired * @NoAdminRequired * @NoSubAdminRequired + * * @param string $username + * * @return DataResponse */ public function info(string $username): Response { @@ -131,16 +140,25 @@ class AccountController extends Controller { $props['posts'] = 1; $props['following'] = 2; $props['followers'] = 3; + return new DataResponse($props); } $account = $this->accountManager->getAccount($user); /** @var IAccountProperty[] $props */ $props = $account->getFilteredProperties(IAccountManager::VISIBILITY_PUBLIC, null); if ($this->userId !== null) { - $props = array_merge($props, $account->getFilteredProperties(IAccountManager::VISIBILITY_CONTACTS_ONLY, null)); + $props = array_merge( + $props, + $account->getFilteredProperties(IAccountManager::VISIBILITY_CONTACTS_ONLY, null) + ); } if (\array_key_exists('avatar', $props)) { - $props['avatar']->setValue(\OC::$server->getURLGenerator()->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $username, 'size' => 128])); + $props['avatar']->setValue( + \OC::$server->getURLGenerator() + ->linkToRouteAbsolute( + 'core.avatar.getAvatar', ['userId' => $username, 'size' => 128] + ) + ); } // Add counters @@ -148,6 +166,7 @@ class AccountController extends Controller { $props['posts'] = 1; $props['following'] = 2; $props['followers'] = 3; + return new DataResponse($props); } diff --git a/lib/Controller/ActivityPubController.php b/lib/Controller/ActivityPubController.php index c3fad8b3..0c4bc52b 100644 --- a/lib/Controller/ActivityPubController.php +++ b/lib/Controller/ActivityPubController.php @@ -33,8 +33,11 @@ namespace OCA\Social\Controller; use daita\MySmallPhpTools\Traits\TNCDataResponse; use Exception; use OCA\Social\AppInfo\Application; -use OCA\Social\Service\ActivityPubService; +use OCA\Social\Db\NotesRequest; +use OCA\Social\Service\ActivityPub\FollowService; +use OCA\Social\Service\ActivityService; use OCA\Social\Service\ActorService; +use OCA\Social\Service\ImportService; use OCA\Social\Service\MiscService; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\Response; @@ -50,12 +53,21 @@ class ActivityPubController extends Controller { /** @var SocialPubController */ private $socialPubController; - /** @var ActivityPubService */ - private $activityPubService; + /** @var ActivityService */ + private $activityService; + + /** @var ImportService */ + private $importService; + + /** @var FollowService */ + private $followService; /** @var ActorService */ private $actorService; + /** @var NotesRequest */ + private $notesRequest; + /** @var MiscService */ private $miscService; @@ -63,22 +75,30 @@ class ActivityPubController extends Controller { /** * ActivityPubController constructor. * - * @param SocialPubController $socialPubController - * @param ActivityPubService $activityPubService - * @param ActorService $actorService * @param IRequest $request + * @param SocialPubController $socialPubController + * @param ActivityService $activityService + * @param ImportService $importService + * @param FollowService $followService + * @param ActorService $actorService + * @param NotesRequest $notesRequest * @param MiscService $miscService */ public function __construct( - SocialPubController $socialPubController, ActivityPubService $activityPubService, - ActorService $actorService, IRequest $request, + IRequest $request, SocialPubController $socialPubController, + ActivityService $activityService, ImportService $importService, + FollowService $followService, ActorService $actorService, NotesRequest $notesRequest, MiscService $miscService ) { parent::__construct(Application::APP_NAME, $request); $this->socialPubController = $socialPubController; - $this->activityPubService = $activityPubService; + + $this->activityService = $activityService; + $this->importService = $importService; + $this->followService = $followService; $this->actorService = $actorService; + $this->notesRequest = $notesRequest; $this->miscService = $miscService; } @@ -104,9 +124,8 @@ class ActivityPubController extends Controller { } try { -// $this->activityPubService->generateActor($userId); - $actor = $this->actorService->getActor($username); + $actor->setTopLevel(true); return $this->directSuccess($actor); } catch (Exception $e) { @@ -114,6 +133,7 @@ class ActivityPubController extends Controller { } } + /** * Alias to the actor() method. * @@ -127,7 +147,7 @@ class ActivityPubController extends Controller { * * @return Response */ - public function aliasactor(string $username): Response { + public function actorAlias(string $username): Response { return $this->actor($username); } @@ -155,18 +175,24 @@ class ActivityPubController extends Controller { * @NoCSRFRequired * @PublicPage * - * @param $username + * @param string $username * * @return Response */ public function inbox(string $username): Response { try { - $this->actorService->getActor($username); - $this->activityPubService->checkRequest($this->request); -// $this->noteService->receiving(file_get_contents('php://input')); + $actor = $this->actorService->getActor($username); + $this->activityService->checkRequest($this->request); $body = file_get_contents('php://input'); + $activity = $this->importService->import($body); + + try { + $this->activityService->save($activity); + } catch (Exception $e) { + } + return $this->success([]); } catch (Exception $e) { return $this->fail($e->getMessage()); @@ -212,12 +238,21 @@ class ActivityPubController extends Controller { * * @return Response */ - public function followers(string $username): Response { + public function followers(string $username, $data): Response { + if (!$this->checkSourceActivityStreams()) { return $this->socialPubController->followers($username); } - return $this->success([$username]); + try { + $actor = $this->actorService->getActor($username); + $followers = $this->followService->getFollowers($actor); + $followers->setTopLevel(true); + + return $this->directSuccess($followers); + } catch (Exception $e) { + return $this->fail($e->getMessage()); + } } diff --git a/lib/Controller/LocalController.php b/lib/Controller/LocalController.php new file mode 100644 index 00000000..4291e047 --- /dev/null +++ b/lib/Controller/LocalController.php @@ -0,0 +1,171 @@ + + * @copyright 2018, Maxence Lange + * @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 . + * + */ + +namespace OCA\Social\Controller; + + +use daita\MySmallPhpTools\Traits\TArrayTools; +use daita\MySmallPhpTools\Traits\TNCDataResponse; +use Exception; +use OCA\Social\AppInfo\Application; +use OCA\Social\Model\Post; +use OCA\Social\Service\ActivityPub\NoteService; +use OCA\Social\Service\ActorService; +use OCA\Social\Service\MiscService; +use OCA\Social\Service\PostService; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\DataResponse; +use OCP\IRequest; + + +/** + * Class LocalController + * + * @package OCA\Social\Controller + */ +class LocalController extends Controller { + + + use TArrayTools; + use TNCDataResponse; + + + /** @var string */ + private $userId; + + /** @var ActorService */ + private $actorService; + + /** @var PostService */ + private $postService; + + /** @var NoteService */ + private $noteService; + + /** @var MiscService */ + private $miscService; + + + /** + * NavigationController constructor. + * + * @param IRequest $request + * @param string $userId + * @param ActorService $actorService + * @param PostService $postService + * @param NoteService $noteService + * @param MiscService $miscService + */ + public function __construct( + IRequest $request, string $userId, ActorService $actorService, PostService $postService, + NoteService $noteService, + MiscService $miscService + ) { + parent::__construct(Application::APP_NAME, $request); + + $this->userId = $userId; + + $this->actorService = $actorService; + $this->postService = $postService; + $this->noteService = $noteService; + $this->miscService = $miscService; + } + + + /** + * Create a new post. + * + * @NoAdminRequired + * @NoSubAdminRequired + * + * @param array $data + * + * @return DataResponse + */ + public function newPost(array $data): DataResponse { + try { + $post = new Post($this->userId); + $post->setContent($this->get('content', $data, '')); + $post->setReplyTo($this->get('replyTo', $data, '')); + $post->setTo($this->getArray('to', $data, [])); + $post->addTo($this->get('to', $data, '')); + + $result = $this->postService->createPost($post); + + return $this->success($result); + } catch (Exception $e) { + return $this->fail($e->getMessage()); + } + } + + + /** + * Get timeline + * + * @NoCSRFRequired + * @NoAdminRequired + * @NoSubAdminRequired + * + * @return DataResponse + */ + public function timeline(): DataResponse { + +// $this->miscService->log('timeline: ' . json_encode($data)); + + try { + $posts = $this->noteService->getTimeline(); + + return $this->success($posts); + } catch (Exception $e) { + return $this->fail($e->getMessage()); + } + } + + + /** + * @NoAdminRequired + * @NoSubAdminRequired + * + * @return DataResponse + */ + public function direct(): DataResponse { + + try { + $actor = $this->actorService->getActorFromUserId($this->userId); + $posts = $this->noteService->getNotesForActor($actor); + + return $this->success($posts); + } catch (Exception $e) { + return $this->fail($e->getMessage()); + } + + + } + +} diff --git a/lib/Controller/NavigationController.php b/lib/Controller/NavigationController.php index 631f9935..bf8f1af7 100644 --- a/lib/Controller/NavigationController.php +++ b/lib/Controller/NavigationController.php @@ -30,6 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Controller; +use daita\MySmallPhpTools\Traits\TArrayTools; use OC\Accounts\AccountManager; use OC\User\NoUserException; use OCA\Social\AppInfo\Application; @@ -47,6 +48,9 @@ use OCP\IURLGenerator; class NavigationController extends Controller { + + use TArrayTools; + /** @var string */ private $userId; @@ -114,8 +118,6 @@ class NavigationController extends Controller { // we do nothing } - - return new TemplateResponse(Application::APP_NAME, 'main', $data); } @@ -152,10 +154,12 @@ class NavigationController extends Controller { * @PublicPage * * @param $username + * * @return RedirectResponse|PublicTemplateResponse */ public function public($username) { - if (\OC::$server->getUserSession()->isLoggedIn()) { + if (\OC::$server->getUserSession() + ->isLoggedIn()) { return $this->navigate(); } @@ -166,6 +170,7 @@ class NavigationController extends Controller { ]; $page = new PublicTemplateResponse(Application::APP_NAME, 'main', $data); $page->setHeaderTitle($this->l10n->t('Social') . ' ' . $username); + return $page; } diff --git a/lib/Controller/SocialPubController.php b/lib/Controller/SocialPubController.php index 51ee6f09..f4fafb4d 100644 --- a/lib/Controller/SocialPubController.php +++ b/lib/Controller/SocialPubController.php @@ -32,7 +32,7 @@ namespace OCA\Social\Controller; use daita\MySmallPhpTools\Traits\TNCDataResponse; use OCA\Social\AppInfo\Application; -use OCA\Social\Service\ActivityPubService; +use OCA\Social\Service\ActivityService; use OCA\Social\Service\ActorService; use OCA\Social\Service\MiscService; use OCP\AppFramework\Controller; @@ -45,8 +45,8 @@ class SocialPubController extends Controller { use TNCDataResponse; - /** @var ActivityPubService */ - private $activityPubService; + /** @var ActivityService */ + private $activityService; /** @var ActorService */ private $actorService; @@ -58,18 +58,18 @@ class SocialPubController extends Controller { /** * SocialPubController constructor. * - * @param ActivityPubService $activityPubService + * @param ActivityService $activityService * @param ActorService $actorService * @param IRequest $request * @param MiscService $miscService */ public function __construct( - ActivityPubService $activityPubService, ActorService $actorService, IRequest $request, + ActivityService $activityService, ActorService $actorService, IRequest $request, MiscService $miscService ) { parent::__construct(Application::APP_NAME, $request); - $this->activityPubService = $activityPubService; + $this->activityService = $activityService; $this->actorService = $actorService; $this->miscService = $miscService; }