Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/activity_notification/apis/notification_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,20 @@ def notify(target_type, notifiable, options = {})
if options[:notify_later]
notify_later(target_type, notifiable, options)
else
targets = notifiable.notification_targets(target_type, options[:pass_full_options] ? options : options[:key])
potential_targets = notifiable.notification_targets(target_type, options[:pass_full_options] ? options : options[:key])
subscribed_targets = []
key = options[:key] || notifiable.default_notification_key
if potential_targets.respond_to?(:find_each)
potential_targets.find_each do |target|
subscribed_targets << target if target.subscribes_to_notification?(key)
end
else
subscribed_targets = potential_targets.select { |target| target.subscribes_to_notification?(key) }
end
subscribed_targets.append(*notifiable.instance_subscription_targets(target_type))
# Optimize blank check to avoid loading all records for ActiveRecord relations
unless targets_empty?(targets)
notify_all(targets, notifiable, options)
unless targets_empty?(subscribed_targets)
notify_all(subscribed_targets, notifiable, options)
end
end
end
Expand Down Expand Up @@ -413,10 +423,7 @@ def notify_later_to(target, notifiable, options = {})
# @option options [Hash] :parameters ({}) Additional parameters of the notifications
def generate_notification(target, notifiable, options = {})
key = options[:key] || notifiable.default_notification_key
if target.subscribes_to_notification?(key)
# Store notification
notification = store_notification(target, notifiable, key, options)
end
store_notification(target, notifiable, key, options)
end

# Opens all notifications of the target.
Expand Down
4 changes: 4 additions & 0 deletions lib/activity_notification/models/concerns/notifiable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def notification_targets(target_type, options = {})
resolved_parameter
end

def instance_subscription_targets(target_type)
Subscription.where(target_type: target_type, notifiable_type: self.class.name, notifiable_id: self.id).map(&:target)
end

# Returns group unit of the notifications from configured field or overridden method.
# This method is able to be overridden.
#
Expand Down
2 changes: 1 addition & 1 deletion lib/activity_notification/models/concerns/subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def notification_keys(options = {})
# @param [Boolean] subscribe_as_default Default subscription value to use when the subscription record does not configured
# @return [Boolean] If the target subscribes to the notification
def _subscribes_to_notification?(key, subscribe_as_default = ActivityNotification.config.subscribe_as_default)
evaluate_subscription(subscriptions.where(key: key).first, :subscribing?, subscribe_as_default)
evaluate_subscription(subscriptions.where(key: key, notifiable_type: nil).first, :subscribing?, subscribe_as_default)
end

# Returns if the target subscribes to the notification email.
Expand Down
5 changes: 4 additions & 1 deletion lib/activity_notification/models/concerns/target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,10 @@ def opened_notification_index(options = {})
# @option options [Hash<String, Hash>] :optional_targets ({}) Options for optional targets, keys are optional target name (:amazon_sns or :slack etc) and values are options
# @return [Notification] Generated notification instance
def receive_notification_of(notifiable, options = {})
Notification.notify_to(self, notifiable, options)
key = options[:key] || notifiable.default_notification_key
if self.subscribes_to_notification?(key)
Notification.notify_to(self, notifiable, options)
end
end
alias_method :receive_notification_now_of, :receive_notification_of

Expand Down
2 changes: 2 additions & 0 deletions lib/generators/templates/migrations/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def change

<% if @migration_tables.include?('subscriptions') %>create_table :subscriptions do |t|
t.belongs_to :target, polymorphic: true, index: true, null: false
t.belongs_to :notifiable, polymorphic: true, index: true
t.string :key, index: true, null: false
t.boolean :subscribing, null: false, default: true
t.boolean :subscribing_to_email, null: false, default: true
Expand All @@ -41,6 +42,7 @@ def change
end
add_index :subscriptions, [:target_type, :target_id, :key], unique: true<% else %># create_table :subscriptions do |t|
# t.belongs_to :target, polymorphic: true, index: true, null: false
# t.belongs_to :notifiable, polymorphic: true, index: true
# t.string :key, index: true, null: false
# t.boolean :subscribing, null: false, default: true
# t.boolean :subscribing_to_email, null: false, default: true
Expand Down
2 changes: 1 addition & 1 deletion spec/concerns/models/target_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ def custom_printable_name
describe "#receive_notification_of" do
it "is an alias of ActivityNotification::Notification.notify_to" do
expect(ActivityNotification::Notification).to receive(:notify_to)
test_instance.receive_notification_of create(:user)
test_instance.receive_notification_of test_notifiable
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def change

create_table :subscriptions do |t|
t.belongs_to :target, polymorphic: true, index: true, null: false
t.belongs_to :notifiable, polymorphic: true, index: true
t.string :key, index: true, null: false
t.boolean :subscribing, null: false, default: true
t.boolean :subscribing_to_email, null: false, default: true
Expand Down
Loading