deliver message always to all participants

message doesn't include Diaspora::Relayable anymore
This commit is contained in:
Benjamin Neff 2016-06-17 20:34:49 +02:00
parent b39f3ccc74
commit 9fa3cc97d4
5 changed files with 39 additions and 25 deletions

View file

@ -2,9 +2,9 @@ class Conversation < ActiveRecord::Base
include Diaspora::Federated::Base include Diaspora::Federated::Base
include Diaspora::Guid include Diaspora::Guid
has_many :conversation_visibilities, :dependent => :destroy has_many :conversation_visibilities, dependent: :destroy
has_many :participants, :class_name => 'Person', :through => :conversation_visibilities, :source => :person has_many :participants, class_name: "Person", through: :conversation_visibilities, source: :person
has_many :messages, -> { order('created_at ASC') } has_many :messages, -> { order("created_at ASC") }, inverse_of: :conversation
belongs_to :author, class_name: "Person" belongs_to :author, class_name: "Person"
delegate :diaspora_handle, to: :author delegate :diaspora_handle, to: :author
@ -50,10 +50,6 @@ class Conversation < ActiveRecord::Base
end end
end end
def public?
false
end
def participant_handles def participant_handles
participants.map(&:diaspora_handle).join(";") participants.map(&:diaspora_handle).join(";")
end end

View file

@ -1,7 +1,6 @@
class Message < ActiveRecord::Base class Message < ActiveRecord::Base
include Diaspora::Federated::Base include Diaspora::Federated::Base
include Diaspora::Guid include Diaspora::Guid
include Diaspora::Relayable
belongs_to :author, class_name: "Person" belongs_to :author, class_name: "Person"
belongs_to :conversation, touch: true belongs_to :conversation, touch: true
@ -9,9 +8,12 @@ class Message < ActiveRecord::Base
delegate :diaspora_handle, to: :author delegate :diaspora_handle, to: :author
delegate :name, to: :author, prefix: true delegate :name, to: :author, prefix: true
# TODO: can be removed when messages are not relayed anymore
alias_attribute :parent, :conversation alias_attribute :parent, :conversation
validates :text, :presence => true validates :conversation, presence: true
validates :author, presence: true
validates :text, presence: true
validate :participant_of_parent_conversation validate :participant_of_parent_conversation
def diaspora_handle=(nh) def diaspora_handle=(nh)
@ -19,9 +21,7 @@ class Message < ActiveRecord::Base
end end
def conversation_guid=(guid) def conversation_guid=(guid)
if cnv = Conversation.find_by_guid(guid) self.conversation_id = Conversation.where(guid: guid).ids.first
self.conversation_id = cnv.id
end
end end
def increase_unread(user) def increase_unread(user)
@ -35,6 +35,15 @@ class Message < ActiveRecord::Base
@message ||= Diaspora::MessageRenderer.new text @message ||= Diaspora::MessageRenderer.new text
end end
# @return [Array<Person>]
def subscribers
if author.local?
conversation.participants
else # for relaying, TODO: can be removed when messages are not relayed anymore
conversation.participants.remote
end
end
private private
def participant_of_parent_conversation def participant_of_parent_conversation

View file

@ -294,9 +294,8 @@ FactoryGirl.define do
factory(:conversation_with_message, parent: :conversation) do factory(:conversation_with_message, parent: :conversation) do
after(:create) do |c| after(:create) do |c|
msg = FactoryGirl.build(:message) msg = FactoryGirl.build(:message, author: c.author)
msg.conversation_id = c.id msg.conversation_id = c.id
c.participants << msg.author
msg.save msg.save
end end
end end

View file

@ -32,7 +32,8 @@ describe Diaspora::Federation::Entities do
end end
context "Conversation" do context "Conversation" do
let(:diaspora_entity) { FactoryGirl.create(:conversation_with_message) } let(:participant) { FactoryGirl.create(:person) }
let(:diaspora_entity) { FactoryGirl.create(:conversation_with_message, participants: [participant]) }
let(:federation_entity) { described_class.build(diaspora_entity) } let(:federation_entity) { described_class.build(diaspora_entity) }
it "builds a conversation" do it "builds a conversation" do
@ -45,7 +46,7 @@ describe Diaspora::Federation::Entities do
it "adds the participants" do it "adds the participants" do
expect(federation_entity.participants) expect(federation_entity.participants)
.to eq("#{diaspora_entity.author.diaspora_handle};#{diaspora_entity.messages.first.author.diaspora_handle}") .to eq("#{participant.diaspora_handle};#{diaspora_entity.author.diaspora_handle}")
end end
it "includes the message" do it "includes the message" do

View file

@ -21,7 +21,7 @@ describe Message, type: :model do
expect(message).not_to be_valid expect(message).not_to be_valid
end end
it_behaves_like "it is relayable" do describe "#subscribers" do
let(:cnv_hash) { let(:cnv_hash) {
{ {
participant_ids: [local_luke.person, local_leia.person, remote_raphael].map(&:id), participant_ids: [local_luke.person, local_leia.person, remote_raphael].map(&:id),
@ -29,14 +29,23 @@ describe Message, type: :model do
messages_attributes: [{author: remote_raphael, text: "hey"}] messages_attributes: [{author: remote_raphael, text: "hey"}]
} }
} }
let(:remote_parent) { Conversation.create(cnv_hash.merge(author: remote_raphael)) } let(:local_conv) { Conversation.create(cnv_hash.merge(author: local_luke.person)) }
let(:local_parent) { Conversation.create(cnv_hash.merge(author: local_luke.person)) } let(:remote_conv) { Conversation.create(cnv_hash.merge(author: remote_raphael)) }
let(:object_on_local_parent) { Message.create(author: local_luke.person, text: "yo", conversation: local_parent) }
let(:object_on_remote_parent) { Message.create(author: local_luke.person, text: "yo", conversation: remote_parent) } it "returns all participants, if the conversation and the author is local" do
let(:remote_object_on_local_parent) { message = Message.create(author: local_luke.person, text: "yo", conversation: local_conv)
Message.create(author: remote_raphael, text: "yo", conversation: local_parent) expect(message.subscribers).to match_array([local_luke.person, local_leia.person, remote_raphael])
} end
let(:relayable) { Message.new(author: alice.person, text: "ohai!", conversation: conversation) }
it "returns all participants, if the author is local and the conversation is remote" do
message = Message.create(author: local_luke.person, text: "yo", conversation: remote_conv)
expect(message.subscribers).to match_array([local_luke.person, local_leia.person, remote_raphael])
end
it "returns only remote participants, if the conversation is local, but the author is remote" do
message = Message.create(author: remote_raphael, text: "yo", conversation: local_conv)
expect(message.subscribers).to match_array([remote_raphael])
end
end end
describe "#increase_unread" do describe "#increase_unread" do