2016-11-02 16:12:51 +01:00
|
|
|
/*
|
2016-11-03 12:48:49 +01:00
|
|
|
Copyright 2016 Aviral Dasgupta
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2020-04-30 17:02:37 +02:00
|
|
|
Copyright 2017-2020 New Vector Ltd
|
2016-11-02 16:12:51 +01:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-06-11 20:19:17 +02:00
|
|
|
import VectorBasePlatform, {updateCheckStatusEnum} from './VectorBasePlatform';
|
2016-11-02 17:02:55 +01:00
|
|
|
import request from 'browser-request';
|
2020-05-14 05:15:30 +02:00
|
|
|
import dis from 'matrix-react-sdk/src/dispatcher/dispatcher';
|
2019-12-20 02:47:40 +01:00
|
|
|
import { _t } from 'matrix-react-sdk/src/languageHandler';
|
2020-05-20 20:56:54 +02:00
|
|
|
import {Room} from "matrix-js-sdk/src/models/room";
|
2020-05-23 00:08:46 +02:00
|
|
|
import { showToast as showUpdateToast, hideToast as hideUpdateToast } from "matrix-react-sdk/src/toasts/UpdateToast";
|
2016-11-02 16:12:51 +01:00
|
|
|
|
2016-11-24 17:46:15 +01:00
|
|
|
import url from 'url';
|
|
|
|
import UAParser from 'ua-parser-js';
|
|
|
|
|
2018-11-22 19:25:55 +01:00
|
|
|
const POKE_RATE_MS = 10 * 60 * 1000; // 10 min
|
2017-06-03 13:50:47 +02:00
|
|
|
|
2016-11-02 18:45:35 +01:00
|
|
|
export default class WebPlatform extends VectorBasePlatform {
|
2020-05-20 20:56:54 +02:00
|
|
|
private runningVersion: string = null;
|
2016-11-02 16:12:51 +01:00
|
|
|
|
2017-05-29 21:03:21 +02:00
|
|
|
getHumanReadableName(): string {
|
2017-05-31 15:51:08 +02:00
|
|
|
return 'Web Platform'; // no translation required: only used for analytics
|
2017-05-29 20:51:28 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 18:36:48 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the platform supports displaying
|
|
|
|
* notifications, otherwise false.
|
|
|
|
*/
|
2017-05-02 22:29:19 +02:00
|
|
|
supportsNotifications(): boolean {
|
2020-05-20 20:56:54 +02:00
|
|
|
return Boolean(window.Notification);
|
2016-11-02 18:36:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the application currently has permission
|
|
|
|
* to display notifications. Otherwise false.
|
|
|
|
*/
|
2017-05-02 22:29:19 +02:00
|
|
|
maySendNotifications(): boolean {
|
2020-05-20 20:56:54 +02:00
|
|
|
return window.Notification.permission === 'granted';
|
2016-11-02 18:36:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Requests permission to send notifications. Returns
|
|
|
|
* a promise that is resolved when the user has responded
|
|
|
|
* to the request. The promise has a single string argument
|
|
|
|
* that is 'granted' if the user allowed the request or
|
|
|
|
* 'denied' otherwise.
|
|
|
|
*/
|
2017-05-02 22:29:19 +02:00
|
|
|
requestNotificationPermission(): Promise<string> {
|
2016-11-02 18:36:48 +01:00
|
|
|
// annoyingly, the latest spec says this returns a
|
|
|
|
// promise, but this is only supported in Chrome 46
|
|
|
|
// and Firefox 47, so adapt the callback API.
|
2018-10-03 02:34:57 +02:00
|
|
|
return new Promise(function(resolve, reject) {
|
2020-05-20 20:56:54 +02:00
|
|
|
window.Notification.requestPermission((result) => {
|
2018-10-03 02:34:57 +02:00
|
|
|
resolve(result);
|
|
|
|
});
|
2018-10-05 17:41:45 +02:00
|
|
|
});
|
2016-11-02 18:36:48 +01:00
|
|
|
}
|
|
|
|
|
2020-05-20 20:56:54 +02:00
|
|
|
displayNotification(title: string, msg: string, avatarUrl: string, room: Room) {
|
2019-05-31 04:04:16 +02:00
|
|
|
const notifBody = {
|
|
|
|
body: msg,
|
|
|
|
tag: "vector",
|
|
|
|
silent: true, // we play our own sounds
|
|
|
|
};
|
|
|
|
if (avatarUrl) notifBody['icon'] = avatarUrl;
|
2020-05-20 20:56:54 +02:00
|
|
|
const notification = new window.Notification(title, notifBody);
|
2016-11-02 16:12:51 +01:00
|
|
|
|
|
|
|
notification.onclick = function() {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
2017-05-02 22:29:19 +02:00
|
|
|
room_id: room.roomId,
|
2016-11-02 16:12:51 +01:00
|
|
|
});
|
2020-05-20 20:56:54 +02:00
|
|
|
window.focus();
|
2016-12-06 14:28:59 +01:00
|
|
|
notification.close();
|
2016-11-02 16:12:51 +01:00
|
|
|
};
|
|
|
|
}
|
2016-11-02 17:02:55 +01:00
|
|
|
|
2017-05-02 22:29:19 +02:00
|
|
|
_getVersion(): Promise<string> {
|
2016-11-17 18:27:21 +01:00
|
|
|
// We add a cachebuster to the request to make sure that we know about
|
|
|
|
// the most recent version on the origin server. That might not
|
|
|
|
// actually be the version we'd get on a reload (particularly in the
|
|
|
|
// presence of intermediate caching proxies), but still: we're trying
|
|
|
|
// to tell the user that there is a new version.
|
2018-10-03 02:34:57 +02:00
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
request(
|
|
|
|
{
|
|
|
|
method: "GET",
|
|
|
|
url: "version",
|
|
|
|
qs: { cachebuster: Date.now() },
|
|
|
|
},
|
|
|
|
(err, response, body) => {
|
|
|
|
if (err || response.status < 200 || response.status >= 300) {
|
|
|
|
if (err === null) err = { status: response.status };
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ver = body.trim();
|
|
|
|
resolve(ver);
|
|
|
|
},
|
|
|
|
);
|
2018-10-05 17:41:45 +02:00
|
|
|
});
|
2016-11-02 17:02:55 +01:00
|
|
|
}
|
|
|
|
|
2017-05-02 22:29:19 +02:00
|
|
|
getAppVersion(): Promise<string> {
|
2016-11-08 11:47:01 +01:00
|
|
|
if (this.runningVersion !== null) {
|
2017-07-13 01:32:07 +02:00
|
|
|
return Promise.resolve(this.runningVersion);
|
2016-11-08 11:47:01 +01:00
|
|
|
}
|
|
|
|
return this._getVersion();
|
|
|
|
}
|
|
|
|
|
2017-06-03 13:50:47 +02:00
|
|
|
startUpdater() {
|
2017-06-11 20:19:17 +02:00
|
|
|
this.pollForUpdate();
|
2020-05-20 21:01:11 +02:00
|
|
|
setInterval(this.pollForUpdate, POKE_RATE_MS);
|
2017-06-03 13:50:47 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 20:56:54 +02:00
|
|
|
async canSelfUpdate(): Promise<boolean> {
|
2018-12-18 18:42:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-20 21:01:11 +02:00
|
|
|
pollForUpdate = () => {
|
2017-06-03 16:12:46 +02:00
|
|
|
return this._getVersion().then((ver) => {
|
2017-05-02 22:29:19 +02:00
|
|
|
if (this.runningVersion === null) {
|
2016-11-02 17:02:55 +01:00
|
|
|
this.runningVersion = ver;
|
2020-05-23 00:08:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.runningVersion !== ver) {
|
|
|
|
showUpdateToast(this.runningVersion, ver);
|
2017-06-11 20:19:17 +02:00
|
|
|
// Return to skip a MatrixChat state update
|
|
|
|
return;
|
2020-05-23 00:08:46 +02:00
|
|
|
} else {
|
|
|
|
hideUpdateToast();
|
2016-11-02 17:02:55 +01:00
|
|
|
}
|
2020-05-23 00:08:46 +02:00
|
|
|
|
2017-06-11 20:19:17 +02:00
|
|
|
return { status: updateCheckStatusEnum.NOTAVAILABLE };
|
2016-11-02 17:02:55 +01:00
|
|
|
}, (err) => {
|
|
|
|
console.error("Failed to poll for update", err);
|
2017-06-11 20:19:17 +02:00
|
|
|
return {
|
|
|
|
status: updateCheckStatusEnum.ERROR,
|
|
|
|
detail: err.message || err.status ? err.status.toString() : 'Unknown Error',
|
|
|
|
};
|
|
|
|
});
|
2020-05-20 21:01:11 +02:00
|
|
|
};
|
2017-06-11 20:19:17 +02:00
|
|
|
|
2020-05-26 11:04:49 +02:00
|
|
|
startUpdateCheck() {
|
2017-06-11 20:19:17 +02:00
|
|
|
if (this.showUpdateCheck) return;
|
|
|
|
super.startUpdateCheck();
|
|
|
|
this.pollForUpdate().then((updateState) => {
|
|
|
|
if (!this.showUpdateCheck) return;
|
|
|
|
if (!updateState) return;
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'check_updates',
|
|
|
|
value: updateState,
|
|
|
|
});
|
2016-11-02 17:02:55 +01:00
|
|
|
});
|
2020-05-26 11:04:49 +02:00
|
|
|
}
|
2016-11-02 17:02:55 +01:00
|
|
|
|
|
|
|
installUpdate() {
|
2018-03-09 02:45:37 +01:00
|
|
|
window.location.reload(true);
|
2016-11-02 17:02:55 +01:00
|
|
|
}
|
2016-11-24 17:46:15 +01:00
|
|
|
|
2017-05-02 22:29:19 +02:00
|
|
|
getDefaultDeviceDisplayName(): string {
|
2016-11-24 17:46:15 +01:00
|
|
|
// strip query-string and fragment from uri
|
2017-05-02 22:29:19 +02:00
|
|
|
const u = url.parse(window.location.href);
|
2020-04-30 17:50:01 +02:00
|
|
|
u.protocol = "";
|
2016-11-24 17:46:15 +01:00
|
|
|
u.search = "";
|
|
|
|
u.hash = "";
|
2020-04-30 17:50:01 +02:00
|
|
|
// Remove trailing slash if present
|
|
|
|
u.pathname = u.pathname.replace(/\/$/, "");
|
|
|
|
|
|
|
|
let appName = u.format();
|
|
|
|
// Remove leading slashes if present
|
|
|
|
appName = appName.replace(/^\/\//, "");
|
|
|
|
// `appName` is now in the format `riot.im/develop`.
|
2016-11-24 17:46:15 +01:00
|
|
|
|
2017-05-02 22:29:19 +02:00
|
|
|
const ua = new UAParser();
|
2017-06-06 13:38:06 +02:00
|
|
|
const browserName = ua.getBrowser().name || "unknown browser";
|
2020-04-30 16:58:53 +02:00
|
|
|
let osName = ua.getOS().name || "unknown OS";
|
|
|
|
// Stylise the value from the parser to match Apple's current branding.
|
|
|
|
if (osName === "Mac OS") osName = "macOS";
|
2020-04-30 17:50:01 +02:00
|
|
|
return _t('%(appName)s (%(browserName)s, %(osName)s)', {
|
|
|
|
appName,
|
|
|
|
browserName,
|
|
|
|
osName,
|
|
|
|
});
|
2016-11-24 17:46:15 +01:00
|
|
|
}
|
2017-01-10 19:39:21 +01:00
|
|
|
|
2020-05-20 20:56:54 +02:00
|
|
|
screenCaptureErrorString(): string | null {
|
2017-01-10 19:39:21 +01:00
|
|
|
// it won't work at all if you're not on HTTPS so whine whine whine
|
2020-05-20 20:56:54 +02:00
|
|
|
if (window.location.protocol !== "https:") {
|
2017-05-31 15:51:08 +02:00
|
|
|
return _t("You need to be using HTTPS to place a screen-sharing call.");
|
2017-01-10 19:39:21 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2017-04-10 18:40:09 +02:00
|
|
|
|
|
|
|
reload() {
|
|
|
|
// forceReload=false since we don't really need new HTML/JS files
|
|
|
|
// we just need to restart the JS runtime.
|
|
|
|
window.location.reload(false);
|
|
|
|
}
|
2016-11-02 16:12:51 +01:00
|
|
|
}
|