From b1f357849bbc2fb2b8fce620e4c265e9538e822c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonne=20Ha=C3=9F?= Date: Sun, 2 Feb 2020 01:08:07 +0100 Subject: [PATCH] API: return post oEmbed data --- app/presenters/post_presenter.rb | 3 ++- lib/schemas/api_v1.json | 9 +++++++++ spec/factories.rb | 7 ++++++- spec/integration/api/posts_controller_spec.rb | 9 +++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index 79615de06..aa8cae292 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -34,7 +34,8 @@ class PostPresenter < BasePresenter photos: build_photos_json, root: root_api_response, own_interaction_state: build_own_interaction_state, - open_graph_object: open_graph_object_api_response + open_graph_object: open_graph_object_api_response, + oembed: @post.o_embed_cache.try(:data) }.compact end diff --git a/lib/schemas/api_v1.json b/lib/schemas/api_v1.json index 1199b5f62..46342dd48 100644 --- a/lib/schemas/api_v1.json +++ b/lib/schemas/api_v1.json @@ -328,6 +328,15 @@ }, "required": ["url", "type", "title", "image"], "additionalProperties": false + }, + "oembed": { + "type": "object", + "description": "An oEmbed response according to 2.3.4 of the oEmbed spec.", + "properties": { + "trusted_endpoint_url": { "type": "string" } + }, + "required": ["trusted_endpoint_url"], + "additionalProperties": true } }, "required": ["guid", "created_at", "title", "body", "public", "nsfw", "author", "interaction_counters", "own_interaction_state", "mentioned_people", "photos"] diff --git a/spec/factories.rb b/spec/factories.rb index a9b7b4516..42f1a04c3 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -292,7 +292,12 @@ FactoryGirl.define do factory(:o_embed_cache) do url "http://youtube.com/kittens" - data {{'data' => 'foo'}} + data { + { + "data" => "foo", + "trusted_endpoint_url" => "https://www.youtube.com/oembed?scheme=https" + } + } end factory(:open_graph_cache) do diff --git a/spec/integration/api/posts_controller_spec.rb b/spec/integration/api/posts_controller_spec.rb index 0a8632630..494119494 100644 --- a/spec/integration/api/posts_controller_spec.rb +++ b/spec/integration/api/posts_controller_spec.rb @@ -88,6 +88,7 @@ describe Api::V1::PostsController do merged_params = merged_params.merge(photos: @alice_photo_ids) status_message = StatusMessageCreationService.new(alice).create(merged_params) status_message.open_graph_cache = FactoryGirl.create(:open_graph_cache, video_url: "http://example.org") + status_message.o_embed_cache = FactoryGirl.create(:o_embed_cache) status_message.save get( @@ -696,6 +697,7 @@ describe Api::V1::PostsController do confirm_location(post["location"], reference_post.location) if reference_post.location confirm_photos(post["photos"], reference_post.photos) if reference_post.photos confirm_open_graph_object(post["open_graph_object"], reference_post.open_graph_cache) + confirm_oembed(post["oembed"], reference_post.o_embed_cache) end def confirm_post_top_level(post, reference_post) @@ -773,6 +775,13 @@ describe Api::V1::PostsController do expect(object["video_url"]).to eq(ref_cache.video_url) end + def confirm_oembed(response, ref_cache) + return unless ref_cache + + expect(response).to eq(ref_cache.data) + expect(response["trusted_endpoint_url"]).to_not be_nil + end + def confirm_reshare_format(post, root_post, root_poster) root = post["root"] expect(root.has_key?("guid")).to be_truthy