Class: Api::OpmlExportsController

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

Overview

Controller to export subscriptions data

Instance Method Summary collapse

Instance Method Details

#createObject

Export a user's subscriptions in OPML format.


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

def create
  current_user.export_subscriptions
rescue => e
  Rails.logger.error "Error exporting OPML data for user #{current_user.email} - #{current_user.name}"
  Rails.logger.error e.message
  Rails.logger.error e.backtrace
  current_user.create_opml_export_job_state state: OpmlExportJobState::ERROR
ensure
  redirect_to read_path
end

#downloadObject

Download the OPML file previously exported by a user.


72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/api/opml_exports_controller.rb', line 72

def download
  @data = current_user.get_opml_export
  send_data @data, filename: OPMLExporter::FILENAME, type: 'application/xml', disposition: 'attachment', status: '200'
rescue => e
  Rails.logger.error "Error downloading OPML export file for user #{current_user.email} - #{current_user.name}"
  Rails.logger.error e.message
  Rails.logger.error e.backtrace
  flash[:alert] = t 'read.alerts.opml_export.download_error'
  redirect_to read_path
end

#showObject

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


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

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

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

#updateObject

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


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

def update
  @opml_export_job_state = current_user.opml_export_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_export_job_state_params[:show_alert]=='false'
    show_alert = false
  else
    show_alert = true
  end
  current_user.set_opml_export_job_state_visible show_alert
  head :ok
rescue => e
  handle_error e
end