media_component_helper.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # frozen_string_literal: true
  2. module MediaComponentHelper
  3. def render_video_component(status, **)
  4. video = status.ordered_media_attachments.first
  5. meta = video.file.meta || {}
  6. component_params = {
  7. sensitive: sensitive_viewer?(status, current_account),
  8. src: full_asset_url(video.file.url(:original)),
  9. preview: full_asset_url(video.thumbnail.present? ? video.thumbnail.url : video.file.url(:small)),
  10. alt: video.description,
  11. blurhash: video.blurhash,
  12. frameRate: meta.dig('original', 'frame_rate'),
  13. inline: true,
  14. aspectRatio: "#{meta.dig('original', 'width')} / #{meta.dig('original', 'height')}",
  15. media: [
  16. serialize_media_attachment(video),
  17. ].as_json,
  18. }.merge(**)
  19. react_component :video, component_params do
  20. render partial: 'statuses/attachment_list', locals: { attachments: status.ordered_media_attachments }
  21. end
  22. end
  23. def render_audio_component(status, **)
  24. audio = status.ordered_media_attachments.first
  25. meta = audio.file.meta || {}
  26. component_params = {
  27. src: full_asset_url(audio.file.url(:original)),
  28. poster: full_asset_url(audio.thumbnail.present? ? audio.thumbnail.url : status.account.avatar_static_url),
  29. alt: audio.description,
  30. backgroundColor: meta.dig('colors', 'background'),
  31. foregroundColor: meta.dig('colors', 'foreground'),
  32. accentColor: meta.dig('colors', 'accent'),
  33. duration: meta.dig('original', 'duration'),
  34. }.merge(**)
  35. react_component :audio, component_params do
  36. render partial: 'statuses/attachment_list', locals: { attachments: status.ordered_media_attachments }
  37. end
  38. end
  39. def render_media_gallery_component(status, **)
  40. component_params = {
  41. sensitive: sensitive_viewer?(status, current_account),
  42. autoplay: prefers_autoplay?,
  43. media: status.ordered_media_attachments.map { |a| serialize_media_attachment(a).as_json },
  44. }.merge(**)
  45. react_component :media_gallery, component_params do
  46. render partial: 'statuses/attachment_list', locals: { attachments: status.ordered_media_attachments }
  47. end
  48. end
  49. private
  50. def serialize_media_attachment(attachment)
  51. ActiveModelSerializers::SerializableResource.new(
  52. attachment,
  53. serializer: REST::MediaAttachmentSerializer
  54. )
  55. end
  56. def sensitive_viewer?(status, account)
  57. if !account.nil? && account.id == status.account_id
  58. status.sensitive
  59. else
  60. status.account.sensitized? || status.sensitive
  61. end
  62. end
  63. end