Skip to content

Commit 64fdb29

Browse files
committed
Move 'new version' support into Platform
1 parent a714edb commit 64fdb29

4 files changed

Lines changed: 86 additions & 59 deletions

File tree

‎src/components/views/globals/NewVersionBar.js‎

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ limitations under the License.
1919
var React = require('react');
2020
var sdk = require('matrix-react-sdk');
2121
import Modal from 'matrix-react-sdk/lib/Modal';
22+
import PlatformPeg from 'matrix-react-sdk/lib/PlatformPeg';
2223

2324
/**
2425
* Check a version string is compatible with the Changelog
@@ -31,35 +32,56 @@ function checkVersion(ver) {
3132

3233
export default function NewVersionBar(props) {
3334
const onChangelogClicked = () => {
34-
const ChangelogDialog = sdk.getComponent('dialogs.ChangelogDialog');
35-
36-
Modal.createDialog(ChangelogDialog, {
37-
version: props.version,
38-
newVersion: props.newVersion,
39-
onFinished: (update) => {
40-
if(update) {
41-
window.location.reload();
35+
if (props.releaseNotes) {
36+
const QuestionDialog = sdk.getComponent('dialogs.QuestionDialog');
37+
Modal.createDialog(QuestionDialog, {
38+
title: "What's New",
39+
description: <pre className="changelog_text">{props.releaseNotes}</pre>,
40+
button: "Update",
41+
onFinished: (update) => {
42+
if(update && PlatformPeg.get()) {
43+
PlatformPeg.get().installUpdate();
44+
}
45+
}
46+
});
47+
} else {
48+
const ChangelogDialog = sdk.getComponent('dialogs.ChangelogDialog');
49+
Modal.createDialog(ChangelogDialog, {
50+
version: props.version,
51+
newVersion: props.newVersion,
52+
releaseNotes: releaseNotes,
53+
onFinished: (update) => {
54+
if(update && PlatformPeg.get()) {
55+
PlatformPeg.get().installUpdate();
56+
}
4257
}
43-
}
44-
});
58+
});
59+
}
60+
};
61+
62+
const onUpdateClicked = () => {
63+
PlatformPeg.get().installUpdate();
4564
};
4665

47-
let changelog_button;
48-
if (checkVersion(props.version) && checkVersion(props.newVersion)) {
49-
changelog_button = <button className="mx_MatrixToolbar_action" onClick={onChangelogClicked}>Changelog</button>;
66+
let action_button;
67+
if (props.releaseNotes || (checkVersion(props.version) && checkVersion(props.newVersion))) {
68+
action_button = <button className="mx_MatrixToolbar_action" onClick={onChangelogClicked}>What's new?</button>;
69+
} else if (PlatformPeg.get()) {
70+
action_button = <button className="mx_MatrixToolbar_action" onClick={onUpdateClicked}>Update</button>;
5071
}
5172
return (
5273
<div className="mx_MatrixToolbar">
5374
<img className="mx_MatrixToolbar_warning" src="img/warning.svg" width="24" height="23" alt="/!\"/>
5475
<div className="mx_MatrixToolbar_content">
55-
A new version of Riot is available. Refresh your browser.
76+
A new version of Riot is available.
5677
</div>
57-
{changelog_button}
78+
{action_button}
5879
</div>
5980
);
6081
}
6182

6283
NewVersionBar.propTypes = {
6384
version: React.PropTypes.string.isRequired,
6485
newVersion: React.PropTypes.string.isRequired,
86+
releaseNotes: React.PropTypes.string,
6587
};

‎src/vector/index.js‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,6 @@ function onHashChange(ev) {
113113
routeUrl(window.location);
114114
}
115115

116-
function onVersion(current, latest) {
117-
window.matrixChat.onVersion(current, latest);
118-
}
119-
120116
var loaded = false;
121117
var lastLoadedScreen = null;
122118

@@ -165,8 +161,7 @@ window.onload = function() {
165161
if (!validBrowser) {
166162
return;
167163
}
168-
UpdateChecker.setVersionListener(onVersion);
169-
UpdateChecker.run();
164+
UpdateChecker.start();
170165
routeUrl(window.location);
171166
loaded = true;
172167
if (lastLoadedScreen) {

‎src/vector/platform/WebPlatform.js‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ limitations under the License.
1818

1919
import BasePlatform from 'matrix-react-sdk/lib/BasePlatform';
2020
import Favico from 'favico.js';
21+
import request from 'browser-request';
2122
import dis from 'matrix-react-sdk/lib/dispatcher.js';
2223
import q from 'q';
2324

2425
export default class WebPlatform extends BasePlatform {
2526
constructor() {
2627
super();
28+
this.runningVersion = null;
2729
// The 'animations' are really low framerate and look terrible.
2830
// Also it re-starts the animationb every time you set the badge,
2931
// and we set the state each time, even if the value hasn't changed,
@@ -88,4 +90,42 @@ export default class WebPlatform extends BasePlatform {
8890
notification.close();
8991
}, 5 * 1000);
9092
}
93+
94+
_getVersion() {
95+
const deferred = q.defer();
96+
request(
97+
{ method: "GET", url: "version" },
98+
(err, response, body) => {
99+
if (err || response.status < 200 || response.status >= 300) {
100+
if (err == null) err = { status: response.status };
101+
deferred.reject(err);
102+
return;
103+
}
104+
105+
const ver = body.trim();
106+
deferred.resolve(ver);
107+
}
108+
);
109+
return deferred.promise;
110+
}
111+
112+
pollForUpdate() {
113+
this._getVersion().done((ver) => {
114+
if (this.runningVersion == null) {
115+
this.runningVersion = ver;
116+
} else if (this.runningVersion != ver) {
117+
dis.dispatch({
118+
action: 'new_version',
119+
currentVersion: this.runningVersion,
120+
newVersion: ver,
121+
});
122+
}
123+
}, (err) => {
124+
console.error("Failed to poll for update", err);
125+
});
126+
}
127+
128+
installUpdate() {
129+
window.location.reload();
130+
}
91131
}

‎src/vector/updater.js‎

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,18 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
var POKE_RATE_MS = 10 * 60 * 1000; // 10 min
17-
var currentVersion = null;
18-
var latestVersion = null;
19-
var listener = function(){}; // NOP
20-
21-
module.exports = {
22-
setVersionListener: function(fn) { // invoked with fn(currentVer, newVer)
23-
listener = fn;
24-
},
2516

26-
run: function() {
27-
var req = new XMLHttpRequest();
28-
req.addEventListener("load", function() {
29-
if (!req.responseText) {
30-
return;
31-
}
32-
var ver = req.responseText.trim();
33-
if (!currentVersion) {
34-
currentVersion = ver;
35-
listener(currentVersion, currentVersion);
36-
}
17+
import PlatformPeg from 'matrix-react-sdk/lib/PlatformPeg';
3718

38-
if (ver !== latestVersion) {
39-
latestVersion = ver;
40-
if (module.exports.hasNewVersion()) {
41-
console.log("Current=%s Latest=%s", currentVersion, latestVersion);
42-
listener(currentVersion, latestVersion);
43-
}
44-
}
45-
});
46-
var cacheBuster = "?ts=" + new Date().getTime();
47-
req.open("GET", "version" + cacheBuster);
48-
req.send(); // can't suppress 404s from being logged.
49-
50-
setTimeout(module.exports.run, POKE_RATE_MS);
51-
},
19+
var POKE_RATE_MS = 10 * 60 * 1000; // 10 min
5220

53-
getCurrentVersion: function() {
54-
return currentVersion;
21+
module.exports = {
22+
start: function() {
23+
module.exports.poll();
24+
setInterval(module.exports.poll, POKE_RATE_MS);
5525
},
5626

57-
hasNewVersion: function() {
58-
return currentVersion && latestVersion && (currentVersion !== latestVersion);
27+
poll: function() {
28+
PlatformPeg.get().pollForUpdate();
5929
}
6030
};

0 commit comments

Comments
 (0)