mirror of https://github.com/vector-im/riot-web
Merge remote-tracking branch 'origin/develop' into dbkr/platform_version
commit
5ae4228bfe
|
@ -36,6 +36,32 @@ export default class BasePlatform {
|
||||||
this.errorDidOccur = errorDidOccur;
|
this.errorDidOccur = errorDidOccur;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the platform supports displaying
|
||||||
|
* notifications, otherwise false.
|
||||||
|
*/
|
||||||
|
supportsNotifications() : boolean {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the application currently has permission
|
||||||
|
* to display notifications. Otherwise false.
|
||||||
|
*/
|
||||||
|
maySendNotifications() : boolean {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<string> {
|
||||||
|
}
|
||||||
|
|
||||||
displayNotification(title: string, msg: string, avatarUrl: string) {
|
displayNotification(title: string, msg: string, avatarUrl: string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ limitations under the License.
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var MatrixClientPeg = require("./MatrixClientPeg");
|
var MatrixClientPeg = require("./MatrixClientPeg");
|
||||||
|
var PlatformPeg = require("./PlatformPeg");
|
||||||
var TextForEvent = require('./TextForEvent');
|
var TextForEvent = require('./TextForEvent');
|
||||||
var Avatar = require('./Avatar');
|
var Avatar = require('./Avatar');
|
||||||
var dis = require("./dispatcher");
|
var dis = require("./dispatcher");
|
||||||
|
@ -30,13 +31,18 @@ var dis = require("./dispatcher");
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Notifier = {
|
var Notifier = {
|
||||||
|
notifsByRoom: {},
|
||||||
|
|
||||||
notificationMessageForEvent: function(ev) {
|
notificationMessageForEvent: function(ev) {
|
||||||
return TextForEvent.textForEvent(ev);
|
return TextForEvent.textForEvent(ev);
|
||||||
},
|
},
|
||||||
|
|
||||||
_displayPopupNotification: function(ev, room) {
|
_displayPopupNotification: function(ev, room) {
|
||||||
if (!global.Notification || global.Notification.permission != 'granted') {
|
const plaf = PlatformPeg.get();
|
||||||
|
if (!plaf) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!plaf.supportsNotifications() || !plaf.maySendNotifications()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (global.document.hasFocus()) {
|
if (global.document.hasFocus()) {
|
||||||
|
@ -67,26 +73,14 @@ var Notifier = {
|
||||||
ev.sender, 40, 40, 'crop'
|
ev.sender, 40, 40, 'crop'
|
||||||
) : null;
|
) : null;
|
||||||
|
|
||||||
var notification = new global.Notification(
|
const notif = plaf.displayNotification(title, msg, avatarUrl);
|
||||||
title,
|
|
||||||
{
|
// if displayNotification returns non-null, the platform supports
|
||||||
"body": msg,
|
// clearing notifications later, so keep track of this.
|
||||||
"icon": avatarUrl,
|
if (notif) {
|
||||||
"tag": "matrixreactsdk"
|
if (this.notifsByRoom[ev.getRoomId()] === undefined) this.notifsByRoom[ev.getRoomId()] = [];
|
||||||
|
this.notifsByRoom[ev.getRoomId()].push(notif);
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
notification.onclick = function() {
|
|
||||||
dis.dispatch({
|
|
||||||
action: 'view_room',
|
|
||||||
room_id: room.roomId
|
|
||||||
});
|
|
||||||
global.focus();
|
|
||||||
};
|
|
||||||
|
|
||||||
global.setTimeout(function() {
|
|
||||||
notification.close();
|
|
||||||
}, 5 * 1000);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
_playAudioNotification: function(ev, room) {
|
_playAudioNotification: function(ev, room) {
|
||||||
|
@ -100,7 +94,9 @@ var Notifier = {
|
||||||
start: function() {
|
start: function() {
|
||||||
this.boundOnRoomTimeline = this.onRoomTimeline.bind(this);
|
this.boundOnRoomTimeline = this.onRoomTimeline.bind(this);
|
||||||
this.boundOnSyncStateChange = this.onSyncStateChange.bind(this);
|
this.boundOnSyncStateChange = this.onSyncStateChange.bind(this);
|
||||||
|
this.boundOnRoomReceipt = this.onRoomReceipt.bind(this);
|
||||||
MatrixClientPeg.get().on('Room.timeline', this.boundOnRoomTimeline);
|
MatrixClientPeg.get().on('Room.timeline', this.boundOnRoomTimeline);
|
||||||
|
MatrixClientPeg.get().on("Room.receipt", this.boundOnRoomReceipt);
|
||||||
MatrixClientPeg.get().on("sync", this.boundOnSyncStateChange);
|
MatrixClientPeg.get().on("sync", this.boundOnSyncStateChange);
|
||||||
this.toolbarHidden = false;
|
this.toolbarHidden = false;
|
||||||
this.isPrepared = false;
|
this.isPrepared = false;
|
||||||
|
@ -109,21 +105,20 @@ var Notifier = {
|
||||||
stop: function() {
|
stop: function() {
|
||||||
if (MatrixClientPeg.get()) {
|
if (MatrixClientPeg.get()) {
|
||||||
MatrixClientPeg.get().removeListener('Room.timeline', this.boundOnRoomTimeline);
|
MatrixClientPeg.get().removeListener('Room.timeline', this.boundOnRoomTimeline);
|
||||||
|
MatrixClientPeg.get().removeListener("Room.receipt", this.boundOnRoomReceipt);
|
||||||
MatrixClientPeg.get().removeListener('sync', this.boundOnSyncStateChange);
|
MatrixClientPeg.get().removeListener('sync', this.boundOnSyncStateChange);
|
||||||
}
|
}
|
||||||
this.isPrepared = false;
|
this.isPrepared = false;
|
||||||
},
|
},
|
||||||
|
|
||||||
supportsDesktopNotifications: function() {
|
supportsDesktopNotifications: function() {
|
||||||
return !!global.Notification;
|
const plaf = PlatformPeg.get();
|
||||||
},
|
return plaf && plaf.supportsNotifications();
|
||||||
|
|
||||||
havePermission: function() {
|
|
||||||
if (!this.supportsDesktopNotifications()) return false;
|
|
||||||
return global.Notification.permission == 'granted';
|
|
||||||
},
|
},
|
||||||
|
|
||||||
setEnabled: function(enable, callback) {
|
setEnabled: function(enable, callback) {
|
||||||
|
const plaf = PlatformPeg.get();
|
||||||
|
if (!plaf) return;
|
||||||
// make sure that we persist the current setting audio_enabled setting
|
// make sure that we persist the current setting audio_enabled setting
|
||||||
// before changing anything
|
// before changing anything
|
||||||
if (global.localStorage) {
|
if (global.localStorage) {
|
||||||
|
@ -134,7 +129,7 @@ var Notifier = {
|
||||||
|
|
||||||
if (enable) {
|
if (enable) {
|
||||||
// Attempt to get permission from user
|
// Attempt to get permission from user
|
||||||
global.Notification.requestPermission(function(result) {
|
plaf.requestNotificationPermission().done((result) => {
|
||||||
if (result !== 'granted') {
|
if (result !== 'granted') {
|
||||||
// The permission request was dismissed or denied
|
// The permission request was dismissed or denied
|
||||||
return;
|
return;
|
||||||
|
@ -164,7 +159,10 @@ var Notifier = {
|
||||||
},
|
},
|
||||||
|
|
||||||
isEnabled: function() {
|
isEnabled: function() {
|
||||||
if (!this.havePermission()) return false;
|
const plaf = PlatformPeg.get();
|
||||||
|
if (!plaf) return false;
|
||||||
|
if (!plaf.supportsNotifications()) return false;
|
||||||
|
if (!plaf.maySendNotifications()) return false;
|
||||||
|
|
||||||
if (!global.localStorage) return true;
|
if (!global.localStorage) return true;
|
||||||
|
|
||||||
|
@ -240,6 +238,24 @@ var Notifier = {
|
||||||
this._playAudioNotification(ev, room);
|
this._playAudioNotification(ev, room);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
onRoomReceipt: function(ev, room) {
|
||||||
|
if (room.getUnreadNotificationCount() == 0) {
|
||||||
|
// ideally we would clear each notification when it was read,
|
||||||
|
// but we have no way, given a read receipt, to know whether
|
||||||
|
// the receipt comes before or after an event, so we can't
|
||||||
|
// do this. Instead, clear all notifications for a room once
|
||||||
|
// there are no notifs left in that room., which is not quite
|
||||||
|
// as good but it's something.
|
||||||
|
const plaf = PlatformPeg.get();
|
||||||
|
if (!plaf) return;
|
||||||
|
if (this.notifsByRoom[room.roomId] === undefined) return;
|
||||||
|
for (const notif of this.notifsByRoom[room.roomId]) {
|
||||||
|
plaf.clearNotification(notif);
|
||||||
|
}
|
||||||
|
delete this.notifsByRoom[room.roomId];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue