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
|
module SocketsHelper
|
||||||
include ApplicationHelper
|
include ApplicationHelper
|
||||||
|
include NotificationsHelper
|
||||||
|
|
||||||
def obj_id(object)
|
def obj_id(object)
|
||||||
object.respond_to?(:post_id) ? object.post_id : object.id
|
object.respond_to?(:post_id) ? object.post_id : object.id
|
||||||
|
|
@ -16,7 +17,7 @@ module SocketsHelper
|
||||||
old_locale = I18n.locale
|
old_locale = I18n.locale
|
||||||
I18n.locale = user.language.to_s
|
I18n.locale = user.language.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
if object.is_a? StatusMessage
|
if object.is_a? StatusMessage
|
||||||
post_hash = {:post => object,
|
post_hash = {:post => object,
|
||||||
:person => object.person,
|
:person => object.person,
|
||||||
|
|
@ -32,15 +33,20 @@ module SocketsHelper
|
||||||
v = render_to_string(:partial => 'shared/stream_element', :locals => post_hash)
|
v = render_to_string(:partial => 'shared/stream_element', :locals => post_hash)
|
||||||
elsif object.is_a? Person
|
elsif object.is_a? Person
|
||||||
person_hash = {
|
person_hash = {
|
||||||
:single_aspect_form => opts["single_aspect_form"],
|
:single_aspect_form => opts["single_aspect_form"],
|
||||||
:person => object,
|
:person => object,
|
||||||
:aspects => user.aspects,
|
:aspects => user.aspects,
|
||||||
:contact => user.contact_for(object),
|
:contact => user.contact_for(object),
|
||||||
:request => user.request_for(object),
|
:request => user.request_for(object),
|
||||||
:current_user => user}
|
:current_user => user}
|
||||||
v = render_to_string(:partial => 'people/person', :locals => person_hash)
|
v = render_to_string(:partial => 'people/person', :locals => person_hash)
|
||||||
|
|
||||||
elsif object.is_a? Comment
|
elsif object.is_a? Comment
|
||||||
v = render_to_string(:partial => 'comments/comment', :locals => {:hash => {:comment => object, :person => object.person}})
|
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
|
else
|
||||||
v = render_to_string(:partial => type_partial(object), :locals => {:post => object, :current_user => user}) unless object.is_a? Retraction
|
v = render_to_string(:partial => type_partial(object), :locals => {:post => object, :current_user => user}) unless object.is_a? Retraction
|
||||||
end
|
end
|
||||||
|
|
@ -58,23 +64,14 @@ module SocketsHelper
|
||||||
post = object.post
|
post = object.post
|
||||||
action_hash[:comment_id] = object.id
|
action_hash[:comment_id] = object.id
|
||||||
action_hash[:my_post?] = (post.person.owner.id == uid)
|
action_hash[:my_post?] = (post.person.owner.id == uid)
|
||||||
action_hash[:notification] = notification(object)
|
|
||||||
action_hash[:post_guid] = post.id
|
action_hash[:post_guid] = post.id
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
action_hash[:mine?] = object.person && (object.person.owner.id == uid) if object.respond_to?(:person)
|
action_hash[:mine?] = object.person && (object.person.owner.id == uid) if object.respond_to?(:person)
|
||||||
|
|
||||||
I18n.locale = old_locale unless user.nil?
|
I18n.locale = old_locale unless user.nil?
|
||||||
|
|
||||||
action_hash.to_json
|
action_hash.to_json
|
||||||
end
|
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
|
end
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,9 @@
|
||||||
# the COPYRIGHT file.
|
# the COPYRIGHT file.
|
||||||
#
|
#
|
||||||
class Notification
|
class Notification
|
||||||
|
require File.join(Rails.root, 'lib/diaspora/web_socket')
|
||||||
include MongoMapper::Document
|
include MongoMapper::Document
|
||||||
|
include Diaspora::Socketable
|
||||||
|
|
||||||
key :target_id, ObjectId
|
key :target_id, ObjectId
|
||||||
key :kind, String
|
key :kind, String
|
||||||
|
|
@ -23,10 +25,12 @@ class Notification
|
||||||
def self.notify(user, object, person)
|
def self.notify(user, object, person)
|
||||||
if object.respond_to? :notification_type
|
if object.respond_to? :notification_type
|
||||||
if kind = object.notification_type(user, person)
|
if kind = object.notification_type(user, person)
|
||||||
Notification.create(:target_id => object.id,
|
n = Notification.create(:target_id => object.id,
|
||||||
:kind => kind,
|
:kind => kind,
|
||||||
:person_id => person.id,
|
:person_id => person.id,
|
||||||
:user_id => user.id)
|
:user_id => user.id)
|
||||||
|
n.socket_to_uid(user.id) if n
|
||||||
|
n
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -25,9 +25,8 @@
|
||||||
#notification_badge
|
#notification_badge
|
||||||
= link_to "", notifications_path, :title => new_notification_text(@notification_count)
|
= link_to "", notifications_path, :title => new_notification_text(@notification_count)
|
||||||
= image_tag 'icons/mail_grey.png'
|
= image_tag 'icons/mail_grey.png'
|
||||||
- if @notification_count > 0
|
#notification_badge_number{:class => ("hidden" if @notification_count == 0)}
|
||||||
#notification_badge_number
|
= @notification_count
|
||||||
= @notification_count
|
|
||||||
|
|
||||||
%ul#user_menu
|
%ul#user_menu
|
||||||
.right
|
.right
|
||||||
|
|
|
||||||
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.
|
-# the COPYRIGHT file.
|
||||||
|
|
||||||
= link_to t('.new', :type => object.class.to_s, :from => object.person.name), object_path(object.post)
|
= 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) {
|
onMessage: function(evt) {
|
||||||
var obj = jQuery.parseJSON(evt.data);
|
var obj = jQuery.parseJSON(evt.data);
|
||||||
if(obj['notice']){
|
if(obj['class'] == 'notifications'){
|
||||||
WebSocketReceiver.processNotification(obj['notice']);
|
WebSocketReceiver.processNotification(obj);
|
||||||
|
|
||||||
}else if (obj['class'] == 'people'){
|
}else if (obj['class'] == 'people'){
|
||||||
WSR.debug("got a " + obj['class']);
|
WSR.debug("got a " + obj['class']);
|
||||||
|
|
@ -34,7 +34,8 @@ var WebSocketReceiver = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
processPerson: function(response){
|
|
||||||
|
processPerson: function(response){
|
||||||
form = $('.webfinger_form');
|
form = $('.webfinger_form');
|
||||||
form.siblings('#loader').hide();
|
form.siblings('#loader').hide();
|
||||||
result_ul = form.siblings('#request_result');
|
result_ul = form.siblings('#request_result');
|
||||||
|
|
@ -52,8 +53,24 @@ processPerson: function(response){
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
processNotification: function(html){
|
processNotification: function(notification){
|
||||||
$('#notification').html(html).fadeIn(200).delay(8000).fadeOut(200, function(){ $(this).html("");});
|
|
||||||
|
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){
|
processRetraction: function(post_id){
|
||||||
|
|
@ -86,10 +103,6 @@ processPerson: function(response){
|
||||||
$(".show_comments", post).removeClass('hidden');
|
$(".show_comments", post).removeClass('hidden');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if( !opts['mine?'] && opts['my_post?']) {
|
|
||||||
WebSocketReceiver.processNotification(opts['notification']);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1523,22 +1523,21 @@ h3 span.current_gs_step
|
||||||
:bottom 21px
|
:bottom 21px
|
||||||
:right 12px
|
:right 12px
|
||||||
|
|
||||||
a
|
:background
|
||||||
:background
|
:color rgb(30,30,30)
|
||||||
:color rgb(30,30,30)
|
:color rgba(30,30,30,0.9)
|
||||||
:color rgba(30,30,30,0.9)
|
|
||||||
|
|
||||||
:-webkit-box-shadow 0 2px 3px #333
|
:-webkit-box-shadow 0 2px 3px #333
|
||||||
:-moz-box-shadow 0 2px 3px #333
|
:-moz-box-shadow 0 2px 3px #333
|
||||||
:box-shadow 0 2px 3px #333
|
:box-shadow 0 2px 3px #333
|
||||||
|
|
||||||
:-webkit-border-radius 5px
|
:-webkit-border-radius 5px
|
||||||
:border-radius 5px
|
:border-radius 5px
|
||||||
:-moz-border-radius 5px
|
:-moz-border-radius 5px
|
||||||
|
|
||||||
:min-width 200px
|
:min-width 200px
|
||||||
:padding 12px
|
:padding 12px
|
||||||
:color #fff
|
:color #fff
|
||||||
|
|
||||||
.bottom_notification
|
.bottom_notification
|
||||||
:position fixed
|
:position fixed
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
|
|
||||||
describe Notification do
|
describe Notification do
|
||||||
before do
|
before do
|
||||||
@sm = Factory(:status_message)
|
@sm = Factory(:status_message)
|
||||||
@person = Factory(:person)
|
@person = Factory(:person)
|
||||||
|
|
@ -31,11 +31,10 @@ describe Notification do
|
||||||
describe '.for' do
|
describe '.for' do
|
||||||
it 'returns all of a users notifications' do
|
it 'returns all of a users notifications' do
|
||||||
user2 = make_user
|
user2 = make_user
|
||||||
Notification.create(@opts)
|
4.times do
|
||||||
Notification.create(@opts)
|
Notification.create(@opts)
|
||||||
Notification.create(@opts)
|
end
|
||||||
Notification.create(@opts)
|
|
||||||
|
|
||||||
@opts.delete(:user_id)
|
@opts.delete(:user_id)
|
||||||
Notification.create(@opts.merge(:user_id => user2.id))
|
Notification.create(@opts.merge(:user_id => user2.id))
|
||||||
|
|
||||||
|
|
@ -44,16 +43,30 @@ describe Notification do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '.notify' do
|
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.should_not_receive(:create)
|
||||||
Notification.notify(@user, @sm, @person)
|
Notification.notify(@user, @sm, @person)
|
||||||
end
|
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)
|
request = Request.instantiate(:from => @user.person, :to => @user2.person, :into => @aspect)
|
||||||
Notification.should_receive(:create).once
|
Notification.should_receive(:create).once
|
||||||
Notification.notify(@user, request, @person)
|
Notification.notify(@user, request, @person)
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue