Rework frontend controllers for page rendering

Signed-off-by: Julius Härtl <jus@bitgrid.net>
pull/244/head
Julius Härtl 2018-12-19 09:48:07 +01:00
rodzic e6b52524d4
commit 4253672fc1
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4C614C6ED2CDE6DF
4 zmienionych plików z 57 dodań i 249 usunięć

Wyświetl plik

@ -43,19 +43,11 @@ return [
'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#documentGet', 'url' => '/document/get', 'verb' => 'GET'],
['name' => 'Navigation#documentGetPublic', 'url' => '/document/public', 'verb' => 'GET'],
// ['name' => 'Account#create', 'url' => '/local/account/{username}', 'verb' => 'POST'],
['name' => 'Account#info', 'url' => '/local/account/{username}', 'verb' => 'GET'],
['name' => 'ActivityPub#actor', 'url' => '/users/{username}', 'verb' => 'GET'],
['name' => 'ActivityPub#actorAlias', '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'],

Wyświetl plik

@ -1,176 +0,0 @@
<?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\Traits\Nextcloud\TNCDataResponse;
use Exception;
use OCA\Social\AppInfo\Application;
use OCA\Social\Service\AccountService;
use OCA\Social\Service\ConfigService;
use OCA\Social\Service\MiscService;
use OCP\Accounts\IAccountManager;
use OCP\Accounts\IAccountProperty;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\Response;
use OCP\IRequest;
use OCP\IUserManager;
class AccountController extends Controller {
use TNCDataResponse;
/** @var string */
private $userId;
/** @var IUserManager */
private $userManager;
/** @var ConfigService */
private $configService;
/** @var AccountService */
private $actorService;
/** @var MiscService */
private $miscService;
/** @var IAccountManager */
private $accountManager;
/**
* AccountController constructor.
*
* @param IRequest $request
* @param IUserManager $userManager
* @param ConfigService $configService
* @param AccountService $actorService
* @param MiscService $miscService
* @param IAccountManager $accountManager
* @param string $userId
*/
public function __construct(
IRequest $request, $userId, IUserManager $userManager, ConfigService $configService,
AccountService $actorService, MiscService $miscService,
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;
}
/**
* Called by the frontend to create a new Social account
*
* @NoAdminRequired
* @NoSubAdminRequired
*
* @param string $username
*
* @return DataResponse
*/
public function create(string $username): DataResponse {
try {
$this->actorService->createActor($this->userId, $username);
return $this->success([]);
} catch (Exception $e) {
return $this->fail($e);
}
}
/**
*
* // TODO - is it still used ? maybe using info from LocalController !?
*
* @PublicPage
* @NoCSRFRequired
*
* @param string $username
*
* @return DataResponse
*/
public function info(string $username): Response {
$user = $this->userManager->get($username);
if ($user === null) {
// TODO: Proper handling of external accounts
$props = [];
$props['cloudId'] = $username;
$props['displayname'] = ['value' => 'External account'];
$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)
);
}
if (\array_key_exists('avatar', $props)) {
$props['avatar']->setValue(
\OC::$server->getURLGenerator()
->linkToRouteAbsolute(
'core.avatar.getAvatar', ['userId' => $username, 'size' => 128]
)
);
}
// Add counters
$props['cloudId'] = $user->getCloudId();
$props['posts'] = 1;
$props['following'] = 2;
$props['followers'] = 3;
return new DataResponse($props);
}
}

Wyświetl plik

@ -238,6 +238,7 @@ class LocalController extends Controller {
/**
* @NoAdminRequired
* @NoSubAdminRequired
* @PublicPage
*
* @param string $username
* @param int $since
@ -424,6 +425,7 @@ class LocalController extends Controller {
/**
* @NoAdminRequired
* @NoSubAdminRequired
* @PublicPage
*
* @param string $username
*
@ -445,6 +447,7 @@ class LocalController extends Controller {
/**
* @NoAdminRequired
* @NoSubAdminRequired
* @PublicPage
*
* @param string $username
*
@ -467,6 +470,7 @@ class LocalController extends Controller {
/**
* @NoAdminRequired
* @NoSubAdminRequired
* @PublicPage
*
* @param string $username
*
@ -626,6 +630,15 @@ class LocalController extends Controller {
* @throws AccountDoesNotExistException
*/
private function initViewer(bool $exception = false) {
// only initialize viewer data for logged in users
if ($this->userId === null) {
// TODO: properly set viewer
$this->followService->setViewerId('guest');
$this->personService->setViewerId('guest');
$this->noteService->setViewerId('guest');
return;
}
try {
$this->viewer = $this->accountService->getActorFromUserId($this->userId, true);

Wyświetl plik

@ -31,16 +31,14 @@ namespace OCA\Social\Controller;
use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse;
use Exception;
use OCA\Social\AppInfo\Application;
use OCA\Social\Exceptions\CacheActorDoesNotExistException;
use OCA\Social\Model\ActivityPub\Actor\Person;
use OCA\Social\Service\AccountService;
use OCA\Social\Service\CacheActorService;
use OCA\Social\Service\FollowService;
use OCA\Social\Service\MiscService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
use OCP\AppFramework\Http\TemplateResponse;
@ -67,8 +65,8 @@ class SocialPubController extends Controller {
/** @var FollowService */
private $followService;
/** @var MiscService */
private $miscService;
/** @var NavigationController */
private $navigationController;
/**
@ -77,23 +75,49 @@ class SocialPubController extends Controller {
* @param $userId
* @param IRequest $request
* @param IL10N $l10n
* @param AccountService $accountService
* @param CacheActorService $cacheActorService
* @param FollowService $followService
* @param MiscService $miscService
* @param NavigationController $navigationController
*/
public function __construct(
$userId, IRequest $request, IL10N $l10n, AccountService $accountService,
CacheActorService $cacheActorService, FollowService $followService, MiscService $miscService
$userId,
IRequest $request,
IL10N $l10n,
CacheActorService $cacheActorService,
NavigationController $navigationController
) {
parent::__construct(Application::APP_NAME, $request);
$this->userId = $userId;
$this->l10n = $l10n;
$this->accountService = $accountService;
$this->cacheActorService = $cacheActorService;
$this->followService = $followService;
$this->miscService = $miscService;
$this->navigationController = $navigationController;
}
private function renderPage($username): Response {
if ($this->userId) {
return $this->navigationController->navigate('');
}
$data = [
'serverData' => [
'public' => true,
],
'application' => 'Social'
];
$status = Http::STATUS_OK;
try {
$actor = $this->cacheActorService->getFromLocalAccount($username);
$displayName = $actor->getName() !== '' ? $actor->getName() : $actor->getPreferredUsername();
$data['application'] = $displayName . ' - ' . $data['application'];
} catch (CacheActorDoesNotExistException $e) {
$status = Http::STATUS_NOT_FOUND;
} catch (\Exception $e) {
return $this->fail($e);
}
$page = new PublicTemplateResponse(Application::APP_NAME, 'main', $data);
$page->setStatus($status);
$page->setHeaderTitle($this->l10n->t('Social'));
return $page;
}
@ -109,42 +133,7 @@ class SocialPubController extends Controller {
* @return Response
*/
public function actor(string $username): Response {
try {
$actor = $this->cacheActorService->getFromLocalAccount($username);
$actor->setCompleteDetails(true);
$logged = false;
$ownAccount = false;
if ($this->userId !== null) {
$logged = true;
$local = $this->accountService->getActorFromUserId($this->userId, true);
if ($local->getId() === $actor->getId()) {
$ownAccount = true;
} else {
$this->fillActorWithLinks($actor, $local);
}
}
$data = [
'serverData' => [
'public' => true,
],
'actor' => $actor,
'logged' => $logged,
'ownAccount' => $ownAccount
];
$page = new PublicTemplateResponse(Application::APP_NAME, 'main', $data);
$page->setHeaderTitle($this->l10n->t('Social') . ' ' . $username);
return $page;
} catch (CacheActorDoesNotExistException $e) {
return new NotFoundResponse();
} catch (Exception $e) {
return $this->fail($e);
}
return $this->renderPage($username);
}
@ -159,8 +148,8 @@ class SocialPubController extends Controller {
*
* @return TemplateResponse
*/
public function followers(string $username): TemplateResponse {
return new TemplateResponse(Application::APP_NAME, 'followers', [], 'blank');
public function followers(string $username): Response {
return $this->renderPage($username);
}
@ -175,8 +164,8 @@ class SocialPubController extends Controller {
*
* @return TemplateResponse
*/
public function following(string $username): TemplateResponse {
return new TemplateResponse(Application::APP_NAME, 'following', [], 'blank');
public function following(string $username): Response {
return $this->renderPage($username);
}
@ -195,15 +184,5 @@ class SocialPubController extends Controller {
return $this->success([$username, $postId]);
}
/**
* @param Person $actor
* @param Person $local
*/
private function fillActorWithLinks(Person $actor, Person $local) {
$links = $this->followService->getLinksBetweenPersons($local, $actor);
$actor->addDetailArray('link', $links);
}
}