Skip to content

Commit

Permalink
notification: add setting to modify favicon notificaiton
Browse files Browse the repository at this point in the history
  • Loading branch information
YJDave committed Nov 21, 2017
1 parent aa50fae commit 1b7478f
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 4 deletions.
56 changes: 56 additions & 0 deletions frontend_tests/node_tests/unread.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var zero_counts = {
private_message_count: 0,
home_unread_messages: 0,
mentioned_message_count: 0,
favicon_message_count: 0,
stream_count: new Dict(),
topic_count: new Dict(),
pm_count: new Dict(),
Expand Down Expand Up @@ -408,6 +409,61 @@ stream_data.get_stream_id = function () {
assert.equal(counts.mentioned_message_count, 0);
}());

(function test_favicon_notification() {
narrow_state.active = function () {
return false;
};
stream_data.is_subscribed = function () {
return true;
};
stream_data.maybe_get_stream_name = function (stream_id) {
if (stream_id === 100) {
return 'temp_stream';
}
};
stream_data.receives_push_notifications = function (stream_name) {
if (stream_name === 'temp_stream') {
return true;
}
};

var counts = unread.get_counts();
assert.equal(counts.favicon_message_count, 0);

var message1 = {
id: 15,
type: 'stream',
stream_id: 999,
subject: 'lunch',
alerted: true,
};

var message2 = {
id: 16,
type: 'stream',
stream_id: 999,
subject: 'lUnch',
mentioned: true,
};

var message3 = {
id: 17,
type: 'stream',
stream_id: 100,
subject: 'luNch',
};

unread.process_loaded_messages([message1, message2, message3]);

counts = unread.get_counts();
assert.equal(counts.favicon_message_count, 3);
unread.mark_as_read(message1.id);
unread.mark_as_read(message2.id);
unread.mark_as_read(message3.id);
counts = unread.get_counts();
assert.equal(counts.favicon_message_count, 0);
}());

(function test_declare_bankruptcy() {
var message = {
id: 16,
Expand Down
15 changes: 13 additions & 2 deletions static/js/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,16 @@ if (window.bridge !== undefined) {

var new_message_count = 0;

exports.update_title_count = function (count) {
new_message_count = count;
exports.update_title_count = function (res) {

if (page_params.enable_all_favicon_dekstop_notifications) {
new_message_count = res.home_unread_messages;
} else {
// Count private message && mentioned message && alert word message &&
// stream push message notifications in favicon notification
new_message_count = res.favicon_message_count;
}

exports.redraw_title();
};

Expand Down Expand Up @@ -680,7 +688,10 @@ exports.handle_global_notification_updates = function (notification_name, settin
page_params.enable_digest_emails = setting;
} else if (notification_name === "pm_content_in_desktop_notifications") {
page_params.pm_content_in_desktop_notifications = setting;
} else if (notification_name === "enable_all_favicon_dekstop_notifications") {
page_params.enable_all_favicon_dekstop_notifications = setting;
}

};

return exports;
Expand Down
1 change: 1 addition & 0 deletions static/js/settings_notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var settings_notifications = (function () {
var exports = {};

var notification_settings = [
"enable_all_favicon_dekstop_notifications",
"enable_desktop_notifications",
"enable_digest_emails",
"enable_offline_email_notifications",
Expand Down
8 changes: 8 additions & 0 deletions static/js/stream_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,14 @@ exports.receives_desktop_notifications = function (stream_name) {
return sub.desktop_notifications;
};

exports.receives_push_notifications = function (stream_name) {
var sub = exports.get_sub(stream_name);
if (sub === undefined) {
return false;
}
return sub.push_notifications;
};

exports.receives_audible_notifications = function (stream_name) {
var sub = exports.get_sub(stream_name);
if (sub === undefined) {
Expand Down
18 changes: 18 additions & 0 deletions static/js/unread.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ exports.unread_topic_counter = (function () {
}());

exports.unread_mentions_counter = make_id_set();
exports.unread_favicon_counter = make_id_set();

exports.message_unread = function (message) {
if (message === undefined) {
Expand Down Expand Up @@ -368,6 +369,7 @@ exports.process_loaded_messages = function (messages) {

if (message.type === 'private') {
exports.unread_pm_counter.add(message);
exports.unread_favicon_counter.add(message.id);
}

if (message.type === 'stream') {
Expand All @@ -376,11 +378,24 @@ exports.process_loaded_messages = function (messages) {
message.subject,
message.id
);

var stream_name = stream_data.maybe_get_stream_name(message.stream_id);
if (stream_name) {
if (stream_data.receives_push_notifications(stream_name)) {
exports.unread_favicon_counter.add(message.id);
}
}
}

if (message.mentioned) {
exports.unread_mentions_counter.add(message.id);
exports.unread_favicon_counter.add(message.id);
}

if (message.alerted) {
exports.unread_favicon_counter.add(message.id);
}

});
};

Expand All @@ -391,13 +406,15 @@ exports.mark_as_read = function (message_id) {
exports.unread_pm_counter.del(message_id);
exports.unread_topic_counter.del(message_id);
exports.unread_mentions_counter.del(message_id);
exports.unread_favicon_counter.del(message_id);
unread_messages.del(message_id);
};

exports.declare_bankruptcy = function () {
exports.unread_pm_counter.clear();
exports.unread_topic_counter.clear();
exports.unread_mentions_counter.clear();
exports.unread_favicon_counter.clear();
unread_messages.clear();
};

Expand All @@ -409,6 +426,7 @@ exports.get_counts = function () {
// should strive to keep it free of side effects on globals or DOM.
res.private_message_count = 0;
res.mentioned_message_count = exports.unread_mentions_counter.count();
res.favicon_message_count = exports.unread_favicon_counter.count();

// This sets stream_count, topic_count, and home_unread_messages
var topic_res = exports.unread_topic_counter.get_counts();
Expand Down
2 changes: 1 addition & 1 deletion static/js/unread_ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ exports.update_unread_counts = function () {
top_left_corner.update_dom_with_unread_counts(res);
stream_list.update_dom_with_unread_counts(res);
pm_list.update_dom_with_unread_counts(res);
notifications.update_title_count(res.home_unread_messages);
notifications.update_title_count(res);
notifications.update_pm_count(res.private_message_count);

exports.set_count_toggle_button($("#streamlist-toggle-unreadcount"),
Expand Down
4 changes: 4 additions & 0 deletions static/swagger/zulip.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ paths:
* `default_language`:
* `enable_all_favicon_dekstop_notifications`:
* `enable_desktop_notifications`:
* `enable_digest_emails`:
Expand Down Expand Up @@ -941,6 +943,8 @@ definitions:
type: string
default_language:
type: string
enable_all_favicon_dekstop_notifications:
type: boolean
enable_desktop_notifications:
type: boolean
enable_digest_emails:
Expand Down
20 changes: 20 additions & 0 deletions static/templates/settings/notification-settings.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,26 @@

</div>

<h3 class="light">{{t "Favicon desktop notification" }}</h3>
<p>{{t "Only include pm, @, alert, stream push notification if not all" }}</p>

<div class="side-padded-container">

<div class="input-group">
<label class="checkbox">
<input type="checkbox" name="enable_all_favicon_dekstop_notifications" id="enable_all_favicon_dekstop_notifications"
{{#if page_params.enable_all_favicon_dekstop_notifications}}
checked="checked"
{{/if}} />
<span></span>
</label>
<label for="enable_all_favicon_dekstop_notifications" class="inline-block">
{{t "All notifications" }}
</label>
</div>

</div>

<div id="other_notifications">

<h3 class="light">{{t "Other notifications I want:" }}</h3>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2017-10-16 08:20
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('zerver', '0117_add_desc_to_user_group'),
]

operations = [
migrations.AddField(
model_name='userprofile',
name='enable_all_favicon_dekstop_notifications',
field=models.BooleanField(default=True),
),
]
4 changes: 4 additions & 0 deletions zerver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,9 @@ class UserProfile(AbstractBaseUser, PermissionsMixin):

### Notifications settings. ###

# Favicon desktop notification
enable_all_favicon_dekstop_notifications = models.BooleanField(default=True) # type: bool

# Stream notifications.
enable_stream_desktop_notifications = models.BooleanField(default=False) # type: bool
enable_stream_push_notifications = models.BooleanField(default=False) # type: bool
Expand Down Expand Up @@ -670,6 +673,7 @@ class UserProfile(AbstractBaseUser, PermissionsMixin):
)

notification_setting_types = dict(
enable_all_favicon_dekstop_notifications=bool,
enable_desktop_notifications=bool,
enable_digest_emails=bool,
enable_offline_email_notifications=bool,
Expand Down
1 change: 1 addition & 0 deletions zerver/tests/test_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def test_home(self) -> None:
"emoji_alt_code",
"emojiset",
"emojiset_choices",
"enable_all_favicon_dekstop_notifications",
"enable_desktop_notifications",
"enable_digest_emails",
"enable_offline_email_notifications",
Expand Down
4 changes: 3 additions & 1 deletion zerver/views/user_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ def update_display_settings_backend(
@human_users_only
@has_request_variables
def json_change_notify_settings(request, user_profile,
enable_all_favicon_dekstop_notifications=REQ(validator=check_bool,
default=None),
enable_stream_desktop_notifications=REQ(validator=check_bool,
default=None),
enable_stream_push_notifications=REQ(validator=check_bool,
Expand All @@ -192,7 +194,7 @@ def json_change_notify_settings(request, user_profile,
default=None),
pm_content_in_desktop_notifications=REQ(validator=check_bool,
default=None)):
# type: (HttpRequest, UserProfile, Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool]) -> HttpResponse
# type: (HttpRequest, UserProfile, Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool]) -> HttpResponse
result = {}

# Stream notification settings.
Expand Down

0 comments on commit 1b7478f

Please sign in to comment.