Sha256: a516b9bd1dadb89c1afe0f0c2d25a66845e89316d02f2bbf7f3ba85227ed7a21

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

# A generic job for sending events to a user and their followers.
#
# This class does not implement a usable action, so it must be implemented in a child class
#
# @attr [String] depositor_id  the user the event is specified for
#
class EventJob
  include Rails.application.routes.url_helpers
  include ActionView::Helpers
  include ActionView::Helpers::DateHelper
  include Hydra::AccessControlsEnforcement
  include SufiaHelper

  # queue to run the job on
  def queue_name
    :event
  end

  attr_accessor :depositor_id

  # @param the id of the user to create the event for
  def initialize(depositor_id)
    @depositor_id = depositor_id
  end

  # Method used to cause the event to be created
  def run
    # Log the event to the depositor's profile stream
    log_user_event

    # Fan out the event to all followers who have access
    log_to_followers
  end

  # override to provide your specific action for the event you are logging
  # @abstract
  def action
    raise(NotImplementedError, "#action should be implemented by an child class of EventJob")
  end

  # create an event with an action and a timestamp for the user
  def event
    @event ||= depositor.create_event(action, Time.now.to_i)
  end

  # the user that will be the subject of the event
  def depositor
    @depositor ||= User.find_by_user_key(depositor_id)
  end

  # log the event to the users event stream
  def log_user_event
    depositor.log_event(event)
  end

  # log the event to the users followers
  def log_to_followers
    depositor.followers.each do |follower|
      follower.log_event(event)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sufia-6.7.0 app/jobs/event_job.rb
sufia-6.6.1 app/jobs/event_job.rb
sufia-6.6.0 app/jobs/event_job.rb
sufia-6.5.0 app/jobs/event_job.rb
sufia-6.4.0 app/jobs/event_job.rb