<commit_before>var mongoose = require('mongoose');
var passport = require('passport');
var _ = require('underscore');
var User = require('../models/User');
var NewsItem = require('../models/NewsItem');
var Vote = require('../models/Vote');
var votesController = require('./votes');
var addVotesToNewsItems = votesController.addVotesFor('news');
var addVotesToItem = votesController.addVotesToItem;
var Comment = require('../models/Comment');
var request = require('request');
var async = require('async');
var marked = require('marked');

marked.setOptions({
  sanitize: true
});

exports.index = function(req, res, next) {

  var page = typeof req.params.page !== 'undefined' ? req.params.page : 1;
  page = isNaN(page) ? 1 : Number(page);
  page = page < 1 ? 1 : page;

  getNewsItems({}, req.user, function (err, newsItems) {
    if(err) return next(err);

    res.render('news/index', {
      title: 'Recent News',
      items: sortByScore(newsItems),
      page: page,
      newsItemsPerPage: newsItemsPerPage
    });
  }, page);
};

/**
 * GET /news/:id
 * View comments on a news item
 */
exports.comments = function (req, res, next) {

  NewsItem
  .findById(req.params.id)
  .populate('poster')
  .exec(function (err, newsItem) {

    if(err) return next(err);

    async.parallel({
      votes: function (cb) {
        addVotesToNewsItems(newsItem, req.user, cb);
      },
      comments: function (cb) {
        Comment
        .find({
          item: newsItem._id,
          itemType: 'news'
        })
        .populate('poster')
        .exec(cb);
      }
    }, function (err, results) {

      if(err) return next(err);

      _.each(results.comments, function (comment,i,l) {
        comment.contents = marked(comment.contents);
      });

      newsItem.summary = marked(newsItem.summary);

      res.render('news/show', {
        title: newsItem.title,
        item: newsItem,
        comments: results.comments
      });
    });<commit_msg>Add redirect when news item not found<commit_after>var mongoose = require('mongoose');
var passport = require('passport');
var _ = require('underscore');
var User = require('../models/User');
var NewsItem = require('../models/NewsItem');
var Vote = require('../models/Vote');
var votesController = require('./votes');
var addVotesToNewsItems = votesController.addVotesFor('news');
var addVotesToItem = votesController.addVotesToItem;
var Comment = require('../models/Comment');
var request = require('request');
var async = require('async');
var marked = require('marked');

marked.setOptions({
  sanitize: true
});

exports.index = function(req, res, next) {

  var page = typeof req.params.page !== 'undefined' ? req.params.page : 1;
  page = isNaN(page) ? 1 : Number(page);
  page = page < 1 ? 1 : page;

  getNewsItems({}, req.user, function (err, newsItems) {
    if(err) return next(err);

    res.render('news/index', {
      title: 'Recent News',
      items: sortByScore(newsItems),
      page: page,
      newsItemsPerPage: newsItemsPerPage
    });
  }, page);
};

/**
 * GET /news/:id
 * View comments on a news item
 */
exports.comments = function (req, res, next) {

  NewsItem
  .findById(req.params.id)
  .populate('poster')
  .exec(function (err, newsItem) {

    if(err) return next(err);
    if (!newsItem) {
      req.flash('errors', { msg: 'News item not found.' });
      return res.redirect('/news');
    }

    async.parallel({
      votes: function (cb) {
        addVotesToNewsItems(newsItem, req.user, cb);
      },
      comments: function (cb) {
        Comment
        .find({
          item: newsItem._id,
          itemType: 'news'
        })
        .populate('poster')
        .exec(cb);
      }
    }, function (err, results) {

      if(err) return next(err);

      _.each(results.comments, function (comment,i,l) {
        comment.contents = marked(comment.contents);
      });

      newsItem.summary = marked(newsItem.summary);

      res.render('news/show', {
        title: newsItem.title,
        item: newsItem,
        comments: results.comments
      });
    });
{'fraction_non_alphanumeric': 0.15957708384558641, 'fraction_numerical': 0.001967051880993361, 'mean_word_length': 2.922854387656702, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4076416', 'n_tokens_mistral': 1293, 'n_tokens_neox': 1262, 'n_words': 296}