ClsiStateManager.coffee 1.6 KB

1234567891011121314151617181920212223242526272829303132333435
  1. Settings = require "settings-sharelatex"
  2. logger = require "logger-sharelatex"
  3. crypto = require "crypto"
  4. ProjectEntityHandler = require "../Project/ProjectEntityHandler"
  5. # The "state" of a project is a hash of the relevant attributes in the
  6. # project object in this case we only need the rootFolder.
  7. #
  8. # The idea is that it will change if any doc or file is
  9. # created/renamed/deleted, and also if the content of any file (not
  10. # doc) changes.
  11. #
  12. # When the hash changes the full set of files on the CLSI will need to
  13. # be updated. If it doesn't change then we can overwrite changed docs
  14. # in place on the clsi, getting them from the docupdater.
  15. #
  16. # The docupdater is responsible for setting the key in redis, and
  17. # unsetting it if it removes any documents from the doc updater.
  18. buildState = (s) ->
  19. return crypto.createHash('sha1').update(s, 'utf8').digest('hex')
  20. module.exports = ClsiStateManager =
  21. computeHash: (project, options, callback = (err, hash) ->) ->
  22. ProjectEntityHandler.getAllEntitiesFromProject project, (err, docs, files) ->
  23. fileList = ("#{f.file._id}:#{f.file.rev}:#{f.file.created}:#{f.path}" for f in files or [])
  24. docList = ("#{d.doc._id}:#{d.path}" for d in docs or [])
  25. sortedEntityList = [docList..., fileList...].sort()
  26. # ignore the isAutoCompile options as it doesn't affect the
  27. # output, but include all other options e.g. draft
  28. optionsList = ("option #{key}:#{value}" for key, value of options or {} when not (key in ['isAutoCompile']))
  29. sortedOptionsList = optionsList.sort()
  30. hash = buildState([sortedEntityList..., sortedOptionsList...].join("\n"))
  31. callback(null, hash)