Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Already pass board list as initial state #5281

Merged
merged 4 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
return [
'routes' => [
['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'],

// boards
Expand Down
62 changes: 40 additions & 22 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,34 @@
namespace OCA\Deck\Controller;

use \OCP\AppFramework\Http\RedirectResponse;
use OCA\Deck\AppInfo\Application;
use OCA\Deck\Db\Acl;
use OCA\Deck\Db\CardMapper;
use OCA\Deck\Service\BoardService;
use OCA\Deck\Service\CardService;
use OCA\Deck\Service\ConfigService;
use OCA\Deck\Service\PermissionService;
use OCA\Files\Event\LoadSidebar;
use OCA\Text\Event\LoadEditor;
use OCA\Viewer\Event\LoadViewer;
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\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as CollaborationResourcesEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IRequest;
use OCP\IURLGenerator;

class PageController extends Controller {
private IInitialStateService $initialState;

public function __construct(
string $AppName,
IRequest $request,
private PermissionService $permissionService,
IInitialStateService $initialStateService,
private IInitialState $initialState,
private BoardService $boardService,
private ConfigService $configService,
private IEventDispatcher $eventDispatcher,
private CardMapper $cardMapper,
Expand All @@ -59,21 +60,16 @@ public function __construct(
private IConfig $config,
) {
parent::__construct($AppName, $request);

$this->initialState = $initialStateService;
}

/**
* Handle main html view from templates/main.php
* This will return the main angular application
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function index() {
$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());
#[NoAdminRequired]
#[NoCSRFRequired]
public function index(): TemplateResponse {
$this->initialState->provideInitialState('maxUploadSize', (int)\OCP\Util::uploadLimit());
$this->initialState->provideInitialState('canCreate', $this->permissionService->canCreate());
$this->initialState->provideInitialState('config', $this->configService->getAll());

$this->initialState->provideInitialState('initialBoards', $this->boardService->findAll());

$this->eventDispatcher->dispatchTyped(new LoadSidebar());
$this->eventDispatcher->dispatchTyped(new CollaborationResourcesEvent());
Expand All @@ -99,10 +95,32 @@ public function index() {
return $response;
}

/**
* @NoAdminRequired
* @NoCSRFRequired
*/
#[NoAdminRequired]
#[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 {
try {
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_READ);
Expand Down
8 changes: 4 additions & 4 deletions lib/Reference/BoardReferenceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public function matchReference(string $referenceText): bool {
$startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/' . Application::APP_ID);

// link example: https://nextcloud.local/index.php/apps/deck/#/board/2
$noIndexMatch = preg_match('/^' . preg_quote($start, '/') . '\/#\/board\/[0-9]+$/', $referenceText) === 1;
$indexMatch = preg_match('/^' . preg_quote($startIndex, '/') . '\/#\/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;

return $noIndexMatch || $indexMatch;
}
Expand Down Expand Up @@ -108,9 +108,9 @@ private function getBoardId(string $url): ?int {
$start = $this->urlGenerator->getAbsoluteURL('/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) {
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) {
return (int) $matches[1];
Expand Down
25 changes: 13 additions & 12 deletions lib/Reference/CardReferenceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public function matchReference(string $referenceText): bool {
$startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/' . Application::APP_ID);

// 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;
$indexMatchFull = preg_match('/^' . preg_quote($startIndex, '/') . '\/#\/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;

// link example: https://nextcloud.local/index.php/apps/deck/card/11
$noIndexMatch = preg_match('/^' . preg_quote($start, '/') . '\/card\/[0-9]+$/', $referenceText) === 1;
Expand All @@ -125,16 +125,17 @@ public function resolveReference(string $referenceText): ?IReference {
if ($this->matchReference($referenceText)) {
$ids = $this->getBoardCardId($referenceText);
if ($ids !== null) {
[$boardId, $cardId] = $ids;
[, $cardId] = $ids;
try {
$card = $this->cardService->find((int) $cardId)->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) {
// Skip throwing if user has no permissions
return null;
}

$boardId = $board['id'];

$card = $this->sanitizeSerializedCard($card);
$board = $this->sanitizeSerializedBoard($board);
Expand All @@ -159,14 +160,14 @@ private function sanitizeSerializedStack(array $stack): array {
$result = $cardDetails->jsonSerialize();
unset($result['assignedUsers']);
return $result;
}, $stack['cards']);
}, $stack['cards'] ?? []);

return $stack;
}

private function sanitizeSerializedBoard(array $board): array {
unset($board['labels']);
$board['owner'] = $board['owner']->jsonSerialize();
$board['owner'] = $board['owner']?->jsonSerialize();
unset($board['acl']);
unset($board['users']);

Expand All @@ -176,18 +177,18 @@ private function sanitizeSerializedBoard(array $board): array {
private function sanitizeSerializedCard(array $card): array {
$card['labels'] = array_map(function (Label $label) {
return $label->jsonSerialize();
}, $card['labels']);
}, $card['labels'] ?? []);
$card['assignedUsers'] = array_map(function (Assignment $assignment) {
$result = $assignment->jsonSerialize();
$result['participant'] = $result['participant']->jsonSerialize();
return $result;
}, $card['assignedUsers']);
$card['owner'] = $card['owner']->jsonSerialize();
}, $card['assignedUsers'] ?? []);
$card['owner'] = $card['owner']?->jsonSerialize() ?? $card['owner'];
unset($card['relatedStack']);
unset($card['relatedBoard']);
$card['attachments'] = array_map(function (Attachment $attachment) {
return $attachment->jsonSerialize();
}, $card['attachments']);
}, $card['attachments'] ?? []);

return $card;
}
Expand All @@ -196,12 +197,12 @@ private function getBoardCardId(string $url): ?array {
$start = $this->urlGenerator->getAbsoluteURL('/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) {
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) {
return [$matches2[1], $matches2[2]];
}
Expand Down
6 changes: 5 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import AppNavigation from './components/navigation/AppNavigation.vue'
import { NcModal, NcContent, NcAppContent } from '@nextcloud/vue'
import { BoardApi } from './services/BoardApi.js'
import { emit, subscribe } from '@nextcloud/event-bus'
import { loadState } from '@nextcloud/initial-state'
const boardApi = new BoardApi()
Expand Down Expand Up @@ -108,7 +109,10 @@ export default {
},
},
created() {
this.$store.dispatch('loadBoards')
const initialState = loadState('deck', 'initialBoards', null)
if (initialState !== null) {
this.$store.dispatch('loadBoards')
}
this.$store.dispatch('loadSharees')
},
mounted() {
Expand Down
3 changes: 3 additions & 0 deletions src/components/navigation/AppNavigationBoardCategory.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,8 @@ export default {
}
},
},
mounted() {
this.opened = this.boards.length > 0
},
}
</script>
15 changes: 14 additions & 1 deletion src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import Overview from './components/overview/Overview.vue'

Vue.use(Router)

export default new Router({
const router = new Router({
mode: 'history',
base: generateUrl('/apps/deck/'),
linkActiveClass: 'active',
routes: [
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/store/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default new Vuex.Store({
sidebarShown: false,
currentBoard: null,
currentCard: null,
boards: [],
boards: loadState('deck', 'initialBoards', []),
sharees: [],
assignableUsers: [],
boardFilter: BOARD_FILTERS.ALL,
Expand Down
54 changes: 48 additions & 6 deletions tests/unit/Reference/CardReferenceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

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\Service\BoardService;
use OCA\Deck\Service\CardService;
Expand Down Expand Up @@ -51,16 +54,55 @@ public function setUp() : void {
);
}

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())
->method('getAbsoluteURL')
->willReturnCallback(function ($path) {
return 'https://localhost/' . ltrim($path, '/');
});
self::assertFalse($this->provider->matchReference('https://nextcloud.com'));
self::assertTrue($this->provider->matchReference('https://localhost/apps/deck/#/board/2/card/11'));
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'));
self::assertTrue($this->provider->matchReference('https://localhost/index.php/apps/deck/card/11'));
$matchExpect = $id !== null;
self::assertEquals($matchExpect, $this->provider->matchReference($url));

$card = Card::fromRow([
'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);
}
}
}
Loading
Loading