Skip to content

Commit cf9d52b

Browse files
committed
feat: redirect when trying to open my map with .index.maps id
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 30f8f00 commit cf9d52b

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

lib/Controller/PageController.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,27 @@
1313
namespace OCA\Maps\Controller;
1414

1515
use OCA\Files\Event\LoadSidebar;
16+
use OCA\Maps\Service\MyMapsService;
1617
use OCA\Viewer\Event\LoadViewer;
1718
use OCP\AppFramework\Controller;
19+
use OCP\AppFramework\Http\RedirectResponse;
1820
use OCP\AppFramework\Http\TemplateResponse;
21+
use OCP\AppFramework\Services\IInitialState;
1922
use OCP\EventDispatcher\IEventDispatcher;
2023
use OCP\IConfig;
21-
use OCP\IInitialStateService;
2224
use OCP\IRequest;
25+
use OCP\IURLGenerator;
2326

2427
class PageController extends Controller {
2528

2629
public function __construct(
2730
string $appName,
2831
IRequest $request,
32+
private string $userId,
2933
private IEventDispatcher $eventDispatcher,
3034
private IConfig $config,
31-
private IInitialStateService $initialStateService,
32-
private string $userId,
35+
private IInitialState $initialState,
36+
private IURLGenerator $urlGenerator,
3337
) {
3438
parent::__construct($appName, $request);
3539
}
@@ -50,7 +54,7 @@ public function index(): TemplateResponse {
5054
$this->eventDispatcher->dispatch(LoadViewer::class, new LoadViewer());
5155

5256
$params = ['user' => $this->userId];
53-
$this->initialStateService->provideInitialState($this->appName, 'photos', $this->config->getAppValue('photos', 'enabled', 'no') === 'yes');
57+
$this->initialState->provideInitialState('photos', $this->config->getAppValue('photos', 'enabled', 'no') === 'yes');
5458
$response = new TemplateResponse('maps', 'main', $params);
5559

5660
$this->addCsp($response);
@@ -62,12 +66,21 @@ public function index(): TemplateResponse {
6266
* @NoAdminRequired
6367
* @NoCSRFRequired
6468
*/
65-
public function indexMyMap($myMapId): TemplateResponse {
69+
public function indexMyMap(int $myMapId, MyMapsService $service): TemplateResponse|RedirectResponse {
70+
$map = $service->getMyMap($myMapId, $this->userId);
71+
if ($map !== null && $map['id'] !== $myMapId) {
72+
// Instead of the id of the map containing folder the '.index.maps' file id was passed so redirect
73+
// this happens if coming from the files app integration
74+
return new RedirectResponse(
75+
$this->urlGenerator->linkToRouteAbsolute('maps.page.indexMyMap', ['myMapId' => $map['id']]),
76+
);
77+
}
78+
6679
$this->eventDispatcher->dispatch(LoadSidebar::class, new LoadSidebar());
6780
$this->eventDispatcher->dispatch(LoadViewer::class, new LoadViewer());
6881

6982
$params = ['user' => $this->userId];
70-
$this->initialStateService->provideInitialState($this->appName, 'photos', $this->config->getAppValue('photos', 'enabled', 'no') === 'yes');
83+
$this->initialState->provideInitialState('photos', $this->config->getAppValue('photos', 'enabled', 'no') === 'yes');
7184
$response = new TemplateResponse('maps', 'main', $params);
7285

7386
$this->addCsp($response);

lib/Service/MyMapsService.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,30 @@ public function getAllMyMaps($userId) {
148148
return $MyMaps;
149149
}
150150

151+
/**
152+
* Try to lookup a my map by id
153+
*
154+
* @param int $id The map id to lookup
155+
* @param string $userId The current user id
156+
* @return null|array Either the MyMap or null if not found with that id for the given user
157+
*/
158+
public function getMyMap(int $id, string $userId) {
159+
$userFolder = $this->root->getUserFolder($userId);
160+
$node = $userFolder->getFirstNodeById($id);
161+
if ($node instanceof Folder) {
162+
try {
163+
$node = $node->get('.index.maps');
164+
} catch (NotFoundException) {
165+
return null;
166+
}
167+
}
168+
169+
if ($node->getMimetype() === 'application/x-nextcloud-maps') {
170+
return $this->node2MyMap($node, $userFolder);
171+
}
172+
return null;
173+
}
174+
151175
public function updateMyMap($id, $values, $userId) {
152176
$userFolder = $this->root->getUserFolder($userId);
153177
$folders = $userFolder->getById($id);
@@ -158,7 +182,7 @@ public function updateMyMap($id, $values, $userId) {
158182
try {
159183
$file = $folder->get('.index.maps');
160184
} catch (NotFoundException $e) {
161-
$file = $folder->newFile('.index.maps', $content = '{}');
185+
$file = $folder->newFile('.index.maps', '{}');
162186
}
163187
$mapData = json_decode($file->getContent(), true);
164188
$renamed = false;

src/files-actions/import-devices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { generateUrl } from '@nextcloud/router'
77
import svgMapMarker from '@mdi/svg/svg/map-marker.svg?raw'
88

99
export default new FileAction({
10-
id: 'maps:import-as-favorite',
10+
id: 'maps:import-device',
1111

1212
displayName() {
1313
return t('maps', 'Import as devices in Maps')

0 commit comments

Comments
 (0)