clean up websocket receiver, add specs to content updater

This commit is contained in:
Dan Hansen 2011-08-22 19:28:46 -05:00
parent 7fd7fa968c
commit 6b040c7b4a
8 changed files with 119 additions and 158 deletions

View file

@ -17,8 +17,6 @@
= @aspects.to_sentence = @aspects.to_sentence
= render 'shared/publisher', :selected_aspects => @aspects, :aspect_ids => aspect_ids, :aspect => @aspect = render 'shared/publisher', :selected_aspects => @aspects, :aspect_ids => aspect_ids, :aspect => @aspect
- if posts.length == 0
= render 'aspects/no_posts_message' = render 'aspects/no_posts_message'
- if current_user.contacts.size < 2 - if current_user.contacts.size < 2

View file

@ -2,5 +2,5 @@
-# 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.
#no_posts.empty_message #no_posts.hidden.empty_message
= t('.start_talking') = t('.start_talking')

View file

@ -1,7 +1,6 @@
WebSocketReceiver.processComment("<%= @comment.post.guid %>", ContentUpdater.addCommentToPost("<%= @comment.post.guid %>",
"<%= @comment.guid%>", "<%= @comment.guid%>",
"<%= escape_javascript(render(:partial => 'comments/comment', :locals => { :comment => @comment, :person => current_user.person}))%>", "<%= escape_javascript(render(:partial => 'comments/comment', :locals => { :comment => @comment, :person => current_user.person}))%>");
false);
Diaspora.page Diaspora.page
.stream .stream

View file

@ -1,2 +1,2 @@
$(".like_action", "#<%=@like.target.guid%>").first().html("<%= escape_javascript(like_action(@like.target))%>"); $(".like_action", "#<%=@like.target.guid%>").first().html("<%= escape_javascript(like_action(@like.target))%>");
WebSocketReceiver.processLike("<%=@like.target.guid%>", "<%= escape_javascript(render("likes/likes_container", :target_id => @like.target_id, :likes_count => @like.target.likes_count, :target_type => @like.target_type)) %>"); ContentUpdater.addLikesToPost("<%=@like.target.guid%>", "<%= escape_javascript(render("likes/likes_container", :target_id => @like.target_id, :likes_count => @like.target.likes_count, :target_type => @like.target_type)) %>");

View file

@ -23,13 +23,32 @@ var ContentUpdater = {
} }
}, },
addLikesToPost: function(postGUID, html) { removePostFromStream: function(postGUID) {
var post = $("#" + postGUID); $("#" + postGUID).fadeOut(400, function() {
$(this).remove();
});
$(".likes_container", post) if(!$("#main_stream .stream_element").length) {
$("#no_posts").removeClass("hidden");
}
},
addCommentToPost: function(postGUID, commentGUID, html) {
var post = $("#" + postGUID),
comments = $("ul.comments", post);
if($("#" + commentGUID, post).length) { return; }
$(html).appendTo(comments).fadeIn("fast");
Diaspora.page.timeAgo.updateTimeAgo();
Diaspora.page.directionDetector.updateBinds();
},
addLikesToPost: function(postGUID, html) {
$(".likes_container", "#" + postGUID)
.fadeOut("fast") .fadeOut("fast")
.html(html) .html(html)
.fadeIn("fast"); .fadeIn("fast");
} }
}; };

View file

@ -1,146 +1,41 @@
var WebSocketReceiver = { var WSR = WebSocketReceiver = {
initialize: function(url) { initialize: function(url) {
var ws = new WebSocket(url); WSR.socket = new WebSocket(url);
WSR.socket = ws;
//Attach onmessage to websocket WSR.socket.onmessage = WSR.onMessage;
ws.onmessage = WSR.onMessage; WSR.socket.onopen = function() {
ws.onclose = function() { WSR.socket.send(location.pathname);
if (websocket_enabled) {
/* Diaspora.widgets.notifications.showNotification({
html: '<div class="notification">' +
Diaspora.I18n.t("web_sockets.disconnected") +
'</div>',
incrementCount: false
}); TODO:figure out why this fires so often */
WSR.debug("socket closed");
}
};
ws.onopen = function() {
ws.send(location.pathname);
WSR.debug("connected...");
}; };
}, },
onMessage: function(evt) { onMessage: function(evt) {
var obj = jQuery.parseJSON(evt.data); var message = $.parseJSON(evt.data);
if(obj['class'].match(/^notifications/)) { if(message["class"].match(/^notifications$/)) {
WebSocketReceiver.processNotification(obj); Diaspora.page.notifications.showNotification(message);
} else if (obj['class'] == 'people') {
WSR.debug("got a " + obj['class']);
WebSocketReceiver.processPerson(obj);
} else {
debug_string = "got a " + obj['class'];
if(obj.aspect_ids !== undefined){
debug_string += " for aspects " + obj.aspect_ids;
} }
else {
WSR.debug(debug_string); switch(message["class"]) {
case "retractions":
if (obj['class']=="retractions") { ContentUpdater.removePostFromStream(message.post_guid);
WebSocketReceiver.processRetraction(obj.post_guid); break;
case "comments":
} else if (obj['class']=="comments") { ContentUpdater.addCommentToPost(message.post_guid, message.comment_guid, message.html);
WebSocketReceiver.processComment(obj.post_guid, obj.comment_guid, obj.html, { break;
'notification': obj.notification, case "likes":
'mine?': obj['mine?'], ContentUpdater.addLikesToPost(message.post_guid, message.html);
'my_post?': obj['my_post?'] break;
}); default:
if(WSR.onPageForAspects(message.aspects_ids)) {
} else if (obj['class']=="likes") { ContentUpdater.addPostToStream(message.html);
WebSocketReceiver.processLike(obj.post_guid, obj.html); }
break;
} else {
WebSocketReceiver.processPost(obj.html, obj.aspect_ids);
} }
} }
}, },
processPerson: function(response) { onPageForAspects: function(aspectIds) {
var form = $('.webfinger_form'); var streamIds = $("#main_stream").attr("data-guids"),
form.siblings('#loader').hide();
var result_ul = form.siblings('#request_result');
if(response.status == 'fail') {
result_ul.siblings('.error').show();
result_ul.find('.error').text(response.response).show();
} else {
stream = $('#people_stream');
stream.find('p').remove();
stream.prepend(response.html).slideDown('slow', function(){});
var first_li = result_ul.find('li:first');
first_li.hide();
first_li.after(response.html);
result_ul.find("[name='request[into]']").val(result_ul.attr('aspect_id'));
result_ul.children(':nth-child(2)').slideDown('fast', function(){});
}
},
processNotification: function(notification){
Diaspora.page.notifications.showNotification(notification);
},
processRetraction: function(post_guid){
$("#" + post_guid).fadeOut(400, function() {
$(this).remove();
});
if($("#main_stream")[0].childElementCount === 0) {
$("#no_posts").fadeIn(200);
}
},
processComment: function(postGUID, commentGUID, html, opts) {
if( $("#"+commentGUID).length === 0 ) {
var post = $("#"+postGUID),
prevComments = $('.comment.posted', post);
if(prevComments.length > 0) {
prevComments.last().after(
$(html).fadeIn("fast", function(){})
);
} else {
$('.comments', post).append(
$(html).fadeIn("fast", function(){})
);
}
var toggler = $('.toggle_post_comments', post).parent();
if(toggler.length > 0){
if( !$(".comments", post).is(':visible') ) {
toggler.click();
}
if( $(".show_comments", post).hasClass('hidden') ){
$(".show_comments", post).removeClass('hidden');
}
}
}
Diaspora.page.timeAgo.updateTimeAgo();
Diaspora.page.directionDetector.updateBinds();
},
processLike: function(targetGUID, html) {
$('.likes', "#" + targetGUID).first().html(html);
},
processPost: function(html, aspectIds) {
if(WebSocketReceiver.onpageForAspects(aspectIds)) {
ContentUpdater.addPostToStream(html);
}
},
onpageForClass: function(className) {
return (location.href.indexOf(className) != -1 );
},
onpageForAspects: function(aspectIds) {
var streamIds = $('#main_stream').attr('data-guids'),
found = false; found = false;
$.each(aspectIds, function(index, value) { $.each(aspectIds, function(index, value) {
@ -149,20 +44,11 @@ var WebSocketReceiver = {
return false; return false;
} }
}); });
return found; return found;
}, },
onStreamForAspect: function(aspectId, streamIds) { onStreamForAspect: function(aspectId, streamIds) {
return (streamIds.search(aspectId) != -1); return (streamIds.search(aspectId) != -1);
},
onpageOne: function() {
var c = document.location.search.charAt(document.location.search.length-1);
return ((c === '') || (c === '1'));
},
debug: function(str) {
$("#debug").append("<p>" + str);
} }
}; };
var WSR = WebSocketReceiver;

View file

@ -2361,10 +2361,6 @@ ul.show_comments,
:color #eee :color #eee
:border 1px solid #ccc :border 1px solid #ccc
.webfinger_form
input[type='search']
:width 100%
#sort_by #sort_by
:float right :float right
:color #777 :color #777

View file

@ -29,4 +29,67 @@ describe("ContentUpdater", function() {
expect($("#no_posts").length).toEqual(0); expect($("#no_posts").length).toEqual(0);
}); });
}); });
describe("removePostFromStream", function() {
var post, postGUID;
beforeEach(function() {
spec.loadFixture("aspects_index_with_posts");
post = $(".stream_element:first"),
postGUID = post.attr("id");
$.fx.off = true;
});
it("removes the post from the stream", function() {
expect($("#" + postGUID).length).toEqual(1);
ContentUpdater.removePostFromStream(postGUID);
expect($("#" + postGUID).length).toEqual(0);
});
it("shows the div that says you have no posts if there are no more post", function() {
$("#main_stream .stream_element").slice(1).remove();
expect($("#no_posts")).toHaveClass("hidden");
ContentUpdater.removePostFromStream(postGUID);
expect($("#no_posts")).not.toHaveClass("hidden");
});
afterEach(function() {
$.fx.off = false;
});
});
describe("addCommentToPost", function() {
var post, postGUID;
beforeEach(function() {
spec.loadFixture("aspects_index_with_posts");
post = $(".stream_element:first"),
postGUID = post.attr("id");
});
it("adds a comment to a post only if it doesn't already exist", function() {
var comments = post.find("ul.comments li");
expect(comments.length).toEqual(0);
ContentUpdater.addCommentToPost(postGUID, "YEAH", "<li id='YEAH'>Comment</li>");
expect(post.find("ul.comments li").length).toEqual(1);
ContentUpdater.addCommentToPost(postGUID, "YEAH", "<li id='YEAH'>Comment</li>");
expect(post.find("ul.comments li").length).toEqual(1);
});
});
describe("addLikesToPost", function() {
var post, postGUID;
beforeEach(function() {
spec.loadFixture("aspects_index_with_posts");
post = $(".stream_element:first"),
postGUID = post.attr("id");
});
it("adds the given html to a post's likes container", function() {
ContentUpdater.addLikesToPost(postGUID, "<p>1 like</p>");
expect(post.find(".likes .likes_container").html()).toEqual("<p>1 like</p>");
});
});
}); });