2015-12-17 06:51:14 -05:00
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
request = require("request")
|
|
|
|
settings = require("settings-sharelatex")
|
2016-01-14 09:53:08 -05:00
|
|
|
ProjectLocator = require("../Project/ProjectLocator")
|
|
|
|
U = require('underscore')
|
2016-01-15 09:41:05 -05:00
|
|
|
Async = require('async')
|
2016-01-20 08:53:28 -05:00
|
|
|
Project = require("../../models/Project").Project
|
|
|
|
UserGetter = require "../User/UserGetter"
|
2015-12-17 06:51:14 -05:00
|
|
|
|
|
|
|
oneMinInMs = 60 * 1000
|
|
|
|
fiveMinsInMs = oneMinInMs * 5
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = ReferencesSearchHandler =
|
|
|
|
|
2016-01-15 09:41:05 -05:00
|
|
|
_findBibDocIds: (project) ->
|
|
|
|
ids = []
|
|
|
|
|
|
|
|
_process = (folder) ->
|
|
|
|
folder.docs.forEach (doc) ->
|
|
|
|
if doc?.name?.match(/^.*\.bib$/)
|
|
|
|
ids.push(doc._id)
|
|
|
|
folder.folders.forEach (folder) ->
|
|
|
|
_process(folder)
|
|
|
|
|
|
|
|
project.rootFolder.forEach (rootFolder) ->
|
|
|
|
_process(rootFolder)
|
|
|
|
|
|
|
|
return ids
|
|
|
|
|
2016-01-21 12:01:24 -05:00
|
|
|
_isFullIndex: (project, callback = (err, result) ->) ->
|
|
|
|
UserGetter.getUser project.owner_ref, {features: 1}, (err, owner) ->
|
2016-01-20 08:53:28 -05:00
|
|
|
return callback(err) if err
|
2016-01-21 12:01:24 -05:00
|
|
|
callback(null, owner.features.references == true)
|
|
|
|
|
|
|
|
loadReferencesKeys: (projectId, callback=(err, data)->) ->
|
|
|
|
logger.log {projectId}, "load references keys for project"
|
|
|
|
Project.findPopulatedById projectId, (err, project) ->
|
|
|
|
if err
|
|
|
|
return callback(err)
|
|
|
|
ReferencesSearchHandler._isFullIndex project, (err, isFullIndex) ->
|
|
|
|
if err
|
|
|
|
return callback(err)
|
|
|
|
bibDocIds = ReferencesSearchHandler._findBibDocIds(project)
|
|
|
|
bibDocUrls = bibDocIds.map (docId) ->
|
|
|
|
ReferencesSearchHandler._buildDocUrl projectId, docId
|
|
|
|
logger.log {projectId, isFullIndex, bibDocIds}, "sending request to references service"
|
|
|
|
request.post {
|
|
|
|
url: "#{settings.apis.references.url}/project/#{projectId}/loadreferenceskeys"
|
|
|
|
json:
|
|
|
|
docUrls: bibDocUrls
|
|
|
|
fullIndex: isFullIndex
|
|
|
|
}, (err, res, result) ->
|
|
|
|
if err
|
|
|
|
return callback(err)
|
|
|
|
if 200 <= res.statusCode < 300
|
|
|
|
return callback(null)
|
|
|
|
else
|
|
|
|
err = new Error("references api responded with non-success code: #{res.statusCode}")
|
|
|
|
logger.log {err, projectId, fileUrl}, "error updating references"
|
|
|
|
return callback(err)
|
|
|
|
|
|
|
|
## ## ## ##
|
2016-01-20 08:53:28 -05:00
|
|
|
|
2016-01-15 09:41:05 -05:00
|
|
|
indexProjectReferences: (project, callback = (err) ->) ->
|
|
|
|
logger.log {projectId: project._id}, "try indexing references from project"
|
|
|
|
ids = ReferencesSearchHandler._findBibDocIds(project)
|
|
|
|
logger.log {projectId: project._id, count: ids.length}, "found bib files in project"
|
|
|
|
Async.eachSeries(
|
|
|
|
ids,
|
|
|
|
(docId, next) ->
|
|
|
|
ReferencesSearchHandler.indexFile project._id, docId, (err) ->
|
|
|
|
next(err)
|
|
|
|
, (err) ->
|
|
|
|
logger.log {projectId: project._id, count: ids.length}, "done index bib files in project"
|
2016-01-14 09:53:08 -05:00
|
|
|
callback(err)
|
2016-01-15 09:41:05 -05:00
|
|
|
)
|
2016-01-14 09:53:08 -05:00
|
|
|
|
2016-01-20 08:53:28 -05:00
|
|
|
indexFile: (projectId, fileId, callback = (err)->) ->
|
|
|
|
target_url = "#{settings.apis.references.url}/project/#{projectId}"
|
|
|
|
fileUrl = ReferencesSearchHandler._buildDocUrl projectId, fileId
|
|
|
|
logger.log {projectId, fileId}, "checking if file should be fully indexed"
|
|
|
|
ReferencesSearchHandler._isFullIndex projectId, (err, isFullIndex) ->
|
2015-12-17 06:51:14 -05:00
|
|
|
if err
|
2016-01-20 08:53:28 -05:00
|
|
|
logger.err {projectId, fileId, err}, "error checking if file should be fully indexed"
|
2015-12-17 06:51:14 -05:00
|
|
|
return callback(err)
|
2016-01-20 08:53:28 -05:00
|
|
|
logger.log {projectId, fileId, isFullIndex}, "sending index request to references api"
|
|
|
|
request.post {
|
|
|
|
url: target_url
|
|
|
|
json:
|
|
|
|
referencesUrl: fileUrl
|
|
|
|
}, (err, res, result) ->
|
|
|
|
if err
|
|
|
|
return callback(err)
|
|
|
|
if 200 <= res.statusCode < 300
|
|
|
|
return callback(null)
|
|
|
|
else
|
|
|
|
err = new Error("references api responded with non-success code: #{res.statusCode}")
|
|
|
|
logger.log {err, projectId, fileUrl}, "error updating references"
|
|
|
|
return callback(err)
|
2015-12-18 11:00:24 -05:00
|
|
|
|
2016-01-20 08:53:28 -05:00
|
|
|
getKeys: (projectId, callback = (err, result)->) ->
|
|
|
|
logger.log {projectId}, "getting keys from remote references api"
|
|
|
|
url = "#{settings.apis.references.url}/project/#{projectId}/keys"
|
2015-12-18 11:00:24 -05:00
|
|
|
request.get {
|
|
|
|
url: url
|
|
|
|
json: true
|
|
|
|
}, (err, res, result) ->
|
|
|
|
if err
|
|
|
|
return callback(err)
|
|
|
|
if 200 <= res.statusCode < 300
|
|
|
|
return callback(null, result)
|
|
|
|
else
|
|
|
|
err = new Error("references api responded with non-success code: #{res.statusCode}")
|
2016-01-20 08:53:28 -05:00
|
|
|
logger.log {err, projectId}, "error getting references keys"
|
2015-12-18 11:00:24 -05:00
|
|
|
return callback(err)
|
2016-01-14 09:53:08 -05:00
|
|
|
|
2016-01-20 08:53:28 -05:00
|
|
|
_buildDocUrl: (projectId, docId) ->
|
|
|
|
"#{settings.apis.web.url}/project/#{projectId}/doc/#{docId}"
|