|
| 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("../../../../src/controllers/organisms/Notifier"); |
| 20 | + |
| 21 | +var MatrixClientPeg = require("../../../../src/MatrixClientPeg"); |
| 22 | +var extend = require("../../../../src/extend"); |
| 23 | +var dis = require("../../../../src/dispatcher"); |
| 24 | + |
| 25 | + |
| 26 | +var NotifierView = { |
| 27 | + notificationMessageForEvent: function(ev) { |
| 28 | + var senderDisplayName = ev.sender.name; |
| 29 | + var message = null; |
| 30 | + |
| 31 | + if (ev.event.type === "m.room.message") { |
| 32 | + message = ev.getContent().body; |
| 33 | + if (ev.getContent().msgtype === "m.emote") { |
| 34 | + message = "* " + senderDisplayName + " " + message; |
| 35 | + } else if (ev.getContent().msgtype === "m.image") { |
| 36 | + message = senderDisplayName + " sent an image."; |
| 37 | + } |
| 38 | + } else if (ev.event.type == "m.room.member") { |
| 39 | + if (ev.event.state_key !== MatrixClientPeg.get().credentials.userId && "join" === ev.getContent().membership) { |
| 40 | + // Notify when another user joins |
| 41 | + message = ev.target.name + " joined"; |
| 42 | + } else if (ev.event.state_key === MatrixClientPeg.get().credentials.userId && "invite" === ev.getContent().membership) { |
| 43 | + // notify when you are invited |
| 44 | + message = senderDisplayName + " invited you to a room"; |
| 45 | + } |
| 46 | + } |
| 47 | + return message; |
| 48 | + }, |
| 49 | + |
| 50 | + displayNotification: function(ev, room) { |
| 51 | + if (!global.Notification || global.Notification.permission != 'granted') { |
| 52 | + return; |
| 53 | + } |
| 54 | + if (global.document.hasFocus()) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + var msg = this.notificationMessageForEvent(ev); |
| 59 | + if (!msg) return; |
| 60 | + |
| 61 | + var title; |
| 62 | + if (room.name == ev.sender.name) { |
| 63 | + title = room.name; |
| 64 | + } else { |
| 65 | + title = ev.sender.name + " (" + room.name + ")"; |
| 66 | + } |
| 67 | + |
| 68 | + var notification = new global.Notification( |
| 69 | + title, |
| 70 | + { |
| 71 | + "body": msg, |
| 72 | + "icon": MatrixClientPeg.get().getAvatarUrlForMember(ev.sender) |
| 73 | + } |
| 74 | + ); |
| 75 | + |
| 76 | + notification.onclick = function() { |
| 77 | + dis.dispatch({ |
| 78 | + action: 'view_room', |
| 79 | + room_id: room.roomId |
| 80 | + }); |
| 81 | + global.focus(); |
| 82 | + }; |
| 83 | + |
| 84 | + /*var audioClip; |
| 85 | + |
| 86 | + if (audioNotification) { |
| 87 | + audioClip = playAudio(audioNotification); |
| 88 | + }*/ |
| 89 | + |
| 90 | + global.setTimeout(function() { |
| 91 | + notification.close(); |
| 92 | + }, 5 * 1000); |
| 93 | + |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +var NotifierClass = function() {}; |
| 98 | +extend(NotifierClass.prototype, NotifierController); |
| 99 | +extend(NotifierClass.prototype, NotifierView); |
| 100 | + |
| 101 | +module.exports = new NotifierClass(); |
| 102 | + |
0 commit comments