diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 9e8f35c2a4a35e61719b8448b2784ce9cfe853ac..f1ab4550ab6fc00d6270773c78e9c8115d128601 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -13,28 +13,31 @@ class CommentsController < ApplicationController target = current_user.find_visible_post_by_id params[:post_id] text = params[:text] - @comment = current_user.build_comment(text, :on => target) + if target + @comment = current_user.build_comment(text, :on => target) - if @comment.save(:safe => true) - raise 'MongoMapper failed to catch a failed save' unless @comment.id - Rails.logger.info("event=comment_create user=#{current_user.diaspora_handle} status=success comment=#{@comment.id}") - current_user.dispatch_comment(@comment) + if @comment.save + Rails.logger.info("event=comment_create user=#{current_user.diaspora_handle} status=success comment=#{@comment.id}") + current_user.dispatch_comment(@comment) - respond_to do |format| - format.js{ - json = { :post_id => @comment.post_id, - :comment_id => @comment.id, - :html => render_to_string( - :partial => 'comments/comment', - :locals => { :hash => { - :comment => @comment, - :person => current_user, - }} - ) - } - render(:json => json, :status => 201) - } - format.html{ render :nothing => true, :status => 201 } + respond_to do |format| + format.js{ + json = { :post_id => @comment.post_id, + :comment_id => @comment.id, + :html => render_to_string( + :partial => 'comments/comment', + :locals => { :hash => { + :comment => @comment, + :person => current_user, + }} + ) + } + render(:json => json, :status => 201) + } + format.html{ render :nothing => true, :status => 201 } + end + else + render :nothing => true, :status => 406 end else render :nothing => true, :status => 406 diff --git a/app/models/comment.rb b/app/models/comment.rb index 75f6ed0e3b562b27a07bf2c500ed2c07cef65b42..885b2481da2041f128b3e8b70be4e486e031ebb3 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -30,7 +30,7 @@ class Comment < ActiveRecord::Base person.diaspora_handle end def diaspora_handle= nh - self.person = Person.where(:diaspora_handle => nh).first + self.person = Webfinger.new(nh).fetch end def post_guid self.post.guid diff --git a/app/models/person.rb b/app/models/person.rb index 61ff6a2bdf2a122528e24e4960e1b62da83d0793..bea5ab77c003a6f95291784874d682e89c02a698 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -126,7 +126,7 @@ class Person < ActiveRecord::Base #hcard_profile = HCard.find profile.hcard.first[:href] Rails.logger.info("event=webfinger_marshal valid=#{new_person.valid?} target=#{new_person.diaspora_handle}") new_person.url = hcard[:url] - new_person.create_profile(:first_name => hcard[:given_name], + new_person.profile = Profile.create!(:first_name => hcard[:given_name], :last_name => hcard[:family_name], :image_url => hcard[:photo], :image_url_medium => hcard[:photo_medium], diff --git a/lib/diaspora/user/connecting.rb b/lib/diaspora/user/connecting.rb index 8e3106df4fa8b92f2c8c9a57f1f43a1a6e5ba6e6..cc9bc9c97d0ddc876128cd054b9dba8b57223ab6 100644 --- a/lib/diaspora/user/connecting.rb +++ b/lib/diaspora/user/connecting.rb @@ -80,7 +80,7 @@ module Diaspora def remove_contact(bad_person) contact = contact_for(bad_person) - posts = raw_visible_posts.where(:person_id => bad_person.id) + posts = raw_visible_posts.where(:person_id => bad_person.id).all visibilities = PostVisibility.joins(:post, :aspect).where( :posts => {:person_id => bad_person.id}, :aspects => {:user_id => self.id} @@ -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 < 1 + if post.post_visibilities(true).count < 1 post.destroy end end diff --git a/spec/controllers/people_controller_spec.rb b/spec/controllers/people_controller_spec.rb index 51589bb7b4203977b28a6fc4cb249740dd8f3d68..67941ee3ad53459de84deecf419b008831ea9ffd 100644 --- a/spec/controllers/people_controller_spec.rb +++ b/spec/controllers/people_controller_spec.rb @@ -253,21 +253,22 @@ describe PeopleController do image_url = user.person.profile.image_url put :update, @params - user.person.reload - user.person.profile.image_url.should == image_url + Person.find(user.person.id).profile.image_url.should == image_url end end it 'does not allow mass assignment' do + person = user.person new_user = Factory.create(:user) + person.owner_id.should == user.id put :update, :id => user.person.id, :owner_id => new_user.id - user.person.reload.owner_id.should_not == new_user.id + Person.find(person.id).owner_id.should == user.id end it 'does not overwrite the profile diaspora handle' do handle_params = {:id => user.person.id, :profile => {:diaspora_handle => 'abc@a.com'} } put :update, handle_params - user.person.reload.profile[:diaspora_handle].should_not == 'abc@a.com' + Person.find(user.person.id).profile[:diaspora_handle].should_not == 'abc@a.com' end end end diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb index dadb0c886654ad704ea4f4f7d49331446d96836e..67a17f9069448b16a22cc0bf7dee19254d44e2df 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -135,7 +135,7 @@ describe Photo do @photo.destroy user2.receive xml, @user.person - new_photo = Photo.find(id) + new_photo = Photo.where(:guid => @photo.guid).first new_photo.url.nil?.should be false new_photo.url.include?(url).should be true new_photo.url(:thumb_medium).include?(thumb_url).should be true diff --git a/spec/models/user/receive_spec.rb b/spec/models/user/receive_spec.rb index 785b4a12ddccecb92f2a2ebd7a3c055ad660bb63..2508165867d08d230057dfa341e05af895f55efa 100644 --- a/spec/models/user/receive_spec.rb +++ b/spec/models/user/receive_spec.rb @@ -99,8 +99,6 @@ describe User do 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 @@ -112,7 +110,21 @@ describe User do user.disconnected_by(person) }.should change(Post, :count).by(-1) end + it 'deletes post_visibilities on disconnected by' do + person = Factory(:person) + user.activate_contact(person, aspect) + post = Factory.create(:status_message, :person => person) + 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(person) + }.should change{post.post_visibilities(true).count}.by(-1) + end it 'should keep track of user references for one person ' do @status_message.reload @status_message.user_refs.should == 2 @@ -143,9 +155,6 @@ describe User do connect_users(user, aspect, user3, aspect3) @post = user.post :status_message, :message => "hello", :to => aspect.id - user2.receive @post.to_diaspora_xml, user.person - user3.receive @post.to_diaspora_xml, user.person - @comment = user3.comment('tada',:on => @post) @comment.post_creator_signature = @comment.sign_with_key(user.encryption_key) @xml = @comment.to_diaspora_xml @@ -156,33 +165,38 @@ describe User do local_person = user3.person user2.reload.raw_visible_posts.size.should == 1 - post_in_db = user2.raw_visible_posts.first + post_in_db = StatusMessage.find(@post.id) post_in_db.comments.should == [] - user2.receive(@xml, user.person) - post_in_db.comments.reset - - post_in_db.comments.include?(@comment).should be true - post_in_db.comments.first.person.should == local_person + lambda{ + user2.receive(@xml, user.person) + }.should change{StatusMessage.find(@post.id).comments.count}.by(1) end it 'should correctly marshal a stranger for the downstream user' do - remote_person = user3.person - remote_person.delete + remote_person = user3.person.dup + user3.person.delete user3.delete + remote_person.id = nil #stubs async webfinger - Person.should_receive(:by_account_identifier).and_return{ |handle| if handle == user.person.diaspora_handle; user.person.save - user.person; else; remote_person.save; remote_person; end } + Person.should_receive(:by_account_identifier).twice.and_return{ |handle| + if handle == user.person.diaspora_handle + user.person.save + user.person + else + remote_person.profile = Factory(:profile) + remote_person.save! + remote_person + end + } user2.reload.raw_visible_posts.size.should == 1 - post_in_db = user2.raw_visible_posts.first + post_in_db = StatusMessage.find(@post.id) post_in_db.comments.should == [] - user2.receive(@xml, user.person) - post_in_db.reload - - post_in_db.comments.include?(@comment).should be true - post_in_db.comments.first.person.should == remote_person + lambda{ + user2.receive(@xml, user.person) + }.should change{StatusMessage.find(@post.id).comments.count}.by(1) end end