From bd9aa90278875180bcf4498c74f067f12bf28c0b Mon Sep 17 00:00:00 2001 From: danielvincent Date: Fri, 5 Nov 2010 14:13:28 -0700 Subject: [PATCH] User receives notification on new comment on post on any page. --- app/helpers/sockets_helper.rb | 15 ++++++++-- app/views/js/_websocket_js.haml | 37 +++++++++++++++++------- app/views/layouts/application.html.haml | 3 ++ app/views/shared/_notification.haml | 5 ++++ public/stylesheets/sass/application.sass | 21 ++++++++++++++ 5 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 app/views/shared/_notification.haml diff --git a/app/helpers/sockets_helper.rb b/app/helpers/sockets_helper.rb index eba28dd5f..b6a82c839 100644 --- a/app/helpers/sockets_helper.rb +++ b/app/helpers/sockets_helper.rb @@ -23,10 +23,21 @@ module SocketsHelper action_hash[:photo_hash] = object.thumb_hash end - if object.person && object.person.owner_id == uid - action_hash[:mine?] = true + if object.is_a? Comment + action_hash[:my_post?] = (object.post.person.owner.id == uid) + action_hash[:notification] = notification(object) end + action_hash[:mine?] = object.person && (object.person.owner.id == uid) + action_hash.to_json end + + def notification(object) + begin + render_to_string(:partial => 'shared/notification', :locals => {:object => object}) + rescue Exception => e + Rails.logger.error("web socket notification failed for object #{object.inspect}.") + end + end end diff --git a/app/views/js/_websocket_js.haml b/app/views/js/_websocket_js.haml index 33fb0e971..b2ba0f4c9 100644 --- a/app/views/js/_websocket_js.haml +++ b/app/views/js/_websocket_js.haml @@ -13,19 +13,24 @@ //Attach onmessage to websocket ws.onmessage = function(evt) { var obj = jQuery.parseJSON(evt.data); - //console.log(obj); - debug("got a " + obj['class'] + " for aspects " + obj['aspect_ids']); - if (obj['class']=="retractions"){ - processRetraction(obj['post_id']); + if(obj['notice']){ + processNotification(obj['notice']); - }else if (obj['class']=="comments"){ - processComment(obj['post_id'], obj['html']) - - }else if (obj['class']=='photos' && onPageForClass('albums')){ - processPhotoInAlbum(obj['photo_hash']) }else{ - processPost(obj['class'], obj['html'], obj['aspect_ids']) + debug("got a " + obj['class'] + " for aspects " + obj['aspect_ids']); + + if (obj['class']=="retractions"){ + processRetraction(obj['post_id']); + + }else if (obj['class']=="comments"){ + processComment(obj['post_id'], obj['html'], {'notification':obj['notification'], 'mine?':obj['mine?'], 'my_post?':obj['my_post?']}) + + }else if (obj['class']=='photos' && onPageForClass('albums')){ + processPhotoInAlbum(obj['photo_hash']) + }else{ + processPost(obj['class'], obj['html'], obj['aspect_ids']) + } } }; @@ -36,6 +41,11 @@ }; }); + + function processNotification(html){ + $('#notification').html(html).fadeIn(200).delay(4000).fadeOut(200, function(){ $(this).html("");}); + } + function processRetraction(post_id){ $("*[data-guid='"+post_id+"']").fadeOut(400, function(){$(this).remove()}); if($("#stream")[0].childElementCount == 0){ @@ -43,7 +53,7 @@ } } - function processComment(post_id, html){ + function processComment(post_id, html, opts){ post = $("*[data-guid='"+post_id+"']'"); $('.comments li:last', post ).before( $(html).fadeIn("fast", function(){}) @@ -58,6 +68,11 @@ toggler.click(); } } + + if( !opts['mine?'] && opts['my_post?']) { + processNotification(opts['notification']); + } + } function processPost(className, html, aspectIds){ diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 5fc9f3b51..e4ee651b6 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -43,6 +43,9 @@ %header .container{:style => "position:relative;"} + + #notification + #diaspora_text = link_to "DIASPORA", (current_user ? root_path : new_user_session_path) %span.sub_text diff --git a/app/views/shared/_notification.haml b/app/views/shared/_notification.haml new file mode 100644 index 000000000..89bc610fb --- /dev/null +++ b/app/views/shared/_notification.haml @@ -0,0 +1,5 @@ +-# 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 "New #{object.class.to_s} from #{object.person.real_name}", object_path(object.post) diff --git a/public/stylesheets/sass/application.sass b/public/stylesheets/sass/application.sass index e876c178f..b631a6eb7 100644 --- a/public/stylesheets/sass/application.sass +++ b/public/stylesheets/sass/application.sass @@ -1472,3 +1472,24 @@ ul.aspects h3 span.current_gs_step :color #22C910 +#notification + :display none + :position fixed + :bottom 21px + :right 12px + + a + :background + :color rgb(30,30,30) + :color rgba(30,30,30,0.9) + + :-webkit-box-shadow 0 2px 3px #333 + + :-webkit-border-radius 5px + :border-radius 5px + :-moz-border-radius 5px + + :min-width 200px + :padding 12px + :color #fff +