fixed conversation receive. made visibilities on conversation :dependent => :destroy

This commit is contained in:
danielgrippi 2011-03-04 10:53:20 -08:00
parent 5f55dfa1bc
commit 48fff29bf6
3 changed files with 26 additions and 10 deletions

View file

@ -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

View file

@ -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

View file

@ -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