Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Deep links handling (incomplete) #1198

Merged
merged 1 commit into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
};