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-08-02 10:28:53 -04:00
|
|
|
_ = 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-05-19 09:58:12 -04:00
|
|
|
_buildFileUrl: (projectId, fileId) ->
|
|
|
|
"#{settings.apis.filestore.url}/project/#{projectId}/file/#{fileId}"
|
|
|
|
|
|
|
|
_findBibFileIds: (project) ->
|
2016-01-15 09:41:05 -05:00
|
|
|
ids = []
|
2016-05-19 09:58:12 -04:00
|
|
|
_process = (folder) ->
|
2016-08-02 10:28:53 -04:00
|
|
|
_.each (folder.fileRefs or []), (file) ->
|
2016-05-19 09:58:12 -04:00
|
|
|
if file?.name?.match(/^.*\.bib$/)
|
|
|
|
ids.push(file._id)
|
2016-08-02 10:28:53 -04:00
|
|
|
_.each (folder.folders or []), (folder) ->
|
2016-05-19 09:58:12 -04:00
|
|
|
_process(folder)
|
2016-08-02 10:28:53 -04:00
|
|
|
_.each (project.rootFolder or []), (rootFolder) ->
|
2016-05-19 09:58:12 -04:00
|
|
|
_process(rootFolder)
|
|
|
|
return ids
|
2016-01-15 09:41:05 -05:00
|
|
|
|
2016-05-19 09:58:12 -04:00
|
|
|
_findBibDocIds: (project) ->
|
|
|
|
ids = []
|
2016-01-15 09:41:05 -05:00
|
|
|
_process = (folder) ->
|
2016-08-02 10:28:53 -04:00
|
|
|
_.each (folder.docs or []), (doc) ->
|
2016-01-15 09:41:05 -05:00
|
|
|
if doc?.name?.match(/^.*\.bib$/)
|
|
|
|
ids.push(doc._id)
|
2016-08-02 10:28:53 -04:00
|
|
|
_.each (folder.folders or []), (folder) ->
|
2016-01-15 09:41:05 -05:00
|
|
|
_process(folder)
|
2016-08-02 10:28:53 -04:00
|
|
|
_.each (project.rootFolder or []), (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)
|
2016-05-19 09:58:12 -04:00
|
|
|
fileIds = ReferencesHandler._findBibFileIds(project)
|
|
|
|
ReferencesHandler._doIndexOperation(projectId, project, docIds, fileIds, 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-05-19 09:58:12 -04:00
|
|
|
ReferencesHandler._doIndexOperation(projectId, project, docIds, [], callback)
|
2016-01-27 11:00:53 -05:00
|
|
|
|
2016-05-19 09:58:12 -04:00
|
|
|
_doIndexOperation: (projectId, project, docIds, fileIds, callback) ->
|
2016-06-24 10:04:10 -04:00
|
|
|
if !settings.apis?.references?.url?
|
|
|
|
return 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-05-19 09:58:12 -04:00
|
|
|
bibFileUrls = fileIds.map (fileId) ->
|
|
|
|
ReferencesHandler._buildFileUrl projectId, fileId
|
|
|
|
allUrls = bibDocUrls.concat(bibFileUrls)
|
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:
|
2016-05-19 09:58:12 -04:00
|
|
|
docUrls: allUrls
|
2016-01-27 11:00:53 -05:00
|
|
|
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
|
|
|
)
|