<commit_before>'use strict';
var SZ           = require('sequelize');
var express      = require('express');
var bcrypt       = require('bcrypt-nodejs');
var _            = require('lodash');
var fs           = require('fs');
var path         = require('path');
var moment       = require('moment');
var RSSFeed      = require('feed');
var striptags    = require('striptags');
var xmlescape    = require('xml-escape');
var xmlbuilder   = require('xmlbuilder');
var sanitizeHtml = require('sanitize-html');
var md5          = require('md5');
var cheerio      = require('cheerio');

//Configurations
var configs  = require('../configs/global.configs');
var Article  = require('../models/Article');
var User     = require('../models/User');
var Category = require('../models/Category');
var Banner   = require('../models/Banner');

Article.belongsTo(User, {foreignKey:'user_id', as:'User'});
Article.belongsTo(Category, {foreignKey:'category_id', as:'Category'});

//init router
var router  = express.Router();
var Promise = SZ.Promise;
var now     = moment().format('YYYY-MM-DD HH:mm:ss');

//GLOBAL conditions
var banner_conditions = { 
	valid: 1, 
	online: 1, 

	start_time: { 
		$or: {
			$eq: null,
			$lte: now
		}
	},

	end_time: {
		$or: {
			$eq: null,
			$gte: now
		}
	}
};
var category_conditions = { valid: 1, level: 1, for_admin: 0 };
var article_conditions  = { valid: 1, approved: 1};
var user_conditions     = { valid: 1 };

var display_time = function(time){
	return moment(time).format('DD MMMM YYYY');
};

var article_read_hanlder = function(req, res, next) {

	var id                  = parseInt(req.params.id);
	var article_conditions  = { id: id, valid: 1, approved: req.query.approved || 1 };
	var category_conditions = { valid: 1 };
	var user_conditions     = { valid: 1 };
	var cache_file_name     = path.join(__dirname, '../public/articles/read', id + '.html');

	// console.info('cache_file_name:', cache_file_name);

	//find the html cache file, if there is none, then create one
	if(fs.existsSync(cache_file_name)){

		res.sendFile(cache_file_name);

	} else {

		Promise.join(

			Article.findOne({ 
				where: article_conditions,
				include: [
					{ model:Category, as:'Category', required: true, where: category_conditions, attributes:['title', 'id'] },
					{ model:User, as:'User', required: true, where: user_conditions, attributes:['username', 'id', 'name'] }
				]
			}),

			Article.findAll({
				where: {valid: 1, approved: 1, '$not': {id: id}},
				limit: configs.latest_artcile_no,
				order: 'id desc',
				include: [
					{ model:Category, as:'Category', required: true, where: category_conditions, attributes:['title', 'id'] }
				]
			}),

			Banner.findAll({
				where: banner_conditions,
				limit: 100,
				order: 'id desc'
			})

		).spread(function(article, latest_articles, banners){

			if(!article || article === null){
				var error = new Error('Invalid article');
				console.error('Invalid article caught:', id);
				next(error);
				return;
			}

			var category_id = article.Category.get('id');
			var title = article.get('title');

			//find the artciles under the same category
			Article.findAll({ 
				where: {
					valid: 1, 
					approved: 1, 
					category_id: category_id
				},
				limit: configs.same_cate_article_no,
				order: 'RAND()',
				include: [
					{ model:Category, as:'Category', required: true, where: category_conditions, attributes:['title', 'id'] }
				]
			}).then(function(same_cate_artciles){

				if(!article.start_time || article.start_time === '0000-00-00 00:00:00'){
					article.start_time = article.created;
				}

				res.locals.banners            = banners;
				res.locals.same_cate_artciles = same_cate_artciles;
				res.locals.latest_articles    = latest_articles;
				res.locals.article            = article;
				res.locals.title              = title || configs.site_title;
				res.locals.configs            = configs;
				res.locals.meta               = {
					title:       article.title || configs.site_title,
					url:         configs.site_url + '/articles/read/' + article.id,
					image:       article.thumbnail,
					site_name:   configs.site_title,
					description: article.abstraction,
					fb_id:       configs.fb_id
				};
				res.locals.display_time = display_time;
				res.locals._ = _;

				res.render('article_read', function(error, html){
					if(error){	
						next(error);
					}

					fs.writeFile(cache_file_name, html);
					res.send(html);
				});
			});
			
		}).catch(function(error){
			next(error);
		}); //eo Promise

	}
};

var category_read_handler = function(req, res, next){
	var id                  = parseInt(req.params.id);
	var page                = parseInt(req.params.page);
	var article_conditions  = { id: id, valid: 1, approved: 1 };
	var category_conditions = { valid: 1, id: id };
	var user_conditions     = { valid: 1 };

	if(isNaN(page) || page <= 0){
		page = 0;
	} 

	var offset = page * configs.category_artcile_no;
	<commit_msg>Added filter for Category<commit_after>'use strict';
var SZ           = require('sequelize');
var express      = require('express');
var bcrypt       = require('bcrypt-nodejs');
var _            = require('lodash');
var fs           = require('fs');
var path         = require('path');
var moment       = require('moment');
var RSSFeed      = require('feed');
var striptags    = require('striptags');
var xmlescape    = require('xml-escape');
var xmlbuilder   = require('xmlbuilder');
var sanitizeHtml = require('sanitize-html');
var md5          = require('md5');
var cheerio      = require('cheerio');

//Configurations
var configs  = require('../configs/global.configs');
var Article  = require('../models/Article');
var User     = require('../models/User');
var Category = require('../models/Category');
var Banner   = require('../models/Banner');

Article.belongsTo(User, {foreignKey:'user_id', as:'User'});
Article.belongsTo(Category, {foreignKey:'category_id', as:'Category'});

//init router
var router  = express.Router();
var Promise = SZ.Promise;
var now     = moment().format('YYYY-MM-DD HH:mm:ss');

//GLOBAL conditions
var banner_conditions = { 
	valid: 1, 
	online: 1, 

	start_time: { 
		$or: {
			$eq: null,
			$lte: now
		}
	},

	end_time: {
		$or: {
			$eq: null,
			$gte: now
		}
	}
};
var category_conditions = { valid: 1, level: 1, for_admin: 0 };
var article_conditions  = { valid: 1, approved: 1};
var user_conditions     = { valid: 1 };

var display_time = function(time){
	return moment(time).format('DD MMMM YYYY');
};

var article_read_hanlder = function(req, res, next) {

	var id                  = parseInt(req.params.id);
	var article_conditions  = { id: id, valid: 1, approved: req.query.approved || 1 };
	var category_conditions = { valid: 1 };
	var user_conditions     = { valid: 1 };
	var cache_file_name     = path.join(__dirname, '../public/articles/read', id + '.html');

	// console.info('cache_file_name:', cache_file_name);

	//find the html cache file, if there is none, then create one
	if(fs.existsSync(cache_file_name)){

		res.sendFile(cache_file_name);

	} else {

		Promise.join(

			Article.findOne({ 
				where: article_conditions,
				include: [
					{ model:Category, as:'Category', required: true, where: category_conditions, attributes:['title', 'id'] },
					{ model:User, as:'User', required: true, where: user_conditions, attributes:['username', 'id', 'name'] }
				]
			}),

			Article.findAll({
				where: {valid: 1, approved: 1, '$not': {id: id}},
				limit: configs.latest_artcile_no,
				order: 'id desc',
				include: [
					{ model:Category, as:'Category', required: true, where: category_conditions, attributes:['title', 'id'] }
				]
			}),

			Banner.findAll({
				where: banner_conditions,
				limit: 100,
				order: 'id desc'
			})

		).spread(function(article, latest_articles, banners){

			if(!article || article === null){
				var error = new Error('Invalid article');
				console.error('Invalid article caught:', id);
				next(error);
				return;
			}

			var category_id = article.Category.get('id');
			var title = article.get('title');

			//find the artciles under the same category
			Article.findAll({ 
				where: {
					valid: 1, 
					approved: 1, 
					category_id: category_id
				},
				limit: configs.same_cate_article_no,
				order: 'RAND()',
				include: [
					{ model:Category, as:'Category', required: true, where: category_conditions, attributes:['title', 'id'] }
				]
			}).then(function(same_cate_artciles){

				if(!article.start_time || article.start_time === '0000-00-00 00:00:00'){
					article.start_time = article.created;
				}

				res.locals.banners            = banners;
				res.locals.same_cate_artciles = same_cate_artciles;
				res.locals.latest_articles    = latest_articles;
				res.locals.article            = article;
				res.locals.title              = title || configs.site_title;
				res.locals.configs            = configs;
				res.locals.meta               = {
					title:       article.title || configs.site_title,
					url:         configs.site_url + '/articles/read/' + article.id,
					image:       article.thumbnail,
					site_name:   configs.site_title,
					description: article.abstraction,
					fb_id:       configs.fb_id
				};
				res.locals.display_time = display_time;
				res.locals._ = _;

				res.render('article_read', function(error, html){
					if(error){	
						next(error);
					}

					fs.writeFile(cache_file_name, html);
					res.send(html);
				});
			});
			
		}).catch(function(error){
			next(error);
		}); //eo Promise

	}
};

var category_read_handler = function(req, res, next){
	var id                  = parseInt(req.params.id);
	var page                = parseInt(req.params.page);
	var article_conditions  = { id: id, valid: 1, approved: 1 };
	var category_conditions = { valid: 1, id: id, display: 1 };
	var user_conditions     = { valid: 1 };

	if(isNaN(page) || page <= 0){
		page = 0;
	} 

	var offset = page * configs.category_artcile_no;
	
{'fraction_non_alphanumeric': 0.15363016446048938, 'fraction_numerical': 0.008323305254713196, 'mean_word_length': 2.982827476038339, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '1429624', 'n_tokens_mistral': 3879, 'n_tokens_neox': 3250, 'n_words': 731}