Skip to content

Commit d8be1db

Browse files
committed
show configured state of preference even if no tokens present #8
1 parent 1f12198 commit d8be1db

File tree

5 files changed

+19
-14
lines changed

5 files changed

+19
-14
lines changed

lib/redmine_expo_push/notification.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ def deliver
3333
end
3434
messages.flatten!
3535

36-
begin
37-
Exponent::Push::Client.new(gzip: true).publish messages
38-
rescue Exception
39-
Rails.logger.error "error sending push notifications:\n#{$!}\n" + $!.backtrace.join("\n")
36+
if messages.any?
37+
begin
38+
Exponent::Push::Client.new(gzip: true).publish messages
39+
rescue Exception
40+
Rails.logger.error "error sending push notifications:\n#{$!}\n" + $!.backtrace.join("\n")
41+
end
4042
end
4143
end
4244
end

lib/redmine_expo_push/patches/user_patch.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def send_push_notifications?
2121
end
2222

2323
def push_skip_emails?
24-
pref.push_notifications == "enabled_no_email"
24+
# only skip emails when at least one device is registered
25+
pref.push_notifications == "enabled_no_email" and push_device_registered?
2526
end
2627

2728
def remove_references_before_destroy

lib/redmine_expo_push/patches/user_preference_patch.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ def self.apply
1313
end
1414

1515
def push_notifications
16-
if user.push_device_registered?
17-
self[:push_notifications] || "enabled_no_email"
18-
else
19-
"disabled"
20-
end
16+
self[:push_notifications] || "enabled_no_email"
2117
end
2218

2319
def push_notifications=(new_value)

test/units/notification_test.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,22 @@ class NotificationTest < ActiveSupport::TestCase
1717
assert_match(/released/, n.body)
1818
end
1919

20-
test "should send notification instead of email" do
20+
test "should send notification instead of email when token present" do
2121
Exponent::Push::Client.any_instance.expects(:publish)
2222

23-
# no token, email is sent
23+
assert_equal 'enabled_no_email', @user.pref.push_notifications
24+
assert @user.send_push_notifications?
25+
refute @user.push_skip_emails?
26+
27+
# no token, email is still sent
2428
mail = Mailer.news_added @user, @news
2529
mail.deliver
2630
assert_equal 1, @emails.size
2731

2832
@emails.clear
2933
@user.expo_push_tokens.create! token: "foo"
34+
assert @user.send_push_notifications?
35+
assert @user.push_skip_emails?
3036

3137
# token is set, push notification but no email is sent
3238
mail = Mailer.news_added @user, @news

test/units/user_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class UserTest < ActiveSupport::TestCase
77
@user = User.find_by_login "jsmith"
88
end
99

10-
test "should default to no push notifs" do
11-
refute @user.send_push_notifications?
10+
test "should not skip emails when no tokens present" do
11+
assert @user.send_push_notifications?
1212
refute @user.push_skip_emails?
1313
end
1414

0 commit comments

Comments
 (0)