Commit 0e7b1f5e authored by danielvincent's avatar danielvincent

status messages and comments made by user will ajax onto the current page, and...

status messages and comments made by user will ajax onto the current page, and websocket everywhere else.
parent ab37fd0d
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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){
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment