Skip to content

Commit

Permalink
Improved a process of notifying users about their items change
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Hankiewicz committed Mar 24, 2017
1 parent c9826ab commit 4a8f745
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 5 deletions.
11 changes: 11 additions & 0 deletions app/controllers/bookmarklets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/controllers/tag_filters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def create

Sidekiq::Client.enqueue(
SendItemChangeNotifications,
'TagFilter',
@tag_filter.id,
@hub.id,
current_user.id,
Expand Down
56 changes: 56 additions & 0 deletions app/models/feed_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions app/models/tag_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,19 @@ 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(','))
end

# 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
Expand All @@ -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|
Expand Down
7 changes: 4 additions & 3 deletions app/workers/send_item_change_notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4a8f745

Please sign in to comment.