diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 58ab2ab2f..27f375af2 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -12,6 +12,10 @@ class Conversation < ActiveRecord::Base has_many :participants, :class_name => 'Person', :through => :conversation_visibilities, :source => :person has_many :messages, :order => 'created_at ASC' + def author + self.messages.first.author + end + def recipients self.participants - [self.author] end @@ -19,4 +23,24 @@ class Conversation < ActiveRecord::Base def participant_handles self.participants.map{|p| p.diaspora_handle}.join(";") end + + def participant_handles= handles + handles.split(';').each do |handle| + self.participants << Webfinger.new(handle).fetch + end + end + def subscribers(user) + self.recipients + end + + def receive(user, person) + cnv = Conversation.find_or_create_by_guid(self.attributes) + 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/app/models/message.rb b/app/models/message.rb index 53dff9305..b02765165 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -5,8 +5,39 @@ class Message < ActiveRecord::Base xml_attr :text xml_attr :created_at + xml_reader :diaspora_handle + xml_reader :conversation_guid belongs_to :author, :class_name => 'Person' belongs_to :conversation + def diaspora_handle + self.author.diaspora_handle + end + + def diaspora_handle= nh + self.author = Webfinger.new(nh).fetch + end + + def conversation_guid + self.conversation.guid + end + + def conversation_guid= guid + if cnv = Conversation.find_by_guid(guid) + self.conversation_id = cnv.id + end + end + + def receive(user, person) + Message.find_or_create_by_guid(self.attributes) + end + + def subscribers(user) + if self.conversation.author == user.person + p = self.conversation.subscribers(user) + else + p = self.conversation.author + end + end end diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index f595adf8d..51683a961 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -3,18 +3,17 @@ require 'spec_helper' describe Conversation do before do @user1 = alice + @user2 = bob + + @create_hash = { :participant_ids => [@user1.contacts.first.person.id, @user1.person.id], :subject => "cool stuff" } + @cnv = Conversation.create(@create_hash) + @message = Message.new(:author => @user1.person, :text => "stuff") + @cnv.messages << @message + @message.save + @xml = @cnv.to_diaspora_xml end describe 'serialization' do - before do - @create_hash = { :participant_ids => [@user1.contacts.first.person.id, @user1.person.id], - :subject => "cool stuff" } - @cnv = Conversation.new(@create_hash) - @message = Message.new(:author => @user1.person, :text => "stuff") - @cnv.messages << @message - @xml = @cnv.to_diaspora_xml - end - it 'serializes the message' do @xml.gsub(/\s/, '').should include(@message.to_xml.to_s.gsub(/\s/, '')) end @@ -30,13 +29,35 @@ describe Conversation do end end - describe "#subscribers?" do + describe '#subscribers' do it 'returns the recipients for the post owner' do - + @cnv.subscribers(@user1).should == @user1.contacts.map{|c| c.person} + end + end + + describe '#receive' do + before do + Conversation.delete_all + Message.delete_all end - it 'returns the author for any other user' do - + it 'creates a message' do + lambda{ + Diaspora::Parser.from_xml(@xml).receive(@user1, @user2.person) + }.should change(Message, :count).by(1) + end + it 'creates a conversation' do + lambda{ + Diaspora::Parser.from_xml(@xml).receive(@user1, @user2.person) + }.should change(Conversation, :count).by(1) + end + it 'creates appropriate visibilities' do + lambda{ + Diaspora::Parser.from_xml(@xml).receive(@user1, @user2.person) + }.should change(ConversationVisibility, :count).by(@cnv.participants.count) + end + it 'does not save before receive' do + Diaspora::Parser.from_xml(@xml).persisted?.should be_false end end end diff --git a/spec/models/message_spec.rb b/spec/models/message_spec.rb new file mode 100644 index 000000000..5c5e11f58 --- /dev/null +++ b/spec/models/message_spec.rb @@ -0,0 +1,53 @@ +require 'spec_helper' + +describe Message do + before do + @user1 = alice + @user2 = bob + + @create_hash = { :participant_ids => [@user1.contacts.first.person.id, @user1.person.id], :subject => "cool stuff" } + @cnv = Conversation.create(@create_hash) + @message = Message.new(:author => @user1.person, :text => "stuff") + @cnv.messages << @message + @message.save + @xml = @message.to_diaspora_xml + end + + describe 'serialization' do + it 'serializes the text' do + @xml.should include(@message.text) + end + + it 'serializes the author_handle' do + @xml.should include(@message.author.diaspora_handle) + end + + it 'serializes the created_at time' do + @xml.should include(@message.created_at.to_s) + end + it 'serializes the conversation_guid time' do + @xml.should include(@message.conversation.guid) + end + end + + describe '#subscribers' do + it 'returns the recipients for the post owner' do + @message.subscribers(@user1).should == @user1.contacts.map{|c| c.person} + end + it 'returns the conversation author for the post owner' do + @message.subscribers(@user2).should == @user1.person + end + end + + describe '#receive' do + before do + Message.delete_all + end + + it 'creates a message' do + lambda{ + Diaspora::Parser.from_xml(@xml).receive(@user1, @user2.person) + }.should change(Message, :count).by(1) + end + end +end