Class: UnsubscribeUserWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
app/workers/unsubscribe_user_worker.rb

Overview

Background job to unsubscribe a user from a feed

This is a Sidekiq worker

Instance Method Summary collapse

Instance Method Details

#perform(user_id, feed_id) ⇒ Object

Unsubscribe a user from a feed

Receives as arguments:

  • id of the user

  • id of the feed

This method is intended to be invoked from Sidekiq, which means it is performed in the background.


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/workers/unsubscribe_user_worker.rb', line 20

def perform(user_id, feed_id)
  # Check if the user actually exists
  if !User.exists? user_id
    Rails.logger.error "Trying to unsubscribe non-existing user @#{user_id}, aborting job"
    return
  end
  user = User.find user_id

  # Check if the feed actually exists and is subscribed by the user
  if !Feed.exists? feed_id
    Rails.logger.error "Trying to unsubscribe user #{user.id} - #{user.email} from non-existing feed #{feed_id}, aborting job"
    return
  end
  feed = Feed.find feed_id
  if !user.feeds.include? feed
    Rails.logger.error "Trying to unsubscribe user #{user.id} - #{user.email} from feed #{feed.id} - #{feed.title} to which he's not subscribed, aborting job"
    return
  end

  Rails.logger.info "Unsubscribing user #{user.id} - #{user.email} from feed #{feed.id} - #{feed.title}"
  user.unsubscribe feed
end