1
0

media_attachment_serializer.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # frozen_string_literal: true
  2. class REST::MediaAttachmentSerializer < ActiveModel::Serializer
  3. include RoutingHelper
  4. # Please update `app/javascript/mastodon/api_types/media_attachments.ts` when making changes to the attributes
  5. attributes :id, :type, :url, :preview_url,
  6. :remote_url, :preview_remote_url, :text_url, :meta,
  7. :description, :blurhash
  8. def id
  9. object.id.to_s
  10. end
  11. def url
  12. if object.not_processed?
  13. nil
  14. elsif object.needs_redownload?
  15. media_proxy_url(object.id, :original)
  16. else
  17. full_asset_url(object.file.url(:original))
  18. end
  19. end
  20. def remote_url
  21. object.remote_url.presence
  22. end
  23. def preview_url
  24. if object.needs_redownload?
  25. media_proxy_url(object.id, :small)
  26. elsif object.thumbnail.present?
  27. full_asset_url(object.thumbnail.url(:original))
  28. elsif object.file.styles.key?(:small)
  29. full_asset_url(object.file.url(:small))
  30. end
  31. end
  32. def preview_remote_url
  33. object.thumbnail_remote_url.presence
  34. end
  35. def text_url
  36. object.local? && object.shortcode.present? ? medium_url(object) : nil
  37. end
  38. def meta
  39. object.file.meta
  40. end
  41. end