diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index fe45b7064..a7c74d516 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -13,7 +13,7 @@ module NotificationsHelper when 'status_message' link_to translation, status_message_path(note.object_id) when 'comment' - link_to translation, object_path(Comment.first(object_id).post) + link_to translation, object_path(Comment.first(:id => object_id).post) when 'photo' link_to translation, photo_path(note.object_id) else diff --git a/app/models/comment.rb b/app/models/comment.rb index 8d2dba86a..39d43d19d 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -42,6 +42,14 @@ class Comment timestamps! + def notification_type(user, person) + if self.post.diaspora_handle == user.diaspora_handle + return "comment_on_post" + else + return false + end + end + #ENCRYPTION xml_reader :creator_signature @@ -71,6 +79,7 @@ class Comment def signature_valid? verify_signature(creator_signature, person) end + def self.hash_from_post_ids post_ids hash = {} comments = self.on_posts(post_ids) @@ -83,6 +92,8 @@ class Comment hash.each_value {|comments| comments.sort!{|c1, c2| c1.created_at <=> c2.created_at }} hash end + + scope :on_posts, lambda { |post_ids| where(:post_id.in => post_ids) } diff --git a/app/models/notification.rb b/app/models/notification.rb index 439b1fe80..3b24dc4fa 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -5,7 +5,6 @@ class Notification include MongoMapper::Document - key :object_id, ObjectId key :kind, String @@ -19,4 +18,15 @@ class Notification def self.for(user, opts={}) self.where(opts.merge(:user_id => user.id)) end + + def self.notify(user, object, person) + if object.respond_to? :notification_type + if kind = object.notification_type(user, person) + Notification.create(:object_id => object.id, + :kind => kind, + :person_id => person.id, + :user_id => user.id) + end + end + end end diff --git a/app/models/request.rb b/app/models/request.rb index b01680173..4a2f8b442 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -72,6 +72,16 @@ class Request senders.each{|sender| senders_hash[sender.id] = sender} requests.map{|r| {:request => r, :sender => senders_hash[r.from_id]}} end + + + def notification_type(user, person) + if Contact.first(:user_id => user.id, :person_id => person.id) + "request_accepted" + else + "new_request" + end + end + private def not_already_connected diff --git a/app/models/user.rb b/app/models/user.rb index 6024e9713..ffbbd09de 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -52,7 +52,7 @@ class User many :aspects, :class => Aspect, :dependent => :destroy many :services, :class => Service - + timestamps! #after_create :seed_aspects before_destroy :disconnect_everyone, :remove_person diff --git a/lib/diaspora/user/receiving.rb b/lib/diaspora/user/receiving.rb index 1c831b7f6..7dd441fae 100644 --- a/lib/diaspora/user/receiving.rb +++ b/lib/diaspora/user/receiving.rb @@ -58,20 +58,21 @@ module Diaspora else receive_object(object,person) Rails.logger.info("event=receive status=complete recipient=#{self.diaspora_handle} sender=#{salmon_author.diaspora_handle} payload_type#{object.class}") + + unless object.is_a? Retraction + Notification.notify(self, object, person) + end + return object end end end def receive_object(object,person) - unless object.is_a? Retraction - Notification.create(:object_id => object.id, :kind => object.class.name, :person_id => person.id, :user_id => self.id) - end if object.is_a?(Request) receive_request object, person elsif object.is_a?(Profile) receive_profile object, person - elsif object.is_a?(Comment) receive_comment object elsif object.is_a?(Retraction) diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 8b46e8538..04174b29d 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -54,6 +54,31 @@ describe Comment do } end end + + describe 'comment#notification_type' do + before do + @not_your_post = user2.post(:status_message, :message => 'yo', :to => aspect2.id) + @hello = user.post(:status_message, :message => "hello", :to => aspect.id) + @c11 = user2.comment "why so formal?", :on => @hello + @c12 = user.comment "I simply felt like issuing a greeting. Do step off.", :on => @hello + @c12 = user2.comment "I simply felt like issuing a greeting. Do step off.", :on => @not_your_post + + end + + it "returns 'comment_on_post' if the comment is on a post you own" do + @c11.notification_type(user, user2.person).should == 'comment_on_post' + + end + + it 'returns false if the comment is not on a post you own' do + @c11.notification_type(user2, user.person).should == false + end + end + + + + + describe 'User#comment' do before do @status = user.post(:status_message, :message => "hello", :to => aspect.id) diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index af54d8391..fc5329e46 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -10,6 +10,8 @@ describe Notification do @sm = Factory(:status_message) @person = Factory(:person) @user = make_user + @user2 = make_user + @aspect = @user.aspects.create(:name => "dudes") @opts = {:object_id => @sm.id, :kind => @sm.class.name, :person_id => @person.id, :user_id => @user.id} @note = Notification.new(@opts) end @@ -40,5 +42,18 @@ describe Notification do Notification.for(@user).count.should == 4 end end + + describe '.notify' do + it ' does not call Notification.create if the object does not notification_type' do + Notification.should_not_receive(:create) + Notification.notify(@user, @sm, @person) + end + + it ' does not call Notification.create if the object does not notification_type' do + request = Request.instantiate(:from => @user.person, :to => @user2.person, :into => @aspect) + Notification.should_receive(:create).once + Notification.notify(@user, request, @person) + end + end end diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb index 0786a8286..aaa1fc1e4 100644 --- a/spec/models/request_spec.rb +++ b/spec/models/request_spec.rb @@ -80,6 +80,19 @@ describe Request do Request.from(@user).to(@user2.person).first.should == @request end end + describe '#notification_type' do + before do + @request = Request.instantiate(:from => @user.person, :to => @user2.person, :into => @aspect) + end + it "returns 'request_accepted' if there is a pending contact" do + Contact.create(:user_id => @user.id, :person_id => @person.id) + @request.notification_type(@user, @person).should == "request_accepted" + end + + it 'returns new_request if there is not a pending contact' do + @request.notification_type(@user, @person).should == "new_request" + end + end describe '.hashes_for_person' do before do diff --git a/spec/models/user/receive_spec.rb b/spec/models/user/receive_spec.rb index 11841fc47..33f74a123 100644 --- a/spec/models/user/receive_spec.rb +++ b/spec/models/user/receive_spec.rb @@ -59,18 +59,6 @@ describe User do end end - describe '#receive_object' do - it 'adds a notification for an object' do - Notification.should_receive(:create) - user = make_user - sm = Factory.create(:status_message) - person = Factory.create(:person) - user.should_receive(:receive_post).and_return(true) - user.receive_object(sm, person) - - end - end - context 'update posts' do it 'does not update posts not marked as mutable' do status = user.post :status_message, :message => "store this!", :to => aspect.id