From 48fff29bf6fcfabd480cdef5abbd202f8e9da137 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Fri, 4 Mar 2011 10:53:20 -0800 Subject: [PATCH] fixed conversation receive. made visibilities on conversation :dependent => :destroy --- app/models/conversation.rb | 13 +++++++++---- lib/diaspora/relayable.rb | 4 ++-- spec/models/conversation_spec.rb | 19 +++++++++++++++---- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/app/models/conversation.rb b/app/models/conversation.rb index d21eccd2c..9223a7b46 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -9,7 +9,7 @@ class Conversation < ActiveRecord::Base xml_reader :diaspora_handle xml_reader :participant_handles - has_many :conversation_visibilities + has_many :conversation_visibilities, :dependent => :destroy has_many :participants, :class_name => 'Person', :through => :conversation_visibilities, :source => :person has_many :messages, :order => 'created_at ASC' @@ -45,18 +45,23 @@ class Conversation < ActiveRecord::Base end end + def last_author + self.messages.last.author if self.messages.size > 0 + end + def subscribers(user) self.recipients end def receive(user, person) cnv = Conversation.find_or_create_by_guid(self.attributes) + + self.participants.each do |participant| + ConversationVisibility.create(:conversation_id => cnv.id, :person_id => participant.id) + end self.messages.each do |msg| msg.conversation_id = cnv.id msg.receive(user, person) end - self.participants.each do |participant| - ConversationVisibility.create(:conversation_id => cnv.id, :person_id => participant.id) - end end end diff --git a/lib/diaspora/relayable.rb b/lib/diaspora/relayable.rb index bbab96963..0197b8b2e 100644 --- a/lib/diaspora/relayable.rb +++ b/lib/diaspora/relayable.rb @@ -40,12 +40,12 @@ module Diaspora #sign object as the parent creator if you've been hit UPSTREAM if user.owns? object.parent object.parent_author_signature = object.sign_with_key(user.encryption_key) - object.save + object.save! end #dispatch object DOWNSTREAM, received it via UPSTREAM unless user.owns?(object) - object.save + object.save! Postzord::Dispatch.new(user, object).post end diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index 96ee16a36..9bfa725bd 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -8,8 +8,9 @@ describe Conversation do before do @user1 = alice @user2 = bob + @participant_ids = [@user1.contacts.first.person.id, @user1.person.id] - @create_hash = { :author => @user1.person, :participant_ids => [@user1.contacts.first.person.id, @user1.person.id], + @create_hash = { :author => @user1.person, :participant_ids => @participant_ids , :subject => "cool stuff", :text => 'hey'} end @@ -19,6 +20,16 @@ describe Conversation do }.should change(Message, :count).by(1) end + describe '#last_author' do + it 'returns the last author to a conversation' do + time = Time.now + cnv = Conversation.create(@create_hash) + Message.create(:author => @user2.person, :created_at => time + 1.second, :text => "last", :conversation_id => cnv.id) + cnv.reload.last_author.id.should == @user2.person.id + end + end + + context 'transport' do before do @cnv = Conversation.create(@create_hash) @@ -50,8 +61,8 @@ describe Conversation do describe '#receive' do before do - Conversation.delete_all - Message.delete_all + Conversation.destroy_all + Message.destroy_all end it 'creates a message' do @@ -67,7 +78,7 @@ describe Conversation do it 'creates appropriate visibilities' do lambda{ Diaspora::Parser.from_xml(@xml).receive(@user1, @user2.person) - }.should change(ConversationVisibility, :count).by(@cnv.participants.count) + }.should change(ConversationVisibility, :count).by(@participant_ids.size) end it 'does not save before receive' do Diaspora::Parser.from_xml(@xml).persisted?.should be_false