<commit_before>define(["config",
        "app/app",
        "socket.io"], function(config, App) {
  App.CometController = Ember.Controller.extend({
    needs: ['timeline', 'search', 'post'],

    subscribedTo: {},

    // TODO: review this method -- hacky solution
    currentController: function() {
      switch (App.properties.get('currentPath')) {
      case 'home':
      case 'user':
      case 'comments':
      case 'likes':
      case 'public':
        return this.get('controllers.timeline')
      case 'search':
        return this.get('controllers.search')
      case 'post':
        return this.get('controllers.post')
      }
    },

    findPost: function(postId) {
      var currentController = this.currentController()
      if (currentController.constructor === App.PostController)
        return currentController.get('content')
      else
        return currentController.get('content.posts').find(function(post) {
          return post.id === postId
        })
    },

    isFirstPage: function() {
      var pageStart = this.currentController().get('pageStart')
      return pageStart === 0 ||
        pageStart === undefined
    },

    newPost: function(data) {
      if (!this.isFirstPage())
        return

      var post = App.Post.createFromProto(data.post)
      this.currentController().get('posts').addObject(post)
    },

    updatePost: function(data) {
      var post = this.findPost(data.post.id)

      if (post)
        post.set('body', data.post.body)
    },

    destroyPost: function(data) {
      var post = this.findPost(data.postId)
      this.currentController().get('content.posts').removeObject(post)
    },

    newComment: function(data) {
      if (!this.isFirstPage())
        return

      var comment = App.Comment.create(data.comment)
      var post = this.findPost(data.comment.postId)

      if (post) {
        post.comments.pushObject(comment)
      } else {
        post = App.Post.find(data.comment.postId)
        this.currentController().get('posts').addObject(post)
      }
    },

    updateComment: function(data) {
      var post = this.findPost(data.comment.postId)

      var index = 0
      var comment = post.comments.find(function(comment) {
        index += 1
        if (comment && comment.id)
          return comment.id === data.comment.id
      })

      if (comment) {
        // FIXME: doesn't work as comment is not an Ember object
        // comment.set('body', data.comment.body)

        var updatedComment = App.Comment.create(data.comment)
        post.comments.removeObject(comment)
        post.comments.insertAt(index-1, updatedComment)
      }
    },

    destroyComment: function(data) {
      var post = this.findPost(data.postId)

      var comment = post.comments.findProperty('id', data.commentId)
      post.comments.removeObject(comment)
    },

    newLike: function(data) {
      if (!this.isFirstPage())
        return

      var user = App.User.create(data.user)
      var post = this.findPost(data.postId)

      if (post) {
        var like = post.likes.find(function(like) {
          return like.id == user.id
        })

        if (!like)
          post.likes.pushObject(user)
      } else {
        post = App.Post.find(data.postId)
        this.currentController().get('posts').addObject(post)
      }
    },

    removeLike: function(data) {
      var post = this.findPost(data.postId)

      if (post) {
        var like = post.get('likes').findProperty('id', data.userId)
        post.get('likes').removeObject(like)
      }
    },

    disconnect: function(data) {
      this.reconnect();
    },

    init: function() {
      this._super();

      this.set('socket', io.connect(config.host + '/'));

      this.get('socket').on('newPost', this.newPost.bind(this));
      this.get('socket').on('updatePost', this.updatePost.bind(this));
      this.get('socket').on('destroyPost', this.destroyPost.bind(this));

      this.get('socket').on('newComment', this.newComment.bind(this));
      this.get('socket').on('updateComment', this.updateComment.bind(this));
      this.get('socket').on('destroyComment', this.destroyComment.bind(this));
<commit_msg>Comet notifications for post hide/unhide<commit_after>define(["config",
        "app/app",
        "socket.io"], function(config, App) {
  App.CometController = Ember.Controller.extend({
    needs: ['timeline', 'search', 'post'],

    subscribedTo: {},

    // TODO: review this method -- hacky solution
    currentController: function() {
      switch (App.properties.get('currentPath')) {
      case 'home':
      case 'user':
      case 'comments':
      case 'likes':
      case 'public':
        return this.get('controllers.timeline')
      case 'search':
        return this.get('controllers.search')
      case 'post':
        return this.get('controllers.post')
      }
    },

    findPost: function(postId) {
      var currentController = this.currentController()
      if (currentController.constructor === App.PostController)
        return currentController.get('content')
      else
        return currentController.get('content.posts').find(function(post) {
          return post.id === postId
        })
    },

    isFirstPage: function() {
      var pageStart = this.currentController().get('pageStart')
      return pageStart === 0 ||
        pageStart === undefined
    },

    newPost: function(data) {
      if (!this.isFirstPage())
        return

      var post = App.Post.createFromProto(data.post)
      this.currentController().get('posts').addObject(post)
    },

    updatePost: function(data) {
      var post = this.findPost(data.post.id)

      if (post)
        post.set('body', data.post.body)
    },

    hidePost: function(data) {
      var post = this.findPost(data.postId)

      if (post)
        post.set('isHidden', true)
    },

    unhidePost: function(data) {
      var post = this.findPost(data.postId)

      if (post)
        post.set('isHidden', false)
    },

    destroyPost: function(data) {
      var post = this.findPost(data.postId)
      this.currentController().get('content.posts').removeObject(post)
    },

    newComment: function(data) {
      if (!this.isFirstPage())
        return

      var comment = App.Comment.create(data.comment)
      var post = this.findPost(data.comment.postId)

      if (post) {
        post.comments.pushObject(comment)
      } else {
        post = App.Post.find(data.comment.postId)
        this.currentController().get('posts').addObject(post)
      }
    },

    updateComment: function(data) {
      var post = this.findPost(data.comment.postId)

      var index = 0
      var comment = post.comments.find(function(comment) {
        index += 1
        if (comment && comment.id)
          return comment.id === data.comment.id
      })

      if (comment) {
        // FIXME: doesn't work as comment is not an Ember object
        // comment.set('body', data.comment.body)

        var updatedComment = App.Comment.create(data.comment)
        post.comments.removeObject(comment)
        post.comments.insertAt(index-1, updatedComment)
      }
    },

    destroyComment: function(data) {
      var post = this.findPost(data.postId)

      var comment = post.comments.findProperty('id', data.commentId)
      post.comments.removeObject(comment)
    },

    newLike: function(data) {
      if (!this.isFirstPage())
        return

      var user = App.User.create(data.user)
      var post = this.findPost(data.postId)

      if (post) {
        var like = post.likes.find(function(like) {
          return like.id == user.id
        })

        if (!like)
          post.likes.pushObject(user)
      } else {
        post = App.Post.find(data.postId)
        this.currentController().get('posts').addObject(post)
      }
    },

    removeLike: function(data) {
      var post = this.findPost(data.postId)

      if (post) {
        var like = post.get('likes').findProperty('id', data.userId)
        post.get('likes').removeObject(like)
      }
    },

    disconnect: function(data) {
      this.reconnect();
    },

    init: function() {
      this._super();

      this.set('socket', io.connect(config.host + '/'));

      this.get('socket').on('newPost', this.newPost.bind(this));
      this.get('socket').on('updatePost', this.updatePost.bind(this));
      this.get('socket').on('destroyPost', this.destroyPost.bind(this));
      this.get('socket').on('hidePost', this.hidePost.bind(this));
      this.get('socket').on('unhidePost', this.unhidePost.bind(this));

      this.get('socket').on('newComment', this.newComment.bind(this));
      this.get('socket').on('updateComment', this.updateComment.bind(this));
      this.get('socket').on('destroyComment', this.destroyComment.bind(this));

{'fraction_non_alphanumeric': 0.14858382823206945, 'fraction_numerical': 0.0009136592051164915, 'mean_word_length': 2.488844621513944, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '664885', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2583, 'n_words': 460}