Skip to content

Commit

Permalink
fix(auto-updates): no updates in platforms that don't support it
Browse files Browse the repository at this point in the history
fix(auto-updates): no updates in platforms that don't support it
  • Loading branch information
sr258 authored Apr 19, 2021
2 parents f01697a + fd44ac7 commit 955cb3a
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 46 deletions.
7 changes: 7 additions & 0 deletions client/src/state/System/SystemAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import superagent from 'superagent';

import { ISystemState } from './SystemTypes';

export async function getSystem(): Promise<ISystemState> {
return (await superagent.get(`/api/v1/system`)).body;
}
37 changes: 37 additions & 0 deletions client/src/state/System/SystemActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as Sentry from '@sentry/browser';

import {
SYSTEM_GET_SYSTEM_REQUEST,
SYSTEM_GET_SYSTEM_ERROR,
SYSTEM_GET_SYSTEM_SUCCESS
} from './SystemTypes';

import * as API from './SystemAPI';

export function getSystem(): any {
return async (dispatch: any) => {
try {
dispatch({
payload: {},
type: SYSTEM_GET_SYSTEM_REQUEST
});

try {
const system = await API.getSystem();

dispatch({
payload: system,
type: SYSTEM_GET_SYSTEM_SUCCESS
});
return system;
} catch (error) {
Sentry.captureException(error);

dispatch({
payload: { error },
type: SYSTEM_GET_SYSTEM_ERROR
});
}
} catch (error) {}
};
}
29 changes: 29 additions & 0 deletions client/src/state/System/SystemReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as Sentry from '@sentry/browser';

import {
ISystemActionTypes,
ISystemState,
SYSTEM_GET_SYSTEM_SUCCESS
} from './SystemTypes';

export const initialState: ISystemState = {
platformSupportsUpdates: true
};

export default function settingsReducer(
state: ISystemState = initialState,
action: ISystemActionTypes
): ISystemState {
try {
switch (action.type) {
case SYSTEM_GET_SYSTEM_SUCCESS:
return action.payload;

default:
return state;
}
} catch (error) {
Sentry.captureException(error);
return state;
}
}
32 changes: 32 additions & 0 deletions client/src/state/System/SystemTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export interface IState {
system: ISystemState;
}

export interface ISystemState {
platformSupportsUpdates: boolean;
}

export const SYSTEM_GET_SYSTEM_REQUEST = 'SYSTEM_GET_SYSTEM_REQUEST';
export const SYSTEM_GET_SYSTEM_SUCCESS = 'SYSTEM_GET_SYSTEM_SUCCESS';
export const SYSTEM_GET_SYSTEM_ERROR = 'SYSTEM_GET_SYSTEM_ERROR';

export interface IGetSystemRequestAction {
payload: {};
type: typeof SYSTEM_GET_SYSTEM_REQUEST;
}

export interface IGetSystemSuccessAction {
payload: ISystemState;
type: typeof SYSTEM_GET_SYSTEM_SUCCESS;
}
export interface IGetSystemErrorAction {
payload: {
error: string;
};
type: typeof SYSTEM_GET_SYSTEM_ERROR;
}

export type ISystemActionTypes =
| IGetSystemErrorAction
| IGetSystemRequestAction
| IGetSystemSuccessAction;
13 changes: 10 additions & 3 deletions client/src/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import * as SettingsTypes from './Settings/SettingsTypes';
import SettingsReducer from './Settings/SettingsReducer';
import * as SettingsActions from './Settings/SettingsActions';

import * as SystemTypes from './System/SystemTypes';
import SystemReducer from './System/SystemReducer';
import * as SystemActions from './System/SystemActions';

import RunReducer from './Run/RunReducer';
import * as RunTypes from './Run/RunTypes';
import * as RunActions from './Run/RunActions';
Expand Down Expand Up @@ -48,7 +52,8 @@ const rootReducer = () =>
h5peditor: H5PEditorReducer,
analytics: AnalyticsReducer,
run: RunReducer,
settings: SettingsReducer
settings: SettingsReducer,
system: SystemReducer
});

const store = createStore(
Expand All @@ -62,14 +67,16 @@ export interface IState
NotificationsTypes.IState,
AnalyticsTypes.IState,
SettingsTypes.IState,
RunTypes.IState {}
RunTypes.IState,
SystemTypes.IState {}

export const actions = {
notifications: NotificationsActions,
h5peditor: H5PEditorActions,
analytics: AnalyticsActions,
settings: SettingsActions,
run: RunActions
run: RunActions,
system: SystemActions
};

export const selectors = {
Expand Down
2 changes: 2 additions & 0 deletions client/src/views/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export default function AppContainer() {
}
}
);

dispatch(actions.system.getSystem());
}, [dispatch, i18n]);

return (
Expand Down
39 changes: 23 additions & 16 deletions client/src/views/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import classnames from 'classnames';

import { useTranslation } from 'react-i18next';
import { useDispatch } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';

import { createStyles, makeStyles, Theme } from '@material-ui/core/styles';
import Dialog from '@material-ui/core/Dialog';
Expand Down Expand Up @@ -35,6 +35,7 @@ import AccountSettingsList from './components/Settings/AccountSettingsList';
import SettingsLibraryManagement from './components/Settings/LibraryManagement';
import UpdateSettings from './components/Settings/UpdatesSettings';

import { IState } from '../state';
import { track } from '../state/track/actions';

const drawerWidth = 240;
Expand Down Expand Up @@ -100,6 +101,10 @@ export default function FullScreenDialog() {
const { t } = useTranslation();
const dispatch = useDispatch();

const platformSupportsUpdates = useSelector(
(state: IState) => state.system.platformSupportsUpdates
);

const [section, setSection] = React.useState('general');

const handleClickOpen = () => {
Expand Down Expand Up @@ -164,21 +169,23 @@ export default function FullScreenDialog() {
primary={t('settings.menu.general')}
/>
</ListItem>
<ListItem
button
key="updates"
onClick={() => setSection('updates')}
className={classnames({
[classes.selected]: section === 'updates'
})}
>
<ListItemIcon>
<UpdateIcon />
</ListItemIcon>
<ListItemText
primary={t('settings.menu.updates')}
/>
</ListItem>
{platformSupportsUpdates && (
<ListItem
button
key="updates"
onClick={() => setSection('updates')}
className={classnames({
[classes.selected]: section === 'updates'
})}
>
<ListItemIcon>
<UpdateIcon />
</ListItemIcon>
<ListItemText
primary={t('settings.menu.updates')}
/>
</ListItem>
)}
<ListItem
button
key="h5p-library-administration"
Expand Down
57 changes: 31 additions & 26 deletions client/src/views/components/SetupDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ const DialogActions = withStyles((theme: Theme) => ({
export default function CustomizedDialogs() {
const dispatch = useDispatch();
const settings = useSelector((state: IState) => state.settings);
const platformSupportsUpdates = useSelector(
(state: IState) => state.system.platformSupportsUpdates
);
const classes = useStyles();
const { t } = useTranslation();

Expand Down Expand Up @@ -227,33 +230,35 @@ export default function CustomizedDialogs() {
/>
</ListItemSecondaryAction>
</ListItem>
<ListItem>
<ListItemIcon>
<UpdateIcon />
</ListItemIcon>
<ListItemText
id="switch-list-label-updates"
primary={t('updates.title')}
secondary={t('updates.consent')}
/>
<ListItemSecondaryAction>
<Switch
edge="end"
onChange={() =>
dispatch(
actions.settings.changeSetting({
autoUpdates: !settings.autoUpdates
})
)
}
checked={settings.autoUpdates}
inputProps={{
'aria-labelledby':
'switch-list-label-updates'
}}
{platformSupportsUpdates && (
<ListItem>
<ListItemIcon>
<UpdateIcon />
</ListItemIcon>
<ListItemText
id="switch-list-label-updates"
primary={t('updates.title')}
secondary={t('updates.consent')}
/>
</ListItemSecondaryAction>
</ListItem>
<ListItemSecondaryAction>
<Switch
edge="end"
onChange={() =>
dispatch(
actions.settings.changeSetting({
autoUpdates: !settings.autoUpdates
})
)
}
checked={settings.autoUpdates}
inputProps={{
'aria-labelledby':
'switch-list-label-updates'
}}
/>
</ListItemSecondaryAction>
</ListItem>
)}
<ListItem>
<ListItemIcon>
<TranslateIcon />
Expand Down
3 changes: 3 additions & 0 deletions server/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import h5pRoutes from './h5pRoutes';
import analyticRoutes from './analyticRoutes';
import settingsRoutes from './settingsRoutes';
import runRoutes from './runRoutes';
import updateRoutes from './systemRoutes';

import User from '../User';

Expand All @@ -43,6 +44,8 @@ export default function (
next();
});

router.use('/api/v1/system', updateRoutes());

router.use(
'/api/v1/settings',
settingsRoutes(serverConfig, browserWindow, app)
Expand Down
28 changes: 28 additions & 0 deletions server/src/routes/systemRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import express from 'express';
import * as Sentry from '@sentry/electron';

import { platformSupportsUpdates } from '../updater';

export default function (): express.Router {
const router = express.Router();

router.get(
`/`,
async (
req: express.Request,
res: express.Response,
next: express.NextFunction
) => {
try {
res.status(200).json({
platformSupportsUpdates: platformSupportsUpdates()
});
} catch (error) {
Sentry.captureException(error);
res.status(500).json(error);
}
}
);

return router;
}
23 changes: 22 additions & 1 deletion server/src/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ import IServerConfig from './IServerConfig';
let updateAvailable: boolean = false;
let updating: boolean = false;

export const platformSupportsUpdates = () => {
if (process.env.DISABLE_UPDATES) {
return false;
}
if (process.platform === 'win32') {
return !process.windowsStore;
}
if (process.platform === 'darwin') {
return !process.mas;
}
if (process.platform === 'linux') {
if (process.env.APPIMAGE) {
return true;
}
}
return false;
};

export default async function boot(
app: Electron.App,
websocket: SocketIO.Server,
Expand Down Expand Up @@ -46,7 +64,10 @@ export default async function boot(
}
});

if ((await fsExtra.readJSON(serverConfig.settingsFile)).autoUpdates) {
if (
platformSupportsUpdates() &&
(await fsExtra.readJSON(serverConfig.settingsFile)).autoUpdates
) {
autoUpdater.checkForUpdates();
}
}

0 comments on commit 955cb3a

Please sign in to comment.