diff --git a/docs/_entities/contact.md b/docs/_entities/contact.md index 1940025..ef79369 100644 --- a/docs/_entities/contact.md +++ b/docs/_entities/contact.md @@ -4,6 +4,8 @@ title: Contact This entity represents a contact state with another person. +When `blocking` is `true`, `following` and `sharing` need to be `false` (and the other way around). + ## Properties | Property | Type | Description | @@ -13,6 +15,12 @@ This entity represents a contact state with another person. | `following` | [Boolean][boolean] | `true` if the author is following the recipient. | | `sharing` | [Boolean][boolean] | `true` if the author is sharing with the recipient. | +## Optional Properties + +| Property | Type | Description | +| ---------- | ------------------ | ----------------------------------------------- | +| `blocking` | [Boolean][boolean] | `true` if the author is blocking the recipient. | + ## Example ~~~xml @@ -21,6 +29,7 @@ This entity represents a contact state with another person. bob@example.com true true + false ~~~ diff --git a/lib/diaspora_federation/entities/contact.rb b/lib/diaspora_federation/entities/contact.rb index 693895d..bd5c0f2 100644 --- a/lib/diaspora_federation/entities/contact.rb +++ b/lib/diaspora_federation/entities/contact.rb @@ -25,10 +25,25 @@ module DiasporaFederation # @return [Boolean] if the author is sharing with the person property :sharing, :boolean, default: true + # @!attribute [r] blocking + # @return [Boolean] if the author is blocking the person + property :blocking, :boolean, optional: true, default: false + # @return [String] string representation of this object def to_s "Contact:#{author}:#{recipient}" end + + private + + def validate + super + + return unless (following || sharing) && blocking + + raise ValidationError, + "flags invalid: following:#{following}/sharing:#{sharing} and blocking:#{blocking} can't both be true" + end end end end diff --git a/lib/diaspora_federation/test/factories.rb b/lib/diaspora_federation/test/factories.rb index dce4e6e..a9a2914 100644 --- a/lib/diaspora_federation/test/factories.rb +++ b/lib/diaspora_federation/test/factories.rb @@ -113,6 +113,7 @@ module DiasporaFederation recipient { Fabricate.sequence(:diaspora_id) } following true sharing true + blocking false end Fabricator(:comment_entity, class_name: DiasporaFederation::Entities::Comment, from: :relayable_entity) do diff --git a/lib/diaspora_federation/validators/contact_validator.rb b/lib/diaspora_federation/validators/contact_validator.rb index c22ecc7..c46d60d 100644 --- a/lib/diaspora_federation/validators/contact_validator.rb +++ b/lib/diaspora_federation/validators/contact_validator.rb @@ -8,6 +8,7 @@ module DiasporaFederation rule :recipient, %i[not_empty diaspora_id] rule :following, :boolean rule :sharing, :boolean + rule :blocking, :boolean end end end diff --git a/spec/lib/diaspora_federation/entities/contact_spec.rb b/spec/lib/diaspora_federation/entities/contact_spec.rb index bf188d5..aea9ef8 100644 --- a/spec/lib/diaspora_federation/entities/contact_spec.rb +++ b/spec/lib/diaspora_federation/entities/contact_spec.rb @@ -8,6 +8,7 @@ module DiasporaFederation #{data[:recipient]} #{data[:following]} #{data[:sharing]} + #{data[:blocking]} XML @@ -16,5 +17,35 @@ XML it_behaves_like "an Entity subclass" it_behaves_like "an XML Entity" + + describe "#validate" do + it "allows 'following' and 'sharing' to be true" do + combinations = [ + {following: true, sharing: true, blocking: false}, + {following: true, sharing: false, blocking: false}, + {following: false, sharing: true, blocking: false} + ] + combinations.each do |combination| + expect { Entities::Contact.new(data.merge(combination)) }.not_to raise_error + end + end + + it "allows 'blocking' to be true" do + expect { + Entities::Contact.new(data.merge(following: false, sharing: false, blocking: true)) + }.not_to raise_error + end + + it "doesn't allow 'following'/'sharing' and 'blocking' to be true" do + combinations = [ + {following: true, sharing: true, blocking: true}, + {following: true, sharing: false, blocking: true}, + {following: false, sharing: true, blocking: true} + ] + combinations.each do |combination| + expect { Entities::Contact.new(data.merge(combination)) }.to raise_error Entity::ValidationError + end + end + end end end diff --git a/spec/lib/diaspora_federation/validators/contact_validator_spec.rb b/spec/lib/diaspora_federation/validators/contact_validator_spec.rb index 476d859..8946f6f 100644 --- a/spec/lib/diaspora_federation/validators/contact_validator_spec.rb +++ b/spec/lib/diaspora_federation/validators/contact_validator_spec.rb @@ -13,7 +13,7 @@ module DiasporaFederation end end - %i[following sharing].each do |prop| + %i[following sharing blocking].each do |prop| describe "##{prop}" do it_behaves_like "a boolean validator" do let(:property) { prop }