闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
870 B

7 years ago
  1. # frozen_string_literal: true
  2. class Api::OEmbedController < ApiController
  3. respond_to :json
  4. def show
  5. @stream_entry = stream_entry_from_url(params[:url])
  6. @width = params[:maxwidth].present? ? params[:maxwidth].to_i : 400
  7. @height = params[:maxheight].present? ? params[:maxheight].to_i : nil
  8. end
  9. private
  10. def stream_entry_from_url(url)
  11. params = Rails.application.routes.recognize_path(url)
  12. raise ActiveRecord::RecordNotFound unless recognized_stream_entry_url?(params)
  13. stream_entry(params)
  14. end
  15. def recognized_stream_entry_url?(params)
  16. %w(stream_entries statuses).include?(params[:controller]) && params[:action] == 'show'
  17. end
  18. def stream_entry(params)
  19. if params[:controller] == 'stream_entries'
  20. StreamEntry.find(params[:id])
  21. else
  22. Status.find(params[:id]).stream_entry
  23. end
  24. end
  25. end