Merge pull request #5281 from nextcloud/perf/initial-state

This commit is contained in:
Julius Härtl
2023-11-13 15:17:04 +01:00
committed by GitHub
10 changed files with 139 additions and 49 deletions

View File

@@ -25,6 +25,11 @@
return [ return [
'routes' => [ 'routes' => [
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'], ['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
['name' => 'page#indexList', 'url' => '/board', 'verb' => 'GET'],
['name' => 'page#indexBoard', 'url' => '/board/{boardId}', 'verb' => 'GET'],
['name' => 'page#indexBoardDetails', 'url' => '/board/{boardId}/details', 'verb' => 'GET'],
['name' => 'page#indexCard', 'url' => '/board/{boardId}/card/{cardId}', 'verb' => 'GET'],
['name' => 'page#redirectToCard', 'url' => '/card/{cardId}', 'verb' => 'GET'], ['name' => 'page#redirectToCard', 'url' => '/card/{cardId}', 'verb' => 'GET'],
// boards // boards

View File

@@ -24,9 +24,9 @@
namespace OCA\Deck\Controller; namespace OCA\Deck\Controller;
use \OCP\AppFramework\Http\RedirectResponse; use \OCP\AppFramework\Http\RedirectResponse;
use OCA\Deck\AppInfo\Application;
use OCA\Deck\Db\Acl; use OCA\Deck\Db\Acl;
use OCA\Deck\Db\CardMapper; use OCA\Deck\Db\CardMapper;
use OCA\Deck\Service\BoardService;
use OCA\Deck\Service\CardService; use OCA\Deck\Service\CardService;
use OCA\Deck\Service\ConfigService; use OCA\Deck\Service\ConfigService;
use OCA\Deck\Service\PermissionService; use OCA\Deck\Service\PermissionService;
@@ -34,23 +34,24 @@ use OCA\Files\Event\LoadSidebar;
use OCA\Text\Event\LoadEditor; use OCA\Text\Event\LoadEditor;
use OCA\Viewer\Event\LoadViewer; use OCA\Viewer\Event\LoadViewer;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\ContentSecurityPolicy; use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as CollaborationResourcesEvent; use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as CollaborationResourcesEvent;
use OCP\EventDispatcher\IEventDispatcher; use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig; use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IRequest; use OCP\IRequest;
use OCP\IURLGenerator; use OCP\IURLGenerator;
class PageController extends Controller { class PageController extends Controller {
private IInitialStateService $initialState;
public function __construct( public function __construct(
string $AppName, string $AppName,
IRequest $request, IRequest $request,
private PermissionService $permissionService, private PermissionService $permissionService,
IInitialStateService $initialStateService, private IInitialState $initialState,
private BoardService $boardService,
private ConfigService $configService, private ConfigService $configService,
private IEventDispatcher $eventDispatcher, private IEventDispatcher $eventDispatcher,
private CardMapper $cardMapper, private CardMapper $cardMapper,
@@ -59,21 +60,16 @@ class PageController extends Controller {
private IConfig $config, private IConfig $config,
) { ) {
parent::__construct($AppName, $request); parent::__construct($AppName, $request);
$this->initialState = $initialStateService;
} }
/** #[NoAdminRequired]
* Handle main html view from templates/main.php #[NoCSRFRequired]
* This will return the main angular application public function index(): TemplateResponse {
* $this->initialState->provideInitialState('maxUploadSize', (int)\OCP\Util::uploadLimit());
* @NoAdminRequired $this->initialState->provideInitialState('canCreate', $this->permissionService->canCreate());
* @NoCSRFRequired $this->initialState->provideInitialState('config', $this->configService->getAll());
*/
public function index() { $this->initialState->provideInitialState('initialBoards', $this->boardService->findAll());
$this->initialState->provideInitialState(Application::APP_ID, 'maxUploadSize', (int)\OCP\Util::uploadLimit());
$this->initialState->provideInitialState(Application::APP_ID, 'canCreate', $this->permissionService->canCreate());
$this->initialState->provideInitialState(Application::APP_ID, 'config', $this->configService->getAll());
$this->eventDispatcher->dispatchTyped(new LoadSidebar()); $this->eventDispatcher->dispatchTyped(new LoadSidebar());
$this->eventDispatcher->dispatchTyped(new CollaborationResourcesEvent()); $this->eventDispatcher->dispatchTyped(new CollaborationResourcesEvent());
@@ -99,10 +95,32 @@ class PageController extends Controller {
return $response; return $response;
} }
/** #[NoAdminRequired]
* @NoAdminRequired #[NoCSRFRequired]
* @NoCSRFRequired public function indexList(): TemplateResponse {
*/ return $this->index();
}
#[NoAdminRequired]
#[NoCSRFRequired]
public function indexBoard(int $boardId): TemplateResponse {
return $this->index();
}
#[NoAdminRequired]
#[NoCSRFRequired]
public function indexBoardDetails(int $boardId): TemplateResponse {
return $this->index();
}
#[NoAdminRequired]
#[NoCSRFRequired]
public function indexCard(int $cardId): TemplateResponse {
return $this->index();
}
#[NoAdminRequired]
#[NoCSRFRequired]
public function redirectToCard($cardId): RedirectResponse { public function redirectToCard($cardId): RedirectResponse {
try { try {
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_READ); $this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_READ);

View File

@@ -55,8 +55,8 @@ class BoardReferenceProvider implements IReferenceProvider {
$startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/' . Application::APP_ID); $startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/' . Application::APP_ID);
// link example: https://nextcloud.local/index.php/apps/deck/#/board/2 // link example: https://nextcloud.local/index.php/apps/deck/#/board/2
$noIndexMatch = preg_match('/^' . preg_quote($start, '/') . '\/#\/board\/[0-9]+$/', $referenceText) === 1; $noIndexMatch = preg_match('/^' . preg_quote($start, '/') . '(?:\/#!?)?\/board\/[0-9]+$/', $referenceText) === 1;
$indexMatch = preg_match('/^' . preg_quote($startIndex, '/') . '\/#\/board\/[0-9]+$/', $referenceText) === 1; $indexMatch = preg_match('/^' . preg_quote($startIndex, '/') . '(?:\/#!?)?\/board\/[0-9]+$/', $referenceText) === 1;
return $noIndexMatch || $indexMatch; return $noIndexMatch || $indexMatch;
} }
@@ -108,9 +108,9 @@ class BoardReferenceProvider implements IReferenceProvider {
$start = $this->urlGenerator->getAbsoluteURL('/apps/' . Application::APP_ID); $start = $this->urlGenerator->getAbsoluteURL('/apps/' . Application::APP_ID);
$startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/' . Application::APP_ID); $startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/' . Application::APP_ID);
preg_match('/^' . preg_quote($start, '/') . '\/#\/board\/([0-9]+)$/', $url, $matches); preg_match('/^' . preg_quote($start, '/') . '(?:\/#!?)?\/board\/([0-9]+)$/', $url, $matches);
if (!$matches) { if (!$matches) {
preg_match('/^' . preg_quote($startIndex, '/') . '\/#\/board\/([0-9]+)$/', $url, $matches); preg_match('/^' . preg_quote($startIndex, '/') . '(?:\/#!?)?\/board\/([0-9]+)$/', $url, $matches);
} }
if ($matches && count($matches) > 1) { if ($matches && count($matches) > 1) {
return (int) $matches[1]; return (int) $matches[1];

View File

@@ -108,8 +108,8 @@ class CardReferenceProvider extends ADiscoverableReferenceProvider implements IS
$startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/' . Application::APP_ID); $startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/' . Application::APP_ID);
// link example: https://nextcloud.local/index.php/apps/deck/#/board/2/card/11 // link example: https://nextcloud.local/index.php/apps/deck/#/board/2/card/11
$noIndexMatchFull = preg_match('/^' . preg_quote($start, '/') . '\/#\/board\/[0-9]+\/card\/[0-9]+$/', $referenceText) === 1; $noIndexMatchFull = preg_match('/^' . preg_quote($start, '/') . '(?:\/#!?)?\/board\/[0-9]+\/card\/[0-9]+$/', $referenceText) === 1;
$indexMatchFull = preg_match('/^' . preg_quote($startIndex, '/') . '\/#\/board\/[0-9]+\/card\/[0-9]+$/', $referenceText) === 1; $indexMatchFull = preg_match('/^' . preg_quote($startIndex, '/') . '(?:\/#!?)?\/board\/[0-9]+\/card\/[0-9]+$/', $referenceText) === 1;
// link example: https://nextcloud.local/index.php/apps/deck/card/11 // link example: https://nextcloud.local/index.php/apps/deck/card/11
$noIndexMatch = preg_match('/^' . preg_quote($start, '/') . '\/card\/[0-9]+$/', $referenceText) === 1; $noIndexMatch = preg_match('/^' . preg_quote($start, '/') . '\/card\/[0-9]+$/', $referenceText) === 1;
@@ -125,16 +125,17 @@ class CardReferenceProvider extends ADiscoverableReferenceProvider implements IS
if ($this->matchReference($referenceText)) { if ($this->matchReference($referenceText)) {
$ids = $this->getBoardCardId($referenceText); $ids = $this->getBoardCardId($referenceText);
if ($ids !== null) { if ($ids !== null) {
[$boardId, $cardId] = $ids; [, $cardId] = $ids;
try { try {
$card = $this->cardService->find((int) $cardId)->jsonSerialize(); $card = $this->cardService->find((int) $cardId)->jsonSerialize();
$stack = $this->stackService->find((int) $card['stackId'])->jsonSerialize(); $stack = $this->stackService->find((int) $card['stackId'])->jsonSerialize();
$board = $this->boardService->find((int)($boardId ?? $stack['boardId']))->jsonSerialize(); $board = $this->boardService->find((int) $stack['boardId'])->jsonSerialize();
} catch (NoPermissionException $e) { } catch (NoPermissionException $e) {
// Skip throwing if user has no permissions // Skip throwing if user has no permissions
return null; return null;
} }
$boardId = $board['id'];
$card = $this->sanitizeSerializedCard($card); $card = $this->sanitizeSerializedCard($card);
$board = $this->sanitizeSerializedBoard($board); $board = $this->sanitizeSerializedBoard($board);
@@ -159,14 +160,14 @@ class CardReferenceProvider extends ADiscoverableReferenceProvider implements IS
$result = $cardDetails->jsonSerialize(); $result = $cardDetails->jsonSerialize();
unset($result['assignedUsers']); unset($result['assignedUsers']);
return $result; return $result;
}, $stack['cards']); }, $stack['cards'] ?? []);
return $stack; return $stack;
} }
private function sanitizeSerializedBoard(array $board): array { private function sanitizeSerializedBoard(array $board): array {
unset($board['labels']); unset($board['labels']);
$board['owner'] = $board['owner']->jsonSerialize(); $board['owner'] = $board['owner']?->jsonSerialize();
unset($board['acl']); unset($board['acl']);
unset($board['users']); unset($board['users']);
@@ -176,18 +177,18 @@ class CardReferenceProvider extends ADiscoverableReferenceProvider implements IS
private function sanitizeSerializedCard(array $card): array { private function sanitizeSerializedCard(array $card): array {
$card['labels'] = array_map(function (Label $label) { $card['labels'] = array_map(function (Label $label) {
return $label->jsonSerialize(); return $label->jsonSerialize();
}, $card['labels']); }, $card['labels'] ?? []);
$card['assignedUsers'] = array_map(function (Assignment $assignment) { $card['assignedUsers'] = array_map(function (Assignment $assignment) {
$result = $assignment->jsonSerialize(); $result = $assignment->jsonSerialize();
$result['participant'] = $result['participant']->jsonSerialize(); $result['participant'] = $result['participant']->jsonSerialize();
return $result; return $result;
}, $card['assignedUsers']); }, $card['assignedUsers'] ?? []);
$card['owner'] = $card['owner']->jsonSerialize(); $card['owner'] = $card['owner']?->jsonSerialize() ?? $card['owner'];
unset($card['relatedStack']); unset($card['relatedStack']);
unset($card['relatedBoard']); unset($card['relatedBoard']);
$card['attachments'] = array_map(function (Attachment $attachment) { $card['attachments'] = array_map(function (Attachment $attachment) {
return $attachment->jsonSerialize(); return $attachment->jsonSerialize();
}, $card['attachments']); }, $card['attachments'] ?? []);
return $card; return $card;
} }
@@ -196,12 +197,12 @@ class CardReferenceProvider extends ADiscoverableReferenceProvider implements IS
$start = $this->urlGenerator->getAbsoluteURL('/apps/' . Application::APP_ID); $start = $this->urlGenerator->getAbsoluteURL('/apps/' . Application::APP_ID);
$startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/' . Application::APP_ID); $startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/' . Application::APP_ID);
preg_match('/^' . preg_quote($start, '/') . '\/#\/board\/([0-9]+)\/card\/([0-9]+)$/', $url, $matches); preg_match('/^' . preg_quote($start, '/') . '(?:\/#!?)?\/board\/([0-9]+)\/card\/([0-9]+)$/', $url, $matches);
if ($matches && count($matches) > 2) { if ($matches && count($matches) > 2) {
return [$matches[1], $matches[2]]; return [$matches[1], $matches[2]];
} }
preg_match('/^' . preg_quote($startIndex, '/') . '\/#\/board\/([0-9]+)\/card\/([0-9]+)$/', $url, $matches2); preg_match('/^' . preg_quote($startIndex, '/') . '(?:\/#!?)?\/board\/([0-9]+)\/card\/([0-9]+)$/', $url, $matches2);
if ($matches2 && count($matches2) > 2) { if ($matches2 && count($matches2) > 2) {
return [$matches2[1], $matches2[2]]; return [$matches2[1], $matches2[2]];
} }

View File

@@ -49,6 +49,7 @@ import AppNavigation from './components/navigation/AppNavigation.vue'
import { NcModal, NcContent, NcAppContent } from '@nextcloud/vue' import { NcModal, NcContent, NcAppContent } from '@nextcloud/vue'
import { BoardApi } from './services/BoardApi.js' import { BoardApi } from './services/BoardApi.js'
import { emit, subscribe } from '@nextcloud/event-bus' import { emit, subscribe } from '@nextcloud/event-bus'
import { loadState } from '@nextcloud/initial-state'
const boardApi = new BoardApi() const boardApi = new BoardApi()
@@ -108,7 +109,10 @@ export default {
}, },
}, },
created() { created() {
const initialState = loadState('deck', 'initialBoards', null)
if (initialState !== null) {
this.$store.dispatch('loadBoards') this.$store.dispatch('loadBoards')
}
this.$store.dispatch('loadSharees') this.$store.dispatch('loadSharees')
}, },
mounted() { mounted() {

View File

@@ -89,5 +89,8 @@ export default {
} }
}, },
}, },
mounted() {
this.opened = this.boards.length > 0
},
} }
</script> </script>

View File

@@ -33,7 +33,8 @@ import Overview from './components/overview/Overview.vue'
Vue.use(Router) Vue.use(Router)
export default new Router({ const router = new Router({
mode: 'history',
base: generateUrl('/apps/deck/'), base: generateUrl('/apps/deck/'),
linkActiveClass: 'active', linkActiveClass: 'active',
routes: [ routes: [
@@ -157,3 +158,15 @@ export default new Router({
}, },
], ],
}) })
router.beforeEach((to, from, next) => {
// Redirect if fullPath begins with a hash (ignore hashes later in path)
if (to.fullPath.substring(0, 2) === '/#') {
const path = to.fullPath.substring(2)
next(path)
return
}
next()
})
export default router

View File

@@ -66,7 +66,7 @@ export default new Vuex.Store({
sidebarShown: false, sidebarShown: false,
currentBoard: null, currentBoard: null,
currentCard: null, currentCard: null,
boards: [], boards: loadState('deck', 'initialBoards', []),
sharees: [], sharees: [],
assignableUsers: [], assignableUsers: [],
boardFilter: BOARD_FILTERS.ALL, boardFilter: BOARD_FILTERS.ALL,

View File

@@ -22,6 +22,9 @@
namespace Reference; namespace Reference;
use OCA\Deck\Db\Board;
use OCA\Deck\Db\Card;
use OCA\Deck\Db\Stack;
use OCA\Deck\Reference\CardReferenceProvider; use OCA\Deck\Reference\CardReferenceProvider;
use OCA\Deck\Service\BoardService; use OCA\Deck\Service\BoardService;
use OCA\Deck\Service\CardService; use OCA\Deck\Service\CardService;
@@ -51,16 +54,55 @@ class CardReferenceProviderTest extends TestCase {
); );
} }
public function testUrl() { public static function dataUrl(): array {
return [
['https://nextcloud.com', null],
['https://localhost/apps/deck/#!/board/2/card/11', 11],
['https://localhost/index.php/apps/deck/#!/board/2/card/11', 11],
['https://localhost/apps/deck/#/board/2/card/11', 11],
['https://localhost/index.php/apps/deck/#/board/2/card/11', 11],
['https://localhost/apps/deck/board/2/card/11', 11],
['https://localhost/index.php/apps/deck/board/2/card/11', 11],
['https://localhost/apps/deck/card/11', 11],
['https://localhost/index.php/apps/deck/card/11', 11],
];
}
/**
* @dataProvider dataUrl
*/
public function testUrl($url, $id) {
$this->urlGenerator->expects($this->any()) $this->urlGenerator->expects($this->any())
->method('getAbsoluteURL') ->method('getAbsoluteURL')
->willReturnCallback(function ($path) { ->willReturnCallback(function ($path) {
return 'https://localhost/' . ltrim($path, '/'); return 'https://localhost/' . ltrim($path, '/');
}); });
self::assertFalse($this->provider->matchReference('https://nextcloud.com')); $matchExpect = $id !== null;
self::assertTrue($this->provider->matchReference('https://localhost/apps/deck/#/board/2/card/11')); self::assertEquals($matchExpect, $this->provider->matchReference($url));
self::assertTrue($this->provider->matchReference('https://localhost/index.php/apps/deck/#/board/2/card/11'));
self::assertTrue($this->provider->matchReference('https://localhost/apps/deck/card/11')); $card = Card::fromRow([
self::assertTrue($this->provider->matchReference('https://localhost/index.php/apps/deck/card/11')); 'id' => $id,
'stackId' => 1234,
]);
$stack = Stack::fromRow([
'boardId' => 9876,
]);
$board = Board::fromRow([
'id' => 9876,
]);
$this->cardService->method('find')->with($id)->willReturn($card);
$this->stackService->method('find')->with(1234)->willReturn($stack);
$this->boardService->method('find')->with(9876)->willReturn($board);
$reference = $this->provider->resolveReference($url);
if ($id !== null) {
self::assertEquals($id, $reference->jsonSerialize()['richObject']['card']['id']);
self::assertEquals(9876, $reference->jsonSerialize()['richObject']['board']['id']);
} else {
self::assertNull($reference);
}
} }
} }

View File

@@ -25,12 +25,13 @@
namespace OCA\Deck\Controller; namespace OCA\Deck\Controller;
use OCA\Deck\Db\CardMapper; use OCA\Deck\Db\CardMapper;
use OCA\Deck\Service\BoardService;
use OCA\Deck\Service\CardService; use OCA\Deck\Service\CardService;
use OCA\Deck\Service\ConfigService; use OCA\Deck\Service\ConfigService;
use OCA\Deck\Service\PermissionService; use OCA\Deck\Service\PermissionService;
use OCP\AppFramework\Services\IInitialState;
use OCP\EventDispatcher\IEventDispatcher; use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig; use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IRequest; use OCP\IRequest;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@@ -40,6 +41,7 @@ class PageControllerTest extends TestCase {
private $request; private $request;
private $permissionService; private $permissionService;
private $initialState; private $initialState;
private $boardService;
private $configService; private $configService;
private $eventDispatcher; private $eventDispatcher;
/** /**
@@ -61,7 +63,8 @@ class PageControllerTest extends TestCase {
$this->request = $this->createMock(IRequest::class); $this->request = $this->createMock(IRequest::class);
$this->permissionService = $this->createMock(PermissionService::class); $this->permissionService = $this->createMock(PermissionService::class);
$this->configService = $this->createMock(ConfigService::class); $this->configService = $this->createMock(ConfigService::class);
$this->initialState = $this->createMock(IInitialStateService::class); $this->initialState = $this->createMock(IInitialState::class);
$this->boardService = $this->createMock(BoardService::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->cardMapper = $this->createMock(CardMapper::class); $this->cardMapper = $this->createMock(CardMapper::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class); $this->urlGenerator = $this->createMock(IURLGenerator::class);
@@ -73,6 +76,7 @@ class PageControllerTest extends TestCase {
$this->request, $this->request,
$this->permissionService, $this->permissionService,
$this->initialState, $this->initialState,
$this->boardService,
$this->configService, $this->configService,
$this->eventDispatcher, $this->eventDispatcher,
$this->cardMapper, $this->cardMapper,