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
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 :set_invites
before_filter :set_locale
@ -22,12 +22,13 @@ class ApplicationController < ActionController::Base
request.env['HTTP_REFERER'] ||= '/aspects'
end
def set_contacts_notifications_and_status
def set_contacts_notifications_unread_count_and_status
if user_signed_in?
@aspect = nil
@object_aspect_ids = []
@all_aspects = current_user.aspects.includes(:aspect_memberships, :post_visibilities)
@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
end
end

View file

@ -24,7 +24,8 @@ class ConversationsController < ApplicationController
params[:conversation][:participant_ids] = person_ids | [current_user.person.id]
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"
if params[:profile]
@ -33,6 +34,7 @@ class ConversationsController < ApplicationController
redirect_to conversations_path(:conversation_id => @conversation.id)
end
end
end
def show
@conversation = Conversation.joins(:conversation_visibilities).where(:id => params[:id],

View file

@ -61,7 +61,7 @@ class Conversation < ActiveRecord::Base
cnv = Conversation.find_or_create_by_guid(self.attributes)
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
self.messages.each do |msg|
msg.conversation_id = cnv.id

View file

@ -57,6 +57,16 @@ class Message < ActiveRecord::Base
self.conversation = parent
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
def participant_of_parent_conversation
if self.parent && !self.parent.participants.include?(self.author)

View file

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

View file

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

View file

@ -50,8 +50,14 @@ module Diaspora
end
object.socket_to_user(user, :aspect_ids => object.parent.aspect_ids) if object.respond_to? :socket_to_user
if object.after_receive(user, person)
object
end
end
def after_receive(user, person)
self
end
def signable_string
raise NotImplementedException("Override this in your encryptable class")

View file

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

View file

@ -37,9 +37,9 @@ describe ConversationsController do
describe '#create' do
before do
@hash = {:conversation => {
:contact_ids => [@user1.contacts.first.id],
:subject => "secret stuff",
:text => 'text'}}
:text => 'text'},
:contact_ids => '@user1.contacts.first.id'}
end
it 'creates a conversation' do
@ -61,6 +61,17 @@ describe ConversationsController do
Message.first.author.should == @user1.person
Conversation.first.author.should == @user1.person
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
describe '#show' do

View file

@ -78,5 +78,16 @@ describe Message do
Postzord::Dispatch.new(@local_luke, @object_on_remote_parent).post
end
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

View file

@ -8,7 +8,7 @@ describe Diaspora::Relayable do
shared_examples_for "it is relayable" do
context 'encryption' 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
end
@ -17,7 +17,7 @@ describe Diaspora::Relayable do
@object_by_recipient.verify_parent_author_signature.should be_false
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.parent_author_signature = @object_by_recipient.send(:sign_with_key, @local_luke.encryption_key)
@object_by_recipient.verify_parent_author_signature.should be_true
@ -35,14 +35,14 @@ describe Diaspora::Relayable do
context 'propagation' 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{
@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
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.receive(@local_leia, @local_luke.person).should == nil
end
@ -65,6 +65,11 @@ describe Diaspora::Relayable do
@object_by_recipient.should_receive(:socket_to_user).exactly(3).times
@object_by_recipient.receive(@local_luke, @local_leia.person)
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
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)
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)
end
end