<commit_before>'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    Note = mongoose.model('Note'),
    Notifications = mongoose.model('Notification'),
    Comment = mongoose.model('Comment'),
    User = mongoose.model('User'),
    _ = require('lodash');


/**
 * List of notes
 */
exports.all = function(req, res) {
    var q = Comment.find({onNote : req.query.noteId});


    
    q.sort({ dateCreate : 'desc' }).populate('createBy', 'name username avatar').exec(function(err, comments) {
        if (err) {
            console.log(err);
            res.render('error', {
                status: 500
            });
        } else {
            res.jsonp(comments);
        }
    });

};

// Get note by id
exports.comment = function(req, res, next, id) {
    Comment.load(id, function(err, comment) {
        if (err) return next(err);
        if (!comment) return next(new Error('Failed to load comment ' + id));
        req.comment = comment;
        next();
    });
};

/**
 * Create a ntoe
 */

 exports.create = function(req, res) {
    var comment = new Comment(req.body);
    comment.createBy = req.user;
    // todo: send to class and member
    comment.save(function(err) {
        if (err) {
            return res.send('users/signup', {
                errors: err.errors,
                comment: comment
            });
        } else {
            // update comment
            res.jsonp(comment);
            // find and pregmatch username from list
            var re = /(@\w+)/gi;
            var found = comment.content.match(re);
            console.log('find username in ' + comment.content);
            console.log(found);
            var userArr =[];
            if(found != undefined && (found instanceof Array))
            {
                for (var i = 0; i < found.length; i++) {
                    userArr[i] = found[i].replace('@','');
                };
                User.find({ username : {'$in': userArr} },'id name username',function(err,users){
                    if (err) {
                        console.error(err)
                    }
                    else
                        for (var i = 0; i < users.length; i++) {
                            userid = users[i]._id;
                            var notify = new Notifications();
                            notify.source = comment;
                            notify.from = req.user;
                            notify.to = userid;
                            notify.type = 'comment';
                            notify.content =  req.user.name + ' has mentioned you on comment.' ;
                            notify.save();
                        };
                })
            }
            

            Note.load(comment.onNote , function(err, note) {
                if (err) return next(err);
                if (!note) return next(new Error('Failed to load note ' + id));
                var query = Comment.find( {onNote : comment.onNote } );
                    query.count(function(err,totals){
                        if (err) {
                            console.error(err);
                        }
                        else
                        {
                            note.totalComments = totals;

                            // notify to member and owner of note
                            for (var i = 0; i < note.sendToMembers.length; i++) {
                                var userid = note.sendToMembers[i];
                                var notify = new Notifications();
                                notify.source = comment;
                                notify.from = req.user;<commit_msg>Debug - comment on note<commit_after>'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    Note = mongoose.model('Note'),
    Notifications = mongoose.model('Notification'),
    Comment = mongoose.model('Comment'),
    User = mongoose.model('User'),
    _ = require('lodash');


/**
 * List of notes
 */
exports.all = function(req, res) {
    var q = Comment.find({onNote : req.query.noteId});


    
    q.sort({ dateCreate : 'desc' }).populate('createBy', 'name username avatar').exec(function(err, comments) {
        if (err) {
            console.log(err);
            res.render('error', {
                status: 500
            });
        } else {
            res.jsonp(comments);
        }
    });

};

// Get note by id
exports.comment = function(req, res, next, id) {
    Comment.load(id, function(err, comment) {
        if (err) return next(err);
        if (!comment) return next(new Error('Failed to load comment ' + id));
        req.comment = comment;
        next();
    });
};

/**
 * Create a ntoe
 */

 exports.create = function(req, res) {
    var comment = new Comment(req.body);
    comment.createBy = req.user;
    // todo: send to class and member
    comment.save(function(err) {
        if (err) {
            return res.send('users/signup', {
                errors: err.errors,
                comment: comment
            });
        } else {
            // update comment
            res.jsonp(comment);
            // find and pregmatch username from list
            var re = /(@\w+)/gi;
            var found = comment.content.match(re);
            console.log('find username in ' + comment.content);
            console.log(found);
            var userArr =[];
            if(found != undefined && (found instanceof Array))
            {
                for (var i = 0; i < found.length; i++) {
                    userArr[i] = found[i].replace('@','');
                };
                User.find({ username : {'$in': userArr} },'id name username',function(err,users){
                    if (err) {
                        console.error(err)
                    }
                    else
                        for (var i = 0; i < users.length; i++) {
                            var userid = users[i]._id;
                            var notify = new Notifications();
                            notify.source = comment;
                            notify.from = req.user;
                            notify.to = userid;
                            notify.type = 'comment';
                            notify.content =  req.user.name + ' has mentioned you on comment.' ;
                            notify.save();
                        };
                })
            }
            

            Note.load(comment.onNote , function(err, note) {
                if (err) return next(err);
                if (!note) return next(new Error('Failed to load note ' + id));
                var query = Comment.find( {onNote : comment.onNote } );
                    query.count(function(err,totals){
                        if (err) {
                            console.error(err);
                        }
                        else
                        {
                            note.totalComments = totals;

                            // notify to member and owner of note
                            for (var i = 0; i < note.sendToMembers.length; i++) {
                                var userid = note.sendToMembers[i];
                                var notify = new Notifications();
                                notify.source = comment;
                                notify.from = req.user;
{'fraction_non_alphanumeric': 0.12255700325732899, 'fraction_numerical': 0.0016286644951140066, 'mean_word_length': 1.1847020456566855, 'pattern_counts': {'":': 0, '<': 9, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '924975', 'n_tokens_mistral': 1908, 'n_tokens_neox': 1838, 'n_words': 457}