diff --git a/app/controllers/bookmarklets_controller.rb b/app/controllers/bookmarklets_controller.rb index d4d9dac7..ec5f85f7 100644 --- a/app/controllers/bookmarklets_controller.rb +++ b/app/controllers/bookmarklets_controller.rb @@ -88,6 +88,17 @@ def add_item end @feed_item.add_tags(new_tags, @hub.tagging_key, current_user) + if new_tags + Sidekiq::Client.enqueue( + SendItemChangeNotifications, + 'FeedItem', + @feed_item.id, + @hub.id, + current_user.id, + @feed_item.id + ) + end + @feed_item.reload.solr_index format.html do diff --git a/app/controllers/tag_filters_controller.rb b/app/controllers/tag_filters_controller.rb index 127cf243..111b4f31 100644 --- a/app/controllers/tag_filters_controller.rb +++ b/app/controllers/tag_filters_controller.rb @@ -72,6 +72,7 @@ def create Sidekiq::Client.enqueue( SendItemChangeNotifications, + 'TagFilter', @tag_filter.id, @hub.id, current_user.id, diff --git a/app/models/feed_item.rb b/app/models/feed_item.rb index 3affeff8..8383717e 100644 --- a/app/models/feed_item.rb +++ b/app/models/feed_item.rb @@ -306,6 +306,62 @@ def self.apply_tag_filters(item_id, hub_ids = []) feed_item.apply_tag_filters(hub_ids) end + # Informing taggers about changes in their items + def notify_about_items_modification(hub, current_user, items_to_process_joined) + # Get configs for notifications + hub_user_notifications_setup = HubUserNotification.where(hub_id: hub) + + # tag filter creators + users_to_notify = [] + tag_filters_applied = TagFilter.where( + id: taggings.where( + tagger_type: 'TagFilter' + ).pluck(:tagger_id) + ) + + # find and match filters owners + tag_filters_applied.each do |tag_filter| + users_to_notify.concat(Role.where( + authorizable_id: tag_filter, + authorizable_type: 'TagFilter' + ).first.users) + end + + # simple taggers + taggings = ActsAsTaggableOn::Tagging.where( + tagger_type: 'User', + taggable_id: id, + taggable_type: 'FeedItem', + context: 'hub_' + hub.id.to_s + ) + + users_to_notify += User.where( + id: taggings.pluck(:tagger_id) + ) + + users_to_notify_allowed = [] + users_to_notify = users_to_notify.uniq - [current_user] + users_to_notify.each do |user| + user_setup = hub_user_notifications_setup.select do |setup| + setup.user_id == user.id + end + + # check if a user wants to reveive notifications + if !user_setup.empty? && user_setup.first.notify_about_modifications + users_to_notify_allowed << user + end + end + + unless users_to_notify_allowed.empty? + Notifications.item_change_notification( + hub, + self, + users_to_notify_allowed, + current_user + ).deliver_later + end + end + private def parse_out_image_url diff --git a/app/models/tag_filter.rb b/app/models/tag_filter.rb index 3827b898..9fbdfcd7 100644 --- a/app/models/tag_filter.rb +++ b/app/models/tag_filter.rb @@ -249,7 +249,6 @@ def notify_about_items_modification(hub, current_user, items_to_process_joined) hub_user_notifications_setup = HubUserNotification.where(hub_id: hub) items_to_process = if items_to_process_joined == 'reprocess' - logger.debug('XXXW - ' + items_to_process_joined) items_to_modify else FeedItem.where(id: items_to_process_joined.split(',')) @@ -257,9 +256,12 @@ def notify_about_items_modification(hub, current_user, items_to_process_joined) # loop through scoped items items_to_process.each do |modified_item| + # tag filter creators users_to_notify = [] tag_filters_applied = TagFilter.where( - id: modified_item.taggings.pluck(:tagger_id) + id: modified_item.taggings.where( + tagger_type: 'TagFilter' + ).pluck(:tagger_id) ) # find and match filters owners @@ -270,6 +272,18 @@ def notify_about_items_modification(hub, current_user, items_to_process_joined) ).first.users) end + # simple taggers + taggings = ActsAsTaggableOn::Tagging.where( + tagger_type: 'User', + taggable_id: modified_item.id, + taggable_type: 'FeedItem', + context: 'hub_' + hub.id.to_s + ) + + users_to_notify += User.where( + id: taggings.pluck(:tagger_id) + ) + users_to_notify_allowed = [] users_to_notify = users_to_notify.uniq - [current_user] users_to_notify.each do |user| diff --git a/app/workers/send_item_change_notifications.rb b/app/workers/send_item_change_notifications.rb index ba5305fd..f64903d9 100644 --- a/app/workers/send_item_change_notifications.rb +++ b/app/workers/send_item_change_notifications.rb @@ -6,11 +6,12 @@ def self.display_name 'Sending an email notification of a modified item' end - def perform(tag_filter_id, hub_id, current_user_id, items_to_process) - tag_filter = TagFilter.find(tag_filter_id) + def perform(scope_class, scope_id, hub_id, current_user_id, items_to_process) hub = Hub.find(hub_id) user = User.find(current_user_id) + scope_model = scope_class.constantize + scope = scope_model.find(scope_id) - tag_filter.notify_about_items_modification(hub, user, items_to_process) + scope.notify_about_items_modification(hub, user, items_to_process) end end