2014-02-12 10:23:40 +00:00
settings = require ( ' settings-sharelatex ' )
logger = require ( ' logger-sharelatex ' )
path = require ( ' path ' )
2018-02-15 12:18:43 +00:00
ProjectGetter = require ( ' ../Project/ProjectGetter ' )
2014-02-12 10:23:40 +00:00
keys = require ( ' ../../infrastructure/Keys ' )
2017-04-03 15:18:30 +00:00
metrics = require ( " metrics-sharelatex " )
2015-06-22 21:28:17 +00:00
request = require ( " request " )
2016-03-08 11:54:45 +00:00
CollaboratorsHandler = require ( ' ../Collaborators/CollaboratorsHandler ' )
2014-02-12 10:23:40 +00:00
buildPath = (user_id, project_name, filePath)->
projectPath = path . join ( project_name , " / " , filePath )
projectPath = encodeURIComponent ( projectPath )
fullPath = path . join ( " /user/ " , " #{ user_id } " , " /entity/ " , projectPath )
return fullPath
2015-06-22 21:28:17 +00:00
2015-07-02 11:09:08 +00:00
tpdsworkerEnabled = -> settings . apis . tpdsworker ? . url ?
if ! tpdsworkerEnabled ( )
logger . log " tpdsworker is not enabled, request will not be sent to it "
2015-06-22 21:28:17 +00:00
module.exports = TpdsUpdateSender =
_enqueue: (group, method, job, callback)->
2015-07-02 11:09:08 +00:00
if ! tpdsworkerEnabled ( )
return callback ( )
2015-06-22 21:28:17 +00:00
opts =
2015-06-23 10:13:05 +00:00
uri : " #{ settings . apis . tpdsworker . url } /enqueue/web_to_tpds_http_requests "
2015-06-22 21:28:17 +00:00
json :
group : group
method : method
job : job
method : " post "
2015-06-23 10:19:23 +00:00
timeout: ( 5 * 1000 )
2015-06-22 21:28:17 +00:00
request opts , (err)->
if err ?
2017-04-11 14:41:17 +00:00
logger . err err : err , " error queuing something in the tpdsworker, continuing anyway "
callback ( )
2015-06-22 21:28:17 +00:00
else
2015-06-23 10:19:23 +00:00
logger . log group : group , " successfully queued up job for tpdsworker "
2015-06-22 21:28:17 +00:00
callback ( )
2014-05-20 12:18:59 +00:00
2014-10-15 13:11:02 +00:00
_addEntity: ( options , callback = (err)-> ) ->
2014-02-12 10:23:40 +00:00
getProjectsUsersIds options . project_id , (err, user_id, allUserIds)->
2016-03-14 10:49:28 +00:00
if err ?
logger . err err : err , options : options , " error getting projects user ids "
return callback ( err )
2014-10-15 13:11:02 +00:00
logger . log project_id: options . project_id , user_id : user_id , path: options . path , uri : options . uri , rev : options . rev , " sending file to third party data store "
2014-02-12 10:23:40 +00:00
postOptions =
method : " post "
2014-10-15 13:11:02 +00:00
headers:
2014-02-12 10:23:40 +00:00
sl_entity_rev : options . rev
sl_project_id : options . project_id
sl_all_user_ids : JSON . stringify ( allUserIds )
uri : " #{ settings . apis . thirdPartyDataStore . url } #{ buildPath ( user_id , options . project_name , options . path ) } "
2014-05-20 12:18:59 +00:00
title: " addFile "
streamOrigin : options . streamOrigin
2015-06-22 21:28:17 +00:00
TpdsUpdateSender . _enqueue options . project_id , " pipeStreamFrom " , postOptions , (err)->
2016-03-14 10:49:28 +00:00
if err ?
logger . err err : err , project_id: options . project_id , user_id : user_id , path: options . path , uri : options . uri , rev : options . rev , " error sending file to third party data store queued up for processing "
return callback ( err )
2014-10-15 13:11:02 +00:00
logger . log project_id: options . project_id , user_id : user_id , path: options . path , uri : options . uri , rev : options . rev , " sending file to third party data store queued up for processing "
2015-06-22 21:28:17 +00:00
callback ( err )
2014-05-20 12:18:59 +00:00
2014-10-15 13:11:02 +00:00
addFile : ( options , callback = (err)-> ) ->
2014-05-20 12:18:59 +00:00
metrics . inc ( " tpds.add-file " )
options.streamOrigin = settings . apis . filestore . url + path . join ( " /project/ #{ options . project_id } /file/ " , " #{ options . file_id } " )
2014-10-15 13:11:02 +00:00
@ _addEntity ( options , callback )
2014-02-12 10:23:40 +00:00
2014-10-15 13:11:02 +00:00
addDoc : ( options , callback = (err)-> ) ->
2014-02-12 10:23:40 +00:00
metrics . inc ( " tpds.add-doc " )
2014-05-21 08:15:50 +00:00
options.streamOrigin = settings . apis . docstore . pubUrl + path . join ( " /project/ #{ options . project_id } /doc/ " , " #{ options . doc_id } /raw " )
2014-10-15 13:11:02 +00:00
@ _addEntity ( options , callback )
2014-02-12 10:23:40 +00:00
2014-10-15 13:11:02 +00:00
moveEntity : ( options , callback = (err)-> ) ->
2014-02-12 10:23:40 +00:00
metrics . inc ( " tpds.move-entity " )
if options . newProjectName ?
startPath = path . join ( " / #{ options . project_name } / " )
endPath = path . join ( " / #{ options . newProjectName } / " )
else
startPath = mergeProjectNameAndPath ( options . project_name , options . startPath )
endPath = mergeProjectNameAndPath ( options . project_name , options . endPath )
getProjectsUsersIds options . project_id , (err, user_id, allUserIds)->
2014-10-15 13:11:02 +00:00
logger . log project_id: options . project_id , user_id : user_id , startPath : startPath , endPath : endPath , uri : options . uri , " moving entity in third party data store "
2014-02-12 10:23:40 +00:00
moveOptions =
method : " put "
title : " moveEntity "
uri : " #{ settings . apis . thirdPartyDataStore . url } /user/ #{ user_id } /entity "
headers:
sl_project_id : options . project_id ,
sl_entity_rev : options . rev
sl_all_user_ids : JSON . stringify ( allUserIds )
json :
user_id : user_id
endPath: endPath
startPath: startPath
2015-06-22 21:28:17 +00:00
TpdsUpdateSender . _enqueue options . project_id , " standardHttpRequest " , moveOptions , callback
2014-02-12 10:23:40 +00:00
2014-10-15 13:11:02 +00:00
deleteEntity : ( options , callback = (err)-> ) ->
2014-02-12 10:23:40 +00:00
metrics . inc ( " tpds.delete-entity " )
getProjectsUsersIds options . project_id , (err, user_id, allUserIds)->
2014-10-15 13:11:02 +00:00
logger . log project_id: options . project_id , user_id : user_id , path: options . path , uri : options . uri , " deleting entity in third party data store "
2014-02-12 10:23:40 +00:00
deleteOptions =
method : " DELETE "
2014-10-15 13:11:02 +00:00
headers:
2014-02-12 10:23:40 +00:00
sl_project_id : options . project_id
sl_all_user_ids : JSON . stringify ( allUserIds )
uri : " #{ settings . apis . thirdPartyDataStore . url } #{ buildPath ( user_id , options . project_name , options . path ) } "
title : " deleteEntity "
sl_all_user_ids : JSON . stringify ( allUserIds )
2015-06-22 21:28:17 +00:00
TpdsUpdateSender . _enqueue options . project_id , " standardHttpRequest " , deleteOptions , callback
2014-08-13 16:26:18 +00:00
pollDropboxForUser: ( user_id , callback = (err) -> ) ->
metrics . inc ( " tpds.poll-dropbox " )
logger . log user_id: user_id , " polling dropbox for user "
options =
method: " POST "
uri : " #{ settings . apis . thirdPartyDataStore . url } /user/poll "
json:
user_ids: [ user_id ]
2015-06-22 21:28:17 +00:00
TpdsUpdateSender . _enqueue " poll-dropbox: #{ user_id } " , " standardHttpRequest " , options , callback
2014-02-12 10:23:40 +00:00
getProjectsUsersIds = ( project_id , callback = (err, owner_id, allUserIds)-> ) ->
2018-02-15 12:18:43 +00:00
ProjectGetter . getProject project_id , { _id: true , owner_ref: true } , (err, project) ->
2016-03-08 11:54:45 +00:00
return callback ( err ) if err ?
2017-09-20 14:48:20 +00:00
CollaboratorsHandler . getInvitedMemberIds project_id , (err, member_ids) ->
2016-03-08 11:54:45 +00:00
return callback ( err ) if err ?
2016-03-15 14:15:25 +00:00
callback err , project ? . owner_ref , member_ids
2014-02-12 10:23:40 +00:00
mergeProjectNameAndPath = (project_name, path)->
if ( path . indexOf ( ' / ' ) == 0 )
path = path . substring ( 1 )
fullPath = " / #{ project_name } / #{ path } "
return fullPath