Extract V1 projects fetch to integration module

OlProjectGetter now just detects if integration module is loaded and proxies
to integration module. If module not loaded, it just bails
This commit is contained in:
Alasdair Smith 2017-11-01 14:01:06 +00:00
parent bb8f80c1e4
commit 8735261022
2 changed files with 18 additions and 23 deletions

View file

@ -1,24 +1,22 @@
request = require 'request'
settings = require 'settings-sharelatex'
fs = require 'fs'
path = require 'path'
logger = require 'logger-sharelatex'
Errors = require '../Errors/Errors'
oAuthRequest = require '../../../../modules/overleaf-integration-web-module/app/coffee/oauth/OAuthRequest'
makeRequest = (opts, callback) ->
if settings.apis?.olProjects?.url?
urlPath = opts.url
opts.url = "#{settings.apis.olProjects.url}#{urlPath}"
request opts, callback
else
callback(new Errors.ServiceNotConfiguredError('OL Projects service not configured'))
INTEGRATION_MODULE_PATH = path.resolve(__dirname, '../../../../modules/overleaf-integration-web-module')
module.exports = OlProjectGetter =
integrationModuleExists: (callback = (error, stats) ->) ->
fs.stat INTEGRATION_MODULE_PATH, (error, stats) ->
if error? or !stats.isDirectory()
return callback(false)
return callback(true)
findAllUsersProjects: (userId, callback = (error, projects) ->) ->
oAuthRequest userId, {
url: "#{settings.overleaf.host}/api/v1/sharelatex/docs"
method: 'GET'
json: true
}, (error, docs) ->
return callback(error) if error?
logger.log {userId, docs}, "got projects from OL"
callback(null, docs)
OlProjectGetter.integrationModuleExists (exists) ->
if exists
logger.log {exists}, "integration module does exist, loading V1 projects"
V1ProjectListGetter = require(path.join(INTEGRATION_MODULE_PATH, 'app/coffee/ProjectList/ProjectListGetter'))
V1ProjectListGetter.findAllUsersProjects(userId, callback)
else
logger.log {exists}, "integration modules doesn't exists, not loading V1 projects"
return callback()

View file

@ -153,10 +153,7 @@ module.exports = ProjectController =
projects: (cb)->
ProjectGetter.findAllUsersProjects user_id, 'name lastUpdated publicAccesLevel archived owner_ref tokens', cb
v1Projects: (cb) ->
if isV1
OlProjectGetter.findAllUsersProjects user_id, cb
else
cb()
OlProjectGetter.findAllUsersProjects user_id, cb
hasSubscription: (cb)->
LimitationsManager.userHasSubscriptionOrIsGroupMember currentUser, cb
user: (cb) ->