diff --git a/app/models/status_message.rb b/app/models/status_message.rb index 5b78fa324285da72cb968feaadc8da8cd24a3904..67fcb23a41522b31db3d3bc24cf73852a82ba8df 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -13,7 +13,7 @@ class StatusMessage < Post validates_length_of :text, :maximum => 65535, :message => proc {|p, v| I18n.t('status_messages.too_long', :count => 65535, :current_length => v[:value].length)} # don't allow creation of empty status messages - validate :presence_of_content, on: :create, if: proc {|sm| sm.author && sm.author.local? } + validate :presence_of_content, on: :create has_many :photos, :dependent => :destroy, :foreign_key => :status_message_guid, :primary_key => :guid @@ -126,9 +126,7 @@ class StatusMessage < Post protected def presence_of_content - if text_and_photos_blank? - errors[:base] << "Cannot create a StatusMessage without content" - end + errors[:base] << "Cannot create a StatusMessage without content" if text_and_photos_blank? end def absence_of_content diff --git a/lib/diaspora/federation/receive.rb b/lib/diaspora/federation/receive.rb index 6ceb4ddb92282db61462ed3faa98da0ff0930d96..9b0ceea714280db17888140fd3ccf31ca77b3fc6 100644 --- a/lib/diaspora/federation/receive.rb +++ b/lib/diaspora/federation/receive.rb @@ -161,9 +161,20 @@ module Diaspora end def self.status_message(entity) - save_status_message(entity).tap do - entity.photos.map do |photo| - ignore_existing_guid(Photo, photo.guid, author_of(photo)) { save_photo(photo) } + try_load_existing_guid(StatusMessage, entity.guid, author_of(entity)) do + StatusMessage.new( + author: author_of(entity), + guid: entity.guid, + text: entity.text, + public: entity.public, + created_at: entity.created_at, + provider_display_name: entity.provider_display_name + ).tap do |status_message| + status_message.location = build_location(entity.location) if entity.location + status_message.poll = build_poll(entity.poll) if entity.poll + status_message.photos = save_or_load_photos(entity.photos) + + status_message.save! end end end @@ -228,6 +239,12 @@ module Diaspora ) end + private_class_method def self.save_or_load_photos(photos) + photos.map do |photo| + try_load_existing_guid(Photo, photo.guid, author_of(photo)) { save_photo(photo) } + end + end + private_class_method def self.receive_relayable(klass, entity) save_relayable(klass, entity) { yield }.tap {|relayable| relay_relayable(relayable) if relayable } end @@ -243,24 +260,6 @@ module Diaspora end end - private_class_method def self.save_status_message(entity) - try_load_existing_guid(StatusMessage, entity.guid, author_of(entity)) do - StatusMessage.new( - author: author_of(entity), - guid: entity.guid, - text: entity.text, - public: entity.public, - created_at: entity.created_at, - provider_display_name: entity.provider_display_name - ).tap do |status_message| - status_message.location = build_location(entity.location) if entity.location - status_message.poll = build_poll(entity.poll) if entity.poll - - status_message.save! - end - end - end - private_class_method def self.retract_if_author_ignored(relayable) parent_author = relayable.parent.author.owner return unless parent_author && parent_author.ignored_people.include?(relayable.author) diff --git a/spec/lib/diaspora/federation/receive_spec.rb b/spec/lib/diaspora/federation/receive_spec.rb index c3ffd040865232f01d98ff5e50a305370cb0978d..6f720e0a2d1cac6c4c0af8a09c5ad7da59ffb367 100644 --- a/spec/lib/diaspora/federation/receive_spec.rb +++ b/spec/lib/diaspora/federation/receive_spec.rb @@ -591,6 +591,19 @@ describe Diaspora::Federation::Receive do expect(status_message.photos.map(&:guid)).to include(photo1.guid, photo2.guid) end + it "receives a status message only with photos and without text" do + entity = DiasporaFederation::Entities::StatusMessage.new(status_message_entity.to_h.merge(text: nil)) + received = Diaspora::Federation::Receive.perform(entity) + + status_message = StatusMessage.find_by!(guid: status_message_entity.guid) + + expect(received).to eq(status_message) + expect(status_message.author).to eq(sender) + + expect(status_message.text).to be_nil + expect(status_message.photos.map(&:guid)).to include(photo1.guid, photo2.guid) + end + it "does not overwrite the photos if they already exist" do received_photo = Diaspora::Federation::Receive.photo(photo1) received_photo.text = "foobar" diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index fb7ef503ce4842841bdf38a4b5a7ef21d687b740..f60f2f332c8bf3f89d6827aba1a8363d5fc4181a 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -114,9 +114,9 @@ describe StatusMessage, type: :model do expect(post.errors.full_messages).to eq([]) end - it "doesn't check for content when author is remote (federation...)" do + it "also checks for content when author is remote" do post = FactoryGirl.build(:status_message, text: nil) - expect(post).to be_valid + expect(post).not_to be_valid end end