2017-08-01 09:39:57 -04:00
|
|
|
Settings = require "settings-sharelatex"
|
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
crypto = require "crypto"
|
2017-08-16 05:49:29 -04:00
|
|
|
ProjectEntityHandler = require "../Project/ProjectEntityHandler"
|
2017-08-01 09:39:57 -04:00
|
|
|
|
2017-08-03 07:15:27 -04:00
|
|
|
# The "state" of a project is a hash of the relevant attributes in the
|
2017-08-03 05:51:21 -04:00
|
|
|
# project object in this case we only need the rootFolder.
|
|
|
|
#
|
2017-08-03 07:15:27 -04:00
|
|
|
# The idea is that it will change if any doc or file is
|
|
|
|
# created/renamed/deleted, and also if the content of any file (not
|
|
|
|
# doc) changes.
|
|
|
|
#
|
|
|
|
# When the hash changes the full set of files on the CLSI will need to
|
|
|
|
# be updated. If it doesn't change then we can overwrite changed docs
|
|
|
|
# in place on the clsi, getting them from the docupdater.
|
2017-08-03 05:51:21 -04:00
|
|
|
#
|
2017-08-07 09:45:04 -04:00
|
|
|
# The docupdater is responsible for setting the key in redis, and
|
|
|
|
# unsetting it if it removes any documents from the doc updater.
|
2017-08-03 05:51:21 -04:00
|
|
|
|
2017-08-16 05:49:29 -04:00
|
|
|
buildState = (s) ->
|
|
|
|
return crypto.createHash('sha1').update(s, 'utf8').digest('hex')
|
2017-08-01 09:39:57 -04:00
|
|
|
|
|
|
|
module.exports = ClsiStateManager =
|
|
|
|
|
2017-09-01 11:36:51 -04:00
|
|
|
computeHash: (project, options, callback = (err, hash) ->) ->
|
2017-08-16 05:49:29 -04:00
|
|
|
ProjectEntityHandler.getAllEntitiesFromProject project, (err, docs, files) ->
|
|
|
|
fileList = ("#{f.file._id}:#{f.file.rev}:#{f.file.created}:#{f.path}" for f in files or [])
|
|
|
|
docList = ("#{d.doc._id}:#{d.path}" for d in docs or [])
|
|
|
|
sortedEntityList = [docList..., fileList...].sort()
|
2017-09-01 11:36:51 -04:00
|
|
|
# ignore the isAutoCompile options as it doesn't affect the
|
|
|
|
# output, but include all other options e.g. draft
|
|
|
|
optionsList = ("option #{key}:#{value}" for key, value of options or {} when not (key in ['isAutoCompile']))
|
|
|
|
sortedOptionsList = optionsList.sort()
|
|
|
|
hash = buildState([sortedEntityList..., sortedOptionsList...].join("\n"))
|
2017-08-16 05:49:29 -04:00
|
|
|
callback(null, hash)
|