From 5921cd0176a7de35776d47d4f762a3a7d498f703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonne=20Ha=C3=9F?= Date: Sun, 2 Feb 2020 00:49:25 +0100 Subject: [PATCH] API: return post open graph metadata --- app/presenters/post_presenter.rb | 17 ++++++++++++++++- lib/schemas/api_v1.json | 13 +++++++++++++ spec/integration/api/posts_controller_spec.rb | 14 ++++++++++++++ spec/presenters/post_presenter_spec.rb | 6 ++++++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index ad39aa0e8..79615de06 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -33,7 +33,8 @@ class PostPresenter < BasePresenter mentioned_people: build_mentioned_people_json, photos: build_photos_json, root: root_api_response, - own_interaction_state: build_own_interaction_state + own_interaction_state: build_own_interaction_state, + open_graph_object: open_graph_object_api_response }.compact end @@ -112,6 +113,20 @@ class PostPresenter < BasePresenter @post.open_graph_cache.try(:as_api_response, :backbone) end + def open_graph_object_api_response + cache = @post.open_graph_cache + return unless cache + + { + type: cache.ob_type, + url: cache.url, + title: cache.title, + image: cache.image, + description: cache.description, + video_url: cache.video_url + } + end + def build_mentioned_people_json @post.mentioned_people.map {|m| PersonPresenter.new(m).as_api_json } end diff --git a/lib/schemas/api_v1.json b/lib/schemas/api_v1.json index 1b2884bac..1199b5f62 100644 --- a/lib/schemas/api_v1.json +++ b/lib/schemas/api_v1.json @@ -315,6 +315,19 @@ }, "required": ["address", "lat", "lng"], "additionalProperties": false + }, + "open_graph_object": { + "type": "object", + "properties": { + "url": { "type": "string" }, + "type": { "type": "string" }, + "title": { "type": "string" }, + "image": { "type": "string" }, + "description": { "type": "string" }, + "video_url": { "type": "string" } + }, + "required": ["url", "type", "title", "image"], + "additionalProperties": false } }, "required": ["guid", "created_at", "title", "body", "public", "nsfw", "author", "interaction_counters", "own_interaction_state", "mentioned_people", "photos"] diff --git a/spec/integration/api/posts_controller_spec.rb b/spec/integration/api/posts_controller_spec.rb index ad4abd7dc..0a8632630 100644 --- a/spec/integration/api/posts_controller_spec.rb +++ b/spec/integration/api/posts_controller_spec.rb @@ -87,6 +87,8 @@ describe Api::V1::PostsController do merged_params = merged_params.merge(poll_params) 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.save get( api_v1_post_path(status_message.guid), @@ -693,6 +695,7 @@ describe Api::V1::PostsController do confirm_poll(post["poll"], reference_post.poll, false) if reference_post.poll 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) end def confirm_post_top_level(post, reference_post) @@ -759,6 +762,17 @@ describe Api::V1::PostsController do end end + def confirm_open_graph_object(object, ref_cache) + return unless ref_cache + + expect(object["type"]).to eq(ref_cache.ob_type) + expect(object["url"]).to eq(ref_cache.url) + expect(object["title"]).to eq(ref_cache.title) + expect(object["image"]).to eq(ref_cache.image) + expect(object["description"]).to eq(ref_cache.description) + expect(object["video_url"]).to eq(ref_cache.video_url) + end + def confirm_reshare_format(post, root_post, root_poster) root = post["root"] expect(root.has_key?("guid")).to be_truthy diff --git a/spec/presenters/post_presenter_spec.rb b/spec/presenters/post_presenter_spec.rb index 14974d073..f886a153c 100644 --- a/spec/presenters/post_presenter_spec.rb +++ b/spec/presenters/post_presenter_spec.rb @@ -214,6 +214,12 @@ describe PostPresenter do post = FactoryGirl.create(:status_message, public: true, open_graph_cache: open_graph_cache) expect(PostPresenter.new(post).send(:build_open_graph_cache)).to eq(open_graph_cache.as_api_response(:backbone)) end + + it "returns the open graph data in the api" do + open_graph_cache = FactoryGirl.create(:open_graph_cache) + post = FactoryGirl.create(:status_message, public: true, open_graph_cache: open_graph_cache) + expect(PostPresenter.new(post).as_api_response[:open_graph_object][:url]).to eq(open_graph_cache.url) + end end end end