<commit_before>define(function(require) {

    'use strict';
  
    var Backbone = require('backbone')
      , _        = require('underscore')
      , log      = require('app/utils/bows.min')('Models:Post')
      , socket   = require('app/utils/socket')

    return Backbone.Model.extend({
      
      xmppEvents: {
        'get': 'xmpp.buddycloud.retrieve',
        'post': 'xmpp.buddycloud.publish'
      },

      defaults: {
        content: null,
        author: {},
        published: null,
        commentCount: 0
      },

      embedReceipts: [
        {
          name: 'WiFi TV',
          regex: /.*connectuptv.pockittv.mobi\/v\/(\w*)/g,
          substitution: '<div class="post-media post-media--video post-media--wifitv"><video width="320" height="240" poster="http://connectuptv.pockittv.mobi/video/image/$1" controls><source src="$&"></video></div>'
        },
        // {
        //   name: 'Images',
        //   regex: /[a-z\-_0-9\/\:\.]*\.(jpg|jpeg|png|gif|svg)/g,
        //   substitution: '<div class="post-media post-media--image"><img src="$&"></img></div>'
        // },
        // {
        //   name: 'Youtube',
        //   regex: /.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/g,
        //   substitution: '<div class="post-media post-media--video  post-media--youtube"><div class="iframe-wrapper"><iframe id="ytplayer" type="text/html" width="320" height="240" src="http://www.youtube.com/embed/$1" frameborder="0"/></div></div>'
        // }
      ],
      
      initialize: function(post) {
        if (!post.entry) {
          /* We probably want the model to 
           * load this for us
           */
          return this.set(post, { silent: true })
        }
        var data = this._mapPost(post)
        this.set(data, { silent: true })
        this.requestCommentCount()
      },

      requestCommentCount: function() {
        var self = this
        var options = {
          node: this.get('node'),
          id: this.get('id'),
          rsm: { max: 1 }
        }

        socket.send('xmpp.buddycloud.items.replies', options, function(error, data, rsm) {
          if (error) {
            return self.trigger('error', error)
          }
          self.set({ commentCount: rsm.count || 0 })
        })
      },

      sync: function(method, collection, options) {
        if (!method) {
          method = 'get'
        }
        
        switch (method) {
          case 'get':
            return this.getPost()
          case 'post':
          case 'create':
            return this.publish()
          default:
            throw new Error('Unhandled method: ' + method)
        }
            
      },

      getPost: function() {
        var event = this.xmppEvents['get']
        var self = this
        var options = {
          node: this.get('node'),
          id: this.get('id')
        }
        socket.send(event, options, function(error, post) {
          if (error) {
            return self.trigger('error', error)
          }
          self.set(self._mapPost(post[0]), { silent: true })
          self.trigger('loaded:post')
        })
      },

      publish: function() {
        var payload = {
            node: this.get('node'),
            content: {
              atom: {
                content: this.get('content')
            },
            'in-reply-to': { ref: this.get('inReplyTo') }
          }
        }
        var self = this
        var event = this.xmppEvents['post']
        socket.send(event, payload, function(error, success) {
          if (error) {
            return self.trigger('publish:error', error)
          }
          self.trigger('publish:success')
        })
      },

      _mapPost: function(post) {
        return {
          displayName: null,
          username: post.entry.atom.author.uri.substr(5),
          published: Date.parse(post.entry.atom.published),
          content: this.parseContent(post.entry.atom.content.content),
          node: post.node,
          channelJid: post.node.split('/')[2],
          id: post.entry.atom.id,
          localId: post.entry.atom.id.split(',')[2] || post.entry.atom.id,
          canComment: true,
          isReply: ('comment' === post.entry.activity),
          inReplyTo: (post.entry['in-reply-to'] || {}).ref,
          likes: 1,
          commentCount: null
        }
      },

      parseContent: function(content) {
        content = _.escape(content)
          .replace(/\{/g, '&#123;')
          .replace(/&#x2F;/g, '/')
          .replace(/\}/g, '&#125;')

        for(var i=0; i<this.embedReceipts.length; i++){
          content = content.replace(this.embedReceipts[i].regex, this.embedReceipts[i].substitution)
        }

        log('parsed content', content)

        return content
      },

      addComment: function() {
        this.set('commentCount', this.attributes.commentCount + 1)
      },

      removeComment: function() {
        this.set(
          'commentCount',
          (this.attributes.commentCount > 0) ? this.attributes.commentCount - 1 : 0
        )<commit_msg>Remove debug + tidy<commit_after>define(function(require) {

    'use strict';
  
    var Backbone = require('backbone')
      , _        = require('underscore')
      , log      = require('app/utils/bows.min')('Models:Post')
      , socket   = require('app/utils/socket')

    return Backbone.Model.extend({
      
      xmppEvents: {
        'get': 'xmpp.buddycloud.retrieve',
        'post': 'xmpp.buddycloud.publish'
      },

      defaults: {
        content: null,
        author: {},
        published: null,
        commentCount: 0
      },

      embedReceipts: [
        {
          name: 'WiFi TV',
          regex: /.*connectuptv.pockittv.mobi\/v\/(\w*)/g,
          substitution: '<div class="post-media post-media--video post-media--wifitv"><video width="320" height="240" poster="http://connectuptv.pockittv.mobi/video/image/$1" controls><source src="$&"></video></div>'
        },
        // {
        //   name: 'Images',
        //   regex: /[a-z\-_0-9\/\:\.]*\.(jpg|jpeg|png|gif|svg)/g,
        //   substitution: '<div class="post-media post-media--image"><img src="$&"></img></div>'
        // },
        // {
        //   name: 'Youtube',
        //   regex: /.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/g,
        //   substitution: '<div class="post-media post-media--video  post-media--youtube"><div class="iframe-wrapper"><iframe id="ytplayer" type="text/html" width="320" height="240" src="http://www.youtube.com/embed/$1" frameborder="0"/></div></div>'
        // }
      ],
      
      initialize: function(post) {
        if (!post.entry) {
          /* We probably want the model to 
           * load this for us
           */
          return this.set(post, { silent: true })
        }
        var data = this._mapPost(post)
        this.set(data, { silent: true })
        this.requestCommentCount()
      },

      requestCommentCount: function() {
        var self = this
        var options = {
          node: this.get('node'),
          id: this.get('id'),
          rsm: { max: 1 }
        }

        socket.send('xmpp.buddycloud.items.replies', options, function(error, data, rsm) {
          if (error) {
            return self.trigger('error', error)
          }
          self.set({ commentCount: rsm.count || 0 })
        })
      },

      sync: function(method, collection, options) {
        if (!method) {
          method = 'get'
        }
        
        switch (method) {
          case 'get':
            return this.getPost()
          case 'post':
          case 'create':
            return this.publish()
          default:
            throw new Error('Unhandled method: ' + method)
        }
            
      },

      getPost: function() {
        var event = this.xmppEvents['get']
        var self = this
        var options = {
          node: this.get('node'),
          id: this.get('id')
        }
        socket.send(event, options, function(error, post) {
          if (error) {
            return self.trigger('error', error)
          }
          self.set(self._mapPost(post[0]), { silent: true })
          self.trigger('loaded:post')
        })
      },

      publish: function() {
        var payload = {
            node: this.get('node'),
            content: {
              atom: {
                content: this.get('content')
            },
            'in-reply-to': { ref: this.get('inReplyTo') }
          }
        }
        var self = this
        var event = this.xmppEvents['post']
        socket.send(event, payload, function(error, success) {
          if (error) {
            return self.trigger('publish:error', error)
          }
          self.trigger('publish:success')
        })
      },

      _mapPost: function(post) {
        return {
          displayName: null,
          username: post.entry.atom.author.uri.substr(5),
          published: Date.parse(post.entry.atom.published),
          content: this.parseContent(post.entry.atom.content.content),
          node: post.node,
          channelJid: post.node.split('/')[2],
          id: post.entry.atom.id,
          localId: post.entry.atom.id.split(',')[2] || post.entry.atom.id,
          canComment: true,
          isReply: ('comment' === post.entry.activity),
          inReplyTo: (post.entry['in-reply-to'] || {}).ref,
          likes: 1,
          commentCount: null
        }
      },

      parseContent: function(content) {
        content = _.escape(content)
          .replace(/\{/g, '&#123;')
          .replace(/&#x2F;/g, '/')
          .replace(/\}/g, '&#125;')

        this.embedReceipts.forEach(function(receipt) {
          content = content
            .replace(receipt.regex, receipt.substitution)
        }, this)
        return content
      },

      addComment: function() {
        this.set('commentCount', this.attributes.commentCount + 1)
      },

      removeComment: function() {
        this.set(
          'commentCount',
          (this.attributes.commentCount > 0) ? this.attributes.commentCount - 1 : 0
        )
{'fraction_non_alphanumeric': 0.16518034086405073, 'fraction_numerical': 0.007233452239397542, 'mean_word_length': 1.6511688993958498, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2211047', 'n_tokens_mistral': 3115, 'n_tokens_neox': 3020, 'n_words': 536}