Merge pull request #1346 from sharelatex/ew-use-new-api-for-project-importing

use different api to check if project is exporting

GitOrigin-RevId: 720012669c833f7482be087ac9d588c354c45147
This commit is contained in:
James Allen 2019-01-07 11:15:38 +00:00 committed by sharelatex
parent 0de359b70b
commit 12a2d71ac9
6 changed files with 25 additions and 32 deletions

View file

@ -33,6 +33,7 @@ crypto = require 'crypto'
Features = require('../../infrastructure/Features')
BrandVariationsHandler = require("../BrandVariations/BrandVariationsHandler")
{ getUserAffiliations } = require("../Institutions/InstitutionsAPI")
V1Handler = require "../V1/V1Handler"
module.exports = ProjectController =
@ -283,9 +284,9 @@ module.exports = ProjectController =
return cb(null, project) unless project.overleaf?.id? and project.tokens?.readAndWrite? and Settings.projectImportingCheckMaxCreateDelta?
createDelta = (new Date().getTime() - new Date(project._id.getTimestamp()).getTime()) / 1000
return cb(null, project) unless createDelta < Settings.projectImportingCheckMaxCreateDelta
TokenAccessHandler.getV1DocInfo project.tokens.readAndWrite, null, (err, doc_info) ->
V1Handler.getDocExported project.tokens.readAndWrite, (err, doc_exported) ->
return next err if err?
project.exporting = doc_info.exporting
project.exporting = doc_exported.exporting
cb(null, project)
)
user: (cb)->

View file

@ -126,20 +126,14 @@ module.exports = TokenAccessHandler =
return callback(null, {
exists: true
exported: false
exporting: false
}) unless Settings.apis?.v1?
if v2UserId?
UserGetter.getUser v2UserId, { overleaf: 1 }, (err, user) ->
return callback(err) if err?
v1UserId = user.overleaf?.id
V1Api.request { url: "/api/v1/sharelatex/users/#{v1UserId}/docs/#{token}/info" }, (err, response, body) ->
return callback err if err?
callback null, body
else
V1Api.request { url: "/api/v1/sharelatex/docs/#{token}/info" }, (err, response, body) ->
return callback err if err?
callback null, body
module.exports.READ_AND_WRITE_TOKEN_REGEX = /^(\d+)(\w+)$/
module.exports.READ_AND_WRITE_URL_REGEX = /^\/(\d+)(\w+)$/

View file

@ -47,3 +47,14 @@ module.exports = V1Handler =
else
err = new Error("Unexpected status from v1 password reset api: #{response.statusCode}")
callback(err, false)
getDocExported: (token, callback=(err, info)->) ->
# default to not exported
return callback(null, {
exported: false
exporting: false
}) unless Settings.apis?.v1?
V1Api.request { url: "/api/v1/sharelatex/docs/#{token}/exported_to_v2" }, (err, response, body) ->
return callback err if err?
callback null, body

View file

@ -458,8 +458,8 @@ describe 'TokenAccess', ->
@owner.getProject @project_id, (err, project) =>
return done(err) if err?
@tokens = project.tokens
MockV1Api.setDocInfo @tokens.readAndWrite, exporting: true
MockV1Api.setDocInfo @tokens.readOnly, exporting: true
MockV1Api.setDocExported @tokens.readAndWrite, exporting: true
MockV1Api.setDocExported @tokens.readOnly, exporting: true
done()
after ->

View file

@ -42,9 +42,9 @@ module.exports = MockV1Api =
setAffiliations: (affiliations) -> @affiliations = affiliations
doc_info: {}
doc_exported: {}
setDocInfo: (token, info) -> @doc_info[token] = info
setDocExported: (token, info) -> @doc_exported[token] = info
run: () ->
app.get "/api/v1/sharelatex/users/:v1_user_id/plan_code", (req, res, next) =>
@ -146,9 +146,9 @@ module.exports = MockV1Api =
app.get '/api/v1/sharelatex/users/:user_id/docs/:token/info', (req, res, next) =>
res.json { exported: false }
app.get '/api/v1/sharelatex/docs/:token/info', (req, res, next) =>
return res.json @doc_info[req.params.token] if @doc_info[req.params.token]?
res.json { exporting: false }
app.get '/api/v1/sharelatex/docs/:token/exported_to_v2', (req, res, next) =>
return res.json @doc_exported[req.params.token] if @doc_exported[req.params.token]?
res.json { exporting: false, exported: false }
app.listen 5000, (error) ->
throw error if error?

View file

@ -541,7 +541,6 @@ describe "TokenAccessHandler", ->
expect(@callback.calledWith null, {
exists: true
exported: false
exporting: false
}).to.equal true
describe 'when v1 api is set', ->
@ -580,18 +579,6 @@ describe "TokenAccessHandler", ->
expect(@V1Api.request.calledWith { url: "/api/v1/sharelatex/users/#{@v1UserId}/docs/#{@token}/info" }).to.equal true
expect(@callback.calledWith null, 'mock-data').to.equal true
describe 'when user id arg is null', ->
beforeEach ->
@v2UserId = null
@UserGetter.getUser = sinon.stub()
@V1Api.request = sinon.stub().callsArgWith(1, null, null, 'mock-data')
@TokenAccessHandler.getV1DocInfo @token, @v2UserId, @callback
it 'should get info without user', ->
expect(@UserGetter.getUser.called).to.equal false
expect(@V1Api.request.calledWith { url: "/api/v1/sharelatex/docs/#{@token}/info" }).to.equal true
expect(@callback.calledWith null, 'mock-data').to.equal true
describe 'on V1Api.request error', ->
beforeEach ->
@UserGetter.getUser = sinon.stub().yields(null, {