2012-11-23 23:53:38 +01:00
|
|
|
var notifications = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2012-11-26 23:57:31 +01:00
|
|
|
var notice_memory = {};
|
2013-09-16 18:22:52 +02:00
|
|
|
|
|
|
|
// When you start Zulip, window_has_focus should be true, but it might not be the
|
|
|
|
// case after a server-initiated reload.
|
|
|
|
var window_has_focus = document.hasFocus && document.hasFocus();
|
|
|
|
|
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
|
|
|
|
2013-06-28 20:51:28 +02:00
|
|
|
var unread_pms_favicon = '/static/images/favicon/favicon-pms.png';
|
|
|
|
var current_favicon;
|
|
|
|
var previous_favicon;
|
|
|
|
var flashing = false;
|
|
|
|
|
2014-05-29 05:00:45 +02:00
|
|
|
var notifications_api;
|
|
|
|
if (window.webkitNotifications) {
|
|
|
|
notifications_api = window.webkitNotifications;
|
|
|
|
} else if (window.Notification) {
|
|
|
|
// Build a shim to the new notification API
|
|
|
|
notifications_api = {
|
|
|
|
checkPermission: function checkPermission() {
|
|
|
|
if (window.Notification.permission === 'granted') {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
requestPermission: window.Notification.requestPermission,
|
|
|
|
createNotification: function createNotification(icon, title, content) {
|
|
|
|
var notification_object = new window.Notification(title, {icon: icon, body: content});
|
|
|
|
notification_object.show = function () {};
|
|
|
|
notification_object.cancel = function () { notification_object.close(); };
|
|
|
|
return notification_object;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-26 23:57:31 +01:00
|
|
|
function browser_desktop_notifications_on () {
|
2014-05-29 05:00:45 +02:00
|
|
|
return (notifications_api &&
|
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
|
2014-05-29 05:00:45 +02:00
|
|
|
notifications_api.checkPermission() === 0) ||
|
2013-05-31 22:25:39 +02:00
|
|
|
// window.bridge is the desktop client
|
|
|
|
(window.bridge !== undefined);
|
2012-11-26 23:57:31 +01:00
|
|
|
}
|
2012-11-23 23:53:38 +01:00
|
|
|
|
2013-08-29 22:58:00 +02:00
|
|
|
function cancel_notification_object (notification_object) {
|
|
|
|
// 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-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;
|
2012-11-27 18:26:36 +01:00
|
|
|
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(notice_memory, function (notice_mem_entry) {
|
2013-08-29 22:58:00 +02:00
|
|
|
cancel_notification_object(notice_mem_entry.obj);
|
2012-11-27 18:26:36 +01:00
|
|
|
});
|
2013-08-29 22:58:00 +02:00
|
|
|
notice_memory = {};
|
2013-07-11 17:14:11 +02:00
|
|
|
|
|
|
|
// Update many places on the DOM to reflect unread
|
|
|
|
// counts.
|
2014-01-31 17:44:21 +01:00
|
|
|
unread.process_visible();
|
2013-07-11 17:14:11 +02:00
|
|
|
|
2012-11-23 23:53:38 +01:00
|
|
|
}).blur(function () {
|
|
|
|
window_has_focus = false;
|
|
|
|
});
|
|
|
|
|
2013-07-05 20:47:41 +02:00
|
|
|
if (window.bridge !== undefined) {
|
|
|
|
supports_sound = true;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-03 21:36:38 +02:00
|
|
|
var audio = $("<audio>");
|
2013-07-05 20:47:41 +02:00
|
|
|
if (audio[0].canPlayType === undefined) {
|
2013-05-03 21:36:38 +02:00
|
|
|
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")
|
2013-10-31 19:04:31 +01:00
|
|
|
.attr("src", "/static/audio/zulip.ogg"));
|
2013-05-03 21:36:38 +02:00
|
|
|
} else {
|
|
|
|
audio.append($("<source>").attr("type", "audio/mpeg")
|
|
|
|
.attr("loop", "yes")
|
2013-10-31 19:04:31 +01:00
|
|
|
.attr("src", "/static/audio/zulip.mp3"));
|
2013-05-03 21:36:38 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-12 20:12:12 +02:00
|
|
|
|
2014-05-29 05:00:45 +02:00
|
|
|
if (notifications_api) {
|
2013-07-12 20:12:12 +02:00
|
|
|
$(document).click(function () {
|
|
|
|
if (!page_params.desktop_notifications_enabled || asked_permission_already) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-29 05:00:45 +02:00
|
|
|
if (notifications_api.checkPermission() !== 0) { // 0 is PERMISSION_ALLOWED
|
2015-10-26 17:44:15 +01:00
|
|
|
if(tutorial.is_running()) {
|
|
|
|
tutorial.defer(function () {
|
|
|
|
notifications_api.requestPermission(function () {
|
|
|
|
asked_permission_already = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
notifications_api.requestPermission(function () {
|
|
|
|
asked_permission_already = true;
|
|
|
|
});
|
|
|
|
}
|
2013-07-12 20:12:12 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-11-23 23:53:38 +01:00
|
|
|
};
|
|
|
|
|
2013-08-14 23:17:15 +02:00
|
|
|
// For web pages, the initial favicon is the same as the favicon we
|
|
|
|
// set for no unread messages and the initial page title is the same
|
|
|
|
// as the page title we set for no unread messages. However, for the
|
|
|
|
// OS X app, the dock icon does not get its badge updated on initial
|
|
|
|
// page load. If the badge icon was wrong right before a reload and
|
|
|
|
// we actually have no unread messages then we will never execute
|
|
|
|
// bridge.updateCount() until the unread count changes. Therefore,
|
|
|
|
// we ensure that bridge.updateCount is always run at least once to
|
|
|
|
// synchronize it with the page title. This can be done before the
|
|
|
|
// DOM is loaded.
|
|
|
|
if (window.bridge !== undefined) {
|
|
|
|
window.bridge.updateCount(0);
|
|
|
|
}
|
|
|
|
|
2014-02-04 00:20:42 +01:00
|
|
|
var new_message_count;
|
|
|
|
|
|
|
|
exports.update_title_count = function (count) {
|
|
|
|
new_message_count = count;
|
|
|
|
exports.redraw_title();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.redraw_title = function () {
|
2013-03-19 21:53:49 +01:00
|
|
|
// Update window title and favicon to reflect unread messages in current view
|
|
|
|
var n;
|
|
|
|
|
2013-05-06 22:35:10 +02:00
|
|
|
var new_title = (new_message_count ? ("(" + new_message_count + ") ") : "")
|
2014-07-15 23:21:03 +02:00
|
|
|
+ page_params.realm_name + " - " + page_params.product_name;
|
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);
|
2013-08-01 17:47:48 +02:00
|
|
|
if (n > 99) {
|
2013-03-19 21:53:49 +01:00
|
|
|
n = 'infinite';
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-03-19 21:53:49 +01:00
|
|
|
|
2013-06-28 20:51:28 +02:00
|
|
|
current_favicon = previous_favicon = '/static/images/favicon/favicon-'+n+'.png';
|
2013-03-19 21:53:49 +01:00
|
|
|
} else {
|
2013-06-28 20:51:28 +02:00
|
|
|
current_favicon = previous_favicon = '/static/favicon.ico?v=2';
|
2013-03-19 21:53:49 +01:00
|
|
|
}
|
2014-03-13 17:44:43 +01:00
|
|
|
favicon.set(current_favicon);
|
2013-03-19 21:53:49 +01:00
|
|
|
}
|
2013-06-02 20:42:03 +02:00
|
|
|
|
|
|
|
if (window.bridge !== undefined) {
|
|
|
|
// We don't use 'n' because we want the exact count. The bridge handles
|
|
|
|
// which icon to show.
|
|
|
|
window.bridge.updateCount(new_message_count);
|
|
|
|
}
|
2013-03-19 21:53:49 +01:00
|
|
|
};
|
|
|
|
|
2013-06-28 20:51:28 +02:00
|
|
|
function flash_pms() {
|
|
|
|
// When you have unread PMs, toggle the favicon between the unread count and
|
|
|
|
// a special icon indicating that you have unread PMs.
|
|
|
|
if (unread.get_counts().private_message_count > 0) {
|
|
|
|
if (current_favicon === unread_pms_favicon) {
|
2014-03-13 17:44:43 +01:00
|
|
|
favicon.set(previous_favicon);
|
2013-06-28 20:51:28 +02:00
|
|
|
current_favicon = previous_favicon;
|
|
|
|
previous_favicon = unread_pms_favicon;
|
|
|
|
} else {
|
2014-03-13 17:44:43 +01:00
|
|
|
favicon.set(unread_pms_favicon);
|
2013-06-28 20:51:28 +02:00
|
|
|
previous_favicon = current_favicon;
|
|
|
|
current_favicon = unread_pms_favicon;
|
|
|
|
}
|
|
|
|
// Toggle every 2 seconds.
|
|
|
|
setTimeout(flash_pms, 2000);
|
|
|
|
} else {
|
|
|
|
flashing = false;
|
|
|
|
// You have no more unread PMs, so back to only showing the unread
|
|
|
|
// count.
|
2014-03-13 17:44:43 +01:00
|
|
|
favicon.set(current_favicon);
|
2013-06-28 20:51:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-19 00:00:40 +02:00
|
|
|
exports.update_pm_count = function (new_pm_count) {
|
|
|
|
if (window.bridge !== undefined && window.bridge.updatePMCount !== undefined) {
|
|
|
|
window.bridge.updatePMCount(new_pm_count);
|
|
|
|
}
|
2013-06-28 20:51:28 +02:00
|
|
|
if (!flashing) {
|
|
|
|
flashing = true;
|
|
|
|
flash_pms();
|
|
|
|
}
|
2013-06-19 00:00:40 +02:00
|
|
|
};
|
|
|
|
|
2013-03-04 23:44:07 +01:00
|
|
|
exports.window_has_focus = function () {
|
|
|
|
return window_has_focus;
|
|
|
|
};
|
|
|
|
|
2013-06-27 22:15:00 +02:00
|
|
|
function in_browser_notify(message, title, content) {
|
|
|
|
var notification_html = $(templates.render('notification', {gravatar_url: ui.small_avatar_url(message),
|
|
|
|
title: title,
|
|
|
|
content: content}));
|
|
|
|
$('.top-right').notify({
|
2013-10-09 22:42:15 +02:00
|
|
|
message: {html: notification_html},
|
|
|
|
fadeOut: {enabled: true, delay: 4000}
|
2013-06-27 22:15:00 +02:00
|
|
|
}).show();
|
|
|
|
}
|
|
|
|
|
2013-11-13 19:40:02 +01:00
|
|
|
exports.notify_above_composebox = function (note, link_class, link_msg_id, link_text) {
|
|
|
|
var notification_html = $(templates.render('compose_notification', {note: note,
|
2013-10-09 22:42:15 +02:00
|
|
|
link_class: link_class,
|
|
|
|
link_msg_id: link_msg_id,
|
|
|
|
link_text: link_text}));
|
2013-11-13 19:40:02 +01:00
|
|
|
exports.clear_compose_notifications();
|
|
|
|
$('#out-of-view-notification').append(notification_html);
|
|
|
|
$('#out-of-view-notification').show();
|
2013-10-09 22:42:15 +02:00
|
|
|
};
|
|
|
|
|
2013-06-19 01:41:27 +02:00
|
|
|
function process_notification(notification) {
|
2013-07-22 22:21:34 +02:00
|
|
|
var i, notification_object, key, content, other_recipients;
|
2013-06-19 01:41:27 +02:00
|
|
|
var message = notification.message;
|
2012-11-23 23:53:38 +01:00
|
|
|
var title = message.sender_full_name;
|
|
|
|
var msg_count = 1;
|
2014-02-05 17:33:07 +01:00
|
|
|
var notification_source;
|
2012-11-23 23:53:38 +01:00
|
|
|
|
2013-07-22 22:21:34 +02:00
|
|
|
// Convert the content to plain text, replacing emoji with their alt text
|
|
|
|
content = $('<div/>').html(message.content);
|
2013-07-23 00:25:25 +02:00
|
|
|
ui.replace_emoji_with_text(content);
|
2013-07-22 22:21:34 +02:00
|
|
|
content = content.text();
|
|
|
|
|
2014-03-10 16:26:39 +01:00
|
|
|
if (message.is_me_message) {
|
|
|
|
content = message.sender_full_name + content.slice(3);
|
|
|
|
}
|
|
|
|
|
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 + ", ", "");
|
2014-02-05 17:33:07 +01:00
|
|
|
notification_source = 'pm';
|
2013-01-09 23:49:54 +01:00
|
|
|
} else {
|
|
|
|
key = message.sender_full_name + " to " +
|
2013-04-18 17:13:43 +02:00
|
|
|
message.stream + " > " + message.subject;
|
2014-02-05 17:33:07 +01:00
|
|
|
if (message.mentioned) {
|
|
|
|
notification_source = 'mention';
|
|
|
|
} else if (message.alerted) {
|
|
|
|
notification_source = 'alert';
|
|
|
|
} else {
|
|
|
|
notification_source = 'stream';
|
|
|
|
}
|
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 += " [...]";
|
|
|
|
}
|
|
|
|
|
2013-05-31 22:25:39 +02:00
|
|
|
if (window.bridge === undefined && notice_memory[key] !== undefined) {
|
2012-11-23 23:53:38 +01:00
|
|
|
msg_count = notice_memory[key].msg_count + 1;
|
|
|
|
title = msg_count + " messages from " + title;
|
|
|
|
notification_object = notice_memory[key].obj;
|
2013-08-29 22:58:00 +02:00
|
|
|
cancel_notification_object(notification_object);
|
2012-11-23 23:53:38 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-06-27 22:15:00 +02:00
|
|
|
if (window.bridge === undefined && notification.webkit_notify === true) {
|
2013-06-13 23:48:23 +02:00
|
|
|
var icon_url = ui.small_avatar_url(message);
|
2013-05-31 22:25:39 +02:00
|
|
|
notice_memory[key] = {
|
2014-05-29 05:00:45 +02:00
|
|
|
obj: notifications_api.createNotification(
|
2013-06-13 23:48:23 +02:00
|
|
|
icon_url, title, content),
|
2013-08-29 22:58:00 +02:00
|
|
|
msg_count: msg_count,
|
|
|
|
message_id: message.id
|
2013-05-31 22:25:39 +02:00
|
|
|
};
|
|
|
|
notification_object = notice_memory[key].obj;
|
|
|
|
notification_object.onclick = function () {
|
|
|
|
notification_object.cancel();
|
2013-11-22 19:49:48 +01:00
|
|
|
if (feature_flags.clicking_notification_causes_narrow) {
|
|
|
|
narrow.by_subject(message.id, {trigger: 'notification'});
|
|
|
|
}
|
2013-05-31 22:25:39 +02:00
|
|
|
window.focus();
|
|
|
|
};
|
|
|
|
notification_object.onclose = function () {
|
|
|
|
delete notice_memory[key];
|
|
|
|
};
|
|
|
|
notification_object.show();
|
2013-06-27 22:15:00 +02:00
|
|
|
} else if (notification.webkit_notify === false && typeof Notification !== "undefined" && $.browser.mozilla === true) {
|
|
|
|
Notification.requestPermission(function (perm) {
|
|
|
|
if (perm === 'granted') {
|
2015-10-01 00:23:18 +02:00
|
|
|
notification_object = new Notification(title, {
|
2013-06-27 22:15:00 +02:00
|
|
|
body: content,
|
|
|
|
iconUrl: ui.small_avatar_url(message)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
in_browser_notify(message, title, content);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (notification.webkit_notify === false) {
|
|
|
|
in_browser_notify(message, title, content);
|
2013-05-31 22:25:39 +02:00
|
|
|
} else {
|
|
|
|
// Shunt the message along to the desktop client
|
2014-02-05 17:33:07 +01:00
|
|
|
window.bridge.desktopNotification(title, content, notification_source);
|
2013-05-31 22:25:39 +02:00
|
|
|
}
|
2012-11-23 23:53:38 +01:00
|
|
|
}
|
|
|
|
|
2013-08-29 22:58:00 +02:00
|
|
|
exports.close_notification = function (message) {
|
|
|
|
_.each(Object.keys(notice_memory), function (key) {
|
|
|
|
if (notice_memory[key].message_id === message.id) {
|
|
|
|
cancel_notification_object(notice_memory[key].obj);
|
|
|
|
delete notice_memory[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-03-12 22:36:13 +01:00
|
|
|
exports.speaking_at_me = function (message) {
|
2013-05-31 16:15:27 +02:00
|
|
|
if (message === undefined) {
|
2013-01-10 04:47:11 +01:00
|
|
|
return false;
|
2013-01-10 17:24:39 +01:00
|
|
|
}
|
2013-05-31 16:15:27 +02:00
|
|
|
|
|
|
|
return message.mentioned;
|
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) {
|
2014-02-05 22:56:30 +01:00
|
|
|
// Independent of the user's notification settings, are there
|
|
|
|
// properties of the message that unconditionally mean we
|
|
|
|
// shouldn't notify about it.
|
2013-10-18 19:28:29 +02:00
|
|
|
|
|
|
|
if (message.sent_by_me) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-08 22:02:02 +01:00
|
|
|
|
|
|
|
// If a message is edited multiple times, we want to err on the side of
|
|
|
|
// not spamming notifications.
|
|
|
|
if (message.notification_sent) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-09 23:13:33 +01:00
|
|
|
// @-mentions take precent over muted-ness. See Trac #1929
|
|
|
|
if (exports.speaking_at_me(message)) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-18 19:47:16 +02:00
|
|
|
if ((message.type === "stream") &&
|
|
|
|
!stream_data.in_home_view(message.stream)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((message.type === "stream") &&
|
|
|
|
muting.is_topic_muted(message.stream, message.subject)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-18 19:28:29 +02:00
|
|
|
|
2014-02-05 22:56:30 +01:00
|
|
|
// Everything else is on the table; next filter based on notification
|
|
|
|
// settings.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function should_send_desktop_notification(message) {
|
|
|
|
// For streams, send if desktop notifications are enabled for this
|
|
|
|
// stream.
|
|
|
|
if ((message.type === "stream") &&
|
|
|
|
subs.receives_desktop_notifications(message.stream)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For PMs and @-mentions, send if desktop notifications are
|
|
|
|
// enabled.
|
|
|
|
if ((message.type === "private") &&
|
|
|
|
page_params.desktop_notifications_enabled) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For alert words and @-mentions, send if desktop notifications
|
|
|
|
// are enabled.
|
|
|
|
if (alert_words.notifies(message) &&
|
|
|
|
page_params.desktop_notifications_enabled) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exports.speaking_at_me(message) &&
|
|
|
|
page_params.desktop_notifications_enabled) {
|
2013-10-18 19:28:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-02-05 22:56:30 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function should_send_audible_notification(message) {
|
|
|
|
// For streams, ding if sounds are enabled for this stream.
|
2013-10-18 19:28:29 +02:00
|
|
|
if ((message.type === "stream") &&
|
2014-02-05 22:56:30 +01:00
|
|
|
subs.receives_audible_notifications(message.stream)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For PMs and @-mentions, ding if sounds are enabled.
|
|
|
|
if ((message.type === "private") && page_params.sounds_enabled) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For alert words and @-mentions, ding if sounds are enabled.
|
|
|
|
if (alert_words.notifies(message) && page_params.sounds_enabled) {
|
2013-10-18 19:28:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-02-05 22:56:30 +01:00
|
|
|
|
|
|
|
if (exports.speaking_at_me(message) && page_params.sounds_enabled) {
|
2013-10-18 19:28:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-05-03 21:49:01 +02:00
|
|
|
}
|
|
|
|
|
2012-11-23 23:53:38 +01:00
|
|
|
exports.received_messages = function (messages) {
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(messages, function (message) {
|
2013-08-01 17:47:48 +02:00
|
|
|
if (!message_is_notifiable(message)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-19 22:58:29 +01:00
|
|
|
// checking for unread flags here is basically proxy for
|
|
|
|
// "is Zulip currently in focus". In the case of auto-scroll forever,
|
|
|
|
// we don't care
|
|
|
|
if (!unread.message_unread(message) && !page_params.autoscroll_forever) {
|
2013-08-01 17:47:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-07-02 20:33:00 +02:00
|
|
|
|
2014-01-08 22:02:02 +01:00
|
|
|
message.notification_sent = true;
|
|
|
|
|
2014-03-03 22:59:15 +01:00
|
|
|
if (should_send_desktop_notification(message)) {
|
|
|
|
if (browser_desktop_notifications_on()) {
|
|
|
|
process_notification({message: message, webkit_notify: true});
|
|
|
|
} else {
|
|
|
|
process_notification({message: message, webkit_notify: false});
|
|
|
|
}
|
2013-06-14 16:46:37 +02:00
|
|
|
}
|
2014-02-05 22:56:30 +01:00
|
|
|
if (should_send_audible_notification(message) && supports_sound) {
|
2013-06-14 16:46:37 +02:00
|
|
|
if (window.bridge !== undefined) {
|
|
|
|
window.bridge.bell();
|
|
|
|
} else {
|
|
|
|
$("#notifications-area").find("audio")[0].play();
|
2012-11-26 23:57:31 +01:00
|
|
|
}
|
2012-11-23 23:53:38 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-11-13 19:40:02 +01:00
|
|
|
function get_message_header(message) {
|
|
|
|
if (message.type === "stream") {
|
|
|
|
return message.stream + ">" + message.subject;
|
|
|
|
}
|
|
|
|
if (message.display_recipient.length > 2) {
|
|
|
|
return "group PM with " + message.display_reply_to;
|
|
|
|
}
|
2014-02-24 18:41:11 +01:00
|
|
|
if (message.reply_to === page_params.email) {
|
2013-11-13 19:40:02 +01:00
|
|
|
return "PM with yourself";
|
|
|
|
}
|
|
|
|
return "PM with " + message.display_reply_to;
|
|
|
|
}
|
|
|
|
|
2013-10-09 22:42:15 +02:00
|
|
|
exports.possibly_notify_new_messages_outside_viewport = function (messages) {
|
|
|
|
_.each(messages, function (message) {
|
|
|
|
if (message.sender_email !== page_params.email) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// queue up offscreen because of narrowed, or (secondarily) offscreen
|
|
|
|
// because it doesn't fit in the currently visible viewport
|
2013-11-13 19:40:02 +01:00
|
|
|
|
2013-10-09 22:42:15 +02:00
|
|
|
var note;
|
|
|
|
var link_class;
|
|
|
|
var link_msg_id = message.id;
|
|
|
|
var link_text;
|
|
|
|
|
|
|
|
var row = current_msg_list.get_row(message.id);
|
2013-12-09 23:15:01 +01:00
|
|
|
if (row.length === 0) {
|
2013-12-10 21:33:18 +01:00
|
|
|
if (message.type === "stream" && muting.is_topic_muted(message.stream, message.subject)) {
|
2014-01-02 20:18:45 +01:00
|
|
|
note = "Sent! Your message was sent to a topic you have muted.";
|
2013-12-10 21:33:18 +01:00
|
|
|
} else if (message.type === "stream" && !stream_data.in_home_view(message.stream)) {
|
2014-01-02 20:18:45 +01:00
|
|
|
note = "Sent! Your message was sent to a stream you have muted.";
|
2013-12-09 23:15:01 +01:00
|
|
|
} else {
|
|
|
|
// offscreen because it is outside narrow
|
|
|
|
// we can only look for these on non-search (can_apply_locally) messages
|
|
|
|
// see also: exports.notify_messages_outside_current_search
|
2014-01-02 20:18:45 +01:00
|
|
|
note = "Sent! Your message is outside your current narrow.";
|
2013-12-09 23:15:01 +01:00
|
|
|
}
|
2013-10-09 22:42:15 +02:00
|
|
|
link_class = "compose_notification_narrow_by_subject";
|
2013-11-13 19:40:02 +01:00
|
|
|
link_text = "Narrow to " + get_message_header(message);
|
2013-10-09 22:42:15 +02:00
|
|
|
} else {
|
|
|
|
// return with _.each is like continue for normal for loops.
|
|
|
|
return;
|
|
|
|
}
|
2013-11-13 19:40:02 +01:00
|
|
|
exports.notify_above_composebox(note, link_class, link_msg_id, link_text);
|
2013-10-09 22:42:15 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// for callback when we have to check with the server if a message should be in
|
|
|
|
// the current_msg_list (!can_apply_locally; a.k.a. "a search").
|
|
|
|
exports.notify_messages_outside_current_search = function (messages) {
|
|
|
|
_.each(messages, function (message) {
|
|
|
|
if (message.sender_email !== page_params.email) {
|
|
|
|
return;
|
|
|
|
}
|
2014-01-06 17:11:28 +01:00
|
|
|
exports.notify_above_composebox("Sent! Your recent message is outside the current search.",
|
2013-10-09 22:42:15 +02:00
|
|
|
"compose_notification_narrow_by_subject",
|
|
|
|
message.id,
|
2013-11-13 19:40:02 +01:00
|
|
|
"Narrow to " + get_message_header(message));
|
2013-10-09 22:42:15 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.clear_compose_notifications = function () {
|
2013-11-13 19:40:02 +01:00
|
|
|
$('#out-of-view-notification').empty();
|
|
|
|
$('#out-of-view-notification').stop(true, true);
|
|
|
|
$('#out-of-view-notification').hide();
|
2013-10-09 22:42:15 +02:00
|
|
|
};
|
|
|
|
|
2013-06-14 00:25:02 +02:00
|
|
|
$(function () {
|
|
|
|
// Shim for Cocoa WebScript exporting top-level JS
|
|
|
|
// objects instead of window.foo objects
|
|
|
|
if (typeof(bridge) !== 'undefined' && window.bridge === undefined) {
|
|
|
|
window.bridge = bridge;
|
|
|
|
}
|
2014-01-28 21:09:23 +01:00
|
|
|
|
|
|
|
$(document).on('message_id_changed', function (event) {
|
|
|
|
var old_id = event.old_id, new_id = event.new_id;
|
|
|
|
|
|
|
|
// If a message ID that we're currently storing (as a link) has changed,
|
|
|
|
// update that link as well
|
|
|
|
_.each($('#out-of-view-notification a'), function (e) {
|
|
|
|
var elem = $(e);
|
|
|
|
var msgid = elem.data('msgid');
|
|
|
|
|
|
|
|
if (msgid === old_id) {
|
|
|
|
elem.data('msgid', new_id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2013-06-14 00:25:02 +02:00
|
|
|
});
|
|
|
|
|
2013-10-09 22:42:15 +02:00
|
|
|
exports.register_click_handlers = function () {
|
2013-11-13 19:40:02 +01:00
|
|
|
$('#out-of-view-notification').on('click', '.compose_notification_narrow_by_subject', function (e) {
|
2013-10-09 22:42:15 +02:00
|
|
|
var msgid = $(e.currentTarget).data('msgid');
|
|
|
|
narrow.by_subject(msgid, {trigger: 'compose_notification'});
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
2013-11-13 19:40:02 +01:00
|
|
|
$('#out-of-view-notification').on('click', '.compose_notification_scroll_to_message', function (e) {
|
2013-10-09 22:42:15 +02:00
|
|
|
var msgid = $(e.currentTarget).data('msgid');
|
2013-11-13 19:40:02 +01:00
|
|
|
current_msg_list.select_id(msgid);
|
|
|
|
scroll_to_selected();
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
$('#out-of-view-notification').on('click', '.out-of-view-notification-close', function (e) {
|
|
|
|
exports.clear_compose_notifications();
|
2013-10-09 22:42:15 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-02-13 23:48:03 +01:00
|
|
|
exports.handle_global_notification_updates = function (notification_name, setting) {
|
|
|
|
// Update the global settings checked when determining if we should notify
|
|
|
|
// for a given message. These settings do not affect whether or not a
|
|
|
|
// particular stream should receive notifications.
|
|
|
|
if (notification_name === "enable_stream_desktop_notifications") {
|
|
|
|
page_params.stream_desktop_notifications_enabled = setting;
|
|
|
|
} else if (notification_name === "enable_stream_sounds") {
|
|
|
|
page_params.stream_sounds_enabled = setting;
|
|
|
|
} else if (notification_name === "enable_desktop_notifications") {
|
|
|
|
page_params.desktop_notifications_enabled = setting;
|
|
|
|
} else if (notification_name === "enable_sounds") {
|
|
|
|
page_params.sounds_enabled = setting;
|
|
|
|
} else if (notification_name === "enable_offline_email_notifications") {
|
|
|
|
page_params.enable_offline_email_notifications = setting;
|
|
|
|
} else if (notification_name === "enable_offline_push_notifications") {
|
|
|
|
page_params.enable_offline_push_notifications= setting;
|
|
|
|
} else if (notification_name === "enable_digest_emails") {
|
|
|
|
page_params.enable_digest_emails = setting;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-23 23:53:38 +01:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|