2016-11-02 15:12:51 +00:00
|
|
|
|
// @flow
|
|
|
|
|
|
|
|
|
|
/*
|
2016-11-03 11:48:49 +00:00
|
|
|
|
Copyright 2016 Aviral Dasgupta
|
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2016-11-02 15:12:51 +00: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.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-11-02 17:45:35 +00:00
|
|
|
|
import VectorBasePlatform from './VectorBasePlatform';
|
2016-11-02 15:12:51 +00:00
|
|
|
|
import Favico from 'favico.js';
|
2016-11-02 16:02:55 +00:00
|
|
|
|
import request from 'browser-request';
|
2016-11-02 15:12:51 +00:00
|
|
|
|
import dis from 'matrix-react-sdk/lib/dispatcher.js';
|
|
|
|
|
import q from 'q';
|
|
|
|
|
|
2016-11-24 16:46:15 +00:00
|
|
|
|
import url from 'url';
|
|
|
|
|
import UAParser from 'ua-parser-js';
|
|
|
|
|
|
2016-11-02 17:45:35 +00:00
|
|
|
|
export default class WebPlatform extends VectorBasePlatform {
|
2016-11-02 15:12:51 +00:00
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
2016-11-02 16:02:55 +00:00
|
|
|
|
this.runningVersion = null;
|
2016-11-02 15:12:51 +00:00
|
|
|
|
// The 'animations' are really low framerate and look terrible.
|
|
|
|
|
// Also it re-starts the animationb every time you set the badge,
|
|
|
|
|
// and we set the state each time, even if the value hasn't changed,
|
|
|
|
|
// so we'd need to fix that if enabling the animation.
|
|
|
|
|
this.favicon = new Favico({animation: 'none'});
|
|
|
|
|
this._updateFavicon();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_updateFavicon() {
|
|
|
|
|
try {
|
|
|
|
|
// This needs to be in in a try block as it will throw
|
|
|
|
|
// if there are more than 100 badge count changes in
|
|
|
|
|
// its internal queue
|
|
|
|
|
let bgColor = "#d00",
|
|
|
|
|
notif = this.notificationCount;
|
|
|
|
|
|
|
|
|
|
if (this.errorDidOccur) {
|
|
|
|
|
notif = notif || "×";
|
|
|
|
|
bgColor = "#f00";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.favicon.badge(notif, {
|
|
|
|
|
bgColor: bgColor
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.warn(`Failed to set badge count: ${e.message}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setNotificationCount(count: number) {
|
|
|
|
|
super.setNotificationCount(count);
|
|
|
|
|
this._updateFavicon();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setErrorStatus(errorDidOccur: boolean) {
|
|
|
|
|
super.setErrorStatus(errorDidOccur);
|
|
|
|
|
this._updateFavicon();
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-02 17:36:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns true if the platform supports displaying
|
|
|
|
|
* notifications, otherwise false.
|
|
|
|
|
*/
|
|
|
|
|
supportsNotifications() : boolean {
|
2016-11-02 19:26:07 +00:00
|
|
|
|
return Boolean(global.Notification);
|
2016-11-02 17:36:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if the application currently has permission
|
|
|
|
|
* to display notifications. Otherwise false.
|
|
|
|
|
*/
|
|
|
|
|
maySendNotifications() : boolean {
|
|
|
|
|
return global.Notification.permission == 'granted';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
requestNotificationPermission() : Promise {
|
|
|
|
|
// 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.
|
|
|
|
|
const defer = q.defer();
|
|
|
|
|
global.Notification.requestPermission((result) => {
|
|
|
|
|
defer.resolve(result);
|
|
|
|
|
});
|
|
|
|
|
return defer.promise;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-06 13:28:59 +00:00
|
|
|
|
displayNotification(title: string, msg: string, avatarUrl: string, room: Object) {
|
2016-11-02 15:12:51 +00:00
|
|
|
|
const notification = new global.Notification(
|
|
|
|
|
title,
|
|
|
|
|
{
|
|
|
|
|
body: msg,
|
|
|
|
|
icon: avatarUrl,
|
|
|
|
|
tag: "vector",
|
|
|
|
|
silent: true, // we play our own sounds
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
notification.onclick = function() {
|
|
|
|
|
dis.dispatch({
|
|
|
|
|
action: 'view_room',
|
|
|
|
|
room_id: room.roomId
|
|
|
|
|
});
|
|
|
|
|
global.focus();
|
2016-12-06 13:28:59 +00:00
|
|
|
|
notification.close();
|
2016-11-02 15:12:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Chrome only dismisses notifications after 20s, which
|
|
|
|
|
// is waaaaay too long
|
|
|
|
|
global.setTimeout(function() {
|
|
|
|
|
notification.close();
|
|
|
|
|
}, 5 * 1000);
|
|
|
|
|
}
|
2016-11-02 16:02:55 +00:00
|
|
|
|
|
|
|
|
|
_getVersion() {
|
|
|
|
|
const deferred = q.defer();
|
2016-11-17 17:27:21 +00: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.
|
2016-11-02 16:02:55 +00:00
|
|
|
|
request(
|
2016-11-17 17:27:21 +00:00
|
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
url: "version",
|
|
|
|
|
qs: { cachebuster: Date.now() },
|
|
|
|
|
},
|
2016-11-02 16:02:55 +00:00
|
|
|
|
(err, response, body) => {
|
|
|
|
|
if (err || response.status < 200 || response.status >= 300) {
|
|
|
|
|
if (err == null) err = { status: response.status };
|
|
|
|
|
deferred.reject(err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ver = body.trim();
|
|
|
|
|
deferred.resolve(ver);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
return deferred.promise;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-08 10:47:01 +00:00
|
|
|
|
getAppVersion() {
|
|
|
|
|
if (this.runningVersion !== null) {
|
|
|
|
|
return q(this.runningVersion);
|
|
|
|
|
}
|
|
|
|
|
return this._getVersion();
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-02 16:02:55 +00:00
|
|
|
|
pollForUpdate() {
|
|
|
|
|
this._getVersion().done((ver) => {
|
|
|
|
|
if (this.runningVersion == null) {
|
|
|
|
|
this.runningVersion = ver;
|
|
|
|
|
} else if (this.runningVersion != ver) {
|
|
|
|
|
dis.dispatch({
|
|
|
|
|
action: 'new_version',
|
|
|
|
|
currentVersion: this.runningVersion,
|
|
|
|
|
newVersion: ver,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, (err) => {
|
|
|
|
|
console.error("Failed to poll for update", err);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
installUpdate() {
|
|
|
|
|
window.location.reload();
|
|
|
|
|
}
|
2016-11-24 16:46:15 +00:00
|
|
|
|
|
|
|
|
|
getDefaultDeviceDisplayName() {
|
|
|
|
|
// strip query-string and fragment from uri
|
|
|
|
|
let u = url.parse(window.location.href);
|
|
|
|
|
u.search = "";
|
|
|
|
|
u.hash = "";
|
|
|
|
|
let app_name = u.format();
|
|
|
|
|
|
|
|
|
|
let ua = new UAParser();
|
|
|
|
|
return app_name + " via " + ua.getBrowser().name +
|
|
|
|
|
" on " + ua.getOS().name;
|
|
|
|
|
}
|
2016-11-02 15:12:51 +00:00
|
|
|
|
}
|