<commit_before>'use strict';

var twitter = require('twitter-text');
var Publico = require('meatspace-publico');

var convert2webm = require('./convert2webm');

var publico = new Publico('none', {
  db: './db/db-messages',
  limit: 15
});

var IMAGE_TYPE = 'jpeg';

var validateImageType = function (data, next) {
  if (!data.length || data.length !== 10) {
    next(new Error('Invalid number of frames'));
    return;
  }

  var isJPG = true;

  for (var i = 0; i < data.length; i ++) {
    if (!data[i].match(/^data:image\/jpeg;base64,/)) {
      isJPG = false;
      break;
    }
  }

  if (!isJPG) {
    next(new Error('No JPEG detected'));
    return;
  }

  next(null, true);
};

exports.recent = function (socket) {
  publico.getChats(true, function (err, c) {
    if (err) {
      console.log(err);
      return;
    }

    if (c.chats && c.chats.length > 0) {
      c.chats.reverse();
    }

    c.chats.forEach(function (chat) {
      setImmediate(function () {
        socket.emit('message', chat.value);
      });
    });
  });
};

exports.addMessage = function (payload, next) {
  validateImageType(payload.media, function (err) {
    if (err) {
      next(err);
      return;
    }

    convert2webm.transform(payload.videoType, payload.media, function (err, media) {
      if (err) {
        next(err);
        return;
      }

      var message = twitter.autoLink(twitter.htmlEscape(payload.message), {
        targetBlank: true
      });

      publico.addChat(message.slice(0, 250), {
        ttl: 600000,
        media: media,
        fingerprint: payload.fingerprint
      }, function (err, chat) {
        if (err) {
          next(err);
          return;
        }<commit_msg>Truncates message at 250 characters before HTML-escaping and linking Fixes meatspaces/meatspace-chat-v2#45<commit_after>'use strict';

var twitter = require('twitter-text');
var Publico = require('meatspace-publico');

var convert2webm = require('./convert2webm');

var publico = new Publico('none', {
  db: './db/db-messages',
  limit: 15
});

var IMAGE_TYPE = 'jpeg';

var validateImageType = function (data, next) {
  if (!data.length || data.length !== 10) {
    next(new Error('Invalid number of frames'));
    return;
  }

  var isJPG = true;

  for (var i = 0; i < data.length; i ++) {
    if (!data[i].match(/^data:image\/jpeg;base64,/)) {
      isJPG = false;
      break;
    }
  }

  if (!isJPG) {
    next(new Error('No JPEG detected'));
    return;
  }

  next(null, true);
};

exports.recent = function (socket) {
  publico.getChats(true, function (err, c) {
    if (err) {
      console.log(err);
      return;
    }

    if (c.chats && c.chats.length > 0) {
      c.chats.reverse();
    }

    c.chats.forEach(function (chat) {
      setImmediate(function () {
        socket.emit('message', chat.value);
      });
    });
  });
};

exports.addMessage = function (payload, next) {
  validateImageType(payload.media, function (err) {
    if (err) {
      next(err);
      return;
    }

    convert2webm.transform(payload.videoType, payload.media, function (err, media) {
      if (err) {
        next(err);
        return;
      }

      var message = twitter.autoLink(twitter.htmlEscape(payload.message.slice(0, 250)), {
        targetBlank: true
      });

      publico.addChat(message, {
        ttl: 600000,
        media: media,
        fingerprint: payload.fingerprint
      }, function (err, chat) {
        if (err) {
          next(err);
          return;
        }
{'fraction_non_alphanumeric': 0.15735167669819433, 'fraction_numerical': 0.013757523645743766, 'mean_word_length': 2.431661750245821, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3658754', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1169, 'n_words': 254}