Skip to content

Commit

Permalink
Add "embed" entity
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperTux88 committed Apr 9, 2018
1 parent 9788806 commit fdc1019
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/diaspora_federation/entities.rb
Expand Up @@ -30,6 +30,7 @@ module Entities
require "diaspora_federation/entities/poll"
require "diaspora_federation/entities/poll_participation"

require "diaspora_federation/entities/embed"
require "diaspora_federation/entities/location"

require "diaspora_federation/entities/event"
Expand Down
45 changes: 45 additions & 0 deletions lib/diaspora_federation/entities/embed.rb
@@ -0,0 +1,45 @@
module DiasporaFederation
module Entities
# This entity is used to specify embed information about an URL that should be embedded.
#
# @see Validators::EmbedValidator
class Embed < Entity
# @!attribute [r] url
# URL that should be embedded.
# @return [String] url
property :url, :string, optional: true

# @!attribute [r] title
# The title of the embedded URL.
# @return [String] title
property :title, :string, optional: true

# @!attribute [r] description
# The description of the embedded URL.
# @return [String] description
property :description, :string, optional: true

# @!attribute [r] image
# The image of the embedded URL.
# @return [String] image
property :image, :string, optional: true

# @!attribute [r] nothing
# True, if nothing should be embedded.
# @return [String] nothing
property :nothing, :boolean, optional: true

# @return [String] string representation of this object
def to_s
"Embed#{":#{url}" if url}"
end

def validate
super

raise ValidationError, "URL set and 'nothing' is 'true'" if url && nothing
raise ValidationError, "URL missing and 'nothing' is not 'true'" unless url || nothing
end
end
end
end
5 changes: 5 additions & 0 deletions lib/diaspora_federation/entities/status_message.rb
Expand Up @@ -36,6 +36,11 @@ class StatusMessage < Entity
# @return [Entities::Event] event
entity :event, Entities::Event, optional: true

# @!attribute [r] embed
# Optional embed information of an URL that should be embedded in the status message
# @return [Entities::Embed] embed
entity :embed, Entities::Embed, optional: true

private

def validate
Expand Down
21 changes: 21 additions & 0 deletions lib/diaspora_federation/schemas/federation_entities.json
Expand Up @@ -10,6 +10,7 @@
{"$ref": "#/definitions/reshare"},
{"$ref": "#/definitions/profile"},
{"$ref": "#/definitions/location"},
{"$ref": "#/definitions/embed"},
{"$ref": "#/definitions/photo"},
{"$ref": "#/definitions/poll"},
{"$ref": "#/definitions/poll_answer"}
Expand Down Expand Up @@ -372,6 +373,26 @@
]
}
}
},

"embed": {
"type": "object",
"properties": {
"entity_type": {
"type": "string",
"pattern": "^embed$"
},
"entity_data": {
"type": "object",
"properties": {
"url": { "type": ["string", "null"] },
"title": { "type": ["string", "null"] },
"description": { "type": ["string", "null"] },
"image": { "type": ["string", "null"] },
"nothing": { "type": "boolean" }
}
}
}
}
}
}
8 changes: 8 additions & 0 deletions lib/diaspora_federation/test/factories.rb
Expand Up @@ -206,6 +206,14 @@ module Factories
edited_at { Time.now.utc }
end

Fabricator(:embed_entity, class_name: DiasporaFederation::Entities::Embed) do
url "https://example.org/"
title "Example Website"
description "This is an example!"
image "https://example.org/example.png"
nothing nil
end

Fabricator(:related_entity, class_name: DiasporaFederation::Entities::RelatedEntity) do
author { Fabricate.sequence(:diaspora_id) }
local true
Expand Down
1 change: 1 addition & 0 deletions lib/diaspora_federation/validators.rb
Expand Up @@ -46,6 +46,7 @@ module Validators
require "diaspora_federation/validators/comment_validator"
require "diaspora_federation/validators/contact_validator"
require "diaspora_federation/validators/conversation_validator"
require "diaspora_federation/validators/embed_validator"
require "diaspora_federation/validators/event_participation_validator"
require "diaspora_federation/validators/event_validator"
require "diaspora_federation/validators/h_card_validator"
Expand Down
13 changes: 13 additions & 0 deletions lib/diaspora_federation/validators/embed_validator.rb
@@ -0,0 +1,13 @@
module DiasporaFederation
module Validators
# This validates a {Entities::Embed}.
class EmbedValidator < OptionalAwareValidator
include Validation

rule :url, :URI
rule :title, length: {maximum: 255}
rule :description, length: {maximum: 65_535}
rule :image, URI: %i[host path]
end
end
end
56 changes: 56 additions & 0 deletions spec/lib/diaspora_federation/entities/embed_spec.rb
@@ -0,0 +1,56 @@
module DiasporaFederation
describe Entities::Embed do
let(:data) { Fabricate.attributes_for(:embed_entity) }

let(:xml) { <<-XML }
<embed>
<url>#{data[:url]}</url>
<title>#{data[:title]}</title>
<description>#{data[:description]}</description>
<image>#{data[:image]}</image>
</embed>
XML

let(:json) { <<-JSON }
{
"entity_type": "embed",
"entity_data": {
"url": "#{data[:url]}",
"title": "#{data[:title]}",
"description": "#{data[:description]}",
"image": "#{data[:image]}"
}
}
JSON

let(:string) { "Embed:#{data[:url]}" }

it_behaves_like "an Entity subclass"

it_behaves_like "an XML Entity"

it_behaves_like "a JSON Entity"

describe "#validate" do
it "allows 'url' to be set if 'nothing' is not true" do
expect { Entities::Embed.new(data) }.not_to raise_error
end

it "allows 'url' to be missing if 'nothing' is true" do
expect { Entities::Embed.new(nothing: true) }.not_to raise_error
end

it "doesn't allow 'url' to be set if 'nothing' is true" do
expect {
Entities::Embed.new(data.merge(nothing: true))
}.to raise_error Entity::ValidationError, "URL set and 'nothing' is 'true'"
end

it "doesn't allow 'url' to be missing if 'nothing' is not true" do
expect {
Entities::Embed.new({})
}.to raise_error Entity::ValidationError, "URL missing and 'nothing' is not 'true'"
end
end
end
end
3 changes: 3 additions & 0 deletions spec/lib/diaspora_federation/entities/status_message_spec.rb
Expand Up @@ -10,6 +10,7 @@ module DiasporaFederation
location: location,
poll: nil,
event: nil,
embed: nil,
provider_display_name: "something"
)
}
Expand Down Expand Up @@ -137,6 +138,8 @@ module DiasporaFederation
expect(parsed_instance.photos).to eq([])
expect(parsed_instance.location).to be_nil
expect(parsed_instance.poll).to be_nil
expect(parsed_instance.event).to be_nil
expect(parsed_instance.embed).to be_nil
expect(parsed_instance.public).to be_falsey
expect(parsed_instance.provider_display_name).to be_nil
end
Expand Down
40 changes: 40 additions & 0 deletions spec/lib/diaspora_federation/validators/embed_validator_spec.rb
@@ -0,0 +1,40 @@
module DiasporaFederation
describe Validators::EmbedValidator do
let(:entity) { :embed_entity }
it_behaves_like "a common validator"

describe "#url" do
it_behaves_like "a property with a value validation/restriction" do
let(:property) { :url }
let(:wrong_values) { %w[https://asdf$%.com example.com] }
let(:correct_values) { [nil, "https://example.org", "https://example.org/index.html"] }
end
end

describe "#title" do
it_behaves_like "a length validator" do
let(:property) { :title }
let(:length) { 255 }
end
end

describe "#description" do
it_behaves_like "a length validator" do
let(:property) { :description }
let(:length) { 65_535 }
end
end

describe "#image" do
it_behaves_like "a property with a value validation/restriction" do
let(:property) { :image }
let(:wrong_values) { %w[https://asdf$%.com example.com] }
let(:correct_values) { [nil] }
end

it_behaves_like "a url path validator" do
let(:property) { :image }
end
end
end
end
Expand Up @@ -27,7 +27,7 @@ module DiasporaFederation
describe "##{prop}" do
it_behaves_like "a property with a value validation/restriction" do
let(:property) { prop }
let(:wrong_values) { %w[https://asdf$.com example.com] }
let(:wrong_values) { %w[https://asdf$%.com example.com] }
let(:correct_values) { [nil] }
end

Expand Down

0 comments on commit fdc1019

Please sign in to comment.