<commit_before>var _ = require('underscore'),
    ForumPost = require('../models/forum').ForumPost,
    ForumReply = require('../models/forum').ForumReply,
    ForumComment = require('../models/forum').ForumComment;

var matchObjectId = /^[0-9a-fA-F]{24}$/;
var isObjectId = function(string){
    return matchObjectId.test(string);
};

module.exports.all = function (req, res, next) {
    ForumPost.find({}).exec(function (err, posts) {
        if (err) {
            return next(err);
        }
        res.json(200, {
            posts: posts
        });
    });
};

module.exports.create_post = function (req, res, next) {
    var post = new ForumPost();
    post.title = req.body.title;
    post.creator = req.user;
    post.mdtext = req.body.mdtext;
    post.save(function (err) {
        if (err) {
            return next(err);
        }
        res.json(200, post);
    });
};

module.exports.get_post = function (req, res, next) {
    // if given id is not a valid ObjectID, return 404.
    if (!isObjectId(req.params.id)){
        return next();
    }

    ForumPost.findById(req.params.id, function (err, post) {
        if (err) {
            return next(err);
        }

        post.replies.reverse();

        res.format({
            json: function(){
                res.json(200, post);
            },

            html: function(){
                res.render('forum/thread', {
                    post: post
                });
            }
        });
    });
};

module.exports.create_reply = function (req, res, next) {
    ForumPost.findById(req.params.id, function (err, post) {
        if (err) {
            return next(err);
        }
        var reply = new ForumReply();
        reply.mdtext = req.body.mdtext;
        reply.creator = req.user;

        post.replies.push(reply);
        post.save(function (err) {
            if (err) {
            return next(err);
            }
            res.json(200, reply);
        });
    });
};

module.exports.get_replies = function (req, res, next) {
    ForumPost.findById(req.params.id, function (err, post) {
        if (err) {
            return next(err);
        }
        res.json(200, post.replies);
    });
};

module.exports.create_comment = function (req, res, next) {
    ForumPost.findOne({'replies._id': req.params.rid}, function (err, post) {
        if (err) {
            return next(err);
        }
        if (post) {
            var comment = new ForumComment();
            comment.mdtext = req.body.mdtext;
            comment.creator = req.user;
            _.find(post.replies, function (reply){
                if (reply._id.toString() === req.params.rid) {
                    reply.comments.push(comment);
                    post.save(function (err) {
                        if (err) {
                            return next(err);
                        }
                        res.json(200, comment);
                    });
                }
            });
        }
    });
};<commit_msg>Delete replies and comments<commit_after>var _ = require('underscore'),
    ForumPost = require('../models/forum').ForumPost,
    ForumReply = require('../models/forum').ForumReply,
    ForumComment = require('../models/forum').ForumComment;

var matchObjectId = /^[0-9a-fA-F]{24}$/;
var isObjectId = function(string){
    return matchObjectId.test(string);
};

module.exports.all = function (req, res, next) {
    ForumPost.find({}).exec(function (err, posts) {
        if (err) {
            return next(err);
        }
        res.json(200, {
            posts: posts
        });
    });
};

module.exports.create_post = function (req, res, next) {
    var post = new ForumPost();
    post.title = req.body.title;
    post.creator = req.user;
    post.mdtext = req.body.mdtext;
    post.save(function (err) {
        if (err) {
            return next(err);
        }
        res.json(200, post);
    });
};

module.exports.get_post = function (req, res, next) {
    // if given id is not a valid ObjectID, return 404.
    if (!isObjectId(req.params.id)){
        return next();
    }

    ForumPost.findById(req.params.id, function (err, post) {
        if (err) {
            return next(err);
        }

        post.replies.reverse();

        res.format({
            json: function(){
                res.json(200, post);
            },

            html: function(){
                res.render('forum/thread', {
                    post: post
                });
            }
        });
    });
};

module.exports.create_reply = function (req, res, next) {
    ForumPost.findById(req.params.id, function (err, post) {
        if (err) {
            return next(err);
        }
        var reply = new ForumReply();
        reply.mdtext = req.body.mdtext;
        reply.creator = req.user;

        post.replies.push(reply);
        post.save(function (err) {
            if (err) {
            return next(err);
            }
            res.json(200, reply);
        });
    });
};

module.exports.delete_reply = function (req, res, next) {
    var postid = req.params.postid,
        replyid = req.params.replyid;

    ForumPost.findById(postid, function (err, post) {
        if (err) {
            return next(err);
        }
        post.replies.pull(replyid);
        post.save(function (err) {
            if (err) {
                return next(err);
            }
            res.json(200);
        });
    });
};

module.exports.get_replies = function (req, res, next) {
    ForumPost.findById(req.params.id, function (err, post) {
        if (err) {
            return next(err);
        }
        res.json(200, post.replies);
    });
};

module.exports.create_comment = function (req, res, next) {
    ForumPost.findOne({'replies._id': req.params.rid}, function (err, post) {
        if (err) {
            return next(err);
        }
        if (post) {
            var comment = new ForumComment();
            comment.mdtext = req.body.mdtext;
            comment.creator = req.user;
            _.find(post.replies, function (reply){
                if (reply._id.toString() === req.params.rid) {
                    reply.comments.push(comment);
                    post.save(function (err) {
                        if (err) {
                            return next(err);
                        }
                        res.json(200, comment);
                    });
                }
            });
            return next(new Error('Reply not found, could not add comment'));
        } else {
            return next(new Error('Post not found, could not add comment'));
        }
    });
};

module.exports.delete_comment = function (req, res, next) {
    var postid = req.params.postid,
        replyid = req.params.replyid,
        commentid = req.params.commentid;

    ForumPost.findById(postid, function (err, post) {
        if (err) {
            return next(err);
        }
        if (post) {
            _.find(post.replies, function (reply){
                if (reply._id.toString() === replyid) {
                    reply.comments.pull(commentid);
                    post.save(function (err) {
                        if (err) {
                            return next(err);
                        }
                        res.json(200);
                    });
                }
            });
            return next(new Error('Reply not found, could not delete comment'));
        } else {
            return next(new Error('Post not found, could not delete comment'));
        }
    });
};
{'fraction_non_alphanumeric': 0.14005565125215316, 'fraction_numerical': 0.00742016695375646, 'mean_word_length': 1.5051443743776967, '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': '6502369', 'n_tokens_mistral': 2267, 'n_tokens_neox': 2204, 'n_words': 455}