<commit_before>logger = require('logger-sharelatex')
Metrics = require('../../infrastructure/Metrics')
sanitize = require('sanitizer')
ProjectEditorHandler = require('../Project/ProjectEditorHandler')
ProjectEntityHandler = require('../Project/ProjectEntityHandler')
ProjectOptionsHandler = require('../Project/ProjectOptionsHandler')
ProjectDetailsHandler = require('../Project/ProjectDetailsHandler')
ProjectDeleter = require("../Project/ProjectDeleter")
ProjectGetter = require('../Project/ProjectGetter')
CollaboratorsHandler = require("../Collaborators/CollaboratorsHandler")
DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
LimitationsManager = require("../Subscription/LimitationsManager")
AuthorizationManager = require("../Security/AuthorizationManager")
EditorRealTimeController = require("./EditorRealTimeController")
TrackChangesManager = require("../TrackChanges/TrackChangesManager")
settings = require('settings-sharelatex')
slReqIdHelper = require('soa-req-id')
tpdsPollingBackgroundTasks = require('../ThirdPartyDataStore/TpdsPollingBackgroundTasks')
async = require('async')
ConnectedUsersManager = require("../ConnectedUsers/ConnectedUsersManager")
_ = require('underscore')
rclientPub = require("redis").createClient(settings.redis.web.port, settings.redis.web.host)
rclientPub.auth(settings.redis.web.password)
rclientSub = require("redis").createClient(settings.redis.web.port, settings.redis.web.host)
rclientSub.auth(settings.redis.web.password)

module.exports = EditorController =
	protocolVersion: 2

	reportError: (client, clientError, callback = () ->) ->
		client.get "project_id", (error, project_id) ->
			client.get "user_id", (error, user_id) ->
				logger.error err: clientError, project_id: project_id, user_id: user_id, "client error"
				callback()

	joinProject: (client, user, project_id, callback) ->
		logger.log user_id:user._id, project_id:project_id, "user joining project"
		Metrics.inc "editor.join-project"
		ProjectGetter.getProjectWithoutDocLines project_id, (error, project) ->
			return callback(error) if error?
			ProjectGetter.populateProjectWithUsers project, (error, project) ->
				return callback(error) if error?
				AuthorizationManager.getPrivilegeLevelForProject project, user,
					(error, canAccess, privilegeLevel) ->
						if error? or !canAccess
							callback new Error("Not authorized")
						else
							client.join(project_id)
							client.set("project_id", project_id)
							client.set("owner_id", project.owner_ref._id)
							client.set("user_id", user._id)
							client.set("first_name", user.first_name)
							client.set("last_name", user.last_name)
							client.set("email", user.email)
							client.set("connected_time", new Date())
							client.set("signup_date", user.signUpDate)
							client.set("login_count", user.loginCount)
							AuthorizationManager.setPrivilegeLevelOnClient client, privilegeLevel
							callback null, ProjectEditorHandler.buildProjectModelView(project), privilegeLevel, EditorController.protocolVersion

							# can be done affter the connection has happened
							EditorRealTimeController.emitToRoom(project_id, "ConnectedUsers.userConnected", user)
							ConnectedUsersManager.markUserAsConnected project_id, user._id, ->

	leaveProject: (client, user) ->
		self = @
		client.get "project_id", (error, project_id) ->
			return if error? or !project_id?
			EditorRealTimeController.emitToRoom(project_id, "clientTracking.clientDisconnected", client.id)
			EditorRealTimeController.emitToRoom(project_id, "ConnectedUsers.userDissconected", user)
			ConnectedUsersManager.markUserAsDisconnected project_id, user._id, ->
			logger.log user_id:user._id, project_id:project_id, "user leaving project"
			self.flushProjectIfEmpty(project_id)

	joinDoc: (client, project_id, doc_id, fromVersion, callback = (error, docLines, version) ->) ->
		# fromVersion is optional
		if typeof fromVersion == "function"
			callback = fromVersion
			fromVersion = -1

		client.get "user_id", (error, user_id) ->
			logger.log user_id: user_id, project_id: project_id, doc_id: doc_id, "user joining doc"
		Metrics.inc "editor.join-doc"
		client.join doc_id
		DocumentUpdaterHandler.getDocument project_id, doc_id, fromVersion, (err, docLines, version, ops)->
			# Encode any binary bits of data so it can go via WebSockets
			# See http://ecmanaut.blogspot.co.uk/2006/07/encoding-decoding-utf8-in-javascript.html
			if docLines?
				docLines = for line in docLines
					if line.text?
						line.text = unescape(encodeURIComponent(line.text))
					else
						line = unescape(encodeURIComponent(line))
					line
			callback(err, docLines, version, ops)

	leaveDoc: (client, project_id, doc_id, callback = (error) ->) ->
		client.get "user_id", (error, user_id) ->
			logger.log user_id: user_id, project_id: project_id, doc_id: doc_id, "user leaving doc"
		Metrics.inc "editor.leave-doc"
		client.leave doc_id
		callback()

	flushProjectIfEmpty: (project_id, callback = ->)->
		setTimeout (->
			io = require('../../infrastructure/Server').io
			peopleStillInProject = io.sockets.clients(project_id).length
			logger.log project_id: project_id, connectedCount: peopleStillInProject, "flushing if empty"
			if peopleStillInProject == 0
				DocumentUpdaterHandler.flushProjectToMongoAndDelete(project_id)
				TrackChangesManager.flushProject(project_id)
			callback()
			), 500
		
	updateClientPosition: (client, cursorData, callback = (error) ->) ->
		client.get "project_id", (error, project_id) ->
			return callback(error) if error?
			client.get "first_name", (error, first_name) ->
				return callback(error) if error?
				client.get "last_name", (error, last_name) ->
					return callback(error) if error?
					client.get "email", (error, email) ->
						return callback(error) if error?
						client.get "user_id", (error, user_id) ->
							return callback(error) if error?
							cursorData.id = client.id
							cursorData.user_id = user_id if user_id?
							cursorData.email = email if email?
							if first_name? and last_name?
								cursorData.name = first_name + " " + last_name
							else
								cursorData.name = "Anonymous"
							EditorRealTimeController.emitToRoom(project_id, "clientTracking.clientUpdated", cursorData)

	addUserToProject: (project_id, email, privileges, callback = (error, collaborator_added)->)->
		email = email.toLowerCase()
		LimitationsManager.isCollaboratorLimitReached project_id, (error, limit_reached) =>
			if error?
				logger.error err:error, "error adding user to to project when checking if collaborator limit has been reached"
				return callback(new Error("Something went wrong"))

			if limit_reached
				callback null, false<commit_msg>refactored updateClientPosition to use async<commit_after>logger = require('logger-sharelatex')
Metrics = require('../../infrastructure/Metrics')
sanitize = require('sanitizer')
ProjectEditorHandler = require('../Project/ProjectEditorHandler')
ProjectEntityHandler = require('../Project/ProjectEntityHandler')
ProjectOptionsHandler = require('../Project/ProjectOptionsHandler')
ProjectDetailsHandler = require('../Project/ProjectDetailsHandler')
ProjectDeleter = require("../Project/ProjectDeleter")
ProjectGetter = require('../Project/ProjectGetter')
CollaboratorsHandler = require("../Collaborators/CollaboratorsHandler")
DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
LimitationsManager = require("../Subscription/LimitationsManager")
AuthorizationManager = require("../Security/AuthorizationManager")
EditorRealTimeController = require("./EditorRealTimeController")
TrackChangesManager = require("../TrackChanges/TrackChangesManager")
settings = require('settings-sharelatex')
slReqIdHelper = require('soa-req-id')
tpdsPollingBackgroundTasks = require('../ThirdPartyDataStore/TpdsPollingBackgroundTasks')
async = require('async')
ConnectedUsersManager = require("../ConnectedUsers/ConnectedUsersManager")
_ = require('underscore')
rclientPub = require("redis").createClient(settings.redis.web.port, settings.redis.web.host)
rclientPub.auth(settings.redis.web.password)
rclientSub = require("redis").createClient(settings.redis.web.port, settings.redis.web.host)
rclientSub.auth(settings.redis.web.password)

module.exports = EditorController =
	protocolVersion: 2

	reportError: (client, clientError, callback = () ->) ->
		client.get "project_id", (error, project_id) ->
			client.get "user_id", (error, user_id) ->
				logger.error err: clientError, project_id: project_id, user_id: user_id, "client error"
				callback()

	joinProject: (client, user, project_id, callback) ->
		logger.log user_id:user._id, project_id:project_id, "user joining project"
		Metrics.inc "editor.join-project"
		ProjectGetter.getProjectWithoutDocLines project_id, (error, project) ->
			return callback(error) if error?
			ProjectGetter.populateProjectWithUsers project, (error, project) ->
				return callback(error) if error?
				AuthorizationManager.getPrivilegeLevelForProject project, user,
					(error, canAccess, privilegeLevel) ->
						if error? or !canAccess
							callback new Error("Not authorized")
						else
							client.join(project_id)
							client.set("project_id", project_id)
							client.set("owner_id", project.owner_ref._id)
							client.set("user_id", user._id)
							client.set("first_name", user.first_name)
							client.set("last_name", user.last_name)
							client.set("email", user.email)
							client.set("connected_time", new Date())
							client.set("signup_date", user.signUpDate)
							client.set("login_count", user.loginCount)
							AuthorizationManager.setPrivilegeLevelOnClient client, privilegeLevel
							callback null, ProjectEditorHandler.buildProjectModelView(project), privilegeLevel, EditorController.protocolVersion

							# can be done affter the connection has happened
							EditorRealTimeController.emitToRoom(project_id, "ConnectedUsers.userConnected", user)
							ConnectedUsersManager.markUserAsConnected project_id, user._id, ->

	leaveProject: (client, user) ->
		self = @
		client.get "project_id", (error, project_id) ->
			return if error? or !project_id?
			EditorRealTimeController.emitToRoom(project_id, "clientTracking.clientDisconnected", client.id)
			EditorRealTimeController.emitToRoom(project_id, "ConnectedUsers.userDissconected", user)
			ConnectedUsersManager.markUserAsDisconnected project_id, user._id, ->
			logger.log user_id:user._id, project_id:project_id, "user leaving project"
			self.flushProjectIfEmpty(project_id)

	joinDoc: (client, project_id, doc_id, fromVersion, callback = (error, docLines, version) ->) ->
		# fromVersion is optional
		if typeof fromVersion == "function"
			callback = fromVersion
			fromVersion = -1

		client.get "user_id", (error, user_id) ->
			logger.log user_id: user_id, project_id: project_id, doc_id: doc_id, "user joining doc"
		Metrics.inc "editor.join-doc"
		client.join doc_id
		DocumentUpdaterHandler.getDocument project_id, doc_id, fromVersion, (err, docLines, version, ops)->
			# Encode any binary bits of data so it can go via WebSockets
			# See http://ecmanaut.blogspot.co.uk/2006/07/encoding-decoding-utf8-in-javascript.html
			if docLines?
				docLines = for line in docLines
					if line.text?
						line.text = unescape(encodeURIComponent(line.text))
					else
						line = unescape(encodeURIComponent(line))
					line
			callback(err, docLines, version, ops)

	leaveDoc: (client, project_id, doc_id, callback = (error) ->) ->
		client.get "user_id", (error, user_id) ->
			logger.log user_id: user_id, project_id: project_id, doc_id: doc_id, "user leaving doc"
		Metrics.inc "editor.leave-doc"
		client.leave doc_id
		callback()

	flushProjectIfEmpty: (project_id, callback = ->)->
		setTimeout (->
			io = require('../../infrastructure/Server').io
			peopleStillInProject = io.sockets.clients(project_id).length
			logger.log project_id: project_id, connectedCount: peopleStillInProject, "flushing if empty"
			if peopleStillInProject == 0
				DocumentUpdaterHandler.flushProjectToMongoAndDelete(project_id)
				TrackChangesManager.flushProject(project_id)
			callback()
			), 500
		
	updateClientPosition: (client, cursorData, callback = (error) ->) ->
		async.parallel {
			project_id: (cb)-> client.get "project_id", cb
			first_name: (cb)-> client.get "first_name", cb
			last_name: (cb)-> client.get "last_name", cb
			email: (cb)-> client.get "email", cb
			user_id: (cb)-> client.get "user_id", cb
		}, (err, results)->
			{first_name, last_name, user_id, email, project_id} = results
			cursorData.id = client.id
			cursorData.user_id = user_id if user_id?
			cursorData.email = email if email?
			if first_name? and last_name?
				cursorData.name = first_name + " " + last_name
			else
				cursorData.name = "Anonymous"
			EditorRealTimeController.emitToRoom(project_id, "clientTracking.clientUpdated", cursorData)

	addUserToProject: (project_id, email, privileges, callback = (error, collaborator_added)->)->
		email = email.toLowerCase()
		LimitationsManager.isCollaboratorLimitReached project_id, (error, limit_reached) =>
			if error?
				logger.error err:error, "error adding user to to project when checking if collaborator limit has been reached"
				return callback(new Error("Something went wrong"))

			if limit_reached
				callback null, false
{'fraction_non_alphanumeric': 0.11766908845779342, 'fraction_numerical': 0.0019474196689386564, 'mean_word_length': 4.83056768558952, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 68, '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': '7320269', 'n_tokens_mistral': 4555, 'n_tokens_neox': 3955, 'n_words': 849}