dispatch the conversation in ConversationsController

This commit is contained in:
danielgrippi 2011-03-07 17:54:25 -08:00
parent 97aff09140
commit fca5310c77
11 changed files with 68 additions and 23 deletions

View file

@ -7,7 +7,7 @@ class ApplicationController < ActionController::Base
protect_from_forgery :except => :receive protect_from_forgery :except => :receive
before_filter :ensure_http_referer_is_set before_filter :ensure_http_referer_is_set
before_filter :set_contacts_notifications_and_status, :except => [:create, :update] before_filter :set_contacts_notifications_unread_count_and_status, :except => [:create, :update]
before_filter :count_requests before_filter :count_requests
before_filter :set_invites before_filter :set_invites
before_filter :set_locale before_filter :set_locale
@ -22,12 +22,13 @@ class ApplicationController < ActionController::Base
request.env['HTTP_REFERER'] ||= '/aspects' request.env['HTTP_REFERER'] ||= '/aspects'
end end
def set_contacts_notifications_and_status def set_contacts_notifications_unread_count_and_status
if user_signed_in? if user_signed_in?
@aspect = nil @aspect = nil
@object_aspect_ids = [] @object_aspect_ids = []
@all_aspects = current_user.aspects.includes(:aspect_memberships, :post_visibilities) @all_aspects = current_user.aspects.includes(:aspect_memberships, :post_visibilities)
@notification_count = Notification.for(current_user, :unread =>true).count @notification_count = Notification.for(current_user, :unread =>true).count
@unread_message_count = ConversationVisibility.sum(:unread, :conditions => "person_id = #{current_user.person.id}")
@user_id = current_user.id @user_id = current_user.id
end end
end end

View file

@ -24,13 +24,15 @@ class ConversationsController < ApplicationController
params[:conversation][:participant_ids] = person_ids | [current_user.person.id] params[:conversation][:participant_ids] = person_ids | [current_user.person.id]
params[:conversation][:author] = current_user.person params[:conversation][:author] = current_user.person
@conversation = Conversation.create(params[:conversation]) if @conversation = Conversation.create(params[:conversation])
Postzord::Dispatch.new(current_user, @conversation).post
flash[:notice] = "Message sent" flash[:notice] = "Message sent"
if params[:profile] if params[:profile]
redirect_to person_path(params[:profile]) redirect_to person_path(params[:profile])
else else
redirect_to conversations_path(:conversation_id => @conversation.id) redirect_to conversations_path(:conversation_id => @conversation.id)
end
end end
end end

View file

@ -61,7 +61,7 @@ class Conversation < ActiveRecord::Base
cnv = Conversation.find_or_create_by_guid(self.attributes) cnv = Conversation.find_or_create_by_guid(self.attributes)
self.participants.each do |participant| self.participants.each do |participant|
ConversationVisibility.create(:conversation_id => cnv.id, :person_id => participant.id) ConversationVisibility.find_or_create_by_conversation_id_and_person_id(cnv.id, participant.id)
end end
self.messages.each do |msg| self.messages.each do |msg|
msg.conversation_id = cnv.id msg.conversation_id = cnv.id

View file

@ -57,6 +57,16 @@ class Message < ActiveRecord::Base
self.conversation = parent self.conversation = parent
end end
def after_receive(user, person)
if vis = ConversationVisibility.where(:conversation_id => self.conversation_id, :person_id => user.person.id).first
vis.unread += 1
vis.save
self
else
raise NotVisibileException("Attempting to access a ConversationVisibility that does not exist!")
end
end
private private
def participant_of_parent_conversation def participant_of_parent_conversation
if self.parent && !self.parent.participants.include?(self.author) if self.parent && !self.parent.participants.include?(self.author)

View file

@ -12,7 +12,6 @@
:css :css
footer{ display:none;} footer{ display:none;}
= hidden_field_tag :contact_json, @all_contacts_and_ids.to_json = hidden_field_tag :contact_json, @all_contacts_and_ids.to_json
#left_pane #left_pane

View file

@ -30,8 +30,8 @@
#message_inbox_badge #message_inbox_badge
= link_to "", conversations_path #, :title => new_notification_text(@notification_count) = link_to "", conversations_path #, :title => new_notification_text(@notification_count)
= image_tag 'icons/mail_grey.png' = image_tag 'icons/mail_grey.png'
.badge_count{:class => ("hidden" if @notification_count == 0)} .badge_count{:class => ("hidden" if @unread_message_count == 0)}
= @notification_count = @unread_message_count
%ul#user_menu %ul#user_menu
.right .right

View file

@ -50,7 +50,13 @@ module Diaspora
end end
object.socket_to_user(user, :aspect_ids => object.parent.aspect_ids) if object.respond_to? :socket_to_user object.socket_to_user(user, :aspect_ids => object.parent.aspect_ids) if object.respond_to? :socket_to_user
object if object.after_receive(user, person)
object
end
end
def after_receive(user, person)
self
end end
def signable_string def signable_string

View file

@ -358,9 +358,9 @@ header
:margin :margin
:bottom 0px :bottom 0px
:font :font
:size 12px :size 13px
:line :line
:height 16px :height 18px
.photo_attachments .photo_attachments
:margin :margin

View file

@ -37,9 +37,9 @@ describe ConversationsController do
describe '#create' do describe '#create' do
before do before do
@hash = {:conversation => { @hash = {:conversation => {
:contact_ids => [@user1.contacts.first.id],
:subject => "secret stuff", :subject => "secret stuff",
:text => 'text'}} :text => 'text'},
:contact_ids => '@user1.contacts.first.id'}
end end
it 'creates a conversation' do it 'creates a conversation' do
@ -61,6 +61,17 @@ describe ConversationsController do
Message.first.author.should == @user1.person Message.first.author.should == @user1.person
Conversation.first.author.should == @user1.person Conversation.first.author.should == @user1.person
end end
it 'dispatches the conversation' do
cnv = Conversation.create(@hash[:conversation].merge({
:author => @user1.person,
:participant_ids => [@user1.contacts.first.person.id]}))
p = Postzord::Dispatch.new(@user1, cnv)
Postzord::Dispatch.stub!(:new).and_return(p)
p.should_receive(:post)
post :create, @hash
end
end end
describe '#show' do describe '#show' do

View file

@ -78,5 +78,16 @@ describe Message do
Postzord::Dispatch.new(@local_luke, @object_on_remote_parent).post Postzord::Dispatch.new(@local_luke, @object_on_remote_parent).post
end end
it_should_behave_like 'it is relayable' it_should_behave_like 'it is relayable'
describe '#after_receive' do
it 'increments the conversation visiblity for the conversation' do
ConversationVisibility.where(:conversation_id => @object_by_recipient.reload.conversation.id,
:person_id => @local_luke.person.id).first.unread.should == 0
@object_by_recipient.receive(@local_luke, @local_leia.person)
ConversationVisibility.where(:conversation_id => @object_by_recipient.reload.conversation.id,
:person_id => @local_luke.person.id).first.unread.should == 1
end
end
end end
end end

View file

@ -8,7 +8,7 @@ describe Diaspora::Relayable do
shared_examples_for "it is relayable" do shared_examples_for "it is relayable" do
context 'encryption' do context 'encryption' do
describe '#parent_author_signature' do describe '#parent_author_signature' do
it 'should sign the comment if the user is the post author' do it 'should sign the object if the user is the post author' do
@object_by_parent_author.verify_parent_author_signature.should be_true @object_by_parent_author.verify_parent_author_signature.should be_true
end end
@ -17,7 +17,7 @@ describe Diaspora::Relayable do
@object_by_recipient.verify_parent_author_signature.should be_false @object_by_recipient.verify_parent_author_signature.should be_false
end end
it 'should verify a comment made on a remote post by a different contact' do it 'should verify a object made on a remote post by a different contact' do
@object_by_recipient.author_signature = @object_by_recipient.send(:sign_with_key, @local_leia.encryption_key) @object_by_recipient.author_signature = @object_by_recipient.send(:sign_with_key, @local_leia.encryption_key)
@object_by_recipient.parent_author_signature = @object_by_recipient.send(:sign_with_key, @local_luke.encryption_key) @object_by_recipient.parent_author_signature = @object_by_recipient.send(:sign_with_key, @local_luke.encryption_key)
@object_by_recipient.verify_parent_author_signature.should be_true @object_by_recipient.verify_parent_author_signature.should be_true
@ -35,14 +35,14 @@ describe Diaspora::Relayable do
context 'propagation' do context 'propagation' do
describe '#receive' do describe '#receive' do
it 'does not overwrite a comment that is already in the db' do it 'does not overwrite a object that is already in the db' do
lambda{ lambda{
@dup_object_by_parent_author.receive(@local_leia, @local_luke.person) @dup_object_by_parent_author.receive(@local_leia, @local_luke.person)
}.should_not change(Comment, :count) }.should_not change(@dup_object_by_parent_author.class, :count)
end end
it 'does not process if post_creator_signature is invalid' do it 'does not process if post_creator_signature is invalid' do
@object_by_parent_author.delete # remove comment from db so we set a creator sig @object_by_parent_author.delete # remove object from db so we set a creator sig
@dup_object_by_parent_author.parent_author_signature = "dsfadsfdsa" @dup_object_by_parent_author.parent_author_signature = "dsfadsfdsa"
@dup_object_by_parent_author.receive(@local_leia, @local_luke.person).should == nil @dup_object_by_parent_author.receive(@local_leia, @local_luke.person).should == nil
end end
@ -65,6 +65,11 @@ describe Diaspora::Relayable do
@object_by_recipient.should_receive(:socket_to_user).exactly(3).times @object_by_recipient.should_receive(:socket_to_user).exactly(3).times
@object_by_recipient.receive(@local_luke, @local_leia.person) @object_by_recipient.receive(@local_luke, @local_leia.person)
end end
it 'calls after_receive callback' do
@object_by_recipient.should_receive(:after_receive)
@object_by_recipient.receive(@local_luke, @local_leia.person)
end
end end
describe '#subscribers' do describe '#subscribers' do
@ -72,7 +77,7 @@ describe Diaspora::Relayable do
@object_by_parent_author.subscribers(@local_luke).map(&:id).should =~ [@local_leia.person, @remote_raphael].map(&:id) @object_by_parent_author.subscribers(@local_luke).map(&:id).should =~ [@local_leia.person, @remote_raphael].map(&:id)
end end
it 'returns the owner of the original post, if the user owns the comment' do it 'returns the owner of the original post, if the user owns the object' do
@object_by_recipient.subscribers(@local_leia).map(&:id).should =~ [@local_luke.person].map(&:id) @object_by_recipient.subscribers(@local_leia).map(&:id).should =~ [@local_luke.person].map(&:id)
end end
end end