discourse/app/controllers/onebox_controller.rb
Krzysztof Kotlarek 427d54b2b0 DEV: Upgrading Discourse to Zeitwerk (#8098)
Zeitwerk simplifies working with dependencies in dev and makes it easier reloading class chains. 

We no longer need to use Rails "require_dependency" anywhere and instead can just use standard 
Ruby patterns to require files.

This is a far reaching change and we expect some followups here.
2019-10-02 14:01:53 +10:00

45 lines
1.1 KiB
Ruby

# frozen_string_literal: true
class OneboxController < ApplicationController
requires_login
def show
unless params[:refresh] == 'true'
preview = Oneboxer.cached_preview(params[:url])
preview = preview.strip if preview.present?
return render(plain: preview) if preview.present?
end
# only 1 outgoing preview per user
return render(body: nil, status: 429) if Oneboxer.is_previewing?(current_user.id)
user_id = current_user.id
category_id = params[:category_id].to_i
topic_id = params[:topic_id].to_i
invalidate = params[:refresh] == 'true'
url = params[:url]
hijack do
Oneboxer.preview_onebox!(user_id)
preview = Oneboxer.preview(url,
invalidate_oneboxes: invalidate,
user_id: user_id,
category_id: category_id,
topic_id: topic_id
)
preview = preview.strip if preview.present?
Oneboxer.onebox_previewed!(user_id)
if preview.blank?
render body: nil, status: 404
else
render plain: preview
end
end
end
end