2014-02-12 05:23:40 -05:00
settings = require ( ' settings-sharelatex ' )
logger = require ( ' logger-sharelatex ' )
path = require ( ' path ' )
Project = require ( ' ../../models/Project ' ) . Project
keys = require ( ' ../../infrastructure/Keys ' )
metrics = require ( " ../../infrastructure/Metrics " )
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
queue = require ( ' fairy ' ) . connect ( settings . redis . fairy ) . queue ( keys . queue . web_to_tpds_http_requests )
module.exports =
2014-05-20 08:18:59 -04:00
2014-10-15 09:11:02 -04:00
_addEntity: ( options , callback = (err)-> ) ->
2014-02-12 05:23:40 -05:00
getProjectsUsersIds options . project_id , (err, user_id, allUserIds)->
2014-10-15 09:11:02 -04: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 05:23:40 -05:00
postOptions =
method : " post "
2014-10-15 09:11:02 -04:00
headers:
2014-02-12 05:23:40 -05: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 08:18:59 -04:00
title: " addFile "
streamOrigin : options . streamOrigin
2014-02-12 05:23:40 -05:00
queue . enqueue options . project_id , " pipeStreamFrom " , postOptions , ->
2014-10-15 09:11:02 -04: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 "
2014-02-12 05:23:40 -05:00
callback ( )
2014-05-20 08:18:59 -04:00
2014-10-15 09:11:02 -04:00
addFile : ( options , callback = (err)-> ) ->
2014-05-20 08:18:59 -04: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 09:11:02 -04:00
@ _addEntity ( options , callback )
2014-02-12 05:23:40 -05:00
2014-10-15 09:11:02 -04:00
addDoc : ( options , callback = (err)-> ) ->
2014-02-12 05:23:40 -05:00
metrics . inc ( " tpds.add-doc " )
2014-05-21 04:15:50 -04:00
options.streamOrigin = settings . apis . docstore . pubUrl + path . join ( " /project/ #{ options . project_id } /doc/ " , " #{ options . doc_id } /raw " )
2014-10-15 09:11:02 -04:00
@ _addEntity ( options , callback )
2014-02-12 05:23:40 -05:00
2014-10-15 09:11:02 -04:00
moveEntity : ( options , callback = (err)-> ) ->
2014-02-12 05:23:40 -05: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 09:11:02 -04: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 05:23:40 -05: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
queue . enqueue options . project_id , " standardHttpRequest " , moveOptions , callback
2014-10-15 09:11:02 -04:00
deleteEntity : ( options , callback = (err)-> ) ->
2014-02-12 05:23:40 -05:00
metrics . inc ( " tpds.delete-entity " )
getProjectsUsersIds options . project_id , (err, user_id, allUserIds)->
2014-10-15 09:11:02 -04: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 05:23:40 -05:00
deleteOptions =
method : " DELETE "
2014-10-15 09:11:02 -04:00
headers:
2014-02-12 05:23:40 -05: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 )
queue . enqueue options . project_id , " standardHttpRequest " , deleteOptions , callback
2014-08-13 12:26:18 -04: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 ]
queue . enqueue " poll-dropbox: #{ user_id } " , " standardHttpRequest " , options , callback
2014-02-12 05:23:40 -05:00
getProjectsUsersIds = ( project_id , callback = (err, owner_id, allUserIds)-> ) ->
Project . findById project_id , " _id owner_ref readOnly_refs collaberator_refs " , (err, project)->
allUserIds = [ ] . concat ( project . collaberator_refs ) . concat ( project . readOnly_refs ) . concat ( project . owner_ref )
callback err , project . owner_ref , allUserIds
mergeProjectNameAndPath = (project_name, path)->
if ( path . indexOf ( ' / ' ) == 0 )
path = path . substring ( 1 )
fullPath = " / #{ project_name } / #{ path } "
return fullPath