Skip to content

Commit 26d9cca

Browse files
alexbraziergdelavald
authored andcommitted
[NEW] Adds rocketchat:// protocol to add/open server (RocketChat#466)
* Add rocketchat:// protocol to add/open server * Update rocketchat protocol * Adds desktop scheme for linux * Add open-url listener before app is ready, set timeout workaround for dialog popup * Refactor makesingleinstance call to antecipate app ready * Refactor dialog to main function * Adds arguments check on app ready * Codacy lint fixes
1 parent 0d970fd commit 26d9cca

File tree

8 files changed

+128
-37
lines changed

8 files changed

+128
-37
lines changed

build/entitlements.mas.plist

+13
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,18 @@
1414
<true/>
1515
<key>com.apple.security.device.microphone</key>
1616
<true/>
17+
<key>CFBundleIdentifier</key>
18+
<string>chat.rocket.AppleScript.RocketChat</string>
19+
<key>CFBundleURLTypes</key>
20+
<array>
21+
<dict>
22+
<key>CFBundleURLName</key>
23+
<string>RocketChat</string>
24+
<key>CFBundleURLSchemes</key>
25+
<array>
26+
<string>rocketchat</string>
27+
</array>
28+
</dict>
29+
</array>
1730
</dict>
1831
</plist>

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"linux": {
4545
"desktop": {
4646
"Categories": "GNOME;GTK;Network;InstantMessaging",
47-
"StartupWMClass": "Rocket.Chat+"
47+
"StartupWMClass": "Rocket.Chat+",
48+
"MimeType": "x-scheme-handler/rocketchat"
4849
},
4950
"target": [
5051
"deb",

src/background.custom.js

+9-20
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ process.env.GOOGLE_API_KEY = 'AIzaSyADqUh_c1Qhji3Cp1NE43YrcpuPkmhXD-c';
2121
let screenshareEvent;
2222
ipcMain.on('screenshare', (event, sources) => {
2323
screenshareEvent = event;
24-
let window = new BrowserWindow({
24+
let mainWindow = new BrowserWindow({
2525
width: 776,
2626
height: 600,
2727
show : false,
2828
skipTaskbar: false
2929
});
3030

31-
window.loadURL('file://'+__dirname+'/public/screenshare.html');
31+
mainWindow.loadURL('file://'+__dirname+'/public/screenshare.html');
3232

3333
//window.openDevTools();
34-
window.webContents.on('did-finish-load', () => {
35-
window.webContents.send('sources', sources);
36-
window.show();
34+
mainWindow.webContents.on('did-finish-load', () => {
35+
mainWindow.webContents.send('sources', sources);
36+
mainWindow.show();
3737
});
3838

39-
window.on('closed', () => {
40-
window = null;
39+
mainWindow.on('closed', () => {
40+
mainWindow = null;
4141
if (screenshareEvent) {
4242
screenshareEvent.sender.send('screenshare-result', 'PermissionDeniedError');
4343
screenshareEvent = null;
@@ -53,20 +53,9 @@ ipcMain.on('source-result', (e, sourceId) => {
5353
});
5454

5555
export function afterMainWindow (mainWindow) {
56-
if (process.platform !== 'darwin') {
57-
const shouldQuit = app.makeSingleInstance(function () {
58-
// Someone tried to run a second instance, we should focus our window.
59-
if (mainWindow) {
60-
mainWindow.show();
61-
mainWindow.focus();
62-
}
63-
});
64-
65-
if (shouldQuit) {
66-
app.quit();
67-
}
56+
if (!app.isDefaultProtocolClient('rocketchat')) {
57+
app.setAsDefaultProtocolClient('rocketchat');
6858
}
69-
7059
// Preserver of the window size and position between app launches.
7160
const mainWindowState = windowStateKeeper('main', {
7261
width: 1000,

src/background.js

+90-1
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,30 @@ if (env.name !== 'production') {
3535
app.setPath('userData', userDataPath + ' (' + env.name + ')');
3636
}
3737

38+
const processProtocolURI = (uri) => {
39+
if (uri && uri.startsWith('rocketchat://')) {
40+
const site = uri.split(/\/|\?/)[2];
41+
if (site) {
42+
let scheme = 'https://';
43+
if (uri.includes('insecure=true')) {
44+
scheme = 'http://';
45+
}
46+
return scheme + site;
47+
}
48+
}
49+
};
50+
const processProtocolArgv = (argv) => {
51+
const protocolURI = argv.find(arg => arg.startsWith('rocketchat://'));
52+
if (protocolURI) {
53+
return processProtocolURI(protocolURI);
54+
}
55+
};
56+
let mainWindow = null;
57+
3858
app.on('ready', function () {
3959
setApplicationMenu();
4060

41-
const mainWindow = createWindow('main', {
61+
mainWindow = createWindow('main', {
4262
width: 1000,
4363
titleBarStyle: 'hidden',
4464
height: 600
@@ -55,8 +75,77 @@ app.on('ready', function () {
5575
if (env.name === 'development') {
5676
mainWindow.openDevTools();
5777
}
78+
if (process.argv.length > 1) {
79+
const site = processProtocolArgv(process.argv);
80+
if (site) {
81+
const dialog = require('electron').dialog;
82+
dialog.showMessageBox({
83+
type: 'question',
84+
buttons: ['Add', 'Cancel'],
85+
defaultId: 0,
86+
title: 'Add Server',
87+
message: `Do you want to add "${site}" to your list of servers?`
88+
}, (response) => {
89+
if (response === 0) {
90+
mainWindow.send('add-host', site);
91+
}
92+
});
93+
}
94+
}
95+
5896
});
5997

6098
app.on('window-all-closed', function () {
6199
app.quit();
62100
});
101+
const appIsReady = new Promise(resolve => {
102+
if (app.isReady()) {
103+
resolve();
104+
} else {
105+
app.on('ready', resolve);
106+
}
107+
});
108+
109+
110+
if (process.platform === 'darwin') {
111+
// Open protocol urls on mac as open-url is not yet implemented on other OS's
112+
app.on('open-url', function (e, url) {
113+
e.preventDefault();
114+
const site = processProtocolURI(url);
115+
if (site) {
116+
appIsReady.then(() => {
117+
mainWindow.send('add-host', site);
118+
});
119+
}
120+
});
121+
} else {
122+
const shouldQuit = app.makeSingleInstance((argv) => {
123+
// Someone tried to run a second instance, we should focus our window.
124+
const site = processProtocolArgv(argv);
125+
if (site) {
126+
const dialog = require('electron').dialog;
127+
dialog.showMessageBox({
128+
type: 'question',
129+
buttons: ['Add', 'Cancel'],
130+
defaultId: 0,
131+
title: 'Add Server',
132+
message: `Do you want to add "${site}" to your list of servers?`
133+
}, (response) => {
134+
if (response === 0) {
135+
mainWindow.send('add-host', site);
136+
}
137+
});
138+
}
139+
if (mainWindow) {
140+
if (mainWindow.isMinimized()) {
141+
mainWindow.restore();
142+
}
143+
mainWindow.show();
144+
mainWindow.focus();
145+
}
146+
});
147+
148+
if (shouldQuit) {
149+
app.quit();
150+
}
151+
}

src/background/autoUpdate.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ try {
1515
const userUpdateFile = userDataDir.read(updateStoreFile, 'json');
1616
updateFile = Object.assign({}, installUpdateFile, userUpdateFile);
1717
} catch (err) {
18-
console.log(err);
18+
console.error(err);
1919
}
2020

2121
function updateDownloaded () {
@@ -52,7 +52,6 @@ function updateAvailable ({version}) {
5252
checkForUpdatesEvent.sender.send('update-result', true);
5353
checkForUpdatesEvent = null;
5454
} else if (updateFile.skip === version) {
55-
console.log(`Skipping version: ${version}`);
5655
return;
5756
}
5857

@@ -112,10 +111,6 @@ function checkForUpdates () {
112111
autoUpdater.on('update-available', updateAvailable);
113112
autoUpdater.on('update-not-available', updateNotAvailable);
114113

115-
autoUpdater.on('download-progress', ({percent}) => {
116-
console.log(`Update progress: ${percent}`);
117-
});
118-
119114
autoUpdater.on('update-downloaded', updateDownloaded);
120115

121116
// Event from about window

src/public/lib/SpellCheck.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class SpellCheck {
103103
}
104104

105105
if (!this.setEnabled('en')) {
106-
console.log('Unable to set a language for the spell checker - Spell checker is disabled');
106+
console.info('Unable to set a language for the spell checker - Spell checker is disabled');
107107
}
108108

109109
}

src/scripts/servers.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@
22

33
import jetpack from 'fs-jetpack';
44
import { EventEmitter } from 'events';
5-
import { remote } from 'electron';
5+
import { remote, ipcRenderer } from 'electron';
66
const remoteServers = remote.require('./background').remoteServers;
77

88
class Servers extends EventEmitter {
99
constructor () {
1010
super();
1111
this.load();
12+
ipcRenderer.on('add-host', (e, host) => {
13+
if (this.hostExists(host)) {
14+
this.setActive(host);
15+
} else {
16+
this.validateHost(host)
17+
.then(() => this.addHost(host))
18+
.then(() => this.setActive(host))
19+
.catch(() => remote.dialog.showErrorBox('Invalid Host', `The host "${host}" could not be validated, so was not added.`));
20+
}
21+
});
1222
}
1323

1424
get hosts () {
@@ -85,7 +95,7 @@ class Servers extends EventEmitter {
8595
}
8696

8797
} catch (e) {
88-
console.log('Server file invalid');
98+
console.error('Server file invalid');
8999
}
90100
}
91101

@@ -112,7 +122,6 @@ class Servers extends EventEmitter {
112122
}
113123

114124
validateHost (hostUrl, timeout) {
115-
console.log('Validating hostUrl', hostUrl);
116125
timeout = timeout || 5000;
117126
return new Promise(function (resolve, reject) {
118127
let resolved = false;
@@ -121,22 +130,19 @@ class Servers extends EventEmitter {
121130
return;
122131
}
123132
resolved = true;
124-
console.log('HostUrl valid', hostUrl);
125133
resolve();
126134
}, function (request) {
127135
if (request.status === 401) {
128136
const authHeader = request.getResponseHeader('www-authenticate');
129137
if (authHeader && authHeader.toLowerCase().indexOf('basic ') === 0) {
130138
resolved = true;
131-
console.log('HostUrl needs basic auth', hostUrl);
132139
reject('basic-auth');
133140
}
134141
}
135142
if (resolved) {
136143
return;
137144
}
138145
resolved = true;
139-
console.log('HostUrl invalid', hostUrl);
140146
reject('invalid');
141147
});
142148
if (timeout) {
@@ -145,7 +151,6 @@ class Servers extends EventEmitter {
145151
return;
146152
}
147153
resolved = true;
148-
console.log('Validating hostUrl TIMEOUT', hostUrl);
149154
reject('timeout');
150155
}, timeout);
151156
}

src/scripts/webview.js

-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ class WebView extends EventEmitter {
170170
}
171171

172172
setActive (hostUrl) {
173-
console.log('active setted', hostUrl);
174173
if (this.isActive(hostUrl)) {
175174
return;
176175
}

0 commit comments

Comments
 (0)