Commit e36251e6 authored by Tasso Evangelista's avatar Tasso Evangelista

Update code style of source code

parent 086393e6
import './branding/branding.js'; import './branding/branding.js';
import { start } from './scripts/start'; import { start } from './scripts/start';
import { remote } from 'electron'; import { remote } from 'electron';
const app = remote.app; const { app } = remote;
Bugsnag.metaData = { Bugsnag.metaData = {
// platformId: app.process.platform, // platformId: app.process.platform,
// platformArch: app.process.arch, // platformArch: app.process.arch,
// electronVersion: app.process.versions.electron, // electronVersion: app.process.versions.electron,
version: app.getVersion() version: app.getVersion(),
// platformVersion: cordova.platformVersion // platformVersion: cordova.platformVersion
// build: appInfo.build // build: appInfo.build
}; };
Bugsnag.appVersion = app.getVersion(); Bugsnag.appVersion = app.getVersion();
......
...@@ -21,90 +21,90 @@ process.env.GOOGLE_API_KEY = 'AIzaSyADqUh_c1Qhji3Cp1NE43YrcpuPkmhXD-c'; ...@@ -21,90 +21,90 @@ process.env.GOOGLE_API_KEY = 'AIzaSyADqUh_c1Qhji3Cp1NE43YrcpuPkmhXD-c';
const isMacOS = process.platform === 'darwin'; const isMacOS = process.platform === 'darwin';
const unsetDefaultApplicationMenu = () => { const unsetDefaultApplicationMenu = () => {
if (!isMacOS) { if (!isMacOS) {
Menu.setApplicationMenu(null); Menu.setApplicationMenu(null);
return; return;
} }
const emptyMenuTemplate = [{ const emptyMenuTemplate = [{
submenu: [ submenu: [
{ {
label: i18n.__('Quit_App', app.getName()), label: i18n.__('Quit_App', app.getName()),
accelerator: 'CommandOrControl+Q', accelerator: 'CommandOrControl+Q',
click () { click() {
app.quit(); app.quit();
} },
} },
] ],
}]; }];
Menu.setApplicationMenu(Menu.buildFromTemplate(emptyMenuTemplate)); Menu.setApplicationMenu(Menu.buildFromTemplate(emptyMenuTemplate));
}; };
const setUserDataPath = () => { const setUserDataPath = () => {
const appName = app.getName(); const appName = app.getName();
const dirName = env.name === 'production' ? appName : `${ appName } (${ env.name })`; const dirName = env.name === 'production' ? appName : `${ appName } (${ env.name })`;
app.setPath('userData', path.join(app.getPath('appData'), dirName)); app.setPath('userData', path.join(app.getPath('appData'), dirName));
}; };
const migrateOlderVersionUserData = () => { const migrateOlderVersionUserData = () => {
const olderAppName = 'Rocket.Chat+'; const olderAppName = 'Rocket.Chat+';
const dirName = env.name === 'production' ? olderAppName : `${ olderAppName } (${ env.name })`; const dirName = env.name === 'production' ? olderAppName : `${ olderAppName } (${ env.name })`;
const olderUserDataPath = path.join(app.getPath('appData'), dirName); const olderUserDataPath = path.join(app.getPath('appData'), dirName);
try { try {
jetpack.copy(olderUserDataPath, app.getPath('userData'), { overwrite: true }); jetpack.copy(olderUserDataPath, app.getPath('userData'), { overwrite: true });
jetpack.remove(olderUserDataPath); jetpack.remove(olderUserDataPath);
} catch (e) { } catch (e) {
return; return;
} }
}; };
const parseProtocolUrls = (args) => const parseProtocolUrls = (args) =>
args.filter(arg => /^rocketchat:\/\/./.test(arg)) args.filter((arg) => /^rocketchat:\/\/./.test(arg))
.map(uri => url.parse(uri)) .map((uri) => url.parse(uri))
.map(({ hostname, pathname, query }) => { .map(({ hostname, pathname, query }) => {
const { insecure } = querystring.parse(query); const { insecure } = querystring.parse(query);
return `${ insecure === 'true' ? 'http' : 'https' }://${ hostname }${ pathname || '' }`; return `${ insecure === 'true' ? 'http' : 'https' }://${ hostname }${ pathname || '' }`;
}); });
const addServers = (protocolUrls) => parseProtocolUrls(protocolUrls) const addServers = (protocolUrls) => parseProtocolUrls(protocolUrls)
.forEach(serverUrl => addServer(serverUrl)); .forEach((serverUrl) => addServer(serverUrl));
const isSecondInstance = app.makeSingleInstance((argv) => { const isSecondInstance = app.makeSingleInstance((argv) => {
addServers(argv.slice(2)); addServers(argv.slice(2));
}); });
if (isSecondInstance && !process.mas) { if (isSecondInstance && !process.mas) {
app.quit(); app.quit();
} }
// macOS only // macOS only
app.on('open-url', (event, url) => { app.on('open-url', (event, url) => {
event.preventDefault(); event.preventDefault();
addServers([ url ]); addServers([url]);
}); });
app.on('ready', () => { app.on('ready', () => {
unsetDefaultApplicationMenu(); unsetDefaultApplicationMenu();
setUserDataPath(); setUserDataPath();
migrateOlderVersionUserData(); migrateOlderVersionUserData();
if (!app.isDefaultProtocolClient('rocketchat')) { if (!app.isDefaultProtocolClient('rocketchat')) {
app.setAsDefaultProtocolClient('rocketchat'); app.setAsDefaultProtocolClient('rocketchat');
} }
createMainWindow(); createMainWindow();
getMainWindow().then(mainWindow => certificate.initWindow(mainWindow)); getMainWindow().then((mainWindow) => certificate.initWindow(mainWindow));
autoUpdate(); autoUpdate();
}); });
app.on('window-all-closed', () => { app.on('window-all-closed', () => {
app.quit(); app.quit();
}); });
ipcMain.on('getSystemIdleTime', (event) => { ipcMain.on('getSystemIdleTime', (event) => {
event.returnValue = idle.getIdleTime(); event.returnValue = idle.getIdleTime();
}); });
...@@ -8,127 +8,127 @@ const userDataDir = jetpack.cwd(app.getPath('userData')); ...@@ -8,127 +8,127 @@ const userDataDir = jetpack.cwd(app.getPath('userData'));
const updateSettingsFileName = 'update.json'; const updateSettingsFileName = 'update.json';
const loadUpdateSettings = (dir) => { const loadUpdateSettings = (dir) => {
try { try {
return dir.read(updateSettingsFileName, 'json') || {}; return dir.read(updateSettingsFileName, 'json') || {};
} catch (error) { } catch (error) {
console.error(error); console.error(error);
return {}; return {};
} }
}; };
const appUpdateSettings = loadUpdateSettings(appDir); const appUpdateSettings = loadUpdateSettings(appDir);
const userUpdateSettings = loadUpdateSettings(userDataDir); const userUpdateSettings = loadUpdateSettings(userDataDir);
const updateSettings = (() => { const updateSettings = (() => {
const defaultUpdateSettings = { autoUpdate: true }; const defaultUpdateSettings = { autoUpdate: true };
if (appUpdateSettings.forced) { if (appUpdateSettings.forced) {
return Object.assign({}, defaultUpdateSettings, appUpdateSettings); return Object.assign({}, defaultUpdateSettings, appUpdateSettings);
} else { } else {
return Object.assign({}, defaultUpdateSettings, appUpdateSettings, userUpdateSettings); return Object.assign({}, defaultUpdateSettings, appUpdateSettings, userUpdateSettings);
} }
})(); })();
delete updateSettings.forced; delete updateSettings.forced;
const saveUpdateSettings = () => { const saveUpdateSettings = () => {
if (appUpdateSettings.forced) { if (appUpdateSettings.forced) {
return; return;
} }
userDataDir.write(updateSettingsFileName, userUpdateSettings, { atomic: true }); userDataDir.write(updateSettingsFileName, userUpdateSettings, { atomic: true });
}; };
let checkForUpdatesEvent; let checkForUpdatesEvent;
function updateDownloaded () { function updateDownloaded() {
dialog.showMessageBox({ dialog.showMessageBox({
title: i18n.__('Update_ready'), title: i18n.__('Update_ready'),
message: i18n.__('Update_ready_message'), message: i18n.__('Update_ready_message'),
buttons: [ buttons: [
i18n.__('Update_Install_Later'), i18n.__('Update_Install_Later'),
i18n.__('Update_Install_Now') i18n.__('Update_Install_Now'),
], ],
defaultId: 1 defaultId: 1,
}, (response) => { }, (response) => {
if (response === 0) { if (response === 0) {
dialog.showMessageBox({ dialog.showMessageBox({
title: i18n.__('Update_installing_later'), title: i18n.__('Update_installing_later'),
message: i18n.__('Update_installing_later_message') message: i18n.__('Update_installing_later_message'),
}); });
} else { } else {
autoUpdater.quitAndInstall(); autoUpdater.quitAndInstall();
setTimeout(() => app.quit(), 1000); setTimeout(() => app.quit(), 1000);
} }
}); });
} }
function updateNotAvailable () { function updateNotAvailable() {
if (checkForUpdatesEvent) { if (checkForUpdatesEvent) {
checkForUpdatesEvent.sender.send('update-result', false); checkForUpdatesEvent.sender.send('update-result', false);
checkForUpdatesEvent = null; checkForUpdatesEvent = null;
} }
} }
function updateAvailable ({version}) { function updateAvailable({ version }) {
if (checkForUpdatesEvent) { if (checkForUpdatesEvent) {
checkForUpdatesEvent.sender.send('update-result', true); checkForUpdatesEvent.sender.send('update-result', true);
checkForUpdatesEvent = null; checkForUpdatesEvent = null;
} else if (updateSettings.skip === version) { } else if (updateSettings.skip === version) {
return; return;
} }
let window = new BrowserWindow({ let window = new BrowserWindow({
title: i18n.__('Update_Available'), title: i18n.__('Update_Available'),
width: 600, width: 600,
height: 330, height: 330,
show : false, show : false,
center: true, center: true,
resizable: false, resizable: false,
maximizable: false, maximizable: false,
minimizable: false minimizable: false,
}); });
window.loadURL(`file://${__dirname}/public/update.html`); window.loadURL(`file://${ __dirname }/public/update.html`);
window.setMenuBarVisibility(false); window.setMenuBarVisibility(false);
window.webContents.on('did-finish-load', () => { window.webContents.on('did-finish-load', () => {
window.webContents.send('new-version', version); window.webContents.send('new-version', version);
window.show(); window.show();
}); });
ipcMain.once('update-response', (e, type) => { ipcMain.once('update-response', (e, type) => {
switch (type) { switch (type) {
case 'skip': case 'skip':
userUpdateSettings.skip = version; userUpdateSettings.skip = version;
saveUpdateSettings(); saveUpdateSettings();
dialog.showMessageBox({ dialog.showMessageBox({
title: i18n.__('Update_skip'), title: i18n.__('Update_skip'),
message: i18n.__('Update_skip_message') message: i18n.__('Update_skip_message'),
}, () => window.close()); }, () => window.close());
break; break;
case 'remind': case 'remind':
dialog.showMessageBox({ dialog.showMessageBox({
title: i18n.__('Update_remind'), title: i18n.__('Update_remind'),
message: i18n.__('Update_remind_message') message: i18n.__('Update_remind_message'),
}, () => window.close()); }, () => window.close());
break; break;
case 'update': case 'update':
dialog.showMessageBox({ dialog.showMessageBox({
title: i18n.__('Update_downloading'), title: i18n.__('Update_downloading'),
message: i18n.__('Update_downloading_message') message: i18n.__('Update_downloading_message'),
}, () => window.close()); }, () => window.close());
autoUpdater.downloadUpdate(); autoUpdater.downloadUpdate();
break; break;
} }
}); });
window.on('closed', () => { window.on('closed', () => {
window = null; window = null;
ipcMain.removeAllListeners('update-response'); ipcMain.removeAllListeners('update-response');
}); });
} }
export const canUpdate = () => export const canUpdate = () =>
(process.platform === 'linux' && Boolean(process.env.APPIMAGE)) || (process.platform === 'linux' && Boolean(process.env.APPIMAGE)) ||
(process.platform === 'win32' && !process.windowsStore) || (process.platform === 'win32' && !process.windowsStore) ||
(process.platform === 'darwin' && !process.mas); (process.platform === 'darwin' && !process.mas);
...@@ -137,28 +137,28 @@ export const canAutoUpdate = () => updateSettings.autoUpdate !== false; ...@@ -137,28 +137,28 @@ export const canAutoUpdate = () => updateSettings.autoUpdate !== false;
export const canSetAutoUpdate = () => !appUpdateSettings.forced || appUpdateSettings.autoUpdate !== false; export const canSetAutoUpdate = () => !appUpdateSettings.forced || appUpdateSettings.autoUpdate !== false;
export const setAutoUpdate = (canAutoUpdate) => { export const setAutoUpdate = (canAutoUpdate) => {
if (!canSetAutoUpdate()) { if (!canSetAutoUpdate()) {
return; return;
} }
updateSettings.autoUpdate = userUpdateSettings.autoUpdate = Boolean(canAutoUpdate); updateSettings.autoUpdate = userUpdateSettings.autoUpdate = Boolean(canAutoUpdate);
saveUpdateSettings(); saveUpdateSettings();
}; };
ipcMain.on('can-update', (event) => { ipcMain.on('can-update', (event) => {
event.returnValue = canUpdate(); event.returnValue = canUpdate();
}); });
ipcMain.on('can-auto-update', (event) => { ipcMain.on('can-auto-update', (event) => {
event.returnValue = canAutoUpdate(); event.returnValue = canAutoUpdate();
}); });
ipcMain.on('can-set-auto-update', (event) => { ipcMain.on('can-set-auto-update', (event) => {
event.returnValue = canSetAutoUpdate(); event.returnValue = canSetAutoUpdate();
}); });
ipcMain.on('set-auto-update', (event, canAutoUpdate) => { ipcMain.on('set-auto-update', (event, canAutoUpdate) => {
setAutoUpdate(canAutoUpdate); setAutoUpdate(canAutoUpdate);
}); });
autoUpdater.autoDownload = false; autoUpdater.autoDownload = false;
...@@ -167,14 +167,14 @@ autoUpdater.on('update-not-available', updateNotAvailable); ...@@ -167,14 +167,14 @@ autoUpdater.on('update-not-available', updateNotAvailable);
autoUpdater.on('update-downloaded', updateDownloaded); autoUpdater.on('update-downloaded', updateDownloaded);
ipcMain.on('check-for-updates', (event) => { ipcMain.on('check-for-updates', (event) => {
if (canAutoUpdate() && canUpdate()) { if (canAutoUpdate() && canUpdate()) {
checkForUpdatesEvent = event; checkForUpdatesEvent = event;
autoUpdater.checkForUpdates(); autoUpdater.checkForUpdates();
} }
}); });
export default () => { export default () => {
if (canAutoUpdate() && canUpdate()) { if (canAutoUpdate() && canUpdate()) {
autoUpdater.checkForUpdates(); autoUpdater.checkForUpdates();
} }
}; };
...@@ -4,108 +4,108 @@ import url from 'url'; ...@@ -4,108 +4,108 @@ import url from 'url';
import i18n from '../i18n/index.js'; import i18n from '../i18n/index.js';
class CertificateStore { class CertificateStore {
initWindow (win) { initWindow(win) {
this.storeFileName = 'certificate.json'; this.storeFileName = 'certificate.json';
this.userDataDir = jetpack.cwd(app.getPath('userData')); this.userDataDir = jetpack.cwd(app.getPath('userData'));
this.load(); this.load();
// Don't ask twice for same cert if loading multiple urls // Don't ask twice for same cert if loading multiple urls
this.queued = {}; this.queued = {};
this.window = win; this.window = win;
app.on('certificate-error', (event, webContents, url, error, certificate, callback) => { app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
event.preventDefault(); event.preventDefault();
if (this.isTrusted(url, certificate)) { if (this.isTrusted(url, certificate)) {
callback(true); callback(true);
return; return;
} }
if (this.queued[certificate.fingerprint]) { if (this.queued[certificate.fingerprint]) {
this.queued[certificate.fingerprint].push(callback); this.queued[certificate.fingerprint].push(callback);
// Call the callback after approved/rejected // Call the callback after approved/rejected
return; return;
} else { } else {
this.queued[certificate.fingerprint] = [callback]; this.queued[certificate.fingerprint] = [callback];
} }
let detail = `URL: ${url}\nError: ${error}`; let detail = `URL: ${ url }\nError: ${ error }`;
if (this.isExisting(url)) { if (this.isExisting(url)) {
detail = i18n.__('Certificate_error_different', detail); detail = i18n.__('Certificate_error_different', detail);
} }
dialog.showMessageBox(this.window, { dialog.showMessageBox(this.window, {
title: i18n.__('Certificate_error'), title: i18n.__('Certificate_error'),
message: i18n.__('Certificate_error_message', certificate.issuerName), message: i18n.__('Certificate_error_message', certificate.issuerName),
detail: detail, detail,
type: 'warning', type: 'warning',
buttons: [ buttons: [
i18n.__('Yes'), i18n.__('Yes'),
i18n.__('No') i18n.__('No'),
], ],
cancelId: 1 cancelId: 1,
}, (response) => { }, (response) => {
if (response === 0) { if (response === 0) {
this.add(url, certificate); this.add(url, certificate);
this.save(); this.save();
if (webContents.getURL().indexOf('file://') === 0) { if (webContents.getURL().indexOf('file://') === 0) {
webContents.send('certificate-reload', url); webContents.send('certificate-reload', url);
} }
} }
//Call all queued callbacks with result // Call all queued callbacks with result
this.queued[certificate.fingerprint].forEach(cb => cb(response === 0)); this.queued[certificate.fingerprint].forEach((cb) => cb(response === 0));
delete this.queued[certificate.fingerprint]; delete this.queued[certificate.fingerprint];
}); });
}); });
} }
load () { load() {
try { try {
this.data = this.userDataDir.read(this.storeFileName, 'json'); this.data = this.userDataDir.read(this.storeFileName, 'json');
} catch (e) { } catch (e) {
console.error(e); console.error(e);
this.data = {}; this.data = {};
} }
if (this.data === undefined) { if (this.data === undefined) {
this.clear(); this.clear();
} }
} }
clear () { clear() {
this.data = {}; this.data = {};
this.save(); this.save();
} }
save () { save() {
this.userDataDir.write(this.storeFileName, this.data, { atomic: true }); this.userDataDir.write(this.storeFileName, this.data, { atomic: true });
} }
parseCertificate (certificate) { parseCertificate(certificate) {
return certificate.issuerName + '\n' + certificate.data.toString(); return `${ certificate.issuerName }\n${ certificate.data.toString() }`;
} }
getHost (certUrl) { getHost(certUrl) {
return url.parse(certUrl).host; return url.parse(certUrl).host;
} }
add (certUrl, certificate) { add(certUrl, certificate) {
const host = this.getHost(certUrl); const host = this.getHost(certUrl);
this.data[host] = this.parseCertificate(certificate); this.data[host] = this.parseCertificate(certificate);
} }
isExisting (certUrl) { isExisting(certUrl) {
const host = this.getHost(certUrl); const host = this.getHost(certUrl);
return this.data.hasOwnProperty(host); return this.data.hasOwnProperty(host);
} }
isTrusted (certUrl, certificate) { isTrusted(certUrl, certificate) {
const host = this.getHost(certUrl); const host = this.getHost(certUrl);
if (!this.isExisting(certUrl)) { if (!this.isExisting(certUrl)) {
return false; return false;
} }
return this.data[host] === this.parseCertificate(certificate); return this.data[host] === this.parseCertificate(certificate);
} }
} }
const certificateStore = new CertificateStore(); const certificateStore = new CertificateStore();
......
...@@ -13,115 +13,115 @@ import env from '../env'; ...@@ -13,115 +13,115 @@ import env from '../env';
let mainWindow = null;