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;
|
2012-11-26 23:57:31 +01:00
|
|
|
var new_message_count = 0;
|
2013-01-08 16:54:44 +01:00
|
|
|
var asked_permission_already = false;
|
2013-01-09 23:49:54 +01:00
|
|
|
var names;
|
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 () {
|
2013-01-18 23:44:29 +01:00
|
|
|
names = fullname.toLowerCase().split(" ");
|
|
|
|
var username = email.split("@")[0].toLowerCase();
|
2013-01-18 22:57:22 +01:00
|
|
|
// If the username is part of the user's full name, then don't add it
|
|
|
|
// to names because we don't send notifications to ambiguous names
|
|
|
|
// that could refer to multiple people in the domain.
|
|
|
|
// (We later add to names all of the names from the domain)
|
|
|
|
if (names.indexOf(username) === -1) {
|
|
|
|
names.push(username);
|
|
|
|
}
|
2013-01-09 23:49:54 +01:00
|
|
|
|
2012-11-23 23:53:38 +01:00
|
|
|
$(window).focus(function () {
|
|
|
|
window_has_focus = true;
|
2012-11-26 23:57:31 +01:00
|
|
|
new_message_count = 0;
|
2012-12-05 20:41:49 +01:00
|
|
|
document.title = domain + " - Humbug";
|
2012-11-27 18:26:36 +01:00
|
|
|
|
|
|
|
$.each(notice_memory, function (index, notice_mem_entry) {
|
|
|
|
notice_mem_entry.obj.cancel();
|
|
|
|
});
|
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-01-08 16:54:44 +01:00
|
|
|
if (!desktop_notifications_enabled || asked_permission_already) {
|
2012-11-23 23:53:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (window.webkitNotifications.checkPermission() !== 0) { // 0 is PERMISSION_ALLOWED
|
|
|
|
window.webkitNotifications.requestPermission();
|
2013-01-08 16:54:44 +01:00
|
|
|
asked_permission_already = true;
|
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 " +
|
|
|
|
message.display_recipient + " | " + message.subject;
|
|
|
|
}
|
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") {
|
|
|
|
title += " (to " + message.display_recipient + " | " + message.subject + ")";
|
|
|
|
}
|
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();
|
|
|
|
delete notice_memory[key];
|
|
|
|
};
|
|
|
|
notification_object.onclose = function () {
|
|
|
|
delete notice_memory[key];
|
|
|
|
};
|
|
|
|
notification_object.show();
|
|
|
|
}
|
|
|
|
|
2013-01-09 23:49:54 +01:00
|
|
|
function speaking_at_me(message) {
|
|
|
|
var content_lc = $('<div/>').html(message.content).text().toLowerCase();
|
|
|
|
var match_so_far = false;
|
|
|
|
var indexof, after_name, after_atname;
|
2013-01-10 21:26:25 +01:00
|
|
|
var punctuation = /[\.,-\/#!$%\^&\*;:{}=\-_`~()\+\?\[\]\s]/;
|
2013-01-09 23:49:54 +01:00
|
|
|
|
2013-01-10 04:47:11 +01:00
|
|
|
if (domain === "mit.edu") {
|
|
|
|
return false;
|
2013-01-10 17:24:39 +01:00
|
|
|
}
|
2013-01-10 04:47:11 +01:00
|
|
|
|
2013-01-09 23:49:54 +01:00
|
|
|
$.each(names, function (index, name) {
|
|
|
|
indexof = content_lc.indexOf(name.toLowerCase());
|
2013-01-10 17:24:39 +01:00
|
|
|
if (indexof === -1) {
|
|
|
|
// If there is no match, we don't need after_name
|
|
|
|
after_name = undefined;
|
|
|
|
} else if (indexof + name.length >= content_lc.length) {
|
|
|
|
// If the @name is at the end of the string, that's OK,
|
|
|
|
// so we set after_name to " " so that the code below
|
|
|
|
// will identify a match
|
|
|
|
after_name = " ";
|
|
|
|
} else {
|
|
|
|
after_name = content_lc.charAt(indexof + name.length);
|
|
|
|
}
|
2013-01-09 23:49:54 +01:00
|
|
|
if ((indexof === 0 &&
|
2013-01-10 21:26:25 +01:00
|
|
|
after_name.match(punctuation) !== null) ||
|
2013-01-10 17:24:39 +01:00
|
|
|
(indexof > 0 && content_lc.charAt(indexof-1) === "@" &&
|
2013-01-10 21:26:25 +01:00
|
|
|
after_name.match(punctuation) !== null)) {
|
2013-01-09 23:49:54 +01:00
|
|
|
if (match_so_far) {
|
|
|
|
match_so_far = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
match_so_far = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return match_so_far;
|
|
|
|
}
|
|
|
|
|
2012-11-23 23:53:38 +01:00
|
|
|
exports.received_messages = function (messages) {
|
2012-11-26 23:57:31 +01:00
|
|
|
var i, title_needs_update = false;
|
|
|
|
if (window_has_focus) {
|
2012-11-23 23:53:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.each(messages, function (index, message) {
|
2012-11-26 23:57:31 +01:00
|
|
|
if (message.sender_email !== email) {
|
|
|
|
new_message_count++;
|
|
|
|
title_needs_update = true;
|
|
|
|
|
|
|
|
if (desktop_notifications_enabled &&
|
|
|
|
browser_desktop_notifications_on() &&
|
2013-01-09 23:49:54 +01:00
|
|
|
(message.type === "private" ||
|
|
|
|
speaking_at_me(message))) {
|
2012-11-26 23:57:31 +01:00
|
|
|
process_desktop_notification(message);
|
|
|
|
}
|
2012-11-23 23:53:38 +01:00
|
|
|
}
|
|
|
|
});
|
2012-11-26 23:57:31 +01:00
|
|
|
|
|
|
|
if (title_needs_update) {
|
2012-12-05 20:41:49 +01:00
|
|
|
document.title = "(" + new_message_count + ") " + domain + " - Humbug";
|
2012-11-26 23:57:31 +01:00
|
|
|
}
|
2012-11-23 23:53:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|