diff --git a/services/web/app/coffee/Features/ReferencesSearch/ReferencesSearchHandler.coffee b/services/web/app/coffee/Features/ReferencesSearch/ReferencesSearchHandler.coffee index 0464fc2274..13bdad9927 100644 --- a/services/web/app/coffee/Features/ReferencesSearch/ReferencesSearchHandler.coffee +++ b/services/web/app/coffee/Features/ReferencesSearch/ReferencesSearchHandler.coffee @@ -4,6 +4,8 @@ settings = require("settings-sharelatex") ProjectLocator = require("../Project/ProjectLocator") U = require('underscore') Async = require('async') +Project = require("../../models/Project").Project +UserGetter = require "../User/UserGetter" oneMinInMs = 60 * 1000 fiveMinsInMs = oneMinInMs * 5 @@ -26,6 +28,13 @@ module.exports = ReferencesSearchHandler = return ids + _isFullIndex: (projectId, callback = (err, result) ->) -> + Project.findById projectId, {owner_ref: 1}, (err, project) -> + return callback(err) if err + UserGetter.getUser project.owner_ref, {features: 1}, (err, owner) -> + return callback(err) if err + callback(null, owner.features.references == true) + indexProjectReferences: (project, callback = (err) ->) -> logger.log {projectId: project._id}, "try indexing references from project" ids = ReferencesSearchHandler._findBibDocIds(project) @@ -40,27 +49,33 @@ module.exports = ReferencesSearchHandler = callback(err) ) - indexFile: (project_id, file_id, callback = (err)->) -> - logger.log {project_id, file_id}, "sending index request to references api" - target_url = "#{settings.apis.references.url}/project/#{project_id}" - file_url = ReferencesSearchHandler._buildDocUrl project_id, file_id - request.post { - url: target_url - json: - referencesUrl: file_url - }, (err, res, result) -> + 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) -> if err + logger.err {projectId, fileId, err}, "error checking if file should be fully indexed" 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, project_id, file_url}, "error updating references" - return callback(err) + logger.log {projectId, fileId, isFullIndex}, "sending index request to references api" + request.post { + url: target_url + json: + referencesUrl: fileUrl + fullIndex: isFullIndex == true + }, (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) - getKeys: (project_id, callback = (err, result)->) -> - logger.log {project_id}, "getting keys from remote references api" - url = "#{settings.apis.references.url}/project/#{project_id}/keys" + getKeys: (projectId, callback = (err, result)->) -> + logger.log {projectId}, "getting keys from remote references api" + url = "#{settings.apis.references.url}/project/#{projectId}/keys" request.get { url: url json: true @@ -71,8 +86,8 @@ module.exports = ReferencesSearchHandler = return callback(null, result) else err = new Error("references api responded with non-success code: #{res.statusCode}") - logger.log {err, project_id}, "error getting references keys" + logger.log {err, projectId}, "error getting references keys" return callback(err) - _buildDocUrl: (project_id, doc_id) -> - "#{settings.apis.web.url}/project/#{project_id}/doc/#{doc_id}" + _buildDocUrl: (projectId, docId) -> + "#{settings.apis.web.url}/project/#{projectId}/doc/#{docId}" diff --git a/services/web/test/UnitTests/coffee/ReferencesSearch/ReferencesSearchHandlerTests.coffee b/services/web/test/UnitTests/coffee/ReferencesSearch/ReferencesSearchHandlerTests.coffee index 987d2e81f7..14abc824fe 100644 --- a/services/web/test/UnitTests/coffee/ReferencesSearch/ReferencesSearchHandlerTests.coffee +++ b/services/web/test/UnitTests/coffee/ReferencesSearch/ReferencesSearchHandlerTests.coffee @@ -24,9 +24,46 @@ describe 'ReferencesSearchHandler', -> get: sinon.stub() post: sinon.stub() } + '../../models/Project': @Project = { + Project: { + findById: sinon.stub().callsArgWith(2, null, {owner_ref: '111'}) + } + } + '../User/UserGetter': @UserGetter = { + getUser: sinon.stub().callsArgWith(2, null, {features: {references: false}}) + } describe 'indexFile', -> + describe 'full index or not', -> + + beforeEach -> + @request.post.callsArgWith(1, null, {statusCode: 200}, {}) + + describe 'when full index is not required', -> + + beforeEach -> + @UserGetter.getUser.callsArgWith(2, null, {features: {references: false}}) + + it 'should set fullIndex to true', (done) -> + @handler.indexFile @project_id, @file_id, (err) => + @request.post.calledOnce.should.equal true + options = @request.post.firstCall.args[0] + options.json.fullIndex.should.equal false + done() + + describe 'when full index is required', -> + + beforeEach -> + @UserGetter.getUser.callsArgWith(2, null, {features: {references: true}}) + + it 'should set fullIndex to true', (done) -> + @handler.indexFile @project_id, @file_id, (err) => + @request.post.calledOnce.should.equal true + options = @request.post.firstCall.args[0] + options.json.fullIndex.should.equal true + done() + describe 'when index operation is successful', -> beforeEach -> @request.post.callsArgWith(1, null, {statusCode: 201}, {})