diff --git a/templates/zephyr/index.html b/templates/zephyr/index.html index c89deb91e8..0e981e22c5 100644 --- a/templates/zephyr/index.html +++ b/templates/zephyr/index.html @@ -66,6 +66,7 @@ var initial_pointer = {{ user_profile.pointer }}; var poll_timeout = {{ poll_timeout }}; +var fullname = "{{ user_profile.full_name|escapejs }}"; var email = "{{ user_profile.user.email|escapejs }}"; var domain = "{{ user_profile.realm.domain|escapejs }}"; var have_initial_messages = {{ have_initial_messages|escapejs }}; diff --git a/tools/jslint/check-all.js b/tools/jslint/check-all.js index 35972d6d79..7ef5ad8f24 100644 --- a/tools/jslint/check-all.js +++ b/tools/jslint/check-all.js @@ -7,7 +7,7 @@ var globals = // index.html + ' initial_pointer email stream_list people_list have_initial_messages' - + ' desktop_notifications_enabled domain poll_timeout' + + ' fullname desktop_notifications_enabled domain poll_timeout' // common.js + ' status_classes' diff --git a/zephyr/static/js/notifications.js b/zephyr/static/js/notifications.js index 487563aa18..b149cac03f 100644 --- a/zephyr/static/js/notifications.js +++ b/zephyr/static/js/notifications.js @@ -6,6 +6,7 @@ var notice_memory = {}; var window_has_focus = true; var new_message_count = 0; var asked_permission_already = false; +var names; function browser_desktop_notifications_on () { return (window.webkitNotifications && @@ -14,6 +15,8 @@ function browser_desktop_notifications_on () { } exports.initialize = function () { + names = fullname.split(" "); + $(window).focus(function () { window_has_focus = true; new_message_count = 0; @@ -47,16 +50,22 @@ function gravatar_url(message) { } function process_desktop_notification(message) { - var i, notification_object; - var key = message.display_reply_to; + var i, notification_object, key; var title = message.sender_full_name; var content = $('
').html(message.content).text(); - var other_recipients = message.display_reply_to; + var other_recipients; var msg_count = 1; - // 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 + ", ", ""); + 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; + } if (content.length > 150) { // Truncate content at a word boundary @@ -89,6 +98,9 @@ function process_desktop_notification(message) { } title += " (to you and " + other_recipients + ")"; } + if (message.type === "stream") { + title += " (to " + message.display_recipient + " | " + message.subject + ")"; + } notice_memory[key] = { obj: window.webkitNotifications.createNotification( @@ -106,6 +118,32 @@ function process_desktop_notification(message) { notification_object.show(); } +function speaking_at_me(message) { + var content_lc = $('
').html(message.content).text().toLowerCase(); + var match_so_far = false; + var indexof, after_name, after_atname; + + $.each(names, function (index, name) { + indexof = content_lc.indexOf(name.toLowerCase()); + after_name = content_lc.charAt(name.length); + after_atname = content_lc.charAt(name.length+1); + if ((indexof === 0 && + (after_name === " " || after_name === ":")) || + (indexof === 1 && content_lc.charAt(0) === "@" && + (after_atname === " " || after_atname === ":"))) { + if (match_so_far) { + match_so_far = false; + return false; + } + else { + match_so_far = true; + } + } + }); + + return match_so_far; +} + exports.received_messages = function (messages) { var i, title_needs_update = false; if (window_has_focus) { @@ -119,7 +157,8 @@ exports.received_messages = function (messages) { if (desktop_notifications_enabled && browser_desktop_notifications_on() && - message.type === "private") { + (message.type === "private" || + speaking_at_me(message))) { process_desktop_notification(message); } }