Commit 3ae9623b2ccbfff7cdea27024eb0f25445ba8a89

Authored by Rodrigo Souto
1 parent c0508424

Chat notification

public/javascripts/application.js
... ... @@ -1056,3 +1056,41 @@ function apply_zoom_to_images(zoom_text) {
1056 1056 });
1057 1057 });
1058 1058 }
  1059 +
  1060 +function notifyMe(title, options) {
  1061 + // This might be useful in the future
  1062 + //
  1063 + // Let's check if the browser supports notifications
  1064 + // if (!("Notification" in window)) {
  1065 + // alert("This browser does not support desktop notification");
  1066 + // }
  1067 +
  1068 + // Let's check if the user is okay to get some notification
  1069 + var notification = null;
  1070 + if (Notification.permission === "granted") {
  1071 + // If it's okay let's create a notification
  1072 + notification = new Notification(title, options);
  1073 + }
  1074 +
  1075 + // Otherwise, we need to ask the user for permission
  1076 + // Note, Chrome does not implement the permission static property
  1077 + // So we have to check for NOT 'denied' instead of 'default'
  1078 + else if (Notification.permission !== 'denied') {
  1079 + Notification.requestPermission(function (permission) {
  1080 + // Whatever the user answers, we make sure we store the information
  1081 + if (!('permission' in Notification)) {
  1082 + Notification.permission = permission;
  1083 + }
  1084 +
  1085 + // If the user is okay, let's create a notification
  1086 + if (permission === "granted") {
  1087 + notification = new Notification(title, options);
  1088 + }
  1089 + });
  1090 + }
  1091 + return notification;
  1092 + // At last, if the user already denied any notification, and you
  1093 + // want to be respectful there is no need to bother them any more.
  1094 +}
  1095 +
  1096 +window.isHidden = function isHidden() { return (typeof(document.hidden) != 'undefined') ? document.hidden : !document.hasFocus() };
... ...
public/javascripts/chat.js
... ... @@ -337,7 +337,7 @@ jQuery(function($) {
337 337 var name = Jabber.name_of(jid_id);
338 338 create_conversation_tab(name, jid_id);
339 339 Jabber.show_message(jid, name, escape_html(message.body), 'other', Strophe.getNodeFromJid(jid));
340   - $.sound.play('/sounds/receive.wav');
  340 + notifyMessage(message);
341 341 return true;
342 342 },
343 343  
... ... @@ -353,8 +353,8 @@ jQuery(function($) {
353 353 else if ($own_name != name) {
354 354 var jid = Jabber.rooms[Jabber.jid_to_id(message.from)][name];
355 355 Jabber.show_message(message.from, name, escape_html(message.body), name, Strophe.getNodeFromJid(jid));
356   - $.sound.play('/sounds/receive.wav');
357 356 }
  357 + notifyMessage(message);
358 358 return true;
359 359 },
360 360  
... ... @@ -492,8 +492,11 @@ jQuery(function($) {
492 492 }
493 493 else {
494 494 log('opening chat with ' + jid);
495   - create_conversation_tab(name, jid_id);
  495 + var conversation = create_conversation_tab(name, jid_id);
  496 + conversation.find('.conversation').show();
  497 + recent_messages(jid);
496 498 }
  499 + conversation.find('.input').focus();
497 500 }
498 501 });
499 502  
... ... @@ -664,6 +667,21 @@ jQuery(function($) {
664 667 Jabber.show_status('offline');
665 668 }
666 669  
  670 + function notifyMessage(message) {
  671 + var jid = Strophe.getBareJidFromJid(message.from);
  672 + var jid_id = Jabber.jid_to_id(jid);
  673 + var name = Jabber.name_of(jid_id);
  674 + var identifier = Strophe.getNodeFromJid(jid);
  675 + var avatar = "/chat/avatar/"+identifier
  676 + if(!$('#chat').is(':visible') || window.isHidden()) {
  677 + var options = {body: message.body, icon: avatar, tag: jid_id};
  678 + notifyMe(name, options).onclick = function(){
  679 + open_chat_window(this, '#'+jid);
  680 + };
  681 + $.sound.play('/sounds/receive.wav');
  682 + }
  683 + }
  684 +
667 685 $('.title-bar a').click(function() {
668 686 $(this).parents('.status-group').find('.buddy-list').toggle('fast');
669 687 });
... ...