oembed_serializer.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # frozen_string_literal: true
  2. class OEmbedSerializer < ActiveModel::Serializer
  3. include RoutingHelper
  4. include ActionView::Helpers::TagHelper
  5. attributes :type, :version, :title, :author_name,
  6. :author_url, :provider_name, :provider_url,
  7. :cache_age, :html, :width, :height
  8. def type
  9. 'rich'
  10. end
  11. def version
  12. '1.0'
  13. end
  14. def author_name
  15. object.account.display_name.presence || object.account.username
  16. end
  17. def author_url
  18. short_account_url(object.account)
  19. end
  20. def provider_name
  21. Rails.configuration.x.local_domain
  22. end
  23. def provider_url
  24. root_url
  25. end
  26. def cache_age
  27. 86_400
  28. end
  29. def html
  30. attributes = {
  31. src: embed_short_account_status_url(object.account, object),
  32. class: 'mastodon-embed',
  33. style: 'max-width: 100%; border: 0',
  34. width: width,
  35. height: height,
  36. allowfullscreen: true,
  37. }
  38. content_tag(:iframe, nil, attributes) + content_tag(:script, nil, src: full_asset_url('embed.js', skip_pipeline: true), async: true)
  39. end
  40. def width
  41. instance_options[:width]
  42. end
  43. def height
  44. instance_options[:height]
  45. end
  46. end