From 1d9c886518ab389917ddf3f6c73daa31536d7fe9 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 5 Sep 2016 18:57:59 +0200 Subject: [PATCH] Adding enclosures to Atom and statuses JSON --- app/helpers/atom_builder_helper.rb | 10 ++++++++++ app/models/status.rb | 2 +- app/views/api/statuses/show.rabl | 4 ++++ spec/helpers/atom_builder_helper_spec.rb | 4 ++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/helpers/atom_builder_helper.rb b/app/helpers/atom_builder_helper.rb index a98cb7b3b2..ea54b9fdb6 100644 --- a/app/helpers/atom_builder_helper.rb +++ b/app/helpers/atom_builder_helper.rb @@ -125,6 +125,10 @@ module AtomBuilderHelper xml.link(rel: 'mentioned', href: uri_for_target(account)) end + def link_enclosure(xml, media) + xml.link(rel: 'enclosure', href: full_asset_url(media.file.url), type: media.file_content_type, length: media.file_size) + end + def link_avatar(xml, account) single_link_avatar(xml, account, :large, 300) single_link_avatar(xml, account, :medium, 96) @@ -205,6 +209,12 @@ module AtomBuilderHelper stream_entry.mentions.each do |mentioned| link_mention xml, mentioned end + + if stream_entry.activity.is_a?(Status) + stream_entry.activity.media_attachments.each do |media| + link_enclosure xml, media + end + end end private diff --git a/app/models/status.rb b/app/models/status.rb index 05ae1a06ed..df9eceaff9 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -18,7 +18,7 @@ class Status < ApplicationRecord validates :text, presence: true, if: Proc.new { |s| s.local? && !s.reblog? } scope :with_counters, -> { select('statuses.*, (select count(r.id) from statuses as r where r.reblog_of_id = statuses.id) as reblogs_count, (select count(f.id) from favourites as f where f.status_id = statuses.id) as favourites_count') } - scope :with_includes, -> { includes(:account, :mentions, :stream_entry, reblog: [:account, :mentions], thread: :account) } + scope :with_includes, -> { includes(:account, :mentions, :media_attachments, :stream_entry, reblog: [:account, :mentions], thread: :account) } def local? self.uri.nil? diff --git a/app/views/api/statuses/show.rabl b/app/views/api/statuses/show.rabl index 0f44188704..b8cf075fef 100644 --- a/app/views/api/statuses/show.rabl +++ b/app/views/api/statuses/show.rabl @@ -16,3 +16,7 @@ end child :account do extends('api/accounts/show') end + +child :media_attachments do + node(:url) { |media| full_asset_url(media.file.url) } +end diff --git a/spec/helpers/atom_builder_helper_spec.rb b/spec/helpers/atom_builder_helper_spec.rb index 0cc9522752..aa3589df00 100644 --- a/spec/helpers/atom_builder_helper_spec.rb +++ b/spec/helpers/atom_builder_helper_spec.rb @@ -174,6 +174,10 @@ RSpec.describe AtomBuilderHelper, type: :helper do end end + describe '#link_enclosure' do + pending + end + describe '#logo' do it 'creates a logo' do expect(used_in_builder { |xml| helper.logo(xml, 1) }).to match '1'