Class: Api::OpmlImportsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/api/opml_imports_controller.rb

Overview

Controller to import subscriptions data

Instance Method Summary collapse

Instance Method Details

#createObject

Upload a subscriptions data file (probably exported from another feed reader) and subscribe the current user to the feeds there.


38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/api/opml_imports_controller.rb', line 38

def create
  file = opml_import_params[:file]
  current_user.import_subscriptions file.tempfile
rescue => e
  Rails.logger.error "Error importing OPML for user #{current_user.id} - #{current_user.email}"
  Rails.logger.error e.message
  Rails.logger.error e.backtrace
  opml_import_job_state = current_user.create_opml_import_job_state state: OpmlImportJobState::ERROR
ensure
  redirect_to read_path
end

#showObject

Return JSON indicating the state of the “import subscriptions” process for the current user


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/api/opml_imports_controller.rb', line 16

def show
  if OpmlImportJobState.exists? user_id: current_user.id
    @opml_import_job_state = OpmlImportJobState.find_by user_id: current_user.id
  else
    Rails.logger.warn "User #{current_user.id} - #{current_user.email} has no OpmlImportJobState, creating one with state NONE"
    @opml_import_job_state = current_user.create_opml_import_job_state state: OpmlImportJobState::NONE
  end

  # If opml export state has not changed, return a 304
  if stale? etag: EtagCalculator.etag(@opml_import_job_state.updated_at),
            last_modified: @opml_import_job_state.updated_at
    Rails.logger.debug "OpmlImportJobState for user #{current_user.id} - #{current_user.email}: id #{@opml_import_job_state&.id}, state #{@opml_import_job_state&.state}"
    respond_with @opml_import_job_state
  end
rescue => e
  handle_error e
end

#updateObject

Update the OpmlImportJobState for the current user. Currently the only supported change is showing or hiding the alert displaying the state of the process.


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/api/opml_imports_controller.rb', line 54

def update
  @opml_import_job_state = current_user.opml_import_job_state
  # Only if the string "false" is sent, set visibility to false. If anything else
  # is sent in the :show_alert request parameter, set visibility to true. This is the
  # safest default.
  if opml_import_params[:show_alert]=='false'
    show_alert = false
  else
    show_alert = true
  end
  current_user.set_opml_import_job_state_visible show_alert
  head :ok
rescue => e
  handle_error e
end