media_component_helper.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # frozen_string_literal: true
  2. module MediaComponentHelper
  3. def render_video_component(status, **options)
  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(**options)
  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, **options)
  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(**options)
  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, **options)
  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(**options)
  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. def render_card_component(status, **options)
  50. component_params = {
  51. sensitive: sensitive_viewer?(status, current_account),
  52. card: serialize_status_card(status).as_json,
  53. }.merge(**options)
  54. react_component :card, component_params
  55. end
  56. def render_poll_component(status, **options)
  57. component_params = {
  58. disabled: true,
  59. poll: serialize_status_poll(status).as_json,
  60. }.merge(**options)
  61. react_component :poll, component_params do
  62. render partial: 'statuses/poll', locals: { status: status, poll: status.preloadable_poll, autoplay: prefers_autoplay? }
  63. end
  64. end
  65. private
  66. def serialize_media_attachment(attachment)
  67. ActiveModelSerializers::SerializableResource.new(
  68. attachment,
  69. serializer: REST::MediaAttachmentSerializer
  70. )
  71. end
  72. def serialize_status_card(status)
  73. ActiveModelSerializers::SerializableResource.new(
  74. status.preview_card,
  75. serializer: REST::PreviewCardSerializer
  76. )
  77. end
  78. def serialize_status_poll(status)
  79. ActiveModelSerializers::SerializableResource.new(
  80. status.preloadable_poll,
  81. serializer: REST::PollSerializer,
  82. scope: current_user,
  83. scope_name: :current_user
  84. )
  85. end
  86. def sensitive_viewer?(status, account)
  87. if !account.nil? && account.id == status.account_id
  88. status.sensitive
  89. else
  90. status.account.sensitized? || status.sensitive
  91. end
  92. end
  93. end