Skip to content
Extraits de code Groupes Projets
web-socket-receiver.js 4,75 ko
Newer Older
  • Learn to ignore specific revisions
  • var WebSocketReceiver = {
      initialize: function(url) {
    
        var ws = new WebSocket(url);
        WSR.socket = ws;
    
        //Attach onmessage to websocket
        ws.onmessage = WSR.onMessage;
        ws.onclose = function() {
    
           /* Diaspora.widgets.notifications.showNotification({
    
              html: '<div class="notification">' +
                  Diaspora.widgets.i18n.t("web_sockets.disconnected") +
                '</div>',
              incrementCount: false
    
            }); TODO:figure out why this fires so often */
    
        };
        ws.onopen = function() {
          ws.send(location.pathname);
          WSR.debug("connected...");
    
      onMessage: function(evt) {
    
          var obj = jQuery.parseJSON(evt.data);
    
          if(obj['class'].match(/^notifications/)) {
            WebSocketReceiver.processNotification(obj);
          } else if (obj['class'] == 'people') {
            WSR.debug("got a " + obj['class']);
            WebSocketReceiver.processPerson(obj);
    
          } else {
            WSR.debug("got a " + obj['class'] + " for aspects " + obj.aspect_ids);
    
            if (obj['class']=="retractions") {
              WebSocketReceiver.processRetraction(obj.post_id);
    
            } else if (obj['class']=="comments") {
              WebSocketReceiver.processComment(obj.post_id, obj.comment_id, obj.html, {
                'notification': obj.notification,
                'mine?': obj['mine?'],
                'my_post?': obj['my_post?']
              });
    
            } else if (obj['class']=="likes") {
              WebSocketReceiver.processLike(obj.post_id, obj.html);
    
            } else {
              WebSocketReceiver.processPost(obj['class'], obj.post_id, obj.html, obj.aspect_ids);
            }
    
    lfortin's avatar
    lfortin a validé
      processPerson: function(response) {
    
        var form = $('.webfinger_form');
    
    maxwell's avatar
    maxwell a validé
        form.siblings('#loader').hide();
    
        var result_ul = form.siblings('#request_result');
    
    lfortin's avatar
    lfortin a validé
        if(response.status == 'fail') {
    
    maxwell's avatar
    maxwell a validé
          result_ul.siblings('.error').show();
    
    lfortin's avatar
    lfortin a validé
          result_ul.find('.error').text(response.response).show();
        } else {
          $('#people_stream').prepend(response.html).slideDown('slow', function(){});
    
          var first_li = result_ul.find('li:first');
    
    lfortin's avatar
    lfortin a validé
          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.widgets.notifications.showNotification(notification);
    
      processRetraction: function(post_id){
        $("*[data-guid='" + post_id + "']").fadeOut(400, function() {
    
    lfortin's avatar
    lfortin a validé
          $(this).remove();
        });
    
        if($("#main_stream")[0].childElementCount === 0) {
    
          $("#no_posts").fadeIn(200);
        }
      },
    
    
      processComment: function(postId, commentId, html, opts) {
    
        if( $(".comment[data-guid='"+commentId+"']").length === 0 ) {
    
          var post = $("*[data-guid='"+postId+"']'"),
              prevComments = $('.comment.posted', post);
    
          if(prevComments.length > 0) {
            prevComments.last().after(
              $(html).fadeIn("fast", function(){})
            );
          } else {
            $('.comments li:last', post).before(
              $(html).fadeIn("fast", function(){})
            );
          }
    
          var toggler = $('.show_post_comments', post);
    
          if(toggler.length > 0){
            toggler.html(
              toggler.html().replace(/\d+/,$('.comments', post).find('li').length -1)
            );
    
            if( !$(".comments", post).is(':visible') ) {
              toggler.click();
            }
    
            if( $(".show_comments", post).hasClass('hidden') ){
              $(".show_comments", post).removeClass('hidden');
            }
          }
        }
    
        Diaspora.widgets.timeago.updateTimeAgo();
        Diaspora.widgets.directionDetector.updateBinds();
    
      processLike: function(postId, html) {
        var post = $("*[data-guid='"+postId+"']");
    
        $('.likes', post).html(html);
    
      processPost: function(className, postId, html, aspectIds) {
    
    lfortin's avatar
    lfortin a validé
        if(WebSocketReceiver.onPageForAspects(aspectIds)) {
    
          ContentUpdater.addPostToStream(postId, html);
    
    lfortin's avatar
    lfortin a validé
      onPageForClass: function(className) {
    
        return (location.href.indexOf(className) != -1 );
      },
    
    
    lfortin's avatar
    lfortin a validé
      onPageForAspects: function(aspectIds) {
    
        var streamIds = $('#main_stream').attr('data-guids'),
    
        $.each(aspectIds, function(index, value) {
    
          if(WebSocketReceiver.onStreamForAspect(value, streamIds)) {
    
    lfortin's avatar
    lfortin a validé
            found = true;
    
      onStreamForAspect: function(aspectId, streamIds) {
        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);
    
    lfortin's avatar
    lfortin a validé
    var WSR = WebSocketReceiver;