notifications now properly socket (not a hack through comments like before). notification badge updates on new messages.
This commit is contained in:
parent
8b29e99cf8
commit
4ff9622bfc
8 changed files with 86 additions and 51 deletions
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
module SocketsHelper
|
||||
include ApplicationHelper
|
||||
include NotificationsHelper
|
||||
|
||||
def obj_id(object)
|
||||
object.respond_to?(:post_id) ? object.post_id : object.id
|
||||
|
|
@ -39,8 +40,13 @@ module SocketsHelper
|
|||
:request => user.request_for(object),
|
||||
:current_user => user}
|
||||
v = render_to_string(:partial => 'people/person', :locals => person_hash)
|
||||
|
||||
elsif object.is_a? Comment
|
||||
v = render_to_string(:partial => 'comments/comment', :locals => {:hash => {:comment => object, :person => object.person}})
|
||||
|
||||
elsif object.is_a? Notification
|
||||
v = render_to_string(:partial => 'notifications/popup', :locals => {:note => object, :person => object.person})
|
||||
|
||||
else
|
||||
v = render_to_string(:partial => type_partial(object), :locals => {:post => object, :current_user => user}) unless object.is_a? Retraction
|
||||
end
|
||||
|
|
@ -58,7 +64,6 @@ module SocketsHelper
|
|||
post = object.post
|
||||
action_hash[:comment_id] = object.id
|
||||
action_hash[:my_post?] = (post.person.owner.id == uid)
|
||||
action_hash[:notification] = notification(object)
|
||||
action_hash[:post_guid] = post.id
|
||||
|
||||
end
|
||||
|
|
@ -69,12 +74,4 @@ module SocketsHelper
|
|||
|
||||
action_hash.to_json
|
||||
end
|
||||
|
||||
def notification(object)
|
||||
begin
|
||||
render_to_string(:partial => 'shared/notification', :locals => {:object => object})
|
||||
rescue Exception => e
|
||||
Rails.logger.error("event=socket_render status=fail user=#{user.diaspora_handle} object=#{object.id.to_s}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@
|
|||
# the COPYRIGHT file.
|
||||
#
|
||||
class Notification
|
||||
require File.join(Rails.root, 'lib/diaspora/web_socket')
|
||||
include MongoMapper::Document
|
||||
include Diaspora::Socketable
|
||||
|
||||
key :target_id, ObjectId
|
||||
key :kind, String
|
||||
|
|
@ -23,10 +25,12 @@ class Notification
|
|||
def self.notify(user, object, person)
|
||||
if object.respond_to? :notification_type
|
||||
if kind = object.notification_type(user, person)
|
||||
Notification.create(:target_id => object.id,
|
||||
n = Notification.create(:target_id => object.id,
|
||||
:kind => kind,
|
||||
:person_id => person.id,
|
||||
:user_id => user.id)
|
||||
n.socket_to_uid(user.id) if n
|
||||
n
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -25,8 +25,7 @@
|
|||
#notification_badge
|
||||
= link_to "", notifications_path, :title => new_notification_text(@notification_count)
|
||||
= image_tag 'icons/mail_grey.png'
|
||||
- if @notification_count > 0
|
||||
#notification_badge_number
|
||||
#notification_badge_number{:class => ("hidden" if @notification_count == 0)}
|
||||
= @notification_count
|
||||
|
||||
%ul#user_menu
|
||||
|
|
|
|||
6
app/views/notifications/_popup.haml
Normal file
6
app/views/notifications/_popup.haml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
-# Copyright (c) 2010, Diaspora Inc. This file is
|
||||
-# licensed under the Affero General Public License version 3 or later. See
|
||||
-# the COPYRIGHT file.
|
||||
|
||||
= link_to "#{person.name.titleize}", person_path(person)
|
||||
= object_link(note)
|
||||
|
|
@ -3,3 +3,7 @@
|
|||
-# the COPYRIGHT file.
|
||||
|
||||
= link_to t('.new', :type => object.class.to_s, :from => object.person.name), object_path(object.post)
|
||||
|
||||
|
||||
= link_to "#{note.person.name.titelize}", person_path(note.person)
|
||||
= object_link(note)
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@ var WebSocketReceiver = {
|
|||
|
||||
onMessage: function(evt) {
|
||||
var obj = jQuery.parseJSON(evt.data);
|
||||
if(obj['notice']){
|
||||
WebSocketReceiver.processNotification(obj['notice']);
|
||||
if(obj['class'] == 'notifications'){
|
||||
WebSocketReceiver.processNotification(obj);
|
||||
|
||||
}else if (obj['class'] == 'people'){
|
||||
WSR.debug("got a " + obj['class']);
|
||||
|
|
@ -34,7 +34,8 @@ var WebSocketReceiver = {
|
|||
}
|
||||
}
|
||||
},
|
||||
processPerson: function(response){
|
||||
|
||||
processPerson: function(response){
|
||||
form = $('.webfinger_form');
|
||||
form.siblings('#loader').hide();
|
||||
result_ul = form.siblings('#request_result');
|
||||
|
|
@ -52,8 +53,24 @@ processPerson: function(response){
|
|||
},
|
||||
|
||||
|
||||
processNotification: function(html){
|
||||
$('#notification').html(html).fadeIn(200).delay(8000).fadeOut(200, function(){ $(this).html("");});
|
||||
processNotification: function(notification){
|
||||
|
||||
var nBadge = $("#notification_badge_number");
|
||||
|
||||
nBadge.html().replace(/\d+/, function(num){
|
||||
nBadge.html(parseInt(num)+1);
|
||||
});
|
||||
|
||||
if(nBadge.hasClass("hidden")){
|
||||
nBadge.removeClass("hidden");
|
||||
}
|
||||
|
||||
$('#notification').html(notification['html'])
|
||||
.fadeIn(200)
|
||||
.delay(8000)
|
||||
.fadeOut(200, function(){
|
||||
$(this).html("");
|
||||
});
|
||||
},
|
||||
|
||||
processRetraction: function(post_id){
|
||||
|
|
@ -86,10 +103,6 @@ processPerson: function(response){
|
|||
$(".show_comments", post).removeClass('hidden');
|
||||
}
|
||||
}
|
||||
|
||||
if( !opts['mine?'] && opts['my_post?']) {
|
||||
WebSocketReceiver.processNotification(opts['notification']);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
|
|
|
|||
|
|
@ -1523,7 +1523,6 @@ h3 span.current_gs_step
|
|||
:bottom 21px
|
||||
:right 12px
|
||||
|
||||
a
|
||||
:background
|
||||
:color rgb(30,30,30)
|
||||
:color rgba(30,30,30,0.9)
|
||||
|
|
|
|||
|
|
@ -31,10 +31,9 @@ describe Notification do
|
|||
describe '.for' do
|
||||
it 'returns all of a users notifications' do
|
||||
user2 = make_user
|
||||
4.times do
|
||||
Notification.create(@opts)
|
||||
Notification.create(@opts)
|
||||
Notification.create(@opts)
|
||||
Notification.create(@opts)
|
||||
end
|
||||
|
||||
@opts.delete(:user_id)
|
||||
Notification.create(@opts.merge(:user_id => user2.id))
|
||||
|
|
@ -44,16 +43,30 @@ describe Notification do
|
|||
end
|
||||
|
||||
describe '.notify' do
|
||||
it ' does not call Notification.create if the object does not notification_type' 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
|
||||
it 'does 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
|
||||
|
||||
it 'sockets to the recipient' do
|
||||
request = Request.instantiate(:from => @user.person, :to => @user2.person, :into => @aspect)
|
||||
opts = {:target_id => request.id,
|
||||
:kind => request.notification_type(@user, @person),
|
||||
:person_id => @person.id,
|
||||
:user_id => @user.id}
|
||||
|
||||
n = Notification.create(opts)
|
||||
Notification.stub!(:create).and_return n
|
||||
|
||||
n.should_receive(:socket_to_uid).once
|
||||
Notification.notify(@user, request, @person)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue