ReferencesSearchHandler.coffee 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. logger = require("logger-sharelatex")
  2. request = require("request")
  3. settings = require("settings-sharelatex")
  4. Project = require("../../models/Project").Project
  5. DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
  6. U = require('underscore')
  7. Async = require('async')
  8. oneMinInMs = 60 * 1000
  9. fiveMinsInMs = oneMinInMs * 5
  10. module.exports = ReferencesSearchHandler =
  11. _buildDocUrl: (projectId, docId) ->
  12. "#{settings.apis.docstore.url}/project/#{projectId}/doc/#{docId}/raw"
  13. _findBibDocIds: (project) ->
  14. ids = []
  15. _process = (folder) ->
  16. folder.docs.forEach (doc) ->
  17. if doc?.name?.match(/^.*\.bib$/)
  18. ids.push(doc._id)
  19. folder.folders.forEach (folder) ->
  20. _process(folder)
  21. project.rootFolder.forEach (rootFolder) ->
  22. _process(rootFolder)
  23. return ids
  24. _isFullIndex: (project, callback = (err, result) ->) ->
  25. owner = project.owner_ref
  26. callback(null, owner.features.references == true)
  27. # projectId: String, docIds: List[String]|Null
  28. index: (projectId, docIds, callback=(err, data)->) ->
  29. Project.findPopulatedById projectId, (err, project) ->
  30. if err
  31. logger.err {err, projectId}, "error finding project"
  32. return callback(err)
  33. if docIds == "ALL"
  34. logger.log {projectId}, "indexing all bib files in project"
  35. docIds = ReferencesSearchHandler._findBibDocIds(project)
  36. ReferencesSearchHandler._isFullIndex project, (err, isFullIndex) ->
  37. if err
  38. logger.err {err, projectId}, "error checking whether to do full index"
  39. return callback(err)
  40. # TODO: flush documents to mongo
  41. logger.log {projectId, docIds}, 'flushing docs to mongo before calling references service'
  42. Async.series(
  43. docIds.map((docId) -> (cb) -> DocumentUpdaterHandler.flushDocToMongo(projectId, docId, cb)),
  44. (err) ->
  45. # continue
  46. if err
  47. logger.err {err, projectId, docIds}, "error flushing docs to mongo"
  48. return callback(err)
  49. bibDocUrls = docIds.map (docId) ->
  50. ReferencesSearchHandler._buildDocUrl projectId, docId
  51. logger.log {projectId, isFullIndex, docIds, bibDocUrls}, "sending request to references service"
  52. request.post {
  53. url: "#{settings.apis.references.url}/project/#{projectId}/index"
  54. json:
  55. docUrls: bibDocUrls
  56. fullIndex: isFullIndex
  57. }, (err, res, data) ->
  58. if err
  59. logger.err {err, projectId}, "error communicating with references api"
  60. return callback(err)
  61. if 200 <= res.statusCode < 300
  62. logger.log {projectId}, "got keys from references api"
  63. return callback(null, data)
  64. else
  65. err = new Error("references api responded with non-success code: #{res.statusCode}")
  66. logger.log {err, projectId}, "error updating references"
  67. return callback(err)
  68. )