Skip to content

Commit

Permalink
Merge pull request #241 from leclercb/develop
Browse files Browse the repository at this point in the history
Merge develop into master
  • Loading branch information
leclercb authored Apr 19, 2021
2 parents 8ea4680 + a0ee73b commit 9216b31
Show file tree
Hide file tree
Showing 15 changed files with 288 additions and 245 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## New Features

* Upgrade to Electron 12
* Upgrade to Google APIs 71

## Bug Fixes

Expand Down
3 changes: 0 additions & 3 deletions craco.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ module.exports = {
}
},
webpack: {
alias: {
fs: path.resolve(__dirname, 'src/mock-fs.js')
},
configure: webpackConfig => {
webpackConfig.module.rules.push({
test: /\.worker\.js$/,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"electron-log": "^4.3.4",
"electron-updater": "^4.3.8",
"fs-extra": "^9.1.0",
"googleapis": "^48.0.0",
"googleapis": "^71.0.0",
"https-proxy-agent": "^5.0.0",
"inputmask-core": "^2.2.0",
"jspdf": "^2.3.1",
Expand Down
81 changes: 81 additions & 0 deletions public/electronIpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ const { app, dialog, ipcMain, shell, BrowserWindow } = require('electron');
const { autoUpdater, CancellationToken } = require('electron-updater');
const log = require('electron-log');
const fse = require('fs-extra');
const { google } = require('googleapis');
const HttpsProxyAgent = require('https-proxy-agent');
const os = require('os');
const path = require('path');

let googleAuthClient = null;
let googleCalendarClient = null;

async function sendRequest(axiosInstance, config) {
const result = await axiosInstance(config);

Expand Down Expand Up @@ -137,6 +141,83 @@ function initializeIpc(setQuitInitiated) {
return fse.writeFile(file, data);
});

ipcMain.handle('google-set-auth-client', (event, config, settings) => {
const client = new google.auth.OAuth2(
config.clientId,
config.clientSecret,
config.redirectUri
);

if (settings && settings.tokens) {
client.setCredentials(settings.tokens);
}

googleAuthClient = client;
});

ipcMain.handle('google-set-calendar-client', () => {
const client = google.calendar({
version: 'v3',
auth: googleAuthClient
});

googleCalendarClient = client;
});

ipcMain.handle('google-generate-auth-url', async () => {
const url = googleAuthClient.generateAuthUrl({
access_type: 'offline',
prompt: 'consent',
scope: [
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/calendar'
]
});

return url;
});

ipcMain.handle('google-get-tokens', async (event, code) => {
const { tokens } = await googleAuthClient.getToken(code);
return tokens;
});

ipcMain.handle('google-get-user-info', async () => {
const profile = google.oauth2({
version: 'v2',
auth: googleAuthClient
});

const result = await profile.userinfo.get();
return result.data;
});

ipcMain.handle('google-calendars-list', async (event, data) => {
const result = await googleCalendarClient.calendarList.list(data);
return result.data;
});

ipcMain.handle('google-calendars-insert', async (event, data) => {
const result = await googleCalendarClient.calendars.insert(data);
return result.data;
});

ipcMain.handle('google-events-list', async (event, data) => {
const result = await googleCalendarClient.events.list(data);
return result.data;
});

ipcMain.handle('google-events-insert', async (event, data) => {
const result = await googleCalendarClient.events.insert(data);
return result.data;
});

ipcMain.handle('google-events-delete', async (event, data) => {
const result = await googleCalendarClient.events.delete(data);
return result.data;
});

ipcMain.handle('initiate-quit', () => {
setQuitInitiated(true);
});
Expand Down
12 changes: 7 additions & 5 deletions public/electronPreload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld(
'electron',
{
invoke: (channel, ...args) => ipcRenderer.invoke(channel, ...args),
on: (channel, listener) => ipcRenderer.on(channel, listener),
removeListener: (channel, listener) => ipcRenderer.removeListener(channel, listener),
send: (channel, ...args) => ipcRenderer.send(channel, ...args),
sendSync: (channel, ...args) => ipcRenderer.sendSync(channel, ...args)
ipcRenderer: {
invoke: (channel, ...args) => ipcRenderer.invoke(channel, ...args),
on: (channel, listener) => ipcRenderer.on(channel, listener),
removeListener: (channel, listener) => ipcRenderer.removeListener(channel, listener),
send: (channel, ...args) => ipcRenderer.send(channel, ...args),
sendSync: (channel, ...args) => ipcRenderer.sendSync(channel, ...args)
}
}
);
6 changes: 3 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ function App() {
}
};

const electron = window.electron;
const { ipcRenderer } = window.electron;

electron.on('window-close', onClose);
ipcRenderer.on('window-close', onClose);

return () => {
electron.removeListener('window-close', onClose);
ipcRenderer.removeListener('window-close', onClose);
};
}
}, [ // eslint-disable-line react-hooks/exhaustive-deps
Expand Down
14 changes: 7 additions & 7 deletions src/actions/AutoUpdaterActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function checkForUpdates(quiet) {
export function downloadUpdate() {
return async dispatch => {
return new Promise((resolve, reject) => {
const electron = window.electron;
const { ipcRenderer } = window.electron;

const downloadProgressHandler = (event, info) => {
logger.debug('Download progress', info.percent);
Expand All @@ -94,13 +94,13 @@ export function downloadUpdate() {
const updateDownloadedHandler = (event, info) => {
logger.debug('Update downloaded', info);
dispatch(setUpdateDownloaded(info));
electron.removeListener('download-progress', downloadProgressHandler);
electron.removeListener('update-downloaded', updateDownloadedHandler);
ipcRenderer.removeListener('download-progress', downloadProgressHandler);
ipcRenderer.removeListener('update-downloaded', updateDownloadedHandler);
resolve(info);
};

electron.on('download-progress', downloadProgressHandler);
electron.on('update-downloaded', updateDownloadedHandler);
ipcRenderer.on('download-progress', downloadProgressHandler);
ipcRenderer.on('update-downloaded', updateDownloadedHandler);

logger.info('Download update');

Expand All @@ -109,8 +109,8 @@ export function downloadUpdate() {
electronDownloadUpdate().catch(e => {
logger.error('Download update error', e);

electron.removeListener('download-progress', downloadProgressHandler);
electron.removeListener('update-downloaded', updateDownloadedHandler);
ipcRenderer.removeListener('download-progress', downloadProgressHandler);
ipcRenderer.removeListener('update-downloaded', updateDownloadedHandler);
reject(e);
});
});
Expand Down
18 changes: 6 additions & 12 deletions src/actions/publication/googlecal/GoogleCalAccountInfoActions.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import { google } from 'googleapis';
import { setPublicationData } from 'actions/PublicationActions';
import { getClient } from 'actions/publication/googlecal/GoogleCalAuthorizationActions';
import { setAuthClient } from 'actions/publication/googlecal/GoogleCalAuthorizationActions';
import { getSettings } from 'selectors/SettingSelectors';
import logger from 'utils/LogUtils';

export function getGoogleCalAccountInfo() {
return async (dispatch, getState) => {
const settings = getSettings(getState());
setAuthClient(settings);

const client = getClient(settings);
const { ipcRenderer } = window.electron;
const userInfo = await ipcRenderer.invoke('google-get-user-info');

const profile = google.oauth2({
version: 'v2',
auth: client
});

const result = await profile.userinfo.get();

logger.debug('Get account info', result.data);
logger.debug('Get account info', userInfo);

await dispatch(setPublicationData('googlecal', {
accountInfo: result.data
accountInfo: userInfo
}));
};
}
38 changes: 13 additions & 25 deletions src/actions/publication/googlecal/GoogleCalAuthorizationActions.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
import { google } from 'googleapis';
import { updateSettings } from 'actions/SettingActions';
import { getConfig } from 'config/Config';
import { openExternal } from 'utils/ElectronIpc';
import logger from 'utils/LogUtils';

export function getClient(settings = null) {
const client = new google.auth.OAuth2(
getConfig().publication.googlecal.clientId,
getConfig().publication.googlecal.clientSecret,
getConfig().publication.googlecal.redirectUri
);

if (settings && settings.googlecal && settings.googlecal.tokens) {
client.setCredentials(settings.googlecal.tokens);
}
export function setAuthClient(settings = null) {
const { ipcRenderer } = window.electron;
ipcRenderer.invoke('google-set-auth-client', getConfig().publication.googlecal, settings ? settings.googlecal : null);
}

return client;
export function setCalendarClient() {
const { ipcRenderer } = window.electron;
ipcRenderer.invoke('google-set-calendar-client');
}

export function authorize() {
return async () => {
const client = getClient();

const url = client.generateAuthUrl({
access_type: 'offline',
prompt: 'consent',
scope: [
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/calendar'
]
});
setAuthClient();

const { ipcRenderer } = window.electron;
const url = await ipcRenderer.invoke('google-generate-auth-url');
openExternal(url);
};
}
Expand All @@ -40,9 +27,10 @@ export function createToken(code) {
logger.debug('Create token', code);

return async dispatch => {
const client = getClient();
setAuthClient();

const { tokens } = await client.getToken(code);
const { ipcRenderer } = window.electron;
const tokens = await ipcRenderer.invoke('google-get-tokens', code);

await dispatch(updateSettings({
googlecal: {
Expand Down
Loading

0 comments on commit 9216b31

Please sign in to comment.