diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index b8f051e57026bf8a42cf2eaa9ee25fc1bda211ef..5b39a10088bdf6bbdc4c5319eec92f59548320cd 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -3,6 +3,7 @@ # the COPYRIGHT file. class CommentsController < ApplicationController + include ApplicationHelper before_filter :authenticate_user! respond_to :html @@ -15,7 +16,14 @@ class CommentsController < ApplicationController @comment = current_user.comment(text, :on => target) if target if @comment Rails.logger.info("event=comment_create user=#{current_user.inspect} status=success comment=#{@comment.inspect}") - render :nothing => true, :status => 201 + + respond_to do |format| + format.js{ render :json => { :post_id => @comment.post_id, + :comment_id => @comment.id, + :html => render_to_string(:partial => type_partial(@comment), :locals => {:post => @comment, :current_user => current_user})}, + :status => 201 } + format.html{ render :nothing => true, :status => 201 } + end else render :nothing => true, :status => 401 end diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index 54487dbeb168404846a6b85946ec98a2007b5a76..d4992fb254c68aba9889f9fd91fefdbca908fe98 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -26,9 +26,18 @@ class StatusMessagesController < ApplicationController for photo in photos current_user.dispatch_post(photo, :to => params[:status_message][:to]) end + + respond_to do |format| + format.js{ render :json => { :post_id => @status_message.id, + :html => render_to_string(:partial => 'shared/stream_element', :locals => {:post => @status_message, :current_user => current_user})}, + :status => 201 } + format.html{ respond_with @status_message } + end + else + respond_to do |format| + format.js{ render :status => 401 } + end end - - render :nothing => true end diff --git a/app/helpers/sockets_helper.rb b/app/helpers/sockets_helper.rb index d646f0e4ee47286f3f924caff7b071a3694aa9f5..214665ef3170a37b91abcf1469717ce73820a28f 100644 --- a/app/helpers/sockets_helper.rb +++ b/app/helpers/sockets_helper.rb @@ -23,13 +23,14 @@ module SocketsHelper Rails.logger.error("web socket view rendering failed for object #{object.inspect}.") raise e end - action_hash = {:class =>object.class.to_s.underscore.pluralize, :html => v, :post_id => obj_id(object)} + action_hash = {:class =>object.class.to_s.underscore.pluralize, :html => v, :post_id => obj_id(object)} action_hash.merge! opts if object.is_a? Photo action_hash[:photo_hash] = object.thumb_hash end if object.is_a? Comment + action_hash[:comment_id] = object.id action_hash[:my_post?] = (object.post.person.owner.id == uid) action_hash[:notification] = notification(object) end diff --git a/app/views/comments/_comment.html.haml b/app/views/comments/_comment.html.haml index f84d4b6d57cd0439eb84a24c1144399446707c50..fe94bd6fd5b52b3f1fc5bd22e09650a6a1f14b2d 100644 --- a/app/views/comments/_comment.html.haml +++ b/app/views/comments/_comment.html.haml @@ -2,7 +2,7 @@ -# licensed under the Affero General Public License version 3 or later. See -# the COPYRIGHT file. -%li.comment +%li.comment{:data=>{:guid=>post.id}} = person_image_link(post.person) .content .from diff --git a/public/javascripts/stream.js b/public/javascripts/stream.js index a3495e72087f9ef546f9093a25c10e265012ec02..1a8e9b31b9e30260b6bf56174bd431b290dbea39 100644 --- a/public/javascripts/stream.js +++ b/public/javascripts/stream.js @@ -6,6 +6,7 @@ $(document).ready(function(){ var $stream = $(".stream"); + var $publisher = $("#publisher"); // expand all comments on page load $(".stream:not('.show')").find('.comments').each(function(index) { var comments = $(this); @@ -51,6 +52,22 @@ $(document).ready(function(){ } }); + $(".new_status_message").bind('ajax:success', function(data, json, xhr){ + json = $.parseJSON(json); + WebSocketReceiver.addPostToStream(json['post_id'],json['html']); + }); + $(".new_status_message").bind('ajax:failure', function(data, html, xhr){ + alert('failed to post message!'); + }); + + $(".new_comment").live('ajax:success', function(data, json, xhr){ + json = $.parseJSON(json); + WebSocketReceiver.processComment(json['post_id'],json['comment_id'],json['html'],false); + }); + $(".new_comment").live('ajax:failure', function(data, html, xhr){ + alert('failed to post message!'); + }); + });//end document ready diff --git a/public/javascripts/web-socket-receiver.js b/public/javascripts/web-socket-receiver.js index 4fa546f937cc24075f77263da49ca1139ace6df8..66b044cc2efdd7f9ba2165c966ed46bd87ef09f1 100644 --- a/public/javascripts/web-socket-receiver.js +++ b/public/javascripts/web-socket-receiver.js @@ -27,10 +27,10 @@ var WebSocketReceiver = { WebSocketReceiver.processRetraction(obj['post_id']); }else if (obj['class']=="comments"){ - WebSocketReceiver.processComment(obj['post_id'], obj['html'], {'notification':obj['notification'], 'mine?':obj['mine?'], 'my_post?':obj['my_post?']}) + WebSocketReceiver.processComment(obj['post_id'], obj['comment_id'], obj['html'], {'notification':obj['notification'], 'mine?':obj['mine?'], 'my_post?':obj['my_post?']}) }else{ - WebSocketReceiver.processPost(obj['class'], obj['html'], obj['aspect_ids']) + WebSocketReceiver.processPost(obj['class'], obj['post_id'], obj['html'], obj['aspect_ids']) } } }, @@ -62,44 +62,50 @@ var WebSocketReceiver = { } }, - processComment: function(post_id, html, opts){ - post = $("*[data-guid='"+post_id+"']'"); - $('.comments li:last', post ).before( - $(html).fadeIn("fast", function(){}) - ); - toggler = $('.show_post_comments', post); + processComment: function(postId, commentId, html, opts){ - if(toggler.length > 0){ - toggler.html( - toggler.html().replace(/\d+/,$('.comments', post)[0].childElementCount -1) + if( $(".comment[data-guid='"+commentId+"']").length == 0 ){ + + post = $("*[data-guid='"+postId+"']'"); + $('.comments li:last', post ).before( + $(html).fadeIn("fast", function(){}) ); + toggler = $('.show_post_comments', post); + + if(toggler.length > 0){ + toggler.html( + toggler.html().replace(/\d+/,$('.comments', post)[0].childElementCount -1) + ); - if( !$(".comments", post).is(':visible') ){ - toggler.click(); + if( !$(".comments", post).is(':visible') ){ + toggler.click(); + } } - } - if( !opts['mine?'] && opts['my_post?']) { - WebSocketReceiver.processNotification(opts['notification']); + if( !opts['mine?'] && opts['my_post?']) { + WebSocketReceiver.processNotification(opts['notification']); + } } }, - processPost: function(className, html, aspectIds){ + processPost: function(className, postId, html, aspectIds){ if(WebSocketReceiver.onPageForAspects(aspectIds)){ if( $("#no_posts").is(":visible") ){ - $("#no_posts").fadeOut(400, WebSocketReceiver.addPostToStream(html)).hide(); + $("#no_posts").fadeOut(400, WebSocketReceiver.addPostToStream(postId, html)).hide(); } else { - WebSocketReceiver.addPostToStream(html); + WebSocketReceiver.addPostToStream(postId, html); } } }, - addPostToStream: function(html){ - $("#main_stream:not('.show')").prepend( - $(html).fadeIn("fast", function(){ - $("#main_stream").find("label").first().inFieldLabels(); - }) - ); + addPostToStream: function(postId, html){ + if( $(".message[data-guid='"+postId+"']").length == 0 ){ + $("#main_stream:not('.show')").prepend( + $(html).fadeIn("fast", function(){ + $("#main_stream").find("label").first().inFieldLabels(); + }) + ); + } }, onPageForClass: function(className){