conversations federate
This commit is contained in:
parent
1072806d8f
commit
bd908a9b95
4 changed files with 142 additions and 13 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
53
spec/models/message_spec.rb
Normal file
53
spec/models/message_spec.rb
Normal file
|
|
@ -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
|
||||
Loading…
Reference in a new issue