Simplify `notify` and `denotify` methods

This commit is contained in:
Karina Kwiatek 2023-03-05 13:47:46 +01:00
parent 0132d7b251
commit f73fc87991
1 changed files with 9 additions and 12 deletions

View File

@ -36,23 +36,20 @@ class Subscription < ApplicationRecord
end end
def notify(source, target) def notify(source, target)
if source.nil? or target.nil? return nil if source.nil? || target.nil?
return nil
notifications = Subscription.where(answer: target).where.not(user: target.user).map do |s|
{ target_id: source.id, target_type: Comment, recipient_id: s.user_id, new: true, type: Notification::Commented }
end end
Subscription.where(answer: target).each do |subs| Notification.insert_all!(notifications)
next unless not subs.user == source.user
Notification.notify subs.user, source
end
end end
def denotify(source, target) def denotify(source, target)
if source.nil? or target.nil? return nil if source.nil? or target.nil?
return nil
end subs = Subscription.where(answer: target)
Subscription.where(answer: target).each do |subs| Notification.where(target:, recipient: subs.map(&:user)).delete_all
Notification.denotify subs.user, source
end
end end
end end
end end