From b40d8070f35695008d122a685f1dfc9cc32adfbe Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Wed, 11 May 2016 20:02:19 -0400 Subject: Browser notification issued when member enters chat room --- view/tpl/chat.tpl | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) (limited to 'view') diff --git a/view/tpl/chat.tpl b/view/tpl/chat.tpl index afdbdeb28..e92265b5d 100644 --- a/view/tpl/chat.tpl +++ b/view/tpl/chat.tpl @@ -103,6 +103,7 @@ $(document).ready(function() { $('#chatroom_bookmarks, #vcard').hide(); $('#chatroom_list, #chatroom_members').show(); adjustInlineTopBarHeight(); + chatNotificationInit(); }); $(window).resize(function () { @@ -138,6 +139,8 @@ function load_chats() { } +var previousChatRoomMembers = null; // initialize chat room member change register +var currentChatRoomMembers = null; // initialize chat room member change register function update_inroom(inroom) { var html = document.createElement('div'); var count = inroom.length; @@ -147,9 +150,37 @@ function update_inroom(inroom) { $(newNode).html('' + item.name + ' ' + '' + item.name + '
' + item.status + ''); html.appendChild(newNode); }); + memberChange = chatRoomMembersChange(inroom); // get list of arrivals and departures + if(memberChange.membersArriving.length > 0) { + // Issue pop-up notification if anyone enters the room. + chat_issue_notification(JSON.stringify(memberChange.membersArriving.pop().name) + ' entered the room', 'Hubzilla Chat'); + } $('#chatMembers').html(html); } +// Determine if the new list of chat room members has any new members or if any have left +function chatRoomMembersChange(inroom) { + previousChatRoomMembers = currentChatRoomMembers; + currentChatRoomMembers = inroom; + var membersArriving = []; + var membersLeaving = []; + if(previousChatRoomMembers !== null) { + var newMember = false; + $.each( currentChatRoomMembers, function(index, currMember) { + newMember = true; + $.each( previousChatRoomMembers, function(index, prevMember) { + if (prevMember.name === currMember.name) { + newMember = false; + } + }); + if (newMember === true) { + membersArriving.push(currMember); + } + }); + } + return {membersArriving: membersArriving, membersLeaving: membersLeaving}; +} + function update_chats(chats) { var count = chats.length; $.each( chats, function(index, item) { @@ -172,6 +203,51 @@ function update_chats(chats) { }); } +var chat_notify_granted = false; // Initialize notification permission to denied +// Request notification access from the user +// TODO: Check Hubzilla member config setting before requesting permission +function chatNotificationInit() { + + if (!("Notification" in window)) { + window.console.log("This browser does not support system notifications"); + } + // Let's check whether notification permissions have already been granted + else if (Notification.permission === "granted") { + // If it's okay let's create a notification + chat_notify_granted = true; //var notification = new Notification("Hi there!"); + } + + // Otherwise, we need to ask the user for permission + else if (Notification.permission !== 'denied') { + Notification.requestPermission(function (permission) { + // If the user accepts, let's create a notification + if (permission === "granted") { + chat_notify_granted = true; //var notification = new Notification("Hi there!"); + } + }); + } +} + +// Issue a pop-up notification using the web standard Notification API +// https://developer.mozilla.org/docs/Web/API/notification +var chat_issue_notification = function (theBody,theTitle) { + if ( !chat_notify_granted ) { + return; + } + var nIcon = "/images/icons/48/group.png"; + var options = { + body: theBody, + icon: nIcon, + silent: false + } + var n = new Notification(theTitle,options); + n.onclick = function (event) { + setTimeout(n.close.bind(n), 300); + } + // TODO: Allow audio notification option + //chat_notify_audio.play(); +} + function chatJotGetLink() { reply = prompt("{{$linkurl}}"); if(reply && reply.length) { -- cgit v1.2.3