diff --git a/app/models/post.rb b/app/models/post.rb index 51e76b760..b0d47093e 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -60,7 +60,7 @@ class Post < ActiveRecord::Base end def propogate_retraction - self.person.owner.retract(self) + self.person.owner.retract(self) if self.person.owner end end diff --git a/lib/diaspora/user/connecting.rb b/lib/diaspora/user/connecting.rb index 8fdbd6330..8e3106df4 100644 --- a/lib/diaspora/user/connecting.rb +++ b/lib/diaspora/user/connecting.rb @@ -88,7 +88,7 @@ module Diaspora visibility_ids = visibilities.map{|v| v.id} PostVisibility.where(:id => visibility_ids).delete_all posts.each do |post| - if post.user_refs == 0 + if post.user_refs < 1 post.destroy end end diff --git a/lib/diaspora/user/receiving.rb b/lib/diaspora/user/receiving.rb index a5b7a152d..66b0452b2 100644 --- a/lib/diaspora/user/receiving.rb +++ b/lib/diaspora/user/receiving.rb @@ -32,6 +32,8 @@ module Diaspora xml_author = (owns?(object.post)) ? object.diaspora_handle : object.post.person.diaspora_handle else xml_author = object.diaspora_handle + pp xml_author + pp salmon_author end if (salmon_author.diaspora_handle != xml_author) @@ -137,8 +139,12 @@ module Diaspora comment end - def exsists_on_pod?(post) - post.class.find_by_id(post.id) + def existing_post(post) + post.class.where(:guid => post.guid).first + end + + def post_visible?(post) + raw_visible_posts.where(:guid => post.guid).first end def receive_post post @@ -149,42 +155,39 @@ module Diaspora #you know about it, and it is mutable #you know about it, and it is not mutable # - on_pod = exsists_on_pod?(post) - if on_pod && on_pod.diaspora_handle == post.diaspora_handle - known_post = find_visible_post_by_id(post.id) - if known_post - if known_post.mutable? - known_post.update_attributes(post.to_mongo) + on_pod = existing_post(post) + log_string = "event=receive payload_type=#{post.class} sender=#{post.diaspora_handle} " + if on_pod + puts "On pod" + if post_visible?(post) + puts "visible" + if post.mutable? + on_pod.caption = post.caption + on_pod.save! else - Rails.logger.info("event=receive payload_type=#{post.class} update=true status=abort sender=#{post.diaspora_handle} reason=immutable existing_post=#{known_post.id}") + Rails.logger.info(log_string << "update=true status=abort reason=immutable existing_post=#{on_pod.id}") end - elsif on_pod == post - update_user_refs_and_add_to_aspects(on_pod) - Rails.logger.info("event=receive payload_type=#{post.class} update=true status=complete sender=#{post.diaspora_handle} existing_post=#{on_pod.id}") - post + else + add_post_to_aspects(on_pod) + Rails.logger.info(log_string << "update=false status=complete") + on_pod end - elsif !on_pod - update_user_refs_and_add_to_aspects(post) - Rails.logger.info("event=receive payload_type=#{post.class} update=false status=complete sender=#{post.diaspora_handle}") - post else - Rails.logger.info("event=receive payload_type=#{post.class} update=true status=abort sender=#{post.diaspora_handle} reason='update not from post owner' existing_post=#{post.id}") + post.save! + add_post_to_aspects(post) + Rails.logger.info(log_string << "update=false status=complete") + post end end - def update_user_refs_and_add_to_aspects(post) - Rails.logger.debug("Saving post: #{post}") - post.user_refs += 1 - post.save - - self.raw_visible_posts << post - self.save + def add_post_to_aspects(post) + Rails.logger.debug("event=add_post_to_aspects user_id=#{self.id} post_id=#{post.id}") + puts("event=add_post_to_aspects user_id=#{self.id} post_id=#{post.id}") aspects = self.aspects_with_person(post.person) aspects.each do |aspect| aspect.posts << post - aspect.save end post.socket_to_uid(id, :aspect_ids => aspects.map{|x| x.id}) if (post.respond_to?(:socket_to_uid) && !self.owns?(post)) post diff --git a/spec/factories.rb b/spec/factories.rb index 0d7e1053e..2d6a24a02 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -49,6 +49,9 @@ end Factory.define :status_message do |m| m.sequence(:message) { |n| "jimmy's #{n} whales" } m.association :person + m.after_build do|m| + m.diaspora_handle = m.person.diaspora_handle + end end Factory.define :photo do |p| diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index cb5e89245..197734acd 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -47,16 +47,34 @@ describe StatusMessage do end describe "XML" do - it 'should serialize to XML' do - message = Factory.create(:status_message, :message => "I hate WALRUSES!", :person => @user.person) - message.to_xml.to_s.should include "I hate WALRUSES!" + before do + @message = Factory.create(:status_message, :message => "I hate WALRUSES!", :person => @user.person) + @xml = @message.to_xml.to_s + end + it 'serializes the message' do + @xml.should include "I hate WALRUSES!" end - it 'should marshal serialized XML to object' do - xml = "I hate WALRUSES!" - parsed = StatusMessage.from_xml(xml) - parsed.message.should == "I hate WALRUSES!" - parsed.valid?.should be_true + it 'serializes the author address' do + @xml.should include(@user.person.diaspora_handle) + end + + describe '.from_xml' do + before do + @marshalled = StatusMessage.from_xml(@xml) + end + it 'marshals the message' do + @marshalled.message.should == "I hate WALRUSES!" + end + it 'marshals the guid' do + @marshalled.guid.should == @message.guid + end + it 'marshals the author' do + @marshalled.person.should == @message.person + end + it 'marshals the diaspora_handle' do + @marshalled.diaspora_handle.should == @message.diaspora_handle + end end end diff --git a/spec/models/user/receive_spec.rb b/spec/models/user/receive_spec.rb index c0cd550f2..785b4a12d 100644 --- a/spec/models/user/receive_spec.rb +++ b/spec/models/user/receive_spec.rb @@ -19,10 +19,10 @@ describe User do connect_users(user, aspect, user2, aspect2) end - it 'should stream only one message to the everyone aspect when a multi-aspected contacts posts' do + it 'streams only one message to the everyone aspect when a multi-aspected contacts posts' do contact = user.contact_for(user2.person) user.add_contact_to_aspect(contact, user.aspects.create(:name => "villains")) - status = user2.post(:status_message, :message => "Users do things", :to => aspect2.id) + status = user2.build_post(:status_message, :message => "Users do things", :to => aspect2.id) xml = status.to_diaspora_xml Diaspora::WebSocket.should_receive(:queue_to_user).exactly(:once) user.receive xml, user2.person @@ -96,12 +96,20 @@ describe User do end it 'deletes a post if the noone links to it' do - person = user2.person - person.owner_id = nil - person.save + person = Factory(:person) + user.activate_contact(person, aspect) + post = Factory.create(:status_message, :person => person) + puts + pp post + post.post_visibilities.should be_empty + user.receive post.to_diaspora_xml, person + aspect.post_visibilities.reset + aspect.posts(true).should include(post) + post.post_visibilities.reset + post.post_visibilities.length.should == 1 lambda { - user.disconnected_by(user2.person) + user.disconnected_by(person) }.should change(Post, :count).by(-1) end @@ -115,6 +123,9 @@ describe User do end it 'should not override userrefs on receive by another person' do + @status_message.post_visibilities.reset + @status_message.user_refs.should == 2 + user3.activate_contact(user2.person, aspect3) user3.receive @status_message.to_diaspora_xml, user2.person @@ -148,7 +159,7 @@ describe User do post_in_db = user2.raw_visible_posts.first post_in_db.comments.should == [] user2.receive(@xml, user.person) - post_in_db.reload + post_in_db.comments.reset post_in_db.comments.include?(@comment).should be true post_in_db.comments.first.person.should == local_person