<commit_before>'use strict';

var utils = require('../../collabjs.utils')
  , pool = require('./collabjs.pool.mysql').pool
  , passwordHash = require('password-hash');

/**
 * Creates mentions for the given post.
 * @param {object} connection Database connection.
 * @param {number} postId Post id.
 * @param {string} content Post content.
 * @param {function(object)} callback Callback function.
 */
function addMentions(connection, postId, content, callback) {
  var accounts = utils.parseAccountNames(content, ',');
  if (accounts) {
    connection.query('CALL add_mentions (?,?)', [postId, accounts], function (err) {
      callback(err);
    });
  } else {
    callback(false);
  }
}

/**
 * Adds or creates hash tags for the given post.
 * @param {Object} connection Database connection.
 * @param {number} postId Post id.
 * @param {string} content Post content.
 * @param {function(object)} callback Callback function.
 */
function addHashTags(connection, postId, content, callback) {
  var tags = utils.parseHashTags(content, ',');
  if (tags) {
    connection.query('CALL assign_tags (?,?)', [postId, tags], function (err) {
      callback(err);
    });
  } else {
    callback(false);
  }
}

/**
 * Get list of user ids followed by current user.
 * @param {object} connection Database connection.
 * @param {number} userId Current user id.
 * @param {function(Object,Array)} callback Callback function.
 */
// TODO: move evaluation to stored procedure
function getFollowedUsers(connection, userId, callback) {
  var command = "SELECT GROUP_CONCAT(f.targetId separator ',') as list FROM following AS f WHERE f.userId = ?";
  connection.query(command, [userId], function (err, result) {
    if (err) { callback(err, []); }
    else {
      var list = result[0].list;
      var ids = list.split(',').map(Number);
      callback(false, ids);
    }
  });
}

/**
 * MySQL provider implementation.
 * @constructor
 */
function Provider() {}

Provider.prototype = {
  // TODO: introduce paging support
  getAccounts: function (callback) {
    pool.getConnection(function (err, connection) {
      var command = 'SELECT id, account, name, created, email, roles FROM vw_accounts';
      connection.query(command, function (err, result) {
        connection.release();
        if (err) { return callback(err, null); }
        callback(err, result);
      });
    });
  },
  getAccountById: function (id, callback) {
    pool.getConnection(function (err, connection) {
      var command = "SELECT * FROM vw_accounts WHERE id = ? LIMIT 1";
      connection.query(command, [id], function (err, result) {
        connection.release();
        if (err) { return callback(err, null); }
        if (result.length > 0) {
          var account = result[0];
          account.pictureUrl = utils.getAvatarUrl(account.pictureId);
          return callback(err, account);
        }
        else { return callback(err, null); }
      });
    });
  },
  getAccount: function (account, callback) {
    pool.getConnection(function (err, connection) {
      var command = "SELECT * FROM vw_accounts WHERE account = ? LIMIT 1";
      connection.query(command, [account], function (err, result) {
        connection.release();
        if (err) { return callback(err, null); }
        if (result.length > 0) {
          var acc = result[0];
          acc.pictureUrl = utils.getAvatarUrl(acc.pictureId);
          return callback(err, acc);
        }
        else { return callback(err, null); }
      });
    });
  },
  createAccount: function (json, callback) {
    pool.getConnection(function (err, connection) {
      json.emailHash = utils.createHash(json.email);
      connection.query('INSERT INTO users SET ?', json, function (err, result) {
        connection.release();
        if (err) {
          var errorMessage = 'Error creating account.';
          if (err.code === 'ER_DUP_ENTRY') {
            errorMessage = 'User with such account already exists.';
          }
          callback(errorMessage);
        } else {
          callback(null, { id: result.insertId });
        }
      });
    });
  },
  deleteAccount: function (account, callback) {
    pool.getConnection(function (err, connection) {
      var command = 'DELETE FROM users WHERE account = ?';
      connection.query(command, account, function (err, result) {
        connection.release();
        if (err) {
          callback('Error removing account.');
        } else {
          var succeeded = result.affectedRows > 0;<commit_msg>restrict removing system accounts from admin panel<commit_after>'use strict';

var utils = require('../../collabjs.utils')
  , pool = require('./collabjs.pool.mysql').pool
  , passwordHash = require('password-hash');

/**
 * Creates mentions for the given post.
 * @param {object} connection Database connection.
 * @param {number} postId Post id.
 * @param {string} content Post content.
 * @param {function(object)} callback Callback function.
 */
function addMentions(connection, postId, content, callback) {
  var accounts = utils.parseAccountNames(content, ',');
  if (accounts) {
    connection.query('CALL add_mentions (?,?)', [postId, accounts], function (err) {
      callback(err);
    });
  } else {
    callback(false);
  }
}

/**
 * Adds or creates hash tags for the given post.
 * @param {Object} connection Database connection.
 * @param {number} postId Post id.
 * @param {string} content Post content.
 * @param {function(object)} callback Callback function.
 */
function addHashTags(connection, postId, content, callback) {
  var tags = utils.parseHashTags(content, ',');
  if (tags) {
    connection.query('CALL assign_tags (?,?)', [postId, tags], function (err) {
      callback(err);
    });
  } else {
    callback(false);
  }
}

/**
 * Get list of user ids followed by current user.
 * @param {object} connection Database connection.
 * @param {number} userId Current user id.
 * @param {function(Object,Array)} callback Callback function.
 */
// TODO: move evaluation to stored procedure
function getFollowedUsers(connection, userId, callback) {
  var command = "SELECT GROUP_CONCAT(f.targetId separator ',') as list FROM following AS f WHERE f.userId = ?";
  connection.query(command, [userId], function (err, result) {
    if (err) { callback(err, []); }
    else {
      var list = result[0].list;
      var ids = list.split(',').map(Number);
      callback(false, ids);
    }
  });
}

/**
 * MySQL provider implementation.
 * @constructor
 */
function Provider() {}

Provider.prototype = {
  // TODO: introduce paging support
  getAccounts: function (callback) {
    pool.getConnection(function (err, connection) {
      var command = 'SELECT id, account, name, created, email, roles FROM vw_accounts';
      connection.query(command, function (err, result) {
        connection.release();
        if (err) { return callback(err, null); }
        callback(err, result);
      });
    });
  },
  getAccountById: function (id, callback) {
    pool.getConnection(function (err, connection) {
      var command = "SELECT * FROM vw_accounts WHERE id = ? LIMIT 1";
      connection.query(command, [id], function (err, result) {
        connection.release();
        if (err) { return callback(err, null); }
        if (result.length > 0) {
          var account = result[0];
          account.pictureUrl = utils.getAvatarUrl(account.pictureId);
          return callback(err, account);
        }
        else { return callback(err, null); }
      });
    });
  },
  getAccount: function (account, callback) {
    pool.getConnection(function (err, connection) {
      var command = "SELECT * FROM vw_accounts WHERE account = ? LIMIT 1";
      connection.query(command, [account], function (err, result) {
        connection.release();
        if (err) { return callback(err, null); }
        if (result.length > 0) {
          var acc = result[0];
          acc.pictureUrl = utils.getAvatarUrl(acc.pictureId);
          return callback(err, acc);
        }
        else { return callback(err, null); }
      });
    });
  },
  createAccount: function (json, callback) {
    pool.getConnection(function (err, connection) {
      json.emailHash = utils.createHash(json.email);
      connection.query('INSERT INTO users SET ?', json, function (err, result) {
        connection.release();
        if (err) {
          var errorMessage = 'Error creating account.';
          if (err.code === 'ER_DUP_ENTRY') {
            errorMessage = 'User with such account already exists.';
          }
          callback(errorMessage);
        } else {
          callback(null, { id: result.insertId });
        }
      });
    });
  },
  deleteAccount: function (account, callback) {
    pool.getConnection(function (err, connection) {
      var command = 'DELETE FROM users WHERE account = ? AND system = 0';
      connection.query(command, account, function (err, result) {
        connection.release();
        if (err) {
          callback('Error removing account.');
        } else {
          var succeeded = result.affectedRows > 0;
{'fraction_non_alphanumeric': 0.1381637168141593, 'fraction_numerical': 0.0018805309734513274, 'mean_word_length': 2.9087764807609164, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '1542654', 'n_tokens_mistral': 2565, 'n_tokens_neox': 2560, 'n_words': 730}