<commit_before>"use strict";

import { default as userModel } from '../model/user';
import { respondError, respondSuccess } from '../util/response';
import { codes } from '../util/status';
import B from 'bluebird';
import { logger } from '../util/logger';

function isAdmin(req) {
  return req.info.user.admin;
}

function isOwner(req) {
  let ownerId = null;

  if (req.params !== undefined &&
    req.params.uid !== undefined) {
    ownerId = req.params.uid;
  } else if (req.query !== undefined &&
    req.query.uid !== undefined) {
    ownerId = req.query.uid;
  }

  return req.info.user._id.toString() === ownerId;
}

async function sanitizeObjectId(proposedId) {
  try {
    let objID = new ObjectId(proposedId);
    return proposedId === objID.toString();

  } catch (e) {
    return false;
  }
}

async function loadUserOnRequest(req, res, param) {
  try {
    let user = await userModel.findOne(param).exec();

    if (null === user) {
      return codes.UserNotFound.code;
    } else {
      req.info = req.info || {};
      req.info.user = user;
      return null;
    }
  } catch (e) {
    logger.error(e.message);
    return codes.UserNotFound.code;
  }
}


class UserCtrl {
  constructor() {

  }

  async createUser(req, res) {
    try {
      let nickname = req.body.nickname;
      if (nickname === '') {
        // default nickname
        nickname = 'chitchatter';
      }

      let newUser = new userModel({
        fbid: req.body.fbid,
        nickname: nickname,
        name: req.body.name,
        gender: req.body.gender,
        email: req.body.email,
        admin: false,
        token: req.body.token,
      });

      let user = await userModel.findOne({ fbid: newUser.fbid }).exec();

      if (null === user) {
        let savedUser = await newUser.save();

        respondSuccess(req, res, savedUser.token);
      } else {
        respondError(req, res, codes.UserEmailAlreadyInUse.code);
      }
    } catch (e) {
      logger.error(e.message);
      respondError(req, res, codes.UnknownError.code);
    }
  }

  async deleteUser(req, res) {
    try {
      let deletedUser = await userModel.findOneAndRemove({ _id: req.params.uid }).exec();

      if (deletedUser === null) {
        respondError(req, res, codes.UserNotFound.code);
      } else {
        respondSuccess(req, res, deletedUser);
      }
    } catch (e) {
      logger.error(e.message);
      respondError(req, res, codes.UserRemovalFailed.code);
    }
  }

  async getUser(req, res) {
    try {
      let user = await userModel.findOne({ _id: req.params.uid }).exec();

      if (user === null) {
        respondError(req, res, codes.UserNotFound.code);
      } else {
        respondSuccess(req, res, user);
      }<commit_msg>update log<commit_after>"use strict";

import { default as userModel } from '../model/user';
import { respondError, respondSuccess } from '../util/response';
import { codes } from '../util/status';
import B from 'bluebird';
import { logger } from '../util/logger';

function isAdmin(req) {
  return req.info.user.admin;
}

function isOwner(req) {
  let ownerId = null;

  if (req.params !== undefined &&
    req.params.uid !== undefined) {
    ownerId = req.params.uid;
  } else if (req.query !== undefined &&
    req.query.uid !== undefined) {
    ownerId = req.query.uid;
  }

  return req.info.user._id.toString() === ownerId;
}

async function sanitizeObjectId(proposedId) {
  try {
    let objID = new ObjectId(proposedId);
    return proposedId === objID.toString();

  } catch (e) {
    return false;
  }
}

async function loadUserOnRequest(req, res, param) {
  try {
    let user = await userModel.findOne(param).exec();

    if (null === user) {
      return codes.UserNotFound.code;
    } else {
      req.info = req.info || {};
      req.info.user = user;
      return null;
    }
  } catch (e) {
    logger.error(e.message);
    return codes.UserNotFound.code;
  }
}


class UserCtrl {
  constructor() {

  }

  async createUser(req, res) {
    try {
      let nickname = req.body.nickname;
      if (nickname === '') {
        // default nickname
        nickname = 'chitchatter';
      }

      let newUser = new userModel({
        fbid: req.body.fbid,
        nickname: nickname,
        name: req.body.name,
        gender: req.body.gender,
        email: req.body.email,
        admin: false,
        token: req.body.token,
      });

      let user = await userModel.findOne({ fbid: newUser.fbid }).exec();

      if (null === user) {
        let savedUser = await newUser.save();

        respondSuccess(req, res, savedUser.token);
      } else {
        respondError(req, res, codes.UserEmailAlreadyInUse.code);
      }
    } catch (e) {
      logger.error(JSON.stringify(e));
      respondError(req, res, codes.UnknownError.code);
    }
  }

  async deleteUser(req, res) {
    try {
      let deletedUser = await userModel.findOneAndRemove({ _id: req.params.uid }).exec();

      if (deletedUser === null) {
        respondError(req, res, codes.UserNotFound.code);
      } else {
        respondSuccess(req, res, deletedUser);
      }
    } catch (e) {
      logger.error(e.message);
      respondError(req, res, codes.UserRemovalFailed.code);
    }
  }

  async getUser(req, res) {
    try {
      let user = await userModel.findOne({ _id: req.params.uid }).exec();

      if (user === null) {
        respondError(req, res, codes.UserNotFound.code);
      } else {
        respondSuccess(req, res, user);
      }
{'fraction_non_alphanumeric': 0.1448692152917505, 'fraction_numerical': 0.0, 'mean_word_length': 2.4089775561097255, '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': '3713204', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1730, 'n_words': 407}