| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- /* eslint-disable
- camelcase,
- handle-callback-err,
- max-len,
- no-irregular-whitespace,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- let TpdsUpdateSender, tpdsUrl
- const settings = require('settings-sharelatex')
- const logger = require('logger-sharelatex')
- const path = require('path')
- const ProjectGetter = require('../Project/ProjectGetter')
- const keys = require('../../infrastructure/Keys')
- const metrics = require('metrics-sharelatex')
- const request = require('request')
- const CollaboratorsHandler = require('../Collaborators/CollaboratorsHandler')
- const buildPath = function(user_id, project_name, filePath) {
- let projectPath = path.join(project_name, '/', filePath)
- projectPath = encodeURIComponent(projectPath)
- const fullPath = path.join('/user/', `${user_id}`, '/entity/', projectPath)
- return fullPath
- }
- const tpdsworkerEnabled = () =>
- (settings.apis.tpdsworker != null
- ? settings.apis.tpdsworker.url
- : undefined) != null
- if (!tpdsworkerEnabled()) {
- logger.log('tpdsworker is not enabled, request will not be sent to it')
- }
- if (settings.apis.thirdPartyDataStore.linode_url != null) {
- tpdsUrl = settings.apis.thirdPartyDataStore.linode_url
- } else {
- tpdsUrl = settings.apis.thirdPartyDataStore.url
- }
- module.exports = TpdsUpdateSender = {
- _enqueue(group, method, job, callback) {
- if (!tpdsworkerEnabled()) {
- return callback()
- }
- const opts = {
- uri: `${settings.apis.tpdsworker.url}/enqueue/web_to_tpds_http_requests`,
- json: {
- group,
- method,
- job
- },
- method: 'post',
- timeout: 5 * 1000
- }
- return request(opts, function(err) {
- if (err != null) {
- logger.err(
- { err },
- 'error queuing something in the tpdsworker, continuing anyway'
- )
- return callback()
- } else {
- logger.log({ group, job }, 'successfully queued up job for tpdsworker')
- return callback()
- }
- })
- },
- _addEntity(options, callback) {
- if (callback == null) {
- callback = function(err) {}
- }
- return getProjectsUsersIds(options.project_id, function(
- err,
- user_id,
- allUserIds
- ) {
- if (err != null) {
- logger.warn({ err, options }, 'error getting projects user ids')
- return callback(err)
- }
- logger.log(
- {
- project_id: options.project_id,
- user_id,
- path: options.path,
- uri: options.uri,
- rev: options.rev
- },
- 'sending file to third party data store'
- )
- const postOptions = {
- method: 'post',
- headers: {
- sl_entity_rev: options.rev,
- sl_project_id: options.project_id,
- sl_all_user_ids: JSON.stringify(allUserIds)
- },
- uri: `${tpdsUrl}${buildPath(
- user_id,
- options.project_name,
- options.path
- )}`,
- title: 'addFile',
- streamOrigin: options.streamOrigin
- }
- return TpdsUpdateSender._enqueue(
- options.project_id,
- 'pipeStreamFrom',
- postOptions,
- function(err) {
- if (err != null) {
- logger.warn(
- {
- err,
- project_id: options.project_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)
- }
- logger.log(
- {
- project_id: options.project_id,
- user_id,
- path: options.path,
- uri: options.uri,
- rev: options.rev
- },
- 'sending file to third party data store queued up for processing'
- )
- return callback(err)
- }
- )
- })
- },
- addFile(options, callback) {
- if (callback == null) {
- callback = function(err) {}
- }
- metrics.inc('tpds.add-file')
- options.streamOrigin =
- (settings.apis.filestore.linode_url || settings.apis.filestore.url) +
- path.join(`/project/${options.project_id}/file/`, `${options.file_id}`)
- return this._addEntity(options, callback)
- },
- addDoc(options, callback) {
- if (callback == null) {
- callback = function(err) {}
- }
- metrics.inc('tpds.add-doc')
- options.streamOrigin =
- (settings.apis.docstore.linode_url || settings.apis.docstore.pubUrl) +
- path.join(`/project/${options.project_id}/doc/`, `${options.doc_id}/raw`)
- return this._addEntity(options, callback)
- },
- moveEntity(options, callback) {
- let endPath, startPath
- if (callback == null) {
- callback = function(err) {}
- }
- metrics.inc('tpds.move-entity')
- if (options.newProjectName != null) {
- 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)
- }
- return getProjectsUsersIds(options.project_id, function(
- err,
- user_id,
- allUserIds
- ) {
- logger.log(
- {
- project_id: options.project_id,
- user_id,
- startPath,
- endPath,
- uri: options.uri
- },
- 'moving entity in third party data store'
- )
- const moveOptions = {
- method: 'put',
- title: 'moveEntity',
- uri: `${tpdsUrl}/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,
- endPath,
- startPath
- }
- }
- return TpdsUpdateSender._enqueue(
- options.project_id,
- 'standardHttpRequest',
- moveOptions,
- callback
- )
- })
- },
- deleteEntity(options, callback) {
- if (callback == null) {
- callback = function(err) {}
- }
- metrics.inc('tpds.delete-entity')
- return getProjectsUsersIds(options.project_id, function(
- err,
- user_id,
- allUserIds
- ) {
- logger.log(
- {
- project_id: options.project_id,
- user_id,
- path: options.path,
- uri: options.uri
- },
- 'deleting entity in third party data store'
- )
- const deleteOptions = {
- method: 'DELETE',
- headers: {
- sl_project_id: options.project_id,
- sl_all_user_ids: JSON.stringify(allUserIds)
- },
- uri: `${tpdsUrl}${buildPath(
- user_id,
- options.project_name,
- options.path
- )}`,
- title: 'deleteEntity',
- sl_all_user_ids: JSON.stringify(allUserIds)
- }
- return TpdsUpdateSender._enqueue(
- options.project_id,
- 'standardHttpRequest',
- deleteOptions,
- callback
- )
- })
- },
- pollDropboxForUser(user_id, callback) {
- if (callback == null) {
- callback = function(err) {}
- }
- metrics.inc('tpds.poll-dropbox')
- logger.log({ user_id }, 'polling dropbox for user')
- const options = {
- method: 'POST',
- uri: `${tpdsUrl}/user/poll`,
- json: {
- user_ids: [user_id]
- }
- }
- return TpdsUpdateSender._enqueue(
- `poll-dropbox:${user_id}`,
- 'standardHttpRequest',
- options,
- callback
- )
- }
- }
- var getProjectsUsersIds = function(project_id, callback) {
- if (callback == null) {
- callback = function(err, owner_id, allUserIds) {}
- }
- return ProjectGetter.getProject(
- project_id,
- { _id: true, owner_ref: true },
- function(err, project) {
- if (err != null) {
- return callback(err)
- }
- return CollaboratorsHandler.getInvitedMemberIds(project_id, function(
- err,
- member_ids
- ) {
- if (err != null) {
- return callback(err)
- }
- return callback(
- err,
- project != null ? project.owner_ref : undefined,
- member_ids
- )
- })
- }
- )
- }
- var mergeProjectNameAndPath = function(project_name, path) {
- if (path.indexOf('/') === 0) {
- path = path.substring(1)
- }
- const fullPath = `/${project_name}/${path}`
- return fullPath
- }
|