2015-12-17 11:51:14 +00:00
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
request = require("request")
|
|
|
|
settings = require("settings-sharelatex")
|
2016-03-18 11:20:33 +00:00
|
|
|
ProjectGetter = require "../Project/ProjectGetter"
|
|
|
|
UserGetter = require "../User/UserGetter"
|
2016-01-27 13:33:42 +00:00
|
|
|
DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
|
2016-08-02 14:28:53 +00:00
|
|
|
_ = require('underscore')
|
2016-01-15 14:41:05 +00:00
|
|
|
Async = require('async')
|
2015-12-17 11:51:14 +00:00
|
|
|
|
|
|
|
oneMinInMs = 60 * 1000
|
|
|
|
fiveMinsInMs = oneMinInMs * 5
|
|
|
|
|
2018-05-30 14:28:59 +00:00
|
|
|
if !settings.apis?.references?.url?
|
|
|
|
logger.log "references search not enabled"
|
2015-12-17 11:51:14 +00:00
|
|
|
|
2016-02-08 17:04:27 +00:00
|
|
|
module.exports = ReferencesHandler =
|
2015-12-17 11:51:14 +00:00
|
|
|
|
2016-01-26 16:14:17 +00:00
|
|
|
_buildDocUrl: (projectId, docId) ->
|
2016-01-27 09:58:54 +00:00
|
|
|
"#{settings.apis.docstore.url}/project/#{projectId}/doc/#{docId}/raw"
|
2016-01-26 16:14:17 +00:00
|
|
|
|
2016-05-19 13:58:12 +00:00
|
|
|
_buildFileUrl: (projectId, fileId) ->
|
|
|
|
"#{settings.apis.filestore.url}/project/#{projectId}/file/#{fileId}"
|
|
|
|
|
|
|
|
_findBibFileIds: (project) ->
|
2016-01-15 14:41:05 +00:00
|
|
|
ids = []
|
2016-05-19 13:58:12 +00:00
|
|
|
_process = (folder) ->
|
2016-08-02 14:28:53 +00:00
|
|
|
_.each (folder.fileRefs or []), (file) ->
|
2016-05-19 13:58:12 +00:00
|
|
|
if file?.name?.match(/^.*\.bib$/)
|
|
|
|
ids.push(file._id)
|
2016-08-02 14:28:53 +00:00
|
|
|
_.each (folder.folders or []), (folder) ->
|
2016-05-19 13:58:12 +00:00
|
|
|
_process(folder)
|
2016-08-02 14:28:53 +00:00
|
|
|
_.each (project.rootFolder or []), (rootFolder) ->
|
2016-05-19 13:58:12 +00:00
|
|
|
_process(rootFolder)
|
|
|
|
return ids
|
2016-01-15 14:41:05 +00:00
|
|
|
|
2016-05-19 13:58:12 +00:00
|
|
|
_findBibDocIds: (project) ->
|
|
|
|
ids = []
|
2016-01-15 14:41:05 +00:00
|
|
|
_process = (folder) ->
|
2016-08-02 14:28:53 +00:00
|
|
|
_.each (folder.docs or []), (doc) ->
|
2016-01-15 14:41:05 +00:00
|
|
|
if doc?.name?.match(/^.*\.bib$/)
|
|
|
|
ids.push(doc._id)
|
2016-08-02 14:28:53 +00:00
|
|
|
_.each (folder.folders or []), (folder) ->
|
2016-01-15 14:41:05 +00:00
|
|
|
_process(folder)
|
2016-08-02 14:28:53 +00:00
|
|
|
_.each (project.rootFolder or []), (rootFolder) ->
|
2016-01-15 14:41:05 +00:00
|
|
|
_process(rootFolder)
|
|
|
|
return ids
|
|
|
|
|
2016-01-21 17:01:24 +00:00
|
|
|
_isFullIndex: (project, callback = (err, result) ->) ->
|
2016-03-18 11:20:33 +00:00
|
|
|
UserGetter.getUser project.owner_ref, { features: true }, (err, owner) ->
|
2016-03-08 14:05:56 +00:00
|
|
|
return callback(err) if err?
|
2018-05-14 10:31:59 +00:00
|
|
|
features = owner?.features
|
|
|
|
callback(null, features?.references == true || features?.referencesSearch == true)
|
2016-01-21 17:01:24 +00:00
|
|
|
|
2016-01-27 16:00:53 +00:00
|
|
|
indexAll: (projectId, callback=(err, data)->) ->
|
2016-03-18 11:20:33 +00:00
|
|
|
ProjectGetter.getProject projectId, {rootFolder: true, owner_ref: 1}, (err, project) ->
|
2016-01-27 16:00:53 +00: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 17:04:27 +00:00
|
|
|
docIds = ReferencesHandler._findBibDocIds(project)
|
2016-05-19 13:58:12 +00:00
|
|
|
fileIds = ReferencesHandler._findBibFileIds(project)
|
|
|
|
ReferencesHandler._doIndexOperation(projectId, project, docIds, fileIds, callback)
|
2016-01-27 16:00:53 +00:00
|
|
|
|
2016-01-22 14:23:59 +00:00
|
|
|
index: (projectId, docIds, callback=(err, data)->) ->
|
2016-03-18 11:20:33 +00:00
|
|
|
ProjectGetter.getProject projectId, {rootFolder: true, owner_ref: 1}, (err, project) ->
|
2016-01-21 17:01:24 +00:00
|
|
|
if err
|
2016-01-22 14:23:59 +00:00
|
|
|
logger.err {err, projectId}, "error finding project"
|
2016-01-21 17:01:24 +00:00
|
|
|
return callback(err)
|
2016-05-19 13:58:12 +00:00
|
|
|
ReferencesHandler._doIndexOperation(projectId, project, docIds, [], callback)
|
2016-01-27 16:00:53 +00:00
|
|
|
|
2016-05-19 13:58:12 +00:00
|
|
|
_doIndexOperation: (projectId, project, docIds, fileIds, callback) ->
|
2016-06-24 14:04:10 +00:00
|
|
|
if !settings.apis?.references?.url?
|
|
|
|
return callback()
|
2016-02-08 17:04:27 +00:00
|
|
|
ReferencesHandler._isFullIndex project, (err, isFullIndex) ->
|
2016-01-27 16:00:53 +00: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 17:04:27 +00:00
|
|
|
ReferencesHandler._buildDocUrl projectId, docId
|
2016-05-19 13:58:12 +00:00
|
|
|
bibFileUrls = fileIds.map (fileId) ->
|
|
|
|
ReferencesHandler._buildFileUrl projectId, fileId
|
|
|
|
allUrls = bibDocUrls.concat(bibFileUrls)
|
2016-01-27 16:00:53 +00: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 13:58:12 +00:00
|
|
|
docUrls: allUrls
|
2016-01-27 16:00:53 +00:00
|
|
|
fullIndex: isFullIndex
|
|
|
|
}, (err, res, data) ->
|
2016-01-27 13:33:42 +00:00
|
|
|
if err
|
2016-01-27 16:00:53 +00: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 13:33:42 +00:00
|
|
|
return callback(err)
|
2016-01-27 16:00:53 +00:00
|
|
|
)
|