From 757604fd600f7bec109a805588d08a61c00876de Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 2 Nov 2016 17:36:48 +0000 Subject: [PATCH 1/4] Add Notification support to the Web Platform Except display notification which was already accidentally included in a previous PR --- src/vector/platform/WebPlatform.js | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/vector/platform/WebPlatform.js b/src/vector/platform/WebPlatform.js index 7ef2b6b8a8..b3274021fd 100644 --- a/src/vector/platform/WebPlatform.js +++ b/src/vector/platform/WebPlatform.js @@ -63,6 +63,40 @@ export default class WebPlatform extends BasePlatform { this._updateFavicon(); } + /** + * Returns true if the platform supports displaying + * notifications, otherwise false. + */ + supportsNotifications() : boolean { + return true; + } + + /** + * 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; + } + displayNotification(title: string, msg: string, avatarUrl: string) { const notification = new global.Notification( title, From 19238b9326696bcae9ce6542a3b43b8a42a06d94 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 2 Nov 2016 17:57:27 +0000 Subject: [PATCH 2/4] Use the defaults if given a blank config file This allows Vector to load from file:// URIs without breaking if there is no config file (because we explicitly look for 404s, and file:// URIs don't return 404s). --- src/vector/index.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/vector/index.js b/src/vector/index.js index f260ac01f1..5d00d07823 100644 --- a/src/vector/index.js +++ b/src/vector/index.js @@ -179,14 +179,27 @@ function getConfig() { let deferred = q.defer(); request( - { method: "GET", url: "config.json", json: true }, + { method: "GET", url: "config.json" }, (err, response, body) => { if (err || response.status < 200 || response.status >= 300) { + // Lack of a config isn't an error, we should + // just use the defaults. + // Also treat a blank config as no config because + // we don't get 404s from file: URIs so this is the + // only way we can not fail if the file doesn't exist + // when loading from a file:// URI. + if (( err && err.response.status == 404) || body == '') { + deferred.resolve({}); + } deferred.reject({err: err, response: response}); return; } - deferred.resolve(body); + // We parse the JSON ourselves rather than use the JSON + // parameter, since this throws a parse error on empty + // which breaks if there's no config.json and we're + // loading from the filesystem (see above). + deferred.resolve(JSON.parse(body)); } ); @@ -239,13 +252,7 @@ async function loadApp() { try { configJson = await getConfig(); } catch (e) { - // On 404 errors, carry on without a config, - // but on other errors, fail, otherwise it will - // lead to subtle errors where the app runs with - // the default config if it fails to fetch config.json. - if (e.response.status != 404) { - configError = e; - } + configError = e; } console.log("Vector starting at "+window.location); From 6aba9f8edaa749247917002fc7a7b83b6e859731 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 2 Nov 2016 19:26:07 +0000 Subject: [PATCH 3/4] Don't always claim we have notif support Only if the browser has the Notification API --- src/vector/platform/WebPlatform.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vector/platform/WebPlatform.js b/src/vector/platform/WebPlatform.js index b3274021fd..3477fc8bc1 100644 --- a/src/vector/platform/WebPlatform.js +++ b/src/vector/platform/WebPlatform.js @@ -68,7 +68,7 @@ export default class WebPlatform extends BasePlatform { * notifications, otherwise false. */ supportsNotifications() : boolean { - return true; + return Boolean(global.Notification); } /** From 3bcb447e039d71d5c4d651bca1eeb63f12a90044 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 3 Nov 2016 11:48:49 +0000 Subject: [PATCH 4/4] Fix copyright --- src/vector/platform/WebPlatform.js | 3 ++- src/vector/platform/index.js | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/vector/platform/WebPlatform.js b/src/vector/platform/WebPlatform.js index 3477fc8bc1..f4adbd5d54 100644 --- a/src/vector/platform/WebPlatform.js +++ b/src/vector/platform/WebPlatform.js @@ -1,7 +1,8 @@ // @flow /* -Copyright 2016 Aviral Dasgupta and OpenMarket Ltd +Copyright 2016 Aviral Dasgupta +Copyright 2016 OpenMarket Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/src/vector/platform/index.js b/src/vector/platform/index.js index c0f17ae99b..741f1df0a7 100644 --- a/src/vector/platform/index.js +++ b/src/vector/platform/index.js @@ -1,7 +1,8 @@ // @flow /* -Copyright 2016 Aviral Dasgupta and OpenMarket Ltd +Copyright 2016 Aviral Dasgupta +Copyright 2016 OpenMarket Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.