diff --git a/src/main.js b/src/main.js index 8907adec7f..3a3f9b03f0 100644 --- a/src/main.js +++ b/src/main.js @@ -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'; @@ -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); @@ -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); }); } @@ -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); })(); diff --git a/src/main/deepLinks.js b/src/main/deepLinks.js new file mode 100644 index 0000000000..c423276f73 --- /dev/null +++ b/src/main/deepLinks.js @@ -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; + } + } +};