Skip to content

Commit

Permalink
Merge 3080534 into 2da3340
Browse files Browse the repository at this point in the history
  • Loading branch information
jhass committed Feb 2, 2020
2 parents 2da3340 + 3080534 commit 80efb77
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
18 changes: 17 additions & 1 deletion app/presenters/post_presenter.rb
Expand Up @@ -32,7 +32,9 @@ def as_api_response # rubocop:disable Metrics/AbcSize
poll: PollPresenter.new(@post.poll, current_user).as_api_json,
mentioned_people: build_mentioned_people_json,
photos: build_photos_json,
root: root_api_response
root: root_api_response,
open_graph_object: open_graph_object_api_response,
oembed: @post.o_embed_cache.try(:data)
}.compact
end

Expand Down Expand Up @@ -111,6 +113,20 @@ def build_open_graph_cache
@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
Expand Down
22 changes: 22 additions & 0 deletions lib/schemas/api_v1.json
Expand Up @@ -304,6 +304,28 @@
},
"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
},
"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", "mentioned_people", "photos"]
Expand Down
7 changes: 6 additions & 1 deletion spec/factories.rb
Expand Up @@ -292,7 +292,12 @@ def r_str

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
Expand Down
23 changes: 23 additions & 0 deletions spec/integration/api/posts_controller_spec.rb
Expand Up @@ -87,6 +87,9 @@
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.o_embed_cache = FactoryGirl.create(:o_embed_cache)
status_message.save

get(
api_v1_post_path(status_message.guid),
Expand Down Expand Up @@ -683,6 +686,8 @@ def confirm_post_format(post, user, reference_post, mentions=[])
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)
confirm_oembed(post["oembed"], reference_post.o_embed_cache)
end

def confirm_post_top_level(post, reference_post)
Expand Down Expand Up @@ -743,6 +748,24 @@ def confirm_photos(photos, ref_photos)
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_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
Expand Down
6 changes: 6 additions & 0 deletions spec/presenters/post_presenter_spec.rb
Expand Up @@ -214,6 +214,12 @@
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

0 comments on commit 80efb77

Please sign in to comment.