<commit_before>request = require("request")
settings = require("settings-sharelatex")
logger = require("logger-sharelatex")
_ = require("underscore")
ErrorController = require "../Errors/ErrorController"
StaticPageHelpers = require("./StaticPageHelpers")
sanitize = require('sanitizer')
Settings = require("settings-sharelatex")
contentful = require('contentful')
marked = require("marked")
sixpack = require("../../infrastructure/Sixpack")
AuthenticationController = require '../Authentication/AuthenticationController'



module.exports = UniversityController =

	getPage: (req, res, next)->
		url = req.url?.toLowerCase()
		universityUrl = "#{settings.apis.university.url}#{url}"
		if StaticPageHelpers.shouldProxy(url)
			return UniversityController._directProxy universityUrl, res

		logger.log url:url, "proxying request to university api"
		request.get universityUrl, (err, r, data)->
			if r?.statusCode == 404
				return UniversityController.getContentfulPage(req, res, next)
			if err?
				return res.send 500
			data = data.trim()
			try
				data = JSON.parse(data)
				data.content = data.content.replace(/__ref__/g, sanitize.escape(req.query.ref))
			catch err
				logger.err err:err, data:data, "error parsing data from data"
			res.render "university/university_holder", data


	getIndexPage: (req, res)->
		user = AuthenticationController.getSessionUser(req)
		client = sixpack.client(user?._id?.toString() || req.ip)
		client.participate 'instapage-pages', ['default', 'instapage'], (err, response)->
			if response?.alternative?.name == "instapage"
				return res.redirect("/i/university")
			else
				req.url = "/university/index.html"
				UniversityController.getPage req, res

	_directProxy: (originUrl, res)-><commit_msg>get user_id directly<commit_after>request = require("request")
settings = require("settings-sharelatex")
logger = require("logger-sharelatex")
_ = require("underscore")
ErrorController = require "../Errors/ErrorController"
StaticPageHelpers = require("./StaticPageHelpers")
sanitize = require('sanitizer')
Settings = require("settings-sharelatex")
contentful = require('contentful')
marked = require("marked")
sixpack = require("../../infrastructure/Sixpack")
AuthenticationController = require '../Authentication/AuthenticationController'



module.exports = UniversityController =

	getPage: (req, res, next)->
		url = req.url?.toLowerCase()
		universityUrl = "#{settings.apis.university.url}#{url}"
		if StaticPageHelpers.shouldProxy(url)
			return UniversityController._directProxy universityUrl, res

		logger.log url:url, "proxying request to university api"
		request.get universityUrl, (err, r, data)->
			if r?.statusCode == 404
				return UniversityController.getContentfulPage(req, res, next)
			if err?
				return res.send 500
			data = data.trim()
			try
				data = JSON.parse(data)
				data.content = data.content.replace(/__ref__/g, sanitize.escape(req.query.ref))
			catch err
				logger.err err:err, data:data, "error parsing data from data"
			res.render "university/university_holder", data


	getIndexPage: (req, res)->
		user_id = AuthenticationController.getLoggedInUserId(req)
		client = sixpack.client(user_id.toString() || req.ip)
		client.participate 'instapage-pages', ['default', 'instapage'], (err, response)->
			if response?.alternative?.name == "instapage"
				return res.redirect("/i/university")
			else
				req.url = "/university/index.html"
				UniversityController.getPage req, res

	_directProxy: (originUrl, res)->
{'fraction_non_alphanumeric': 0.1370137299771167, 'fraction_numerical': 0.003432494279176201, 'mean_word_length': 4.947278911564626, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '1567045', 'n_tokens_mistral': 1152, 'n_tokens_neox': 1024, 'n_words': 229}