Skip to content

Commit

Permalink
Mobile: Fixes #11455: Clicking on an external note link from within a…
Browse files Browse the repository at this point in the history
… note logs an error (#11619)
  • Loading branch information
personalizedrefrigerator authored Jan 9, 2025
1 parent e8f305d commit 72575e3
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions packages/app-mobile/commands/openItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,56 @@ import BaseItem from '@joplin/lib/models/BaseItem';
import { BaseItemEntity } from '@joplin/lib/services/database/types';
import { ModelType } from '@joplin/lib/BaseModel';
import showResource from './util/showResource';
import { isCallbackUrl, parseCallbackUrl } from '@joplin/lib/callbackUrlUtils';

const logger = Logger.create('openItemCommand');

export const declaration: CommandDeclaration = {
name: 'openItem',
};

const openItemById = async (itemId: string, hash?: string) => {
logger.info(`Navigating to item ${itemId}`);
const item: BaseItemEntity = await BaseItem.loadItemById(itemId);

if (item.type_ === ModelType.Note) {
await goToNote(itemId, hash);
} else if (item.type_ === ModelType.Resource) {
await showResource(item);
} else {
throw new Error(`Unsupported item type for links: ${item.type_}`);
}
};

export const runtime = (): CommandRuntime => {
return {
execute: async (_context: CommandContext, link: string) => {
if (!link) throw new Error('Link cannot be empty');

if (link.startsWith('joplin://') || link.startsWith(':/')) {
const parsedUrl = parseResourceUrl(link);
if (parsedUrl) {
const { itemId, hash } = parsedUrl;
try {
if (link.startsWith('joplin://') || link.startsWith(':/')) {
const parsedResourceUrl = parseResourceUrl(link);
const parsedCallbackUrl = isCallbackUrl(link) ? parseCallbackUrl(link) : null;

logger.info(`Navigating to item ${itemId}`);
const item: BaseItemEntity = await BaseItem.loadItemById(itemId);
if (item.type_ === ModelType.Note) {
await goToNote(itemId, hash);
} else if (item.type_ === ModelType.Resource) {
await showResource(item);
if (parsedResourceUrl) {
const { itemId, hash } = parsedResourceUrl;
await openItemById(itemId, hash);
} else if (parsedCallbackUrl) {
const id = parsedCallbackUrl.params.id;
if (!id) {
throw new Error('Missing item ID');
}
await openItemById(id);
} else {
logger.error('Unsupported item type for links:', item.type_);
throw new Error('Unsupported link format.');
}
} else if (urlProtocol(link)) {
shim.openUrl(link);
} else {
logger.error(`Invalid Joplin link: ${link}`);
throw new Error('Unsupported protocol');
}
} else if (urlProtocol(link)) {
shim.openUrl(link);
} else {
const errorMessage = _('Unsupported link or message: %s', link);
} catch (error) {
const errorMessage = _('Unsupported link or message: %s.\nError: %s', link, error);
logger.error(errorMessage);
await shim.showErrorDialog(errorMessage);
}
Expand Down

0 comments on commit 72575e3

Please sign in to comment.