|
| 1 | +/* |
| 2 | +Copyright 2015 OpenMarket Ltd |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +var NotifierController = require('matrix-react-sdk/lib/controllers/organisms/Notifier') |
| 20 | + |
| 21 | +var TextForEvent = require('matrix-react-sdk/lib/TextForEvent'); |
| 22 | +var extend = require('matrix-react-sdk/lib/extend'); |
| 23 | +var dis = require('matrix-react-sdk/lib/dispatcher'); |
| 24 | +var Avatar = require('matrix-react-sdk/lib/Avatar'); |
| 25 | + |
| 26 | +var NotifierView = { |
| 27 | + notificationMessageForEvent: function(ev) { |
| 28 | + return TextForEvent.textForEvent(ev); |
| 29 | + }, |
| 30 | + |
| 31 | + displayNotification: function(ev, room) { |
| 32 | + if (!global.Notification || global.Notification.permission != 'granted') { |
| 33 | + return; |
| 34 | + } |
| 35 | + if (global.document.hasFocus()) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + var msg = this.notificationMessageForEvent(ev); |
| 40 | + if (!msg) return; |
| 41 | + |
| 42 | + var title; |
| 43 | + if (!ev.sender || room.name == ev.sender.name) { |
| 44 | + title = room.name; |
| 45 | + // notificationMessageForEvent includes sender, |
| 46 | + // but we already have the sender here |
| 47 | + if (ev.getContent().body) msg = ev.getContent().body; |
| 48 | + } else if (ev.getType() == 'm.room.member') { |
| 49 | + // context is all in the message here, we don't need |
| 50 | + // to display sender info |
| 51 | + title = room.name; |
| 52 | + } else if (ev.sender) { |
| 53 | + title = ev.sender.name + " (" + room.name + ")"; |
| 54 | + // notificationMessageForEvent includes sender, |
| 55 | + // but we've just out sender in the title |
| 56 | + if (ev.getContent().body) msg = ev.getContent().body; |
| 57 | + } |
| 58 | + |
| 59 | + var avatarUrl = ev.sender ? Avatar.avatarUrlForMember( |
| 60 | + ev.sender, 40, 40, 'crop' |
| 61 | + ) : null; |
| 62 | + |
| 63 | + var notification = new global.Notification( |
| 64 | + title, |
| 65 | + { |
| 66 | + "body": msg, |
| 67 | + "icon": avatarUrl, |
| 68 | + "tag": "vector" |
| 69 | + } |
| 70 | + ); |
| 71 | + |
| 72 | + notification.onclick = function() { |
| 73 | + dis.dispatch({ |
| 74 | + action: 'view_room', |
| 75 | + room_id: room.roomId |
| 76 | + }); |
| 77 | + global.focus(); |
| 78 | + }; |
| 79 | + |
| 80 | + /*var audioClip; |
| 81 | + |
| 82 | + if (audioNotification) { |
| 83 | + audioClip = playAudio(audioNotification); |
| 84 | + }*/ |
| 85 | + |
| 86 | + global.setTimeout(function() { |
| 87 | + notification.close(); |
| 88 | + }, 5 * 1000); |
| 89 | + |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +var NotifierClass = function() {}; |
| 94 | +extend(NotifierClass.prototype, NotifierController); |
| 95 | +extend(NotifierClass.prototype, NotifierView); |
| 96 | + |
| 97 | +module.exports = new NotifierClass(); |
| 98 | + |
0 commit comments