<commit_before>/* eslint-disable max-lines,no-magic-numbers */

var db = require('tresdb-db');
var eventsDal = require('../events/dal');
var path = require('path');
var _ = require('lodash');
var purifyMarkdown = require('purify-markdown');

// Private methods

var insertOne = function (entry, callback) {
  // Parameters:
  //   entry
  //     raw entry to insert
  //   callback
  //     function (err, entryId);
  var coll = db.collection('entries');

  coll.insertOne(entry, function (err, result) {
    if (err) {
      return callback(err);
    }
    return callback(null, result.insertedId);
  });
};

var removeOne = function (entryId, callback) {
  // Parameters
  //   entryId
  //     object id
  //   callback
  //     function (err)

  var coll = db.get().collection('entries');
  var q = { _id: entryId };

  coll.deleteOne(q, {}, function (err) {
    if (err) {
      return callback(err);
    }
    return callback();
  });
};


// Public methods

exports.changeLocationEntry = function (params, callback) {
  // Modify entry
  //
  // Parameters:
  //   params:
  //     oldEntry
  //       raw entry object
  //     locationName
  //       because entries do not store location name but events do
  //     markdown
  //     isVisit
  //     filepath
  //     mimetype
  //     thumbfilepath
  //     thumbmimetype
  //   callback
  //     function (err)
  //
  var coll = db.collection('entries');
  var q = { _id: params.oldEntry._id };

  // Backward compatibility for comments. Reset to [].
  var oldComments = params.oldEntry.comments;
  var nextComments = oldComments ? oldComments : [];

  // Sanitize
  var sanitizedMarkdown = purifyMarkdown(params.markdown).trim();

  var changedEntry = {
    type: 'location_entry',
    user: params.oldEntry.user,
    time: params.oldEntry.time,
    locationId: params.oldEntry.locationId,
    deleted: params.oldEntry.deleted,
    data: {
      markdown: sanitizedMarkdown,
      isVisit: params.isVisit,
      filepath: params.filepath,
      mimetype: params.mimetype,
      thumbfilepath: params.thumbfilepath,
      thumbmimetype: params.thumbmimetype,
    },
    comments: nextComments,
  };

  coll.replaceOne(q, changedEntry, function (err) {
    if (err) {
      return callback(err);
    }

    var eventParams = Object.assign({}, params, {
      markdown: sanitizedMarkdown,
    });

    eventsDal.createLocationEntryChanged(eventParams, callback);
  });
};

exports.createBlank = function (params, callback) {
  // Parameters:
  //   params
  //     locationId
  //       ObjectId
  //     locationName
  //       string
  //     username
  //       string
  //   callback
  //     function (err, blankEntry)
  //
  var blankEntry = {
    type: 'location_entry',
    user: params.username,
    time: db.timestamp(),
    locationId: params.locationId,
    created: false,
    deleted: false,
    published: false,
    markdown: '',
    flags: [], // e.g. 'visit'
    attachments: [],
    comments: [],
  };

  insertOne(blankEntry, function (err, newEntryId) {
    if (err) {
      return callback(err);
    }

    blankEntry._id = newEntryId;
    return callback(null, blankEntry);
  });
};

exports.createLocationEntry = function (params, callback) {
  // Parameters:
  //   params:
  //     locationId
  //       ObjectId
  //     locationName
  //       string
  //     username
  //       string
  //     markdown
  //       string or null
  //     isVisit
  //       boolean
  //     filepath
  //       string or null
  //       The relative path of the file in the uploads dir
  //     mimetype
  //       string or null
  //     thumbfilepath
  //       string or null
  //       The relative path of the thumbnail file in the uploads dir
  //     thumbmimetype
  //       string or null
  //     overlay
  //       overlay-import object or null
  //   callback
  //     function (err, insertedId)

  var sanitizedMarkdown = purifyMarkdown(params.markdown).trim();

  var newEntry = {
    type: 'location_entry',
    user: params.username,
    time: db.timestamp(),
    locationId: params.locationId,
    deleted: false,
    data: {
      markdown: sanitizedMarkdown,
      isVisit: params.isVisit,
      filepath: params.filepath,
      mimetype: params.mimetype,
      thumbfilepath: params.thumbfilepath,
      thumbmimetype: params.thumbmimetype,
      overlay: params.overlay ? params.overlay : null,
    },
    comments: [],
  };

  insertOne(newEntry, function (err, newEntryId) {
    if (err) {
      return callback(err);
    }

    var eventParams = Object.assign({}, params, {
      entryId: newEntryId,
      markdown: sanitizedMarkdown,
    });
<commit_msg>Migrate createLocationEntry<commit_after>/* eslint-disable max-lines,no-magic-numbers */

var db = require('tresdb-db');
var eventsDal = require('../events/dal');
var path = require('path');
var _ = require('lodash');
var purifyMarkdown = require('purify-markdown');

// Private methods

var insertOne = function (entry, callback) {
  // Parameters:
  //   entry
  //     raw entry to insert
  //   callback
  //     function (err, entryId);
  var coll = db.collection('entries');

  coll.insertOne(entry, function (err, result) {
    if (err) {
      return callback(err);
    }
    return callback(null, result.insertedId);
  });
};

var removeOne = function (entryId, callback) {
  // Parameters
  //   entryId
  //     object id
  //   callback
  //     function (err)

  var coll = db.get().collection('entries');
  var q = { _id: entryId };

  coll.deleteOne(q, {}, function (err) {
    if (err) {
      return callback(err);
    }
    return callback();
  });
};


// Public methods

exports.changeLocationEntry = function (params, callback) {
  // Modify entry
  //
  // Parameters:
  //   params:
  //     oldEntry
  //       raw entry object
  //     locationName
  //       because entries do not store location name but events do
  //     markdown
  //     isVisit
  //     filepath
  //     mimetype
  //     thumbfilepath
  //     thumbmimetype
  //   callback
  //     function (err)
  //
  var coll = db.collection('entries');
  var q = { _id: params.oldEntry._id };

  // Backward compatibility for comments. Reset to [].
  var oldComments = params.oldEntry.comments;
  var nextComments = oldComments ? oldComments : [];

  // Sanitize
  var sanitizedMarkdown = purifyMarkdown(params.markdown).trim();

  var changedEntry = {
    type: 'location_entry',
    user: params.oldEntry.user,
    time: params.oldEntry.time,
    locationId: params.oldEntry.locationId,
    deleted: params.oldEntry.deleted,
    data: {
      markdown: sanitizedMarkdown,
      isVisit: params.isVisit,
      filepath: params.filepath,
      mimetype: params.mimetype,
      thumbfilepath: params.thumbfilepath,
      thumbmimetype: params.thumbmimetype,
    },
    comments: nextComments,
  };

  coll.replaceOne(q, changedEntry, function (err) {
    if (err) {
      return callback(err);
    }

    var eventParams = Object.assign({}, params, {
      markdown: sanitizedMarkdown,
    });

    eventsDal.createLocationEntryChanged(eventParams, callback);
  });
};

exports.createBlank = function (params, callback) {
  // Parameters:
  //   params
  //     locationId
  //       ObjectId
  //     locationName
  //       string
  //     username
  //       string
  //   callback
  //     function (err, blankEntry)
  //
  var blankEntry = {
    type: 'location_entry',
    user: params.username,
    time: db.timestamp(),
    locationId: params.locationId,
    created: false,
    deleted: false,
    published: false,
    markdown: '',
    flags: [], // e.g. 'visit'
    attachments: [],
    comments: [],
  };

  insertOne(blankEntry, function (err, newEntryId) {
    if (err) {
      return callback(err);
    }

    blankEntry._id = newEntryId;
    return callback(null, blankEntry);
  });
};

exports.createLocationEntry = function (params, callback) {
  // Parameters:
  //   params:
  //     locationId
  //       ObjectId
  //     locationName
  //       string, for event
  //     username
  //       string
  //     markdown
  //       optional string or null
  //     attachments
  //       optional array of attachment keys
  //     flags
  //       optional array of instance-specific flags e.g. 'visit'
  //   callback
  //     function (err, insertedId)
  //
  var sanitizedMarkdown = purifyMarkdown(params.markdown).trim();

  if (typeof params.markdown !== 'string') {
    params.markdown = '';
  }
  if (typeof params.attachments !== 'object') {
    params.attachments = [];
  }
  if (typeof params.flags !== 'object') {
    params.flags = [];
  }

  var newEntry = {
    type: 'location_entry',
    user: params.username,
    time: db.timestamp(),
    locationId: params.locationId,
    deleted: false,
    published: false,
    markdown: sanitizedMarkdown,
    attachments: params.attachments,
    comments: [],
    flags: params.flags,
  };

  insertOne(newEntry, function (err, newEntryId) {
    if (err) {
      return callback(err);
    }

    newEntry._id = newEntryId;
    var eventParams = {
      locationName: params.locationName,
      entry: newEntry,
    };

{'fraction_non_alphanumeric': 0.12891223462464435, 'fraction_numerical': 0.0, 'mean_word_length': 2.277977044476327, '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': '891460', 'n_tokens_mistral': 2873, 'n_tokens_neox': 2792, 'n_words': 662}