From 3ae9623b2ccbfff7cdea27024eb0f25445ba8a89 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Wed, 15 Oct 2014 23:10:37 +0000 Subject: [PATCH] Chat notification --- public/javascripts/application.js | 38 ++++++++++++++++++++++++++++++++++++++ public/javascripts/chat.js | 24 +++++++++++++++++++++--- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/public/javascripts/application.js b/public/javascripts/application.js index c2e69ab..5ba3416 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -1056,3 +1056,41 @@ function apply_zoom_to_images(zoom_text) { }); }); } + +function notifyMe(title, options) { + // This might be useful in the future + // + // Let's check if the browser supports notifications + // if (!("Notification" in window)) { + // alert("This browser does not support desktop notification"); + // } + + // Let's check if the user is okay to get some notification + var notification = null; + if (Notification.permission === "granted") { + // If it's okay let's create a notification + notification = new Notification(title, options); + } + + // Otherwise, we need to ask the user for permission + // Note, Chrome does not implement the permission static property + // So we have to check for NOT 'denied' instead of 'default' + else if (Notification.permission !== 'denied') { + Notification.requestPermission(function (permission) { + // Whatever the user answers, we make sure we store the information + if (!('permission' in Notification)) { + Notification.permission = permission; + } + + // If the user is okay, let's create a notification + if (permission === "granted") { + notification = new Notification(title, options); + } + }); + } + return notification; + // At last, if the user already denied any notification, and you + // want to be respectful there is no need to bother them any more. +} + +window.isHidden = function isHidden() { return (typeof(document.hidden) != 'undefined') ? document.hidden : !document.hasFocus() }; diff --git a/public/javascripts/chat.js b/public/javascripts/chat.js index 3d93a3d..bc7c081 100644 --- a/public/javascripts/chat.js +++ b/public/javascripts/chat.js @@ -337,7 +337,7 @@ jQuery(function($) { var name = Jabber.name_of(jid_id); create_conversation_tab(name, jid_id); Jabber.show_message(jid, name, escape_html(message.body), 'other', Strophe.getNodeFromJid(jid)); - $.sound.play('/sounds/receive.wav'); + notifyMessage(message); return true; }, @@ -353,8 +353,8 @@ jQuery(function($) { else if ($own_name != name) { var jid = Jabber.rooms[Jabber.jid_to_id(message.from)][name]; Jabber.show_message(message.from, name, escape_html(message.body), name, Strophe.getNodeFromJid(jid)); - $.sound.play('/sounds/receive.wav'); } + notifyMessage(message); return true; }, @@ -492,8 +492,11 @@ jQuery(function($) { } else { log('opening chat with ' + jid); - create_conversation_tab(name, jid_id); + var conversation = create_conversation_tab(name, jid_id); + conversation.find('.conversation').show(); + recent_messages(jid); } + conversation.find('.input').focus(); } }); @@ -664,6 +667,21 @@ jQuery(function($) { Jabber.show_status('offline'); } + function notifyMessage(message) { + var jid = Strophe.getBareJidFromJid(message.from); + var jid_id = Jabber.jid_to_id(jid); + var name = Jabber.name_of(jid_id); + var identifier = Strophe.getNodeFromJid(jid); + var avatar = "/chat/avatar/"+identifier + if(!$('#chat').is(':visible') || window.isHidden()) { + var options = {body: message.body, icon: avatar, tag: jid_id}; + notifyMe(name, options).onclick = function(){ + open_chat_window(this, '#'+jid); + }; + $.sound.play('/sounds/receive.wav'); + } + } + $('.title-bar a').click(function() { $(this).parents('.status-group').find('.buddy-list').toggle('fast'); }); -- libgit2 0.21.2