refactored notifications

This commit is contained in:
maxwell 2010-12-16 17:23:21 -08:00
parent 647c48d530
commit e7cca604e6
10 changed files with 92 additions and 19 deletions

View file

@ -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

View file

@ -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)
}

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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