Skip to content

Commit

Permalink
add hidden setting to default observer notifications off
Browse files Browse the repository at this point in the history
test plan:
* run the following in a rails console:
 Account.default.tap do |a|
   a.settings[default_notifications_disabled_for_observers] = true
   a.save!
 end
* use a sis batch import with user_observers.csv to create
 an observation link for a student and observer
* as the observer, view the "Notifications" section of their
 account settings
* they should all default to "Never"

closes #COMMS-1898

Change-Id: Iabf5bff42efa521bdad34ccf7de277cee0e1789c
Reviewed-on: https://gerrit.instructure.com/184022
Tested-by: Jenkins
Reviewed-by: Landon Gilbert-Bland <[email protected]>
Reviewed-by: Steven Burnett <[email protected]>
QA-Review: Steven Burnett <[email protected]>
Product-Review: Steven Burnett <[email protected]>
  • Loading branch information
maneframe authored and sdb1228 committed Mar 7, 2019
1 parent e3481fa commit 6f5944f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ def related_user_setting(user, root_account)
end
end

def default_frequency(_user = nil)
# user arg is used in plugins
def default_frequency(user = nil)
return FREQ_NEVER if user&.default_notifications_disabled?
case category
when 'All Submissions'
FREQ_NEVER
Expand Down
10 changes: 10 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,16 @@ def create_announcements_unlocked(bool)
preferences[:create_announcements_unlocked] = bool
end

def default_notifications_disabled=(val)
# if this is set then all notifications will be disabled by default
# for the user and will need to be explicitly enabled
preferences[:default_notifications_disabled] = val
end

def default_notifications_disabled?
!!preferences[:default_notifications_disabled]
end

def use_new_conversations?
true
end
Expand Down
11 changes: 6 additions & 5 deletions lib/notification_message_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,11 @@ def delayed_policies_for(user, channel=user.email_channel)
policies += unretired_policies_for(user).for(@notification).by(['daily', 'weekly']).where("communication_channels.path_type='email'")
elsif channel &&
channel.active? &&
channel.path_type == 'email' &&
['daily', 'weekly'].include?(@notification.default_frequency)
policies << channel.notification_policies.create!(:notification => @notification,
:frequency => @notification.default_frequency)
channel.path_type == 'email'
frequency = @notification.default_frequency(user)
if ['daily', 'weekly'].include?(frequency)
policies << channel.notification_policies.create!(:notification => @notification, :frequency => frequency)
end
end
policies
end
Expand Down Expand Up @@ -284,7 +285,7 @@ def immediate_channels_for(user)
immediate_channel_scope = user.communication_channels.active.for_notification_frequency(@notification, 'immediately')

user_has_a_policy = active_channel_scope.where.not(path_type: 'push').exists?
if !user_has_a_policy && @notification.default_frequency == 'immediately'
if !user_has_a_policy && @notification.default_frequency(user) == 'immediately'
return [user.email_channel, *immediate_channel_scope.where(path_type: 'push')].compact
end
immediate_channel_scope
Expand Down
7 changes: 7 additions & 0 deletions lib/sis/user_observer_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def process_user_observer(observer_id, student_id, status)
def add_remove_observer(observer, student, observer_id, student_id, status)
case status.downcase
when 'active'
check_observer_notification_settings(observer)
user_observer = UserObservationLink.create_or_restore(observer: observer, student: student, root_account: @root_account)
when 'deleted'
user_observer = observer.as_observer_observation_links.for_root_accounts(@root_account).where(user_id: student).take
Expand All @@ -96,6 +97,12 @@ def add_remove_observer(observer, student, observer_id, student_id, status)
@success_count += 1
end

def check_observer_notification_settings(observer)
if @root_account.settings[:default_notifications_disabled_for_observers]
observer.default_notifications_disabled = true
observer.save if observer.changed?
end
end
end
end
end
16 changes: 16 additions & 0 deletions spec/lib/notification_message_creator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,22 @@ def notification_set(opts = {})
expect(DelayedMessage.last.send_at).to be > Time.now.utc
end

it "should be able to set default policies to never for a specific user" do
notification_model(:category => 'TestImmediately', :name => "New notification")
expect(@notification.default_frequency).to eql("immediately")

u1 = user_model(:name => "user 1", :workflow_state => "registered")
u1.default_notifications_disabled = true
u1.save!
cc = u1.communication_channels.create(:path => "[email protected]", :workflow_state => 'active')
u2 = user_model(:name => "user 2", :workflow_state => "registered")
cc2 = u2.communication_channels.create(:path => "[email protected]", :workflow_state => 'active')

@a = assignment_model
messages = NotificationMessageCreator.new(@notification, @a, :to_list => [u1, u2]).create_message
expect(messages.map(&:communication_channel).compact).to eq [cc2] # doesn't include u1's cc
end

it "should make a delayed message for each user policy with a delayed frequency" do
notification_set
NotificationPolicy.delete_all
Expand Down
13 changes: 13 additions & 0 deletions spec/lib/sis/csv/user_observer_importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,17 @@
)
expect(UserObservationLink.active.count).to eq before_count
end

it "should be able to disable notifications by default for observers" do
@account.settings[:default_notifications_disabled_for_observers] = true
@account.save!
observer = user_with_managed_pseudonym(account: @account, sis_user_id: 'U001')
user_with_managed_pseudonym(account: @account, sis_user_id: 'U002')
before_count = UserObservationLink.active.count
process_csv_data_cleanly(
"observer_id,student_id,status",
"U001,U002,ACTIVE"
)
expect(observer.reload.default_notifications_disabled?).to eq true
end
end

0 comments on commit 6f5944f

Please sign in to comment.