<commit_before>var _ = require('lodash');
var mongoose = require('mongoose');
var Comment = require('../../models/comment');
var Project = require('../../models/project');
var Task = require('../../models/task');

var notMember = new Error('Not member');
notMember.status = 400;
var forbidden = new Error('Forbidden');
forbidden.status = 403;
var notFound = new Error('Not Found');
notFound.status = 404;
var alreadyJoined = new Error('Already joined');
alreadyJoined.status = 409;

function deepPopulateComment(comment, next) {
  Comment
  .populate(comment, 'user task', function(err, _commentWithTask) {
    if (err) {
      return next(err);
    }
    Comment.populate(_commentWithTask, {
      path: 'task.project',
      model: 'Project'
    }, function(err, _commentWithProject) {
      if (err) {
        return next(err);
      }
      Comment.populate(_commentWithProject, {
        path: 'task.project.owner task.project.members',
        model: 'User'
      }, function(err, comment) {
        if (err) {
          return next(err);
        }
        return next(null, comment);
      });
    });
  });
}

function isTaskInProject(task, project_id) {
  if (!project_id) return true;
  return task.project._id == project_id;
}

function isProjectVisible(project, user) {
  return project.isPublic || project.owner.username === user.username ||
    _.some(project.members, 'username', user.username);
}

module.exports.addComment = function(req, res, task_id, project_id, next) {
  Task
    .findOne({ _id: task_id, project: project_id })
    .populate('owner assignedUsers project')
    .exec(function(err, _task) {
      if (err) {
        return next(err);
      }
      Task.populate(_task, {
        path: 'project.owner project.members',
        model: 'User',
      }, function(err, task) {<commit_msg>populate users of tasks of comments<commit_after>var _ = require('lodash');
var mongoose = require('mongoose');
var Comment = require('../../models/comment');
var Project = require('../../models/project');
var Task = require('../../models/task');

var notMember = new Error('Not member');
notMember.status = 400;
var forbidden = new Error('Forbidden');
forbidden.status = 403;
var notFound = new Error('Not Found');
notFound.status = 404;
var alreadyJoined = new Error('Already joined');
alreadyJoined.status = 409;

function deepPopulateComment(comment, next) {
  Comment
    .populate(comment, 'user task', function(err, _commentWithTask) {
      if (err) {
        return next(err);
      }
      Comment.populate(_commentWithTask, {
        path: 'task.project',
        model: 'Project'
      }, function(err, _commentWithProject) {
        if (err) {
          return next(err);
        }
        Comment.populate(_commentWithProject, {
          path: 'task.project.owner task.project.members task.assignedUsers task.owner',
          model: 'User'
        }, function(err, comment) {
          if (err) {
            return next(err);
          }
          return next(null, comment);
        });
      });
    });
}

function isTaskInProject(task, project_id) {
  if (!project_id) return true;
  return task.project._id == project_id;
}

function isProjectVisible(project, user) {
  return project.isPublic || project.owner.username === user.username ||
    _.some(project.members, 'username', user.username);
}

module.exports.addComment = function(req, res, task_id, project_id, next) {
  Task
    .findOne({ _id: task_id, project: project_id })
    .populate('owner assignedUsers project')
    .exec(function(err, _task) {
      if (err) {
        return next(err);
      }
      Task.populate(_task, {
        path: 'project.owner project.members',
        model: 'User',
      }, function(err, task) {
{'fraction_non_alphanumeric': 0.1472951258703803, 'fraction_numerical': 0.006427423674343867, 'mean_word_length': 3.033477321814255, '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': '6235437', 'n_tokens_mistral': 1145, 'n_tokens_neox': 1120, 'n_words': 265}