From f6a6a6b22026cd8963ef83dcf8b4d6d7b718dec0 Mon Sep 17 00:00:00 2001 From: Zev Benjamin Date: Mon, 8 Apr 2013 20:14:13 -0400 Subject: [PATCH] Add per-stream desktop notifications (imported from commit b4a0576847b3aec1495f017ca9805febe80c9275) --- zephyr/lib/actions.py | 3 ++- zephyr/static/js/notifications.js | 14 +++++++---- zephyr/static/js/subs.js | 24 +++++++++++++++++-- .../static/templates/subscription.handlebars | 4 ++++ zephyr/tests.py | 2 +- zephyr/views.py | 3 ++- 6 files changed, 41 insertions(+), 9 deletions(-) diff --git a/zephyr/lib/actions.py b/zephyr/lib/actions.py index c1f4e54755..f9bccb2886 100644 --- a/zephyr/lib/actions.py +++ b/zephyr/lib/actions.py @@ -737,7 +737,8 @@ def gather_subscriptions(user_profile): result.append({'name': stream_name, 'in_home_view': sub.in_home_view, 'invite_only': invite_only, - 'color': sub.color}) + 'color': sub.color, + 'notifications': sub.notifications}) return sorted(result) diff --git a/zephyr/static/js/notifications.js b/zephyr/static/js/notifications.js index b7f539f3a2..c68c1b7bd8 100644 --- a/zephyr/static/js/notifications.js +++ b/zephyr/static/js/notifications.js @@ -191,6 +191,15 @@ exports.speaking_at_me = function (message) { return found_match; }; +function should_show_notification(message) { + return page_params.desktop_notifications_enabled && + browser_desktop_notifications_on() && + (message.type === "private" || + exports.speaking_at_me(message) || + (message.type === "stream" && + subs.receives_notifications(message.display_recipient))); +} + exports.received_messages = function (messages) { var i, title_needs_update = false; if (window_has_focus) { @@ -201,10 +210,7 @@ exports.received_messages = function (messages) { if (message.sender_email !== page_params.email && narrow.message_in_home(message)) { title_needs_update = true; - if (page_params.desktop_notifications_enabled && - browser_desktop_notifications_on() && - (message.type === "private" || - exports.speaking_at_me(message))) { + if (should_show_notification(message)) { process_desktop_notification(message); } } diff --git a/zephyr/static/js/subs.js b/zephyr/static/js/subs.js index 162746ab90..f1026357bc 100644 --- a/zephyr/static/js/subs.js +++ b/zephyr/static/js/subs.js @@ -182,6 +182,15 @@ function stream_home_view_clicked(e) { set_stream_property(stream, 'in_home_view', sub.in_home_view); } +function stream_notifications_clicked(e) { + var sub_row = $(e.target).closest('.subscription_row'); + var stream = sub_row.find('.subscription_name').text(); + + var sub = get_sub(stream); + sub.notifications = ! sub.notifications; + set_stream_property(stream, 'notifications', sub.notifications); +} + function set_color(stream_name, color) { update_stream_color(stream_name, color, {update_historical: true}); set_stream_property(stream_name, 'color', color); @@ -214,7 +223,8 @@ function create_sub(stream_name, attrs) { sub = $.extend({}, {name: stream_name, color: pick_color(), id: next_sub_id++, render_subscribers: should_render_subscribers(), - subscribed: true, in_home_view: true, invite_only: false}, attrs); + subscribed: true, in_home_view: true, invite_only: false, + notifications: false}, attrs); add_sub(stream_name, sub); if (sub.subscribed) { @@ -417,6 +427,14 @@ exports.get_invite_only = function (stream_name) { return sub.invite_only; }; +exports.receives_notifications = function (stream_name) { + var sub = get_sub(stream_name); + if (sub === undefined) { + return false; + } + return sub.notifications; +}; + function populate_subscriptions(subs) { var sub_rows = []; subs.sort(function (a, b) { @@ -425,7 +443,8 @@ function populate_subscriptions(subs) { subs.forEach(function (elem) { var stream_name = elem.name; var sub = create_sub(stream_name, {color: elem.color, in_home_view: elem.in_home_view, - invite_only: elem.invite_only, subscribed: true}); + invite_only: elem.invite_only, + notifications: elem.notifications, subscribed: true}); add_sub(stream_name, sub); sub_rows.push(sub); }); @@ -734,6 +753,7 @@ $(function () { } }); $("#subscriptions_table").on("click", "#sub_setting_in_home_view", stream_home_view_clicked); + $("#subscriptions_table").on("click", "#sub_setting_notifications", stream_notifications_clicked); if (! should_render_subscribers()) { return; diff --git a/zephyr/static/templates/subscription.handlebars b/zephyr/static/templates/subscription.handlebars index 9851e9a779..4aa4759bf9 100644 --- a/zephyr/static/templates/subscription.handlebars +++ b/zephyr/static/templates/subscription.handlebars @@ -30,6 +30,10 @@ Include in home view +
  • + + Show desktop notifications for traffic on this stream +
  • Stream color
  • diff --git a/zephyr/tests.py b/zephyr/tests.py index d9b661289d..941d6de74f 100644 --- a/zephyr/tests.py +++ b/zephyr/tests.py @@ -652,7 +652,7 @@ class SubscriptionPropertiesTest(AuthedTestCase): new_subs = gather_subscriptions(self.get_user_profile(test_email)) sub = {'name': stream_name, 'in_home_view': True, 'color': new_color, - 'invite_only': invite_only} + 'invite_only': invite_only, 'notifications': False} self.assertIn(sub, new_subs) new_subs.remove(sub) diff --git a/zephyr/views.py b/zephyr/views.py index 9997e0ee8b..43bfab912e 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -1150,7 +1150,8 @@ def json_subscription_property(request, user_profile, stream_name=REQ, properties. """ property_converters = dict(color=lambda x: x, - in_home_view=json_to_bool) + in_home_view=json_to_bool, + notifications=json_to_bool) if property not in property_converters: return json_error("Unknown subscription property: %s" % (property,))