Skip to content
Extraits de code Groupes Projets
Valider 0e7b1f5e rédigé par danielvincent's avatar danielvincent
Parcourir les fichiers

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
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
# the COPYRIGHT file. # the COPYRIGHT file.
class CommentsController < ApplicationController class CommentsController < ApplicationController
include ApplicationHelper
before_filter :authenticate_user! before_filter :authenticate_user!
respond_to :html respond_to :html
...@@ -15,7 +16,14 @@ class CommentsController < ApplicationController ...@@ -15,7 +16,14 @@ class CommentsController < ApplicationController
@comment = current_user.comment(text, :on => target) if target @comment = current_user.comment(text, :on => target) if target
if @comment if @comment
Rails.logger.info("event=comment_create user=#{current_user.inspect} status=success comment=#{@comment.inspect}") 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 else
render :nothing => true, :status => 401 render :nothing => true, :status => 401
end end
......
...@@ -26,9 +26,18 @@ class StatusMessagesController < ApplicationController ...@@ -26,9 +26,18 @@ class StatusMessagesController < ApplicationController
for photo in photos for photo in photos
current_user.dispatch_post(photo, :to => params[:status_message][:to]) current_user.dispatch_post(photo, :to => params[:status_message][:to])
end 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 end
render :nothing => true
end end
......
...@@ -23,13 +23,14 @@ module SocketsHelper ...@@ -23,13 +23,14 @@ module SocketsHelper
Rails.logger.error("web socket view rendering failed for object #{object.inspect}.") Rails.logger.error("web socket view rendering failed for object #{object.inspect}.")
raise e raise e
end 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 action_hash.merge! opts
if object.is_a? Photo if object.is_a? Photo
action_hash[:photo_hash] = object.thumb_hash action_hash[:photo_hash] = object.thumb_hash
end end
if object.is_a? Comment if object.is_a? Comment
action_hash[:comment_id] = object.id
action_hash[:my_post?] = (object.post.person.owner.id == uid) action_hash[:my_post?] = (object.post.person.owner.id == uid)
action_hash[:notification] = notification(object) action_hash[:notification] = notification(object)
end end
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
-# licensed under the Affero General Public License version 3 or later. See -# licensed under the Affero General Public License version 3 or later. See
-# the COPYRIGHT file. -# the COPYRIGHT file.
%li.comment %li.comment{:data=>{:guid=>post.id}}
= person_image_link(post.person) = person_image_link(post.person)
.content .content
.from .from
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
$(document).ready(function(){ $(document).ready(function(){
var $stream = $(".stream"); var $stream = $(".stream");
var $publisher = $("#publisher");
// expand all comments on page load // expand all comments on page load
$(".stream:not('.show')").find('.comments').each(function(index) { $(".stream:not('.show')").find('.comments').each(function(index) {
var comments = $(this); var comments = $(this);
...@@ -51,6 +52,22 @@ $(document).ready(function(){ ...@@ -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 });//end document ready
......
...@@ -27,10 +27,10 @@ var WebSocketReceiver = { ...@@ -27,10 +27,10 @@ var WebSocketReceiver = {
WebSocketReceiver.processRetraction(obj['post_id']); WebSocketReceiver.processRetraction(obj['post_id']);
}else if (obj['class']=="comments"){ }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{ }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 = { ...@@ -62,44 +62,50 @@ var WebSocketReceiver = {
} }
}, },
processComment: function(post_id, html, opts){ processComment: function(postId, commentId, html, opts){
post = $("*[data-guid='"+post_id+"']'");
$('.comments li:last', post ).before(
$(html).fadeIn("fast", function(){})
);
toggler = $('.show_post_comments', post);
if(toggler.length > 0){ if( $(".comment[data-guid='"+commentId+"']").length == 0 ){
toggler.html(
toggler.html().replace(/\d+/,$('.comments', post)[0].childElementCount -1) 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') ){ if( !$(".comments", post).is(':visible') ){
toggler.click(); toggler.click();
}
} }
}
if( !opts['mine?'] && opts['my_post?']) { if( !opts['mine?'] && opts['my_post?']) {
WebSocketReceiver.processNotification(opts['notification']); WebSocketReceiver.processNotification(opts['notification']);
}
} }
}, },
processPost: function(className, html, aspectIds){ processPost: function(className, postId, html, aspectIds){
if(WebSocketReceiver.onPageForAspects(aspectIds)){ if(WebSocketReceiver.onPageForAspects(aspectIds)){
if( $("#no_posts").is(":visible") ){ if( $("#no_posts").is(":visible") ){
$("#no_posts").fadeOut(400, WebSocketReceiver.addPostToStream(html)).hide(); $("#no_posts").fadeOut(400, WebSocketReceiver.addPostToStream(postId, html)).hide();
} else { } else {
WebSocketReceiver.addPostToStream(html); WebSocketReceiver.addPostToStream(postId, html);
} }
} }
}, },
addPostToStream: function(html){ addPostToStream: function(postId, html){
$("#main_stream:not('.show')").prepend( if( $(".message[data-guid='"+postId+"']").length == 0 ){
$(html).fadeIn("fast", function(){ $("#main_stream:not('.show')").prepend(
$("#main_stream").find("label").first().inFieldLabels(); $(html).fadeIn("fast", function(){
}) $("#main_stream").find("label").first().inFieldLabels();
); })
);
}
}, },
onPageForClass: function(className){ onPageForClass: function(className){
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter