Refactoring intent receivers for invitations

This commit is contained in:
Eric van der Vlist 2023-04-17 14:45:04 +02:00
parent f82181347c
commit d5a8118c2a
5 changed files with 36 additions and 36 deletions

View File

@ -36,27 +36,8 @@ const fetchInvitation = () => {
}); });
}; };
export const searchInvitationInIntent = (intent: any) => { export const openInvitation = (payload: string) => {
if (intent.action !== 'android.intent.action.VIEW') {
return false;
}
let url;
try { try {
url = new URL(intent.data);
} catch (error) {
console.error({ caller: 'searchInvitationInIntent', intent, error });
return false;
}
if (url.protocol !== 'geo:') {
return false;
}
const q = url.search;
const matches = q.match(/invitation=(.+)/);
if (!matches) {
return false;
}
try {
const payload = matches[1];
const decoded = atob(payload); const decoded = atob(payload);
const url = new URL(decoded); const url = new URL(decoded);
setInvitation({ url }); setInvitation({ url });
@ -64,8 +45,7 @@ export const searchInvitationInIntent = (intent: any) => {
} catch { } catch {
console.warn({ console.warn({
caller: 'Invitation / searchInvitationInIntent', caller: 'Invitation / searchInvitationInIntent',
intent, payload,
matches,
message: 'Query is not a base64 encoded URL', message: 'Query is not a base64 encoded URL',
}); });
} }

View File

@ -1 +1 @@
export { default, searchInvitation, searchInvitationInIntent } from './Invitation'; export { default, searchInvitation } from './Invitation';

View File

@ -1,5 +1,5 @@
import { Component, createEffect, createSignal, onMount } from 'solid-js'; import { Component, createEffect, createSignal, onMount } from 'solid-js';
import { useParams, useNavigate, useLocation } from '@solidjs/router'; import { useParams, useNavigate } from '@solidjs/router';
import OlMap from 'ol/Map'; import OlMap from 'ol/Map';
import View from 'ol/View'; import View from 'ol/View';
import TileLayer from 'ol/layer/Tile'; import TileLayer from 'ol/layer/Tile';
@ -16,7 +16,7 @@ import DragRotate from 'ol/interaction/DragRotate';
import 'ol/ol.css'; import 'ol/ol.css';
import './Map.css'; import './Map.css';
import { Collection, Overlay } from 'ol'; import { Collection } from 'ol';
import { Point } from 'ol/geom'; import { Point } from 'ol/geom';
import { Style, Icon } from 'ol/style'; import { Style, Icon } from 'ol/style';
import GetLocation, { getCurrentLocation } from '../get-location'; import GetLocation, { getCurrentLocation } from '../get-location';
@ -26,7 +26,6 @@ import GpxImport, { importUrls } from '../import';
import AllGpxes from '../all-gpxes'; import AllGpxes from '../all-gpxes';
import MapTileProvider, { mapTileProviders } from '../map-tile-provider'; import MapTileProvider, { mapTileProviders } from '../map-tile-provider';
import Interaction from 'ol/interaction/Interaction'; import Interaction from 'ol/interaction/Interaction';
import DoubleClickZoom from 'ol/interaction/DoubleClickZoom';
import DragPan from 'ol/interaction/DragPan'; import DragPan from 'ol/interaction/DragPan';
import PinchRotate from 'ol/interaction/PinchRotate'; import PinchRotate from 'ol/interaction/PinchRotate';
import PinchZoom from 'ol/interaction/PinchZoom'; import PinchZoom from 'ol/interaction/PinchZoom';
@ -41,16 +40,10 @@ import dispatch from '../../workers/dispatcher-main';
import { debounce } from 'lodash'; import { debounce } from 'lodash';
import { AndroidFullScreen } from '@awesome-cordova-plugins/android-full-screen'; import { AndroidFullScreen } from '@awesome-cordova-plugins/android-full-screen';
import Account from '../account';
import { Overlays } from '../overlays/Overlays'; import { Overlays } from '../overlays/Overlays';
import Finder, { findLocation } from '../finder'; import Finder, { findLocation } from '../finder';
import { ZoomIn } from '@suid/icons-material';
import Note from '../note'; import Note from '../note';
import Invitation, { import Invitation, { searchInvitation } from '../invitation';
searchInvitation,
searchInvitationInIntent,
} from '../invitation';
import { returnOrUpdate } from 'ol/extent';
import Updater from '../updater'; import Updater from '../updater';
import SystemDrawer from '../system-drawer'; import SystemDrawer from '../system-drawer';
import { receiveIntent } from '../../lib/intent-receivers'; import { receiveIntent } from '../../lib/intent-receivers';
@ -99,9 +92,6 @@ const Map: Component = () => {
); );
window.plugins.intentShim.onIntent(function (intent: any) { window.plugins.intentShim.onIntent(function (intent: any) {
console.log({ caller: 'Intent receiver', intent }); console.log({ caller: 'Intent receiver', intent });
if (searchInvitationInIntent(intent)) {
return;
}
if (receiveIntent(intent)) { if (receiveIntent(intent)) {
return; return;
} }

View File

@ -1,6 +1,8 @@
import { gpxReceiver } from './gpx-receiver'; import { gpxReceiver } from './gpx-receiver';
import { invitationReceiver } from './invitation-receiver';
const receivers = { const receivers = {
invitationReceiver,
gpxReceiver, gpxReceiver,
}; };

View File

@ -0,0 +1,28 @@
import { openInvitation } from '../../components/invitation/Invitation';
const ACCEPTED_ACTIONS = ['android.intent.action.VIEW'];
const ACCEPTED_PROTOCOLS = ['geo:'];
export const invitationReceiver = (intent: any, url: URL | undefined) => {
if (!ACCEPTED_ACTIONS.includes(intent.action)) {
return false;
}
if (!url) {
return false;
}
if (!ACCEPTED_PROTOCOLS.includes(url.protocol)) {
return false;
}
const q = url.search;
const matches = q.match(/invitation=(.+)/);
if (!matches) {
return false;
}
const payload = matches[1];
openInvitation(payload);
return true;
};