diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2174c8f2d2f1d1e3994ae203def62b93f6266b5b..93ede8c9a0a4416995d35bee7416c559dd0721a8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/conversations_controller.rb b/app/controllers/conversations_controller.rb index d803ee65f76598665fc533ed2223900d6a67ab6a..dd083860265ef21933a695a773479b01eb51506c 100644 --- a/app/controllers/conversations_controller.rb +++ b/app/controllers/conversations_controller.rb @@ -24,13 +24,15 @@ class ConversationsController < ApplicationController params[:conversation][:participant_ids] = person_ids | [current_user.person.id] params[:conversation][:author] = current_user.person - @conversation = Conversation.create(params[:conversation]) - - flash[:notice] = "Message sent" - if params[:profile] - redirect_to person_path(params[:profile]) - else - redirect_to conversations_path(:conversation_id => @conversation.id) + if @conversation = Conversation.create(params[:conversation]) + Postzord::Dispatch.new(current_user, @conversation).post + + flash[:notice] = "Message sent" + if params[:profile] + redirect_to person_path(params[:profile]) + else + redirect_to conversations_path(:conversation_id => @conversation.id) + end end end diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 640984f16c6f7913ad381624f37c5f05fab15934..a1e40665d7369ca215ce961437eb7f806ed425f7 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -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 diff --git a/app/models/message.rb b/app/models/message.rb index 6a2f1adf456dc4d2a735c98490312b44f199faf7..77c5faba026eed599c8b76b6405e36994c1a6b6c 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -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) diff --git a/app/views/conversations/index.haml b/app/views/conversations/index.haml index 28fecb93cb186be9d7bd2946a0f2000cb4e28785..870833e927e9a066a6928bfca1d4b0e902380161 100644 --- a/app/views/conversations/index.haml +++ b/app/views/conversations/index.haml @@ -12,7 +12,6 @@ :css footer{ display:none;} - = hidden_field_tag :contact_json, @all_contacts_and_ids.to_json #left_pane diff --git a/app/views/layouts/_header.html.haml b/app/views/layouts/_header.html.haml index 40b3f503128edefb26c276e4578da27003aa4aa9..7184c98e6cd97b56bff288deb6012ef108c75a03 100644 --- a/app/views/layouts/_header.html.haml +++ b/app/views/layouts/_header.html.haml @@ -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 diff --git a/lib/diaspora/relayable.rb b/lib/diaspora/relayable.rb index 0197b8b2e4981da95886d43294f21ad619142cb5..476ae7649845ac0060221bec35eaa1d08ffb55ea 100644 --- a/lib/diaspora/relayable.rb +++ b/lib/diaspora/relayable.rb @@ -50,7 +50,13 @@ module Diaspora end 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 def signable_string diff --git a/public/stylesheets/sass/application.sass b/public/stylesheets/sass/application.sass index 59bd5304dd4109a056c07b0164432cb0ff603e2f..d017757a54e6a18cc792c1cd445e2ec2a03f17ab 100644 --- a/public/stylesheets/sass/application.sass +++ b/public/stylesheets/sass/application.sass @@ -358,9 +358,9 @@ header :margin :bottom 0px :font - :size 12px + :size 13px :line - :height 16px + :height 18px .photo_attachments :margin diff --git a/spec/controllers/conversations_controller_spec.rb b/spec/controllers/conversations_controller_spec.rb index d567d9b62de701b098b89f4b614a1da6e9dfbc55..50f6ed14a0686265fcf264904fc5cba77031b46d 100644 --- a/spec/controllers/conversations_controller_spec.rb +++ b/spec/controllers/conversations_controller_spec.rb @@ -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 diff --git a/spec/models/message_spec.rb b/spec/models/message_spec.rb index faf2bda984da530c05de4ebe29c8d7529cb09b80..99aca42df6eff9fdf049b9156386d0a8f2d66b96 100644 --- a/spec/models/message_spec.rb +++ b/spec/models/message_spec.rb @@ -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 diff --git a/spec/shared_behaviors/relayable.rb b/spec/shared_behaviors/relayable.rb index b24a3a50704255764a2c9ca3ad3173c581dea951..ba809119a3b9b57d0be72a8815185150b57814fa 100644 --- a/spec/shared_behaviors/relayable.rb +++ b/spec/shared_behaviors/relayable.rb @@ -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