2012-11-23 23:53:38 +01:00
|
|
|
var notifications = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2012-11-26 23:57:31 +01:00
|
|
|
var notice_memory = {};
|
2012-11-23 23:53:38 +01:00
|
|
|
var window_has_focus = true;
|
2013-01-08 16:54:44 +01:00
|
|
|
var asked_permission_already = false;
|
2013-01-09 23:49:54 +01:00
|
|
|
var names;
|
2013-05-03 21:36:38 +02:00
|
|
|
var supports_sound;
|
2012-11-23 23:53:38 +01:00
|
|
|
|
2012-11-26 23:57:31 +01:00
|
|
|
function browser_desktop_notifications_on () {
|
|
|
|
return (window.webkitNotifications &&
|
2013-01-21 21:50:33 +01:00
|
|
|
// Firefox on Ubuntu claims to do webkitNotifications but its notifications are terrible
|
|
|
|
$.browser.webkit &&
|
2012-11-26 23:57:31 +01:00
|
|
|
// 0 is PERMISSION_ALLOWED
|
|
|
|
window.webkitNotifications.checkPermission() === 0);
|
|
|
|
}
|
2012-11-23 23:53:38 +01:00
|
|
|
|
2012-11-26 23:57:31 +01:00
|
|
|
exports.initialize = function () {
|
2012-11-23 23:53:38 +01:00
|
|
|
$(window).focus(function () {
|
|
|
|
window_has_focus = true;
|
2013-03-19 21:53:49 +01:00
|
|
|
exports.update_title_count();
|
2012-11-27 18:26:36 +01:00
|
|
|
|
|
|
|
$.each(notice_memory, function (index, notice_mem_entry) {
|
|
|
|
notice_mem_entry.obj.cancel();
|
|
|
|
});
|
2013-03-04 23:44:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
process_visible_unread_messages();
|
2012-11-23 23:53:38 +01:00
|
|
|
}).blur(function () {
|
|
|
|
window_has_focus = false;
|
|
|
|
});
|
|
|
|
|
2012-11-26 23:57:31 +01:00
|
|
|
if (!window.webkitNotifications) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-23 23:53:38 +01:00
|
|
|
$(document).click(function () {
|
2013-03-25 23:26:14 +01:00
|
|
|
if (!page_params.desktop_notifications_enabled || asked_permission_already) {
|
2012-11-23 23:53:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (window.webkitNotifications.checkPermission() !== 0) { // 0 is PERMISSION_ALLOWED
|
2013-02-26 23:46:53 +01:00
|
|
|
window.webkitNotifications.requestPermission(function () {});
|
2013-01-08 16:54:44 +01:00
|
|
|
asked_permission_already = true;
|
2012-11-23 23:53:38 +01:00
|
|
|
}
|
|
|
|
});
|
2013-05-03 21:36:38 +02:00
|
|
|
var audio = $("<audio>");
|
|
|
|
if (audio[0].canPlayType === undefined) {
|
|
|
|
supports_sound = false;
|
|
|
|
} else {
|
|
|
|
supports_sound = true;
|
|
|
|
$("#notifications-area").append(audio);
|
|
|
|
if (audio[0].canPlayType('audio/ogg; codecs="vorbis"')) {
|
|
|
|
audio.append($("<source>").attr("type", "audio/ogg")
|
|
|
|
.attr("loop", "yes")
|
|
|
|
.attr("src", "/static/audio/humbug.ogg"));
|
|
|
|
} else {
|
|
|
|
audio.append($("<source>").attr("type", "audio/mpeg")
|
|
|
|
.attr("loop", "yes")
|
|
|
|
.attr("src", "/static/audio/humbug.mp3"));
|
|
|
|
}
|
|
|
|
}
|
2012-11-23 23:53:38 +01:00
|
|
|
};
|
|
|
|
|
2013-03-19 21:53:49 +01:00
|
|
|
exports.update_title_count = function () {
|
|
|
|
// Update window title and favicon to reflect unread messages in current view
|
|
|
|
var n;
|
|
|
|
|
|
|
|
var new_message_count = unread_in_current_view();
|
2013-05-06 22:35:10 +02:00
|
|
|
var new_title = (new_message_count ? ("(" + new_message_count + ") ") : "")
|
2013-03-25 23:26:14 +01:00
|
|
|
+ page_params.domain + " - Humbug";
|
2013-03-19 21:53:49 +01:00
|
|
|
|
2013-05-06 22:35:10 +02:00
|
|
|
if (document.title === new_title) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
document.title = new_title;
|
|
|
|
|
2013-03-19 21:53:49 +01:00
|
|
|
// IE doesn't support PNG favicons, *shrug*
|
|
|
|
if (! $.browser.msie) {
|
|
|
|
// Indicate the message count in the favicon
|
|
|
|
if (new_message_count) {
|
|
|
|
// Make sure we're working with a number, as a defensive programming
|
|
|
|
// measure. And we don't have images above 99, so display those as
|
|
|
|
// 'infinite'.
|
|
|
|
n = (+new_message_count);
|
|
|
|
if (n > 99)
|
|
|
|
n = 'infinite';
|
|
|
|
|
|
|
|
util.set_favicon('/static/images/favicon/favicon-'+n+'.png');
|
|
|
|
} else {
|
|
|
|
util.set_favicon('/static/favicon.ico?v=2');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-04 23:44:07 +01:00
|
|
|
exports.window_has_focus = function () {
|
|
|
|
return window_has_focus;
|
|
|
|
};
|
|
|
|
|
2012-11-23 23:53:38 +01:00
|
|
|
function gravatar_url(message) {
|
|
|
|
return "https://secure.gravatar.com/avatar/" + message.gravatar_hash +
|
|
|
|
"?d=identicon&s=30?stamp=" + ui.get_gravatar_stamp();
|
|
|
|
}
|
|
|
|
|
2012-11-26 23:57:31 +01:00
|
|
|
function process_desktop_notification(message) {
|
2013-01-09 23:49:54 +01:00
|
|
|
var i, notification_object, key;
|
2012-11-23 23:53:38 +01:00
|
|
|
var title = message.sender_full_name;
|
|
|
|
var content = $('<div/>').html(message.content).text();
|
2013-01-09 23:49:54 +01:00
|
|
|
var other_recipients;
|
2012-11-23 23:53:38 +01:00
|
|
|
var msg_count = 1;
|
|
|
|
|
2013-01-09 23:49:54 +01:00
|
|
|
if (message.type === "private") {
|
|
|
|
key = message.display_reply_to;
|
|
|
|
other_recipients = message.display_reply_to;
|
|
|
|
// Remove the sender from the list of other recipients
|
|
|
|
other_recipients = other_recipients.replace(", " + message.sender_full_name, "");
|
|
|
|
other_recipients = other_recipients.replace(message.sender_full_name + ", ", "");
|
|
|
|
} else {
|
|
|
|
key = message.sender_full_name + " to " +
|
2013-04-18 17:13:43 +02:00
|
|
|
message.stream + " > " + message.subject;
|
2013-01-09 23:49:54 +01:00
|
|
|
}
|
2012-11-23 23:53:38 +01:00
|
|
|
|
|
|
|
if (content.length > 150) {
|
|
|
|
// Truncate content at a word boundary
|
|
|
|
for (i = 150; i > 0; i--) {
|
|
|
|
if (content[i] === ' ') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
content = content.substring(0, i);
|
|
|
|
content += " [...]";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (notice_memory[key] !== undefined) {
|
|
|
|
msg_count = notice_memory[key].msg_count + 1;
|
|
|
|
title = msg_count + " messages from " + title;
|
|
|
|
notification_object = notice_memory[key].obj;
|
|
|
|
// We must remove the .onclose so that it does not trigger on .cancel
|
|
|
|
notification_object.onclose = function () {};
|
|
|
|
notification_object.onclick = function () {};
|
|
|
|
notification_object.cancel();
|
|
|
|
}
|
|
|
|
|
2012-12-03 19:49:12 +01:00
|
|
|
if (message.type === "private" && message.display_recipient.length > 2) {
|
2012-11-23 23:53:38 +01:00
|
|
|
// If the message has too many recipients to list them all...
|
|
|
|
if (content.length + title.length + other_recipients.length > 230) {
|
|
|
|
// Then count how many people are in the conversation and summarize
|
|
|
|
// by saying the conversation is with "you and [number] other people"
|
|
|
|
other_recipients = other_recipients.replace(/[^,]/g, "").length +
|
|
|
|
" other people";
|
|
|
|
}
|
|
|
|
title += " (to you and " + other_recipients + ")";
|
|
|
|
}
|
2013-01-09 23:49:54 +01:00
|
|
|
if (message.type === "stream") {
|
2013-04-18 17:13:43 +02:00
|
|
|
title += " (to " + message.stream + " > " + message.subject + ")";
|
2013-01-09 23:49:54 +01:00
|
|
|
}
|
2012-11-23 23:53:38 +01:00
|
|
|
|
|
|
|
notice_memory[key] = {
|
|
|
|
obj: window.webkitNotifications.createNotification(
|
|
|
|
gravatar_url(message), title, content),
|
|
|
|
msg_count: msg_count
|
|
|
|
};
|
|
|
|
notification_object = notice_memory[key].obj;
|
|
|
|
notification_object.onclick = function () {
|
|
|
|
notification_object.cancel();
|
2013-03-12 21:03:42 +01:00
|
|
|
window.focus();
|
2012-11-23 23:53:38 +01:00
|
|
|
};
|
|
|
|
notification_object.onclose = function () {
|
|
|
|
delete notice_memory[key];
|
|
|
|
};
|
|
|
|
notification_object.show();
|
|
|
|
}
|
|
|
|
|
2013-03-12 22:36:13 +01:00
|
|
|
exports.speaking_at_me = function (message) {
|
2013-05-17 19:50:36 +02:00
|
|
|
if (message === undefined || message.flags === undefined) {
|
2013-01-10 04:47:11 +01:00
|
|
|
return false;
|
2013-01-10 17:24:39 +01:00
|
|
|
}
|
2013-05-17 19:50:36 +02:00
|
|
|
return message.flags.indexOf('mentioned') > -1;
|
2013-03-12 22:36:13 +01:00
|
|
|
};
|
2013-01-09 23:49:54 +01:00
|
|
|
|
2013-05-03 21:49:01 +02:00
|
|
|
function message_is_notifiable(message) {
|
|
|
|
// based purely on message contents, can we notify the user about the message?
|
|
|
|
return (message.type === "private" ||
|
|
|
|
exports.speaking_at_me(message) ||
|
|
|
|
(message.type === "stream" &&
|
|
|
|
subs.receives_notifications(message.stream)));
|
|
|
|
}
|
|
|
|
|
2012-11-23 23:53:38 +01:00
|
|
|
exports.received_messages = function (messages) {
|
Fix unread count in favicon when focusing window.
This fixes a pretty subtle bug where the window-focus handler
wasn't updating the unread counts in the title, but it was
hard to notice, because as soon as you moved the mouse, the
problem fixed itself.
Apart from fixing the bug, this patch eliminates the expensive
mouseover handler, which is a big win.
The fix to the window-focus involved some unrelated cleanup. I
decoupled update_title_count() from received_messages(), as the
former method will probably live somewhere else soon.
Also, in order to get window-focus to update the title count,
I went pretty deep in the stack and added a call to
update_title_count() inside of update_unread_counts(). This
fixes window-focus as well as restoring that behavior to
code paths that were calling received_messages().
You'll see that call to update_title_count() is now adjacent
to the call to update_dom_with_unread_counts(), which is
fairly sensible, but then are calls to similar methods like
notifications.received_messages() that happen higher up in
the call chain, which seems kind of inconsistent to me. I
also don't like the fact that you have to go through a
mostly model-based function to get to view-based stuff, so
there are some refactorings coming.
(imported from commit 2261450f205f1aa81d30194b371a1c5ac6a7bdec)
2013-05-17 18:31:04 +02:00
|
|
|
var i;
|
2012-11-23 23:53:38 +01:00
|
|
|
|
|
|
|
$.each(messages, function (index, message) {
|
2013-05-08 21:15:40 +02:00
|
|
|
if (message.sender_email !== page_params.email &&
|
|
|
|
narrow.message_in_home(message)) {
|
2013-05-08 21:31:26 +02:00
|
|
|
|
|
|
|
// We send notifications for messages which the user has
|
|
|
|
// configured as notifiable, as long as they haven't been
|
|
|
|
// marked as read by process_visible_unread_messages
|
|
|
|
// (which occurs if the message arrived onscreen while the
|
|
|
|
// window had focus).
|
2013-05-17 21:32:26 +02:00
|
|
|
if (!(message_is_notifiable(message) && unread.message_unread(message))) {
|
2013-05-08 21:15:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (page_params.desktop_notifications_enabled &&
|
|
|
|
browser_desktop_notifications_on()) {
|
2012-11-26 23:57:31 +01:00
|
|
|
process_desktop_notification(message);
|
2013-05-03 21:49:01 +02:00
|
|
|
}
|
2013-05-08 21:15:40 +02:00
|
|
|
if (page_params.sounds_enabled && supports_sound) {
|
2013-05-03 21:49:01 +02:00
|
|
|
$("#notifications-area").find("audio")[0].play();
|
2012-11-26 23:57:31 +01:00
|
|
|
}
|
2012-11-23 23:53:38 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|