BetterDiscordApp-v2/client/src/modules/updater.js

191 lines
5.8 KiB
JavaScript
Raw Normal View History

2018-02-14 21:15:27 +01:00
/**
* BetterDiscord Updater Module
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
2019-03-06 11:57:19 +01:00
import { Notifications } from 'ui';
import { Reflection, Globals } from 'modules';
2019-03-05 22:46:42 +01:00
import Events from './events';
import Module from './imodule';
export default new class extends Module {
get updates() { return this.state.updates }
get bdUpdates() { return this.state.updates.bd }
2019-03-06 08:04:03 +01:00
get error() { return null; }
get updatesAvailable() { return this.state.updatesAvailable; }
2018-02-14 21:15:27 +01:00
constructor() {
2019-03-05 22:46:42 +01:00
super({
updatesAvailable: false,
error: null,
2019-03-06 08:04:03 +01:00
updates: { bd: [] },
updating: false
2019-03-05 22:46:42 +01:00
});
}
2019-03-06 11:57:19 +01:00
bindings() {
this.restartNotif = this.restartNotif.bind(this);
this.reloadNotif = this.reloadNotif.bind(this);
2019-03-07 10:35:24 +01:00
this.startUpdate = this.startUpdate.bind(this);
this.setUpdateStatus = this.setUpdateStatus.bind(this);
2019-03-08 12:57:02 +01:00
this.testUi = this.testUi.bind(this);
2019-03-06 11:57:19 +01:00
}
restartNotif() {
Notifications.add('Updates Finished!', 'Restart required.', [
{
text: 'Restart Later',
onClick: () => { setTimeout(this.restartNotif, 5 * 60000); return true; }
},
{
text: 'Restart Now',
onClick: () => {
try {
const { remote } = Globals.require('electron');
window.close();
Reflection.module.byProps('showToken', 'hideToken').showToken();
remote.app.relaunch();
remote.app.exit(0);
} catch (err) {
console.err(err);
return true;
}
}
}
]);
}
reloadNotif() {
Notifications.add('Updates Finished!', 'Reload required.', [
{
text: 'Reload Later',
onClick: () => { setTimeout(this.reloadNotif, 5 * 60000); return true; }
},
{
text: 'Reload Now',
onClick: () => {
document.location.reload();
}
}
]);
}
2019-03-05 22:46:42 +01:00
events(ipc) {
ipc.on('updater-checkForUpdates', () => {
2019-03-06 08:04:03 +01:00
if (this.state.updating) return; // We're already updating. Updater should be paused anyways at this point.
2019-03-05 22:46:42 +01:00
Events.emit('update-check-start');
});
2019-03-06 07:36:54 +01:00
ipc.on('updater-noUpdates', () => {
2019-03-06 08:04:03 +01:00
if (this.state.updatesAvailable) return; // If for some reason we get this even though we have updates already.
this.setState({
updatesAvailable: false,
updates: {}
});
2019-03-06 07:36:54 +01:00
});
ipc.on('updater-updatesAvailable', (_, updates) => {
2019-03-08 12:57:02 +01:00
console.log(updates);
2019-03-06 08:04:03 +01:00
if (this.state.updating) return; // If for some reason we get more updates when we're already updating
2019-03-06 08:29:23 +01:00
updates.bd = updates.bd.map(update => {
update.text = `${update.id.charAt(0).toUpperCase()}${update.id.slice(1)}`;
update.hint = `Current: ${update.currentVersion} | Latest: ${update.version}`;
update.status = {
update: true,
updating: false,
updated: false,
error: null
};
return update;
});
2019-03-06 08:04:03 +01:00
this.setState({
updates,
updatesAvailable: true
});
2019-03-06 07:36:54 +01:00
});
2019-03-06 11:57:19 +01:00
ipc.on('updater-updated', (_, info) => {
const { reloadRequired, restartRequired } = info;
if (restartRequired) {
this.restartNotif();
return;
}
if (reloadRequired) {
this.reloadNotif();
return;
}
});
2019-03-07 10:35:24 +01:00
ipc.on('updater-updateFinished', (_, update) => {
this.setUpdateStatus(update.id, 'updated', true);
});
ipc.on('updater-updateError', (_, update) => {
this.setUpdateStatus(update.id, 'error', update.error);
});
2018-02-14 21:15:27 +01:00
}
2019-03-06 08:04:03 +01:00
stateChanged(oldState, newState) {
if (!newState.updatesAvailable) return Events.emit('update-check-end');
2019-03-07 10:41:32 +01:00
if (!oldState.updatesAvailable && newState.updatesAvailable) {
Events.emit('updates-available');
Notifications.add('', 'Updates Available!', [
{
text: 'Ignore',
onClick: () => { return true; }
},
{
text: 'Show Updates',
onClick: () => {
Events.emit('bd-open-menu', 'updater');
return true;
}
}
]);
}
2019-03-06 08:04:03 +01:00
}
2019-03-07 10:35:24 +01:00
setUpdateStatus(updateId, statusChild, statusValue) {
for (const u of this.bdUpdates) {
if (u.id === updateId) {
u.status[statusChild] = statusValue;
return;
}
}
}
2019-03-06 08:29:23 +01:00
toggleUpdate(update) {
update.status.update = !update.status.update;
}
2019-03-07 10:35:24 +01:00
async startUpdate() {
console.log('start update');
const updates = { bd: [] };
for (const update of this.bdUpdates) {
if (update.status.update) {
update.status.updating = true;
updates.bd.push(update);
}
}
console.log(updates);
this.send('updater-startUpdate', updates);
}
2019-03-08 12:57:02 +01:00
testUi(updates) {
this.setState({
updates,
updatesAvailable: true
});
}
2018-02-14 21:15:27 +01:00
}