2015-12-17 06:51:14 -05:00
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
request = require("request")
|
|
|
|
settings = require("settings-sharelatex")
|
2016-03-18 07:20:33 -04:00
|
|
|
ProjectGetter = require "../Project/ProjectGetter"
|
|
|
|
UserGetter = require "../User/UserGetter"
|
2016-01-27 08:33:42 -05:00
|
|
|
DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
|
2016-01-14 09:53:08 -05:00
|
|
|
U = require('underscore')
|
2016-01-15 09:41:05 -05:00
|
|
|
Async = require('async')
|
2015-12-17 06:51:14 -05:00
|
|
|
|
|
|
|
oneMinInMs = 60 * 1000
|
|
|
|
fiveMinsInMs = oneMinInMs * 5
|
|
|
|
|
|
|
|
|
2016-02-08 12:04:27 -05:00
|
|
|
module.exports = ReferencesHandler =
|
2015-12-17 06:51:14 -05:00
|
|
|
|
2016-01-26 11:14:17 -05:00
|
|
|
_buildDocUrl: (projectId, docId) ->
|
2016-01-27 04:58:54 -05:00
|
|
|
"#{settings.apis.docstore.url}/project/#{projectId}/doc/#{docId}/raw"
|
2016-01-26 11:14:17 -05:00
|
|
|
|
2016-01-15 09:41:05 -05:00
|
|
|
_findBibDocIds: (project) ->
|
|
|
|
ids = []
|
|
|
|
|
|
|
|
_process = (folder) ->
|
2016-03-23 06:54:36 -04:00
|
|
|
(folder.docs or []).forEach (doc) ->
|
2016-01-15 09:41:05 -05:00
|
|
|
if doc?.name?.match(/^.*\.bib$/)
|
|
|
|
ids.push(doc._id)
|
2016-03-23 06:54:36 -04:00
|
|
|
(folder.folders or []).forEach (folder) ->
|
2016-01-15 09:41:05 -05:00
|
|
|
_process(folder)
|
|
|
|
|
2016-03-23 06:54:36 -04:00
|
|
|
(project.rootFolder or []).forEach (rootFolder) ->
|
2016-01-15 09:41:05 -05:00
|
|
|
_process(rootFolder)
|
|
|
|
|
|
|
|
return ids
|
|
|
|
|
2016-01-21 12:01:24 -05:00
|
|
|
_isFullIndex: (project, callback = (err, result) ->) ->
|
2016-03-18 07:20:33 -04:00
|
|
|
UserGetter.getUser project.owner_ref, { features: true }, (err, owner) ->
|
2016-03-08 09:05:56 -05:00
|
|
|
return callback(err) if err?
|
|
|
|
callback(null, owner?.features?.references == true)
|
2016-01-21 12:01:24 -05:00
|
|
|
|
2016-01-27 11:00:53 -05:00
|
|
|
indexAll: (projectId, callback=(err, data)->) ->
|
2016-03-18 07:20:33 -04:00
|
|
|
ProjectGetter.getProject projectId, {rootFolder: true, owner_ref: 1}, (err, project) ->
|
2016-01-27 11:00:53 -05:00
|
|
|
if err
|
|
|
|
logger.err {err, projectId}, "error finding project"
|
|
|
|
return callback(err)
|
|
|
|
logger.log {projectId}, "indexing all bib files in project"
|
2016-02-08 12:04:27 -05:00
|
|
|
docIds = ReferencesHandler._findBibDocIds(project)
|
|
|
|
ReferencesHandler._doIndexOperation(projectId, project, docIds, callback)
|
2016-01-27 11:00:53 -05:00
|
|
|
|
2016-01-22 09:23:59 -05:00
|
|
|
index: (projectId, docIds, callback=(err, data)->) ->
|
2016-03-18 07:20:33 -04:00
|
|
|
ProjectGetter.getProject projectId, {rootFolder: true, owner_ref: 1}, (err, project) ->
|
2016-01-21 12:01:24 -05:00
|
|
|
if err
|
2016-01-22 09:23:59 -05:00
|
|
|
logger.err {err, projectId}, "error finding project"
|
2016-01-21 12:01:24 -05:00
|
|
|
return callback(err)
|
2016-02-08 12:04:27 -05:00
|
|
|
ReferencesHandler._doIndexOperation(projectId, project, docIds, callback)
|
2016-01-27 11:00:53 -05:00
|
|
|
|
|
|
|
_doIndexOperation: (projectId, project, docIds, callback) ->
|
2016-02-08 12:04:27 -05:00
|
|
|
ReferencesHandler._isFullIndex project, (err, isFullIndex) ->
|
2016-01-27 11:00:53 -05:00
|
|
|
if err
|
|
|
|
logger.err {err, projectId}, "error checking whether to do full index"
|
|
|
|
return callback(err)
|
|
|
|
logger.log {projectId, docIds}, 'flushing docs to mongo before calling references service'
|
|
|
|
Async.series(
|
|
|
|
docIds.map((docId) -> (cb) -> DocumentUpdaterHandler.flushDocToMongo(projectId, docId, cb)),
|
|
|
|
(err) ->
|
|
|
|
# continue
|
|
|
|
if err
|
|
|
|
logger.err {err, projectId, docIds}, "error flushing docs to mongo"
|
|
|
|
return callback(err)
|
|
|
|
bibDocUrls = docIds.map (docId) ->
|
2016-02-08 12:04:27 -05:00
|
|
|
ReferencesHandler._buildDocUrl projectId, docId
|
2016-01-27 11:00:53 -05:00
|
|
|
logger.log {projectId, isFullIndex, docIds, bibDocUrls}, "sending request to references service"
|
|
|
|
request.post {
|
|
|
|
url: "#{settings.apis.references.url}/project/#{projectId}/index"
|
|
|
|
json:
|
|
|
|
docUrls: bibDocUrls
|
|
|
|
fullIndex: isFullIndex
|
|
|
|
}, (err, res, data) ->
|
2016-01-27 08:33:42 -05:00
|
|
|
if err
|
2016-01-27 11:00:53 -05:00
|
|
|
logger.err {err, projectId}, "error communicating with references api"
|
|
|
|
return callback(err)
|
|
|
|
if 200 <= res.statusCode < 300
|
|
|
|
logger.log {projectId}, "got keys from references api"
|
|
|
|
return callback(null, data)
|
|
|
|
else
|
|
|
|
err = new Error("references api responded with non-success code: #{res.statusCode}")
|
|
|
|
logger.log {err, projectId}, "error updating references"
|
2016-01-27 08:33:42 -05:00
|
|
|
return callback(err)
|
2016-01-27 11:00:53 -05:00
|
|
|
)
|