Skip to content

Commit

Permalink
Add a module to handle deep links following the documentation (#1198)
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan authored Apr 30, 2019
1 parent dd739db commit c271fce
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
24 changes: 4 additions & 20 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { app } from 'electron';
import querystring from 'querystring';
import url from 'url';
import appData from './main/appData';
import './main/basicAuth';
import { processDeepLink } from './main/deepLinks';
import './main/systemIdleTime';
import './main/updates';
import { getMainWindow } from './main/mainWindow';
Expand All @@ -17,21 +16,6 @@ export { default as notifications } from './main/notifications';
export { default as certificate } from './main/certificateStore';


function parseCommandLineArguments(args) {
args
.filter((arg) => /^rocketchat:\/\/./.test(arg))
.map((uri) => url.parse(uri))
.map(({ hostname, pathname, query }) => {
const { insecure } = querystring.parse(query);
return `${ insecure === 'true' ? 'http' : 'https' }://${ hostname }${ pathname || '' }`;
})
.slice(0, 1)
.forEach(async (serverUrl) => {
const mainWindow = await getMainWindow();
mainWindow.send('add-host', serverUrl);
});
}

function handleUncaughtException(error) {
console.error(error);
app.exit(1);
Expand Down Expand Up @@ -71,11 +55,11 @@ async function prepareApp() {

app.on('open-url', (event, url) => {
event.preventDefault();
parseCommandLineArguments([url]);
processDeepLink(url);
});

app.on('second-instance', (event, argv) => {
parseCommandLineArguments(argv.slice(2));
argv.slice(2).forEach(processDeepLink);
});
}

Expand All @@ -85,5 +69,5 @@ async function prepareApp() {
await i18n.initialize();
app.emit('start');
await getMainWindow();
parseCommandLineArguments(process.argv.slice(2));
process.argv.slice(2).forEach(processDeepLink);
})();
44 changes: 44 additions & 0 deletions src/main/deepLinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import querystring from 'querystring';
import url from 'url';
import { getMainWindow } from './mainWindow';


const normalizeUrl = (hostUrl) => {
if (!/^https?:\/\//.test(hostUrl)) {
return `https://${ hostUrl }`;
}

return hostUrl;
};

const processAuth = async ({ host, token, userId }) => {
const mainWindow = await getMainWindow();
const hostUrl = normalizeUrl(host);
mainWindow.send('add-host', hostUrl, { token, userId });
};

const processRoom = async ({ host, rid, path }) => {
const mainWindow = await getMainWindow();
const hostUrl = normalizeUrl(host);
mainWindow.send('add-host', hostUrl);
mainWindow.send('open-room', hostUrl, { rid, path });
};

export const processDeepLink = (link) => {
const { protocol, hostname: action, query } = url.parse(link);

if (protocol !== 'rocketchat:') {
return;
}

switch (action) {
case 'auth': {
processAuth(querystring.parse(query));
break;
}
case 'room': {
processRoom(querystring.parse(query));
break;
}
}
};

0 comments on commit c271fce

Please sign in to comment.